LikePipeline.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Jobs\LikePipeline;
  3. use App\Like;
  4. use App\Notification;
  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 LikePipeline implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $like;
  17. /**
  18. * Delete the job if its models no longer exist.
  19. *
  20. * @var bool
  21. */
  22. public $deleteWhenMissingModels = true;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(Like $like)
  29. {
  30. $this->like = $like;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $like = $this->like;
  40. $status = $this->like->status;
  41. $actor = $this->like->actor;
  42. if (!$status || $status->url !== null) {
  43. // Ignore notifications to remote statuses, or deleted statuses
  44. return;
  45. }
  46. $exists = Notification::whereProfileId($status->profile_id)
  47. ->whereActorId($actor->id)
  48. ->whereAction('like')
  49. ->whereItemId($status->id)
  50. ->whereItemType('App\Status')
  51. ->count();
  52. if ($actor->id === $status->profile_id || $exists !== 0) {
  53. return true;
  54. }
  55. try {
  56. $notification = new Notification();
  57. $notification->profile_id = $status->profile_id;
  58. $notification->actor_id = $actor->id;
  59. $notification->action = 'like';
  60. $notification->message = $like->toText();
  61. $notification->rendered = $like->toHtml();
  62. $notification->item_id = $status->id;
  63. $notification->item_type = "App\Status";
  64. $notification->save();
  65. Cache::forever('notification.'.$notification->id, $notification);
  66. $redis = Redis::connection();
  67. $key = config('cache.prefix').':user.'.$status->profile_id.'.notifications';
  68. $redis->lpush($key, $notification->id);
  69. } catch (Exception $e) {
  70. Log::error($e);
  71. }
  72. }
  73. }