HandleUpdateActivity.php 2.7 KB

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