LikePipeline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(Like $like)
  31. {
  32. $this->like = $like;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. $like = $this->like;
  42. $status = $this->like->status;
  43. $actor = $this->like->actor;
  44. if (!$status) {
  45. // Ignore notifications to deleted statuses
  46. return;
  47. }
  48. if($status->url && $actor->domain == null) {
  49. return $this->remoteLikeDeliver();
  50. }
  51. $exists = Notification::whereProfileId($status->profile_id)
  52. ->whereActorId($actor->id)
  53. ->whereAction('like')
  54. ->whereItemId($status->id)
  55. ->whereItemType('App\Status')
  56. ->count();
  57. if ($actor->id === $status->profile_id || $exists !== 0) {
  58. return true;
  59. }
  60. try {
  61. $notification = new Notification();
  62. $notification->profile_id = $status->profile_id;
  63. $notification->actor_id = $actor->id;
  64. $notification->action = 'like';
  65. $notification->message = $like->toText($status->in_reply_to_id ? 'comment' : 'post');
  66. $notification->rendered = $like->toHtml($status->in_reply_to_id ? 'comment' : 'post');
  67. $notification->item_id = $status->id;
  68. $notification->item_type = "App\Status";
  69. $notification->save();
  70. } catch (Exception $e) {
  71. }
  72. }
  73. public function remoteLikeDeliver()
  74. {
  75. $like = $this->like;
  76. $status = $this->like->status;
  77. $actor = $this->like->actor;
  78. $fractal = new Fractal\Manager();
  79. $fractal->setSerializer(new ArraySerializer());
  80. $resource = new Fractal\Resource\Item($like, new LikeTransformer());
  81. $activity = $fractal->createData($resource)->toArray();
  82. $url = $status->profile->sharedInbox ?? $status->profile->inbox_url;
  83. Helpers::sendSignedObject($actor, $url, $activity);
  84. }
  85. }