CommentPipeline.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Jobs\CommentPipeline;
  3. use App\Notification;
  4. use App\Status;
  5. use Cache;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. use Redis;
  13. class CommentPipeline implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $status;
  17. protected $comment;
  18. /**
  19. * Create a new job instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct(Status $status, Status $comment)
  24. {
  25. $this->status = $status;
  26. $this->comment = $comment;
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $status = $this->status;
  36. $comment = $this->comment;
  37. $target = $status->profile;
  38. $actor = $comment->profile;
  39. if ($actor->id === $target->id) {
  40. return true;
  41. }
  42. try {
  43. $notification = new Notification();
  44. $notification->profile_id = $target->id;
  45. $notification->actor_id = $actor->id;
  46. $notification->action = 'comment';
  47. $notification->message = $comment->replyToText();
  48. $notification->rendered = $comment->replyToHtml();
  49. $notification->item_id = $comment->id;
  50. $notification->item_type = "App\Status";
  51. $notification->save();
  52. Cache::forever('notification.'.$notification->id, $notification);
  53. $redis = Redis::connection();
  54. $nkey = config('cache.prefix').':user.'.$target->id.'.notifications';
  55. $redis->lpush($nkey, $notification->id);
  56. } catch (Exception $e) {
  57. Log::error($e);
  58. }
  59. }
  60. }