1
0

StatusReplyPipeline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if(config('database.default') === 'mysql') {
  59. DB::transaction(function() use($reply) {
  60. $count = DB::select( DB::raw("select id, in_reply_to_id from statuses, (select @pv := :kid) initialisation where id > @pv and find_in_set(in_reply_to_id, @pv) > 0 and @pv := concat(@pv, ',', id)"), [ 'kid' => $reply->id]);
  61. $reply->reply_count = count($count);
  62. $reply->save();
  63. });
  64. }
  65. DB::transaction(function() use($target, $actor, $status) {
  66. $notification = new Notification();
  67. $notification->profile_id = $target->id;
  68. $notification->actor_id = $actor->id;
  69. $notification->action = 'comment';
  70. $notification->message = $status->replyToText();
  71. $notification->rendered = $status->replyToHtml();
  72. $notification->item_id = $status->id;
  73. $notification->item_type = "App\Status";
  74. $notification->save();
  75. NotificationService::setNotification($notification);
  76. NotificationService::set($notification->profile_id, $notification->id);
  77. });
  78. if($exists = Cache::get('status:replies:all:' . $reply->id)) {
  79. if($exists && $exists->count() == 3) {
  80. } else {
  81. Cache::forget('status:replies:all:' . $reply->id);
  82. }
  83. } else {
  84. Cache::forget('status:replies:all:' . $reply->id);
  85. }
  86. return 1;
  87. }
  88. }