1
0

SharePipeline.php 2.1 KB

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