LikePipeline.php 1.7 KB

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