LikePipeline.php 2.9 KB

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