CommentPipeline.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Delete the job if its models no longer exist.
  20. *
  21. * @var bool
  22. */
  23. public $deleteWhenMissingModels = true;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(Status $status, Status $comment)
  30. {
  31. $this->status = $status;
  32. $this->comment = $comment;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. $status = $this->status;
  42. $comment = $this->comment;
  43. $target = $status->profile;
  44. $actor = $comment->profile;
  45. if ($actor->id === $target->id) {
  46. return true;
  47. }
  48. try {
  49. $notification = new Notification();
  50. $notification->profile_id = $target->id;
  51. $notification->actor_id = $actor->id;
  52. $notification->action = 'comment';
  53. $notification->message = $comment->replyToText();
  54. $notification->rendered = $comment->replyToHtml();
  55. $notification->item_id = $comment->id;
  56. $notification->item_type = "App\Status";
  57. $notification->save();
  58. Cache::forever('notification.'.$notification->id, $notification);
  59. $redis = Redis::connection();
  60. $nkey = config('cache.prefix').':user.'.$target->id.'.notifications';
  61. $redis->lpush($nkey, $notification->id);
  62. } catch (Exception $e) {
  63. Log::error($e);
  64. }
  65. }
  66. }