1
0

SendUpdateActor.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) use($bar) {
  76. $bar->advance($startCache);
  77. return $query->where('id', '>', $startCache);
  78. })->chunk(50, function($users) use($bar, $url, $domain) {
  79. foreach($users as $user) {
  80. $this->updateStorageCache($domain, $user->id);
  81. $profile = Profile::find($user->profile_id);
  82. if(!$profile) {
  83. continue;
  84. }
  85. $body = $this->updateObject($profile);
  86. try {
  87. Helpers::sendSignedObject($profile, $url, $body);
  88. } catch (HttpException $e) {
  89. continue;
  90. }
  91. $bar->advance();
  92. }
  93. });
  94. $bar->finish();
  95. $this->line(' ');
  96. $instance = Instance::whereDomain($domain)->firstOrFail();
  97. $instance->actors_last_synced_at = now();
  98. $instance->save();
  99. $this->info('Finished!');
  100. return Command::SUCCESS;
  101. }
  102. return Command::SUCCESS;
  103. }
  104. protected function updateObject($profile)
  105. {
  106. return [
  107. '@context' => [
  108. 'https://w3id.org/security/v1',
  109. 'https://www.w3.org/ns/activitystreams',
  110. [
  111. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
  112. ],
  113. ],
  114. 'id' => $profile->permalink('#updates/' . time()),
  115. 'actor' => $profile->permalink(),
  116. 'type' => 'Update',
  117. 'object' => $this->actorObject($profile)
  118. ];
  119. }
  120. protected function touchStorageCache($domain)
  121. {
  122. $path = 'actor-update-cache/' . $domain;
  123. if(!Storage::exists($path)) {
  124. Storage::put($path, "");
  125. }
  126. }
  127. protected function getStorageCache($domain)
  128. {
  129. $path = 'actor-update-cache/' . $domain;
  130. return Storage::get($path);
  131. }
  132. protected function updateStorageCache($domain, $value)
  133. {
  134. $path = 'actor-update-cache/' . $domain;
  135. Storage::put($path, $value);
  136. }
  137. protected function actorObject($profile)
  138. {
  139. $permalink = $profile->permalink();
  140. return [
  141. 'id' => $permalink,
  142. 'type' => 'Person',
  143. 'following' => $permalink . '/following',
  144. 'followers' => $permalink . '/followers',
  145. 'inbox' => $permalink . '/inbox',
  146. 'outbox' => $permalink . '/outbox',
  147. 'preferredUsername' => $profile->username,
  148. 'name' => $profile->name,
  149. 'summary' => $profile->bio,
  150. 'url' => $profile->url(),
  151. 'manuallyApprovesFollowers' => (bool) $profile->is_private,
  152. 'publicKey' => [
  153. 'id' => $permalink . '#main-key',
  154. 'owner' => $permalink,
  155. 'publicKeyPem' => $profile->public_key,
  156. ],
  157. 'icon' => [
  158. 'type' => 'Image',
  159. 'mediaType' => 'image/jpeg',
  160. 'url' => $profile->avatarUrl(),
  161. ],
  162. 'endpoints' => [
  163. 'sharedInbox' => config('app.url') . '/f/inbox'
  164. ]
  165. ];
  166. }
  167. }