Add status exports

This commit is contained in:
Daniel Supernault 2019-04-24 21:08:35 -06:00
parent d92d9ddaf0
commit 2063bfc9fd
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 64 additions and 2 deletions

View file

@ -5,17 +5,26 @@ namespace App\Http\Controllers\Settings;
use App\AccountLog;
use App\Following;
use App\Report;
use App\Status;
use App\UserFilter;
use Auth, Cookie, DB, Cache, Purify;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Transformer\ActivityPub\ProfileTransformer;
use App\Transformer\ActivityPub\{
ProfileTransformer,
StatusTransformer
};
use App\Transformer\Api\StatusTransformer as StatusApiTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
trait ExportSettings
{
public function __construct()
{
$this->middleware('auth');
}
public function dataExport()
{
@ -89,4 +98,47 @@ trait ExportSettings
]);
}
public function exportStatuses(Request $request)
{
$this->validate($request, [
'type' => 'required|string|in:ap,api'
]);
$limit = 300;
$profile = Auth::user()->profile;
$type = $request->input('type') == 'ap' ? 'ap' : 'api';
$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') {
$data = Cache::remember('account:export:profile:statuses:ap:'.Auth::user()->profile->id, now()->addDays(7), function() {
$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';
$data = Cache::remember('account:export:profile:statuses:api:'.Auth::user()->profile->id, now()->addDays(7), function() {
$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'
]);
}
}

View file

@ -35,7 +35,16 @@
<span class="font-weight-bold">Statuses</span>
</div>
<div>
<span class="small text-muted">Coming Soon</span>
<form action="/settings/data-export/statuses" method="post" class="d-inline">
@csrf
<input type="hidden" name="type" value="ap">
<button type="submit" class="font-weight-bold btn btn-outline-primary btn-sm">Download</button>
</form>
{{-- <form action="/settings/data-export/statuses" method="post" class="d-inline">
@csrf
<input type="hidden" name="type" value="api">
<button type="submit" class="font-weight-bold btn btn-outline-primary btn-sm">api.json</button>
</form> --}}
</div>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">

View file

@ -229,6 +229,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::post('data-export/followers', 'SettingsController@exportFollowers')->middleware('dangerzone');
Route::post('data-export/mute-block-list', 'SettingsController@exportMuteBlockList')->middleware('dangerzone');
Route::post('data-export/account', 'SettingsController@exportAccount')->middleware('dangerzone');
Route::post('data-export/statuses', 'SettingsController@exportStatuses')->middleware('dangerzone');
Route::get('developers', 'SettingsController@developers')->name('settings.developers')->middleware('dangerzone');
});