HandleUpdateActivity.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Jobs\ProfilePipeline;
  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\Profile;
  11. use App\Util\ActivityPub\Helpers;
  12. use Cache;
  13. use Purify;
  14. use App\Jobs\AvatarPipeline\RemoteAvatarFetch;
  15. use App\Util\Lexer\Autolink;
  16. class HandleUpdateActivity implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $payload;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct($payload)
  26. {
  27. $this->payload = $payload;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle(): void
  35. {
  36. $payload = $this->payload;
  37. if(empty($payload) || !isset($payload['actor'])) {
  38. return;
  39. }
  40. $profile = Profile::whereRemoteUrl($payload['actor'])->first();
  41. if(!$profile || $profile->domain === null || $profile->private_key) {
  42. return;
  43. }
  44. if($profile->sharedInbox == null || $profile->sharedInbox != $payload['object']['endpoints']['sharedInbox']) {
  45. $profile->sharedInbox = $payload['object']['endpoints']['sharedInbox'];
  46. }
  47. if($profile->public_key !== $payload['object']['publicKey']['publicKeyPem']) {
  48. $profile->public_key = $payload['object']['publicKey']['publicKeyPem'];
  49. }
  50. if($profile->bio !== $payload['object']['summary']) {
  51. $len = strlen(strip_tags($payload['object']['summary']));
  52. if($len) {
  53. if($len > 500) {
  54. $updated = strip_tags($payload['object']['summary']);
  55. $updated = substr($updated, 0, config('pixelfed.max_bio_length'));
  56. $profile->bio = Autolink::create()->autolink($updated);
  57. } else {
  58. $profile->bio = Purify::clean($payload['object']['summary']);
  59. }
  60. } else {
  61. $profile->bio = null;
  62. }
  63. }
  64. if($profile->name !== $payload['object']['name']) {
  65. $profile->name = Purify::clean(substr($payload['object']['name'], 0, config('pixelfed.max_name_length')));
  66. }
  67. if($profile->isDirty()) {
  68. $profile->save();
  69. }
  70. if(isset($payload['object']['icon'])) {
  71. RemoteAvatarFetch::dispatch($profile)->onQueue('low');
  72. } else {
  73. $profile->avatar->update(['remote_url' => null]);
  74. Cache::forget('avatar:' . $profile->id);
  75. }
  76. return;
  77. }
  78. }