AdminProfileActionPipeline.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Jobs\AdminPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Avatar;
  10. use App\Follower;
  11. use App\Instance;
  12. use App\Media;
  13. use App\Profile;
  14. use App\Status;
  15. use Cache;
  16. use Storage;
  17. use Purify;
  18. use App\Services\ActivityPubFetchService;
  19. use App\Services\AccountService;
  20. use App\Services\MediaStorageService;
  21. use App\Services\StatusService;
  22. use App\Jobs\StatusPipeline\RemoteStatusDelete;
  23. class AdminProfileActionPipeline implements ShouldQueue
  24. {
  25. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  26. protected $action;
  27. protected $profile;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct($profile, $action)
  34. {
  35. $this->profile = $profile;
  36. $this->action = $action;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $profile = $this->profile;
  46. $action = $this->action;
  47. switch($action) {
  48. case 'mark-all-cw':
  49. return $this->markAllPostsWithContentWarnings();
  50. break;
  51. case 'unlist-all':
  52. return $this->unlistAllPosts();
  53. break;
  54. case 'purge':
  55. return $this->purgeAllPosts();
  56. break;
  57. case 'refetch':
  58. return $this->refetchAllPosts();
  59. break;
  60. }
  61. }
  62. protected function markAllPostsWithContentWarnings()
  63. {
  64. $profile = $this->profile;
  65. foreach(Status::whereProfileId($profile->id)->lazyById(10, 'id') as $status) {
  66. if($status->scope == 'direct') {
  67. continue;
  68. }
  69. $status->is_nsfw = true;
  70. $status->save();
  71. StatusService::del($status->id);
  72. }
  73. }
  74. protected function unlistAllPosts()
  75. {
  76. $profile = $this->profile;
  77. foreach(Status::whereProfileId($profile->id)->lazyById(10, 'id') as $status) {
  78. if($status->scope != 'public') {
  79. continue;
  80. }
  81. $status->scope = 'unlisted';
  82. $status->visibility = 'unlisted';
  83. $status->save();
  84. StatusService::del($status->id);
  85. }
  86. }
  87. protected function purgeAllPosts()
  88. {
  89. $profile = $this->profile;
  90. foreach(Status::withTrashed()->whereProfileId($profile->id)->lazyById(10, 'id') as $status) {
  91. RemoteStatusDelete::dispatch($status)->onQueue('delete');
  92. }
  93. }
  94. protected function refetchAllPosts()
  95. {
  96. $profile = $this->profile;
  97. $res = ActivityPubFetchService::get($profile->remote_url, false);
  98. if(!$res) {
  99. return;
  100. }
  101. $res = json_decode($res, true);
  102. $profile->following_count = Follower::whereProfileId($profile->id)->count();
  103. $profile->followers_count = Follower::whereFollowingId($profile->id)->count();
  104. $profile->name = isset($res['name']) ? Purify::clean($res['name']) : $profile->username;
  105. $profile->bio = isset($res['summary']) ? Purify::clean($res['summary']) : null;
  106. if(isset($res['publicKey'])) {
  107. $profile->public_key = $res['publicKey']['publicKeyPem'];
  108. }
  109. if(
  110. isset($res['icon']) &&
  111. isset(
  112. $res['icon']['type'],
  113. $res['icon']['mediaType'],
  114. $res['icon']['url']) && $res['icon']['type'] == 'Image'
  115. ) {
  116. if(in_array($res['icon']['mediaType'], ['image/jpeg', 'image/png'])) {
  117. $profile->avatar->remote_url = $res['icon']['url'];
  118. $profile->push();
  119. MediaStorageService::avatar($profile->avatar);
  120. }
  121. }
  122. $profile->save();
  123. AccountService::del($profile->id);
  124. }
  125. }