ExportSettings.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers\Settings;
  3. use App\AccountLog;
  4. use App\Following;
  5. use App\Report;
  6. use App\Status;
  7. use App\UserFilter;
  8. use Auth, Cookie, DB, Cache, Purify;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use App\Transformer\ActivityPub\{
  12. ProfileTransformer,
  13. StatusTransformer
  14. };
  15. use App\Transformer\Api\StatusTransformer as StatusApiTransformer;
  16. use League\Fractal;
  17. use League\Fractal\Serializer\ArraySerializer;
  18. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  19. trait ExportSettings
  20. {
  21. public function __construct()
  22. {
  23. $this->middleware('auth');
  24. }
  25. public function dataExport()
  26. {
  27. return view('settings.dataexport');
  28. }
  29. public function exportAccount()
  30. {
  31. $data = Cache::remember('account:export:profile:actor:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
  32. $profile = Auth::user()->profile;
  33. $fractal = new Fractal\Manager();
  34. $fractal->setSerializer(new ArraySerializer());
  35. $resource = new Fractal\Resource\Item($profile, new ProfileTransformer());
  36. return $fractal->createData($resource)->toArray();
  37. });
  38. return response()->streamDownload(function () use ($data) {
  39. echo json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  40. }, 'account.json', [
  41. 'Content-Type' => 'application/json'
  42. ]);
  43. }
  44. public function exportFollowing()
  45. {
  46. $data = Cache::remember('account:export:profile:following:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
  47. return Auth::user()->profile->following()->get()->map(function($i) {
  48. return $i->url();
  49. });
  50. });
  51. return response()->streamDownload(function () use($data) {
  52. echo $data;
  53. }, 'following.json', [
  54. 'Content-Type' => 'application/json'
  55. ]);
  56. }
  57. public function exportFollowers()
  58. {
  59. $data = Cache::remember('account:export:profile:followers:'.Auth::user()->profile->id, now()->addMinutes(60), function() {
  60. return Auth::user()->profile->followers()->get()->map(function($i) {
  61. return $i->url();
  62. });
  63. });
  64. return response()->streamDownload(function () use($data) {
  65. echo $data;
  66. }, 'followers.json', [
  67. 'Content-Type' => 'application/json'
  68. ]);
  69. }
  70. public function exportMuteBlockList()
  71. {
  72. $profile = Auth::user()->profile;
  73. $exists = UserFilter::select('id')
  74. ->whereUserId($profile->id)
  75. ->exists();
  76. if(!$exists) {
  77. return redirect()->back();
  78. }
  79. $data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, now()->addMinutes(60), function() use($profile) {
  80. return json_encode([
  81. 'muted' => $profile->mutedProfileUrls(),
  82. 'blocked' => $profile->blockedProfileUrls()
  83. ], JSON_PRETTY_PRINT);
  84. });
  85. return response()->streamDownload(function () use($data) {
  86. echo $data;
  87. }, 'muted-and-blocked-accounts.json', [
  88. 'Content-Type' => 'application/json'
  89. ]);
  90. }
  91. public function exportStatuses(Request $request)
  92. {
  93. $this->validate($request, [
  94. 'type' => 'required|string|in:ap,api'
  95. ]);
  96. $limit = 500;
  97. $profile = Auth::user()->profile;
  98. $type = 'ap';
  99. $count = Status::select('id')->whereProfileId($profile->id)->count();
  100. if($count > $limit) {
  101. // fire background job
  102. return redirect('/settings/data-export')->with(['status' => 'You have more than '.$limit.' statuses, we do not support full account export yet.']);
  103. }
  104. $filename = 'outbox.json';
  105. if($type == 'ap') {
  106. $data = Cache::remember('account:export:profile:statuses:ap:'.Auth::user()->profile->id, now()->addHours(1), function() {
  107. $profile = Auth::user()->profile->statuses;
  108. $fractal = new Fractal\Manager();
  109. $fractal->setSerializer(new ArraySerializer());
  110. $resource = new Fractal\Resource\Collection($profile, new StatusTransformer());
  111. return $fractal->createData($resource)->toArray();
  112. });
  113. } else {
  114. $filename = 'api-statuses.json';
  115. $data = Cache::remember('account:export:profile:statuses:api:'.Auth::user()->profile->id, now()->addHours(1), function() {
  116. $profile = Auth::user()->profile->statuses;
  117. $fractal = new Fractal\Manager();
  118. $fractal->setSerializer(new ArraySerializer());
  119. $resource = new Fractal\Resource\Collection($profile, new StatusApiTransformer());
  120. return $fractal->createData($resource)->toArray();
  121. });
  122. }
  123. return response()->streamDownload(function () use ($data, $filename) {
  124. echo json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  125. }, $filename, [
  126. 'Content-Type' => 'application/json'
  127. ]);
  128. }
  129. }