MentionPipeline.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Jobs\MentionPipeline;
  3. use App\Mention;
  4. use App\Notification;
  5. use App\Status;
  6. use App\User;
  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 App\Jobs\PushNotificationPipeline\MentionPushNotifyPipeline;
  13. use App\Services\NotificationAppGatewayService;
  14. use App\Services\PushNotificationService;
  15. use App\Services\StatusService;
  16. class MentionPipeline implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $status;
  20. protected $mention;
  21. /**
  22. * Delete the job if its models no longer exist.
  23. *
  24. * @var bool
  25. */
  26. public $deleteWhenMissingModels = true;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(Status $status, Mention $mention)
  33. {
  34. $this->status = $status;
  35. $this->mention = $mention;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $status = $this->status;
  45. $mention = $this->mention;
  46. $actor = $this->status->profile;
  47. $target = $this->mention->profile_id;
  48. $exists = Notification::whereProfileId($target)
  49. ->whereActorId($actor->id)
  50. ->whereIn('action', ['mention', 'comment'])
  51. ->whereItemId($status->id)
  52. ->whereItemType('App\Status')
  53. ->count();
  54. if ($actor->id === $target || $exists !== 0) {
  55. return;
  56. }
  57. Notification::firstOrCreate(
  58. [
  59. 'profile_id' => $target,
  60. 'actor_id' => $actor->id,
  61. 'action' => 'mention',
  62. 'item_type' => 'App\Status',
  63. 'item_id' => $status->id,
  64. ]
  65. );
  66. StatusService::del($status->id);
  67. if (NotificationAppGatewayService::enabled()) {
  68. if (PushNotificationService::check('mention', $target)) {
  69. $user = User::whereProfileId($target)->first();
  70. if ($user && $user->expo_token && $user->notify_enabled) {
  71. MentionPushNotifyPipeline::dispatch($user->expo_token, $actor->username)->onQueue('pushnotify');
  72. }
  73. }
  74. }
  75. }
  76. }