LikePipeline.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $exists = Notification::whereProfileId($status->profile_id)
  34. ->whereActorId($actor->id)
  35. ->whereAction('like')
  36. ->whereItemId($status->id)
  37. ->whereItemType('App\Status')
  38. ->count();
  39. if($actor->id === $status->profile_id || $exists !== 0) {
  40. return true;
  41. }
  42. try {
  43. $notification = new Notification;
  44. $notification->profile_id = $status->profile_id;
  45. $notification->actor_id = $actor->id;
  46. $notification->action = 'like';
  47. $notification->message = $like->toText();
  48. $notification->rendered = $like->toHtml();
  49. $notification->item_id = $status->id;
  50. $notification->item_type = "App\Status";
  51. $notification->save();
  52. Cache::forever('notification.' . $notification->id, $notification);
  53. $redis = Redis::connection();
  54. $key = config('cache.prefix').':user.' . $status->profile_id . '.notifications';
  55. $redis->lpush($key, $notification->id);
  56. } catch (Exception $e) {
  57. Log::error($e);
  58. }
  59. }
  60. }