1
0

MentionPipeline.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Jobs\MentionPipeline;
  3. use App\Mention;
  4. use App\Notification;
  5. use App\Status;
  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. class MentionPipeline implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. protected $status;
  15. protected $mention;
  16. /**
  17. * Delete the job if its models no longer exist.
  18. *
  19. * @var bool
  20. */
  21. public $deleteWhenMissingModels = true;
  22. /**
  23. * Create a new job instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct(Status $status, Mention $mention)
  28. {
  29. $this->status = $status;
  30. $this->mention = $mention;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $status = $this->status;
  40. $mention = $this->mention;
  41. $actor = $this->status->profile;
  42. $target = $this->mention->profile_id;
  43. $exists = Notification::whereProfileId($target)
  44. ->whereActorId($actor->id)
  45. ->whereIn('action', ['mention', 'comment'])
  46. ->whereItemId($status->id)
  47. ->whereItemType('App\Status')
  48. ->count();
  49. if ($actor->id === $target || $exists !== 0) {
  50. return true;
  51. }
  52. try {
  53. $notification = new Notification();
  54. $notification->profile_id = $target;
  55. $notification->actor_id = $actor->id;
  56. $notification->action = 'mention';
  57. $notification->message = $mention->toText();
  58. $notification->rendered = $mention->toHtml();
  59. $notification->item_id = $status->id;
  60. $notification->item_type = "App\Status";
  61. $notification->save();
  62. } catch (Exception $e) {
  63. }
  64. }
  65. }