1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Http/Controllers/Settings/ExportSettings.php

144 lines
4.7 KiB
PHP
Raw Normal View History

2019-04-24 19:32:23 +00:00
<?php
namespace App\Http\Controllers\Settings;
use App\AccountLog;
use App\Following;
use App\Report;
2019-04-25 03:08:35 +00:00
use App\Status;
2019-04-24 19:32:23 +00:00
use App\UserFilter;
use Auth, Cookie, DB, Cache, Purify;
use Carbon\Carbon;
use Illuminate\Http\Request;
2019-04-25 03:08:35 +00:00
use App\Transformer\ActivityPub\{
ProfileTransformer,
StatusTransformer
};
use App\Transformer\Api\StatusTransformer as StatusApiTransformer;
2019-04-24 19:50:47 +00:00
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2019-04-24 19:32:23 +00:00
trait ExportSettings
{
2019-04-25 03:08:35 +00:00
public function __construct()
{
$this->middleware('auth');
}
2019-04-24 19:32:23 +00:00
public function dataExport()
{
return view('settings.dataexport');
}
2019-04-24 19:50:47 +00:00
public function exportAccount()
{
$data = Cache::remember('account:export:profile:actor:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
$profile = Auth::user()->profile;
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($profile, new ProfileTransformer());
return $fractal->createData($resource)->toArray();
});
return response()->streamDownload(function () use ($data) {
echo json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
2019-04-24 23:37:27 +00:00
}, 'account.json', [
'Content-Type' => 'application/json'
]);
2019-04-24 19:50:47 +00:00
}
2019-04-24 19:32:23 +00:00
public function exportFollowing()
{
$data = Cache::remember('account:export:profile:following:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
return Auth::user()->profile->following()->get()->map(function($i) {
return $i->url();
});
});
return response()->streamDownload(function () use($data) {
echo $data;
2019-04-24 23:37:27 +00:00
}, 'following.json', [
'Content-Type' => 'application/json'
]);
2019-04-24 19:32:23 +00:00
}
public function exportFollowers()
{
$data = Cache::remember('account:export:profile:followers:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
return Auth::user()->profile->followers()->get()->map(function($i) {
return $i->url();
});
});
return response()->streamDownload(function () use($data) {
echo $data;
2019-04-24 23:37:27 +00:00
}, 'followers.json', [
'Content-Type' => 'application/json'
]);
2019-04-24 19:32:23 +00:00
}
public function exportMuteBlockList()
{
$profile = Auth::user()->profile;
$exists = UserFilter::select('id')
->whereUserId($profile->id)
->exists();
if(!$exists) {
return redirect()->back();
}
$data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, now()->addMinutes(60), function() use($profile) {
return json_encode([
'muted' => $profile->mutedProfileUrls(),
'blocked' => $profile->blockedProfileUrls()
], JSON_PRETTY_PRINT);
});
return response()->streamDownload(function () use($data) {
echo $data;
2019-04-24 23:37:27 +00:00
}, 'muted-and-blocked-accounts.json', [
'Content-Type' => 'application/json'
]);
2019-04-24 19:32:23 +00:00
}
2019-04-25 03:08:35 +00:00
public function exportStatuses(Request $request)
{
$this->validate($request, [
'type' => 'required|string|in:ap,api'
]);
2019-07-20 03:37:37 +00:00
$limit = 500;
2019-04-25 03:08:35 +00:00
$profile = Auth::user()->profile;
2019-04-25 03:13:10 +00:00
$type = 'ap';
2019-04-25 03:08:35 +00:00
$count = Status::select('id')->whereProfileId($profile->id)->count();
if($count > $limit) {
// fire background job
return redirect('/settings/data-export')->with(['status' => 'You have more than '.$limit.' statuses, we do not support full account export yet.']);
}
$filename = 'outbox.json';
if($type == 'ap') {
2019-07-20 03:37:37 +00:00
$data = Cache::remember('account:export:profile:statuses:ap:'.Auth::user()->profile->id, now()->addHours(1), function() {
2019-04-25 03:08:35 +00:00
$profile = Auth::user()->profile->statuses;
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($profile, new StatusTransformer());
return $fractal->createData($resource)->toArray();
});
} else {
$filename = 'api-statuses.json';
2019-07-20 03:37:37 +00:00
$data = Cache::remember('account:export:profile:statuses:api:'.Auth::user()->profile->id, now()->addHours(1), function() {
2019-04-25 03:08:35 +00:00
$profile = Auth::user()->profile->statuses;
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($profile, new StatusApiTransformer());
return $fractal->createData($resource)->toArray();
});
}
return response()->streamDownload(function () use ($data, $filename) {
echo json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
}, $filename, [
'Content-Type' => 'application/json'
]);
}
2019-04-24 19:32:23 +00:00
}