CommentPipeline.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. }
  56. if ($actor->id === $target->id || $status->comments_disabled == true) {
  57. return true;
  58. }
  59. $filtered = UserFilter::whereUserId($target->id)
  60. ->whereFilterableType('App\Profile')
  61. ->whereIn('filter_type', ['mute', 'block'])
  62. ->whereFilterableId($actor->id)
  63. ->exists();
  64. if($filtered == true) {
  65. return;
  66. }
  67. DB::transaction(function() use($target, $actor, $comment) {
  68. $notification = new Notification();
  69. $notification->profile_id = $target->id;
  70. $notification->actor_id = $actor->id;
  71. $notification->action = 'comment';
  72. $notification->message = $comment->replyToText();
  73. $notification->rendered = $comment->replyToHtml();
  74. $notification->item_id = $comment->id;
  75. $notification->item_type = "App\Status";
  76. $notification->save();
  77. NotificationService::setNotification($notification);
  78. NotificationService::set($notification->profile_id, $notification->id);
  79. StatusService::del($comment->id);
  80. });
  81. if($exists = Cache::get('status:replies:all:' . $status->id)) {
  82. if($exists && $exists->count() == 3) {
  83. } else {
  84. Cache::forget('status:replies:all:' . $status->id);
  85. }
  86. } else {
  87. Cache::forget('status:replies:all:' . $status->id);
  88. }
  89. }
  90. }