FollowerController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. Follower,
  5. FollowRequest,
  6. Profile,
  7. UserFilter
  8. };
  9. use Auth, Cache;
  10. use Illuminate\Http\Request;
  11. use App\Jobs\FollowPipeline\FollowPipeline;
  12. use App\Util\ActivityPub\Helpers;
  13. class FollowerController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. public function store(Request $request)
  20. {
  21. $this->validate($request, [
  22. 'item' => 'required|string',
  23. ]);
  24. $item = (int) $request->input('item');
  25. $this->handleFollowRequest($item);
  26. return response()->json(200);
  27. }
  28. protected function handleFollowRequest($item)
  29. {
  30. $user = Auth::user()->profile;
  31. $target = Profile::where('id', '!=', $user->id)->whereNull('status')->findOrFail($item);
  32. $private = (bool) $target->is_private;
  33. $remote = (bool) $target->domain;
  34. $blocked = UserFilter::whereUserId($target->id)
  35. ->whereFilterType('block')
  36. ->whereFilterableId($user->id)
  37. ->whereFilterableType('App\Profile')
  38. ->exists();
  39. if($blocked == true) {
  40. abort(400, 'You cannot follow this user.');
  41. }
  42. $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
  43. if($private == true && $isFollowing == 0 || $remote == true) {
  44. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  45. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  46. }
  47. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  48. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  49. }
  50. $follow = FollowRequest::firstOrCreate([
  51. 'follower_id' => $user->id,
  52. 'following_id' => $target->id
  53. ]);
  54. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  55. $this->sendFollow($user, $target);
  56. }
  57. } elseif ($isFollowing == 0) {
  58. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  59. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  60. }
  61. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  62. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  63. }
  64. $follower = new Follower();
  65. $follower->profile_id = $user->id;
  66. $follower->following_id = $target->id;
  67. $follower->save();
  68. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  69. $this->sendFollow($user, $target);
  70. }
  71. FollowPipeline::dispatch($follower);
  72. } else {
  73. $request = FollowRequest::whereFollowerId($user->id)->whereFollowingId($target->id)->exists();
  74. $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
  75. if($remote == true && $request && !$follower) {
  76. $this->sendFollow($user, $target);
  77. }
  78. if($remote == true && $follower) {
  79. $this->sendUndoFollow($user, $target);
  80. }
  81. Follower::whereProfileId($user->id)
  82. ->whereFollowingId($target->id)
  83. ->delete();
  84. }
  85. Cache::forget('profile:following:'.$target->id);
  86. Cache::forget('profile:followers:'.$target->id);
  87. Cache::forget('profile:following:'.$user->id);
  88. Cache::forget('profile:followers:'.$user->id);
  89. Cache::forget('api:local:exp:rec:'.$user->id);
  90. Cache::forget('user:account:id:'.$target->user_id);
  91. Cache::forget('user:account:id:'.$user->user_id);
  92. }
  93. protected function sendFollow($user, $target)
  94. {
  95. if($target->domain == null || $user->domain != null) {
  96. return;
  97. }
  98. $payload = [
  99. '@context' => 'https://www.w3.org/ns/activitystreams',
  100. 'id' => $user->permalink('#follow/'.$target->id.''),
  101. 'type' => 'Follow',
  102. 'actor' => $user->permalink(),
  103. 'object' => $target->permalink()
  104. ];
  105. $inbox = $target->sharedInbox ?? $target->inbox_url;
  106. Helpers::sendSignedObject($user, $inbox, $payload);
  107. }
  108. protected function sendUndoFollow($user, $target)
  109. {
  110. if($target->domain == null || $user->domain != null) {
  111. return;
  112. }
  113. $payload = [
  114. '@context' => 'https://www.w3.org/ns/activitystreams',
  115. 'id' => $user->permalink('#follow/'.$target->id.'/undo'),
  116. 'type' => 'Undo',
  117. 'actor' => $user->permalink(),
  118. 'object' => [
  119. 'id' => $user->permalink('#follows/'.$target->id),
  120. 'actor' => $user->permalink(),
  121. 'object' => $target->permalink(),
  122. 'type' => 'Follow'
  123. ]
  124. ];
  125. $inbox = $target->sharedInbox ?? $target->inbox_url;
  126. Helpers::sendSignedObject($user, $inbox, $payload);
  127. }
  128. }