1
0

StatusReplyPipeline.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. use App\Services\StatusService;
  15. class StatusReplyPipeline implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $status;
  19. /**
  20. * Delete the job if its models no longer exist.
  21. *
  22. * @var bool
  23. */
  24. public $deleteWhenMissingModels = true;
  25. public $timeout = 60;
  26. public $tries = 2;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(Status $status)
  33. {
  34. $this->status = $status;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. $status = $this->status;
  44. $actor = $status->profile;
  45. $reply = Status::find($status->in_reply_to_id);
  46. if(!$actor || !$reply) {
  47. return 1;
  48. }
  49. $target = $reply->profile;
  50. $exists = Notification::whereProfileId($target->id)
  51. ->whereActorId($actor->id)
  52. ->whereIn('action', ['mention', 'comment'])
  53. ->whereItemId($status->id)
  54. ->whereItemType('App\Status')
  55. ->count();
  56. if ($actor->id === $target || $exists !== 0) {
  57. return 1;
  58. }
  59. if(config('database.default') === 'mysql') {
  60. // todo: refactor
  61. // $exp = 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)");
  62. // $expQuery = $exp->getValue(DB::connection()->getQueryGrammar());
  63. // $count = DB::select($expQuery, [ 'kid' => $reply->id ]);
  64. // $reply->reply_count = count($count);
  65. $reply->reply_count = $reply->reply_count + 1;
  66. $reply->save();
  67. } else {
  68. $reply->reply_count = $reply->reply_count + 1;
  69. $reply->save();
  70. }
  71. StatusService::del($reply->id);
  72. StatusService::del($status->id);
  73. Cache::forget('status:replies:all:' . $reply->id);
  74. Cache::forget('status:replies:all:' . $status->id);
  75. DB::transaction(function() use($target, $actor, $status) {
  76. $notification = new Notification();
  77. $notification->profile_id = $target->id;
  78. $notification->actor_id = $actor->id;
  79. $notification->action = 'comment';
  80. $notification->item_id = $status->id;
  81. $notification->item_type = "App\Status";
  82. $notification->save();
  83. NotificationService::setNotification($notification);
  84. NotificationService::set($notification->profile_id, $notification->id);
  85. });
  86. if($exists = Cache::get('status:replies:all:' . $reply->id)) {
  87. if($exists && $exists->count() == 3) {
  88. } else {
  89. Cache::forget('status:replies:all:' . $reply->id);
  90. }
  91. } else {
  92. Cache::forget('status:replies:all:' . $reply->id);
  93. }
  94. return 1;
  95. }
  96. }