FollowPipeline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Jobs\FollowPipeline;
  3. use App\Follower;
  4. use App\Jobs\PushNotificationPipeline\FollowPushNotifyPipeline;
  5. use App\Notification;
  6. use App\Services\AccountService;
  7. use App\Services\FollowerService;
  8. use App\Services\NotificationAppGatewayService;
  9. use App\Services\PushNotificationService;
  10. use App\User;
  11. use Cache;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. use Log;
  18. class FollowPipeline implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $follower;
  22. /**
  23. * Delete the job if its models no longer exist.
  24. *
  25. * @var bool
  26. */
  27. public $deleteWhenMissingModels = true;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct($follower)
  34. {
  35. $this->follower = $follower;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $follower = $this->follower;
  45. $actor = $follower->actor;
  46. $target = $follower->target;
  47. if (! $actor || ! $target) {
  48. return;
  49. }
  50. if ($target->domain || ! $target->private_key) {
  51. return;
  52. }
  53. Cache::forget('profile:following:'.$actor->id);
  54. Cache::forget('profile:following:'.$target->id);
  55. FollowerService::add($actor->id, $target->id);
  56. $count = Follower::whereProfileId($actor->id)->count();
  57. $actor->following_count = $count;
  58. $actor->save();
  59. AccountService::del($actor->id);
  60. $count = Follower::whereFollowingId($target->id)->count();
  61. $target->followers_count = $count;
  62. $target->save();
  63. AccountService::del($target->id);
  64. if ($target->user_id && $target->domain === null) {
  65. try {
  66. $notification = new Notification;
  67. $notification->profile_id = $target->id;
  68. $notification->actor_id = $actor->id;
  69. $notification->action = 'follow';
  70. $notification->item_id = $target->id;
  71. $notification->item_type = "App\Profile";
  72. $notification->save();
  73. } catch (Exception $e) {
  74. Log::error($e);
  75. }
  76. if (NotificationAppGatewayService::enabled()) {
  77. if (PushNotificationService::check('follow', $target->id)) {
  78. $user = User::whereProfileId($target->id)->first();
  79. if ($user && $user->expo_token && $user->notify_enabled) {
  80. FollowPushNotifyPipeline::dispatch($user->expo_token, $actor->username)->onQueue('pushnotify');
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }