LikePipeline.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Jobs\LikePipeline;
  3. use App\Jobs\PushNotificationPipeline\LikePushNotifyPipeline;
  4. use App\Like;
  5. use App\Notification;
  6. use App\Services\NotificationAppGatewayService;
  7. use App\Services\PushNotificationService;
  8. use App\Services\StatusService;
  9. use App\Transformer\ActivityPub\Verb\Like as LikeTransformer;
  10. use App\User;
  11. use App\Util\ActivityPub\Helpers;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. use League\Fractal;
  18. use League\Fractal\Serializer\ArraySerializer;
  19. class LikePipeline implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $like;
  23. /**
  24. * Delete the job if its models no longer exist.
  25. *
  26. * @var bool
  27. */
  28. public $deleteWhenMissingModels = true;
  29. public $timeout = 5;
  30. public $tries = 1;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(Like $like)
  37. {
  38. $this->like = $like;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $like = $this->like;
  48. $status = $this->like->status;
  49. $actor = $this->like->actor;
  50. if (! $status) {
  51. // Ignore notifications to deleted statuses
  52. return;
  53. }
  54. StatusService::refresh($status->id);
  55. if ($status->url && $actor->domain == null) {
  56. return $this->remoteLikeDeliver();
  57. }
  58. $exists = Notification::whereProfileId($status->profile_id)
  59. ->whereActorId($actor->id)
  60. ->whereAction('like')
  61. ->whereItemId($status->id)
  62. ->whereItemType('App\Status')
  63. ->count();
  64. if ($actor->id === $status->profile_id || $exists) {
  65. return true;
  66. }
  67. if ($status->uri === null && $status->object_url === null && $status->url === null) {
  68. try {
  69. $notification = new Notification;
  70. $notification->profile_id = $status->profile_id;
  71. $notification->actor_id = $actor->id;
  72. $notification->action = 'like';
  73. $notification->item_id = $status->id;
  74. $notification->item_type = "App\Status";
  75. $notification->save();
  76. } catch (Exception $e) {
  77. }
  78. if (NotificationAppGatewayService::enabled()) {
  79. if (PushNotificationService::check('like', $status->profile_id)) {
  80. $user = User::whereProfileId($status->profile_id)->first();
  81. if ($user && $user->expo_token && $user->notify_enabled) {
  82. LikePushNotifyPipeline::dispatchSync($user->expo_token, $actor->username);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. public function remoteLikeDeliver()
  89. {
  90. $like = $this->like;
  91. $status = $this->like->status;
  92. $actor = $this->like->actor;
  93. $fractal = new Fractal\Manager;
  94. $fractal->setSerializer(new ArraySerializer);
  95. $resource = new Fractal\Resource\Item($like, new LikeTransformer);
  96. $activity = $fractal->createData($resource)->toArray();
  97. $url = $status->profile->sharedInbox ?? $status->profile->inbox_url;
  98. Helpers::sendSignedObject($actor, $url, $activity);
  99. }
  100. }