FollowPipeline.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Jobs\FollowPipeline;
  3. use App\Follower;
  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 Illuminate\Support\Facades\Redis;
  13. use App\Services\AccountService;
  14. use App\Services\FollowerService;
  15. class FollowPipeline implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $follower;
  19. /**
  20. * Delete the job if its models no longer exist.
  21. *
  22. * @var bool
  23. */
  24. public $deleteWhenMissingModels = true;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct($follower)
  31. {
  32. $this->follower = $follower;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. $follower = $this->follower;
  42. $actor = $follower->actor;
  43. $target = $follower->target;
  44. if(!$actor || !$target) {
  45. return;
  46. }
  47. if($target->domain || !$target->private_key) {
  48. return;
  49. }
  50. Cache::forget('profile:following:' . $actor->id);
  51. Cache::forget('profile:following:' . $target->id);
  52. FollowerService::add($actor->id, $target->id);
  53. $count = Follower::whereProfileId($actor->id)->count();
  54. $actor->following_count = $count;
  55. $actor->save();
  56. AccountService::del($actor->id);
  57. $count = Follower::whereFollowingId($target->id)->count();
  58. $target->followers_count = $count;
  59. $target->save();
  60. AccountService::del($target->id);
  61. try {
  62. $notification = new Notification();
  63. $notification->profile_id = $target->id;
  64. $notification->actor_id = $actor->id;
  65. $notification->action = 'follow';
  66. $notification->item_id = $target->id;
  67. $notification->item_type = "App\Profile";
  68. $notification->save();
  69. } catch (Exception $e) {
  70. Log::error($e);
  71. }
  72. }
  73. }