1
0

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