CommentPipeline.php 2.8 KB

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