LikePipeline.php 3.0 KB

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