StatusReplyPipeline.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\Notification;
  4. use App\Status;
  5. use Cache;
  6. use DB;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Redis;
  13. use App\Services\NotificationService;
  14. class StatusReplyPipeline implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $status;
  18. /**
  19. * Delete the job if its models no longer exist.
  20. *
  21. * @var bool
  22. */
  23. public $deleteWhenMissingModels = true;
  24. public $timeout = 5;
  25. public $tries = 1;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(Status $status)
  32. {
  33. $this->status = $status;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $status = $this->status;
  43. $actor = $status->profile;
  44. $reply = Status::find($status->in_reply_to_id);
  45. if(!$actor || !$reply) {
  46. return 1;
  47. }
  48. $target = $reply->profile;
  49. $exists = Notification::whereProfileId($target->id)
  50. ->whereActorId($actor->id)
  51. ->whereIn('action', ['mention', 'comment'])
  52. ->whereItemId($status->id)
  53. ->whereItemType('App\Status')
  54. ->count();
  55. if ($actor->id === $target || $exists !== 0) {
  56. return 1;
  57. }
  58. DB::transaction(function() use($target, $actor, $status) {
  59. $notification = new Notification();
  60. $notification->profile_id = $target->id;
  61. $notification->actor_id = $actor->id;
  62. $notification->action = 'comment';
  63. $notification->message = $status->replyToText();
  64. $notification->rendered = $status->replyToHtml();
  65. $notification->item_id = $status->id;
  66. $notification->item_type = "App\Status";
  67. $notification->save();
  68. NotificationService::setNotification($notification);
  69. NotificationService::set($notification->profile_id, $notification->id);
  70. });
  71. return 1;
  72. }
  73. }