CommentPipeline.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Jobs\CommentPipeline;
  3. use App\{
  4. Notification,
  5. Status,
  6. UserFilter
  7. };
  8. use App\Services\NotificationService;
  9. use App\Services\StatusService;
  10. use DB, Cache, Log;
  11. use Illuminate\Support\Facades\Redis;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. class CommentPipeline implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $status;
  21. protected $comment;
  22. /**
  23. * Delete the job if its models no longer exist.
  24. *
  25. * @var bool
  26. */
  27. public $deleteWhenMissingModels = true;
  28. public $timeout = 5;
  29. public $tries = 1;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct(Status $status, Status $comment)
  36. {
  37. $this->status = $status;
  38. $this->comment = $comment;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $status = $this->status;
  48. $comment = $this->comment;
  49. $target = $status->profile;
  50. $actor = $comment->profile;
  51. if(config('database.default') === 'mysql') {
  52. $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' => $status->id]);
  53. $status->reply_count = count($count);
  54. $status->save();
  55. } else {
  56. $status->reply_count = $status->reply_count + 1;
  57. $status->save();
  58. }
  59. StatusService::del($comment->id);
  60. StatusService::del($status->id);
  61. Cache::forget('status:replies:all:' . $comment->id);
  62. Cache::forget('status:replies:all:' . $status->id);
  63. if ($actor->id === $target->id || $status->comments_disabled == true) {
  64. return true;
  65. }
  66. $filtered = UserFilter::whereUserId($target->id)
  67. ->whereFilterableType('App\Profile')
  68. ->whereIn('filter_type', ['mute', 'block'])
  69. ->whereFilterableId($actor->id)
  70. ->exists();
  71. if($filtered == true) {
  72. return;
  73. }
  74. DB::transaction(function() use($target, $actor, $comment) {
  75. $notification = new Notification();
  76. $notification->profile_id = $target->id;
  77. $notification->actor_id = $actor->id;
  78. $notification->action = 'comment';
  79. $notification->message = $comment->replyToText();
  80. $notification->rendered = $comment->replyToHtml();
  81. $notification->item_id = $comment->id;
  82. $notification->item_type = "App\Status";
  83. $notification->save();
  84. NotificationService::setNotification($notification);
  85. NotificationService::set($notification->profile_id, $notification->id);
  86. StatusService::del($comment->id);
  87. });
  88. if($exists = Cache::get('status:replies:all:' . $status->id)) {
  89. if($exists && $exists->count() == 3) {
  90. } else {
  91. Cache::forget('status:replies:all:' . $status->id);
  92. }
  93. } else {
  94. Cache::forget('status:replies:all:' . $status->id);
  95. }
  96. }
  97. }