LikePipeline.php 2.1 KB

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