SendUpdateActor.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Storage;
  5. use App\Profile;
  6. use App\User;
  7. use App\Instance;
  8. use App\Util\ActivityPub\Helpers;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. class SendUpdateActor extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'ap:update-actors {--force}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Send Update Actor activities to known remote servers to force updates';
  24. /**
  25. * Execute the console command.
  26. *
  27. * @return int
  28. */
  29. public function handle()
  30. {
  31. $totalUserCount = Profile::whereNotNull('user_id')->count();
  32. $totalInstanceCount = Instance::count();
  33. $this->info('Found ' . $totalUserCount . ' local accounts and ' . $totalInstanceCount . ' remote instances');
  34. $task = $this->choice(
  35. 'What do you want to do?',
  36. [
  37. 'View top instances',
  38. 'Send updates to an instance'
  39. ],
  40. 0
  41. );
  42. if($task === 'View top instances') {
  43. $this->table(
  44. ['domain', 'user_count', 'last_synced'],
  45. Instance::orderByDesc('user_count')->take(20)->get(['domain', 'user_count', 'actors_last_synced_at'])->toArray()
  46. );
  47. return Command::SUCCESS;
  48. } else {
  49. $domain = $this->anticipate('Enter the instance domain', function ($input) {
  50. return Instance::where('domain', 'like', '%' . $input . '%')->pluck('domain')->toArray();
  51. });
  52. if(!$this->confirm('Are you sure you want to send actor updates to ' . $domain . '?')) {
  53. return;
  54. }
  55. if($cur = Instance::whereDomain($domain)->whereNotNull('actors_last_synced_at')->first()) {
  56. if(!$this->option('force')) {
  57. $this->error('ERROR: Cannot re-sync this instance, it was already synced on ' . $cur->actors_last_synced_at);
  58. return;
  59. }
  60. }
  61. $this->touchStorageCache($domain);
  62. $this->line(' ');
  63. $this->error('Keep this window open during this process or it will not complete!');
  64. $sharedInbox = Profile::whereDomain($domain)->whereNotNull('sharedInbox')->first();
  65. if(!$sharedInbox) {
  66. $this->error('ERROR: Cannot find the sharedInbox of ' . $domain);
  67. return;
  68. }
  69. $url = $sharedInbox->sharedInbox;
  70. $this->line(' ');
  71. $this->info('Found sharedInbox: ' . $url);
  72. $bar = $this->output->createProgressBar($totalUserCount);
  73. $bar->start();
  74. $startCache = $this->getStorageCache($domain);
  75. User::whereNull('status')->when($startCache, function($query, $startCache) {
  76. return $query->where('id', '>', $startCache);
  77. })->chunk(50, function($users) use($bar, $url, $domain) {
  78. foreach($users as $user) {
  79. $this->updateStorageCache($domain, $user->id);
  80. $profile = Profile::find($user->profile_id);
  81. if(!$profile) {
  82. continue;
  83. }
  84. $body = $this->updateObject($profile);
  85. try {
  86. Helpers::sendSignedObject($profile, $url, $body);
  87. } catch (HttpException $e) {
  88. continue;
  89. }
  90. $bar->advance();
  91. }
  92. });
  93. $bar->finish();
  94. $this->line(' ');
  95. $instance = Instance::whereDomain($domain)->firstOrFail();
  96. $instance->actors_last_synced_at = now();
  97. $instance->save();
  98. $this->info('Finished!');
  99. return Command::SUCCESS;
  100. }
  101. return Command::SUCCESS;
  102. }
  103. protected function updateObject($profile)
  104. {
  105. return [
  106. '@context' => [
  107. 'https://w3id.org/security/v1',
  108. 'https://www.w3.org/ns/activitystreams',
  109. [
  110. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
  111. ],
  112. ],
  113. 'id' => $profile->permalink('#updates/' . time()),
  114. 'actor' => $profile->permalink(),
  115. 'type' => 'Update',
  116. 'object' => $this->actorObject($profile)
  117. ];
  118. }
  119. protected function touchStorageCache($domain)
  120. {
  121. $path = 'actor-update-cache/' . $domain;
  122. if(!Storage::exists($path)) {
  123. Storage::put($path, "");
  124. }
  125. }
  126. protected function getStorageCache($domain)
  127. {
  128. $path = 'actor-update-cache/' . $domain;
  129. return Storage::get($path);
  130. }
  131. protected function updateStorageCache($domain, $value)
  132. {
  133. $path = 'actor-update-cache/' . $domain;
  134. Storage::put($path, $value);
  135. }
  136. protected function actorObject($profile)
  137. {
  138. $permalink = $profile->permalink();
  139. return [
  140. 'id' => $permalink,
  141. 'type' => 'Person',
  142. 'following' => $permalink . '/following',
  143. 'followers' => $permalink . '/followers',
  144. 'inbox' => $permalink . '/inbox',
  145. 'outbox' => $permalink . '/outbox',
  146. 'preferredUsername' => $profile->username,
  147. 'name' => $profile->name,
  148. 'summary' => $profile->bio,
  149. 'url' => $profile->url(),
  150. 'manuallyApprovesFollowers' => (bool) $profile->is_private,
  151. 'publicKey' => [
  152. 'id' => $permalink . '#main-key',
  153. 'owner' => $permalink,
  154. 'publicKeyPem' => $profile->public_key,
  155. ],
  156. 'icon' => [
  157. 'type' => 'Image',
  158. 'mediaType' => 'image/jpeg',
  159. 'url' => $profile->avatarUrl(),
  160. ],
  161. 'endpoints' => [
  162. 'sharedInbox' => config('app.url') . '/f/inbox'
  163. ]
  164. ];
  165. }
  166. }