1
0

FollowerController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 'force' => 'nullable|boolean',
  24. ]);
  25. $force = (bool) $request->input('force', true);
  26. $item = (int) $request->input('item');
  27. $url = $this->handleFollowRequest($item, $force);
  28. if($request->wantsJson() == true) {
  29. return response()->json(200);
  30. } else {
  31. return redirect($url);
  32. }
  33. }
  34. protected function handleFollowRequest($item, $force)
  35. {
  36. $user = Auth::user()->profile;
  37. $target = Profile::where('id', '!=', $user->id)->whereNull('status')->findOrFail($item);
  38. $private = (bool) $target->is_private;
  39. $remote = (bool) $target->domain;
  40. $blocked = UserFilter::whereUserId($target->id)
  41. ->whereFilterType('block')
  42. ->whereFilterableId($user->id)
  43. ->whereFilterableType('App\Profile')
  44. ->exists();
  45. if($blocked == true) {
  46. abort(400, 'You cannot follow this user.');
  47. }
  48. $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
  49. if($private == true && $isFollowing == 0) {
  50. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  51. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  52. }
  53. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  54. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  55. }
  56. $follow = FollowRequest::firstOrCreate([
  57. 'follower_id' => $user->id,
  58. 'following_id' => $target->id
  59. ]);
  60. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  61. $this->sendFollow($user, $target);
  62. }
  63. } elseif ($private == false && $isFollowing == 0) {
  64. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  65. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  66. }
  67. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  68. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  69. }
  70. $follower = new Follower();
  71. $follower->profile_id = $user->id;
  72. $follower->following_id = $target->id;
  73. $follower->save();
  74. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  75. $this->sendFollow($user, $target);
  76. }
  77. FollowPipeline::dispatch($follower);
  78. } else {
  79. if($force == true) {
  80. $request = FollowRequest::whereFollowerId($user->id)->whereFollowingId($target->id)->exists();
  81. $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->exists();
  82. if($remote == true && $request && !$follower) {
  83. $this->sendFollow($user, $target);
  84. }
  85. if($remote == true && $follower) {
  86. $this->sendUndoFollow($user, $target);
  87. }
  88. Follower::whereProfileId($user->id)
  89. ->whereFollowingId($target->id)
  90. ->delete();
  91. }
  92. }
  93. Cache::forget('profile:following:'.$target->id);
  94. Cache::forget('profile:followers:'.$target->id);
  95. Cache::forget('profile:following:'.$user->id);
  96. Cache::forget('profile:followers:'.$user->id);
  97. Cache::forget('api:local:exp:rec:'.$user->id);
  98. Cache::forget('user:account:id:'.$target->user_id);
  99. Cache::forget('user:account:id:'.$user->user_id);
  100. Cache::forget('px:profile:followers-v1.3:'.$user->id);
  101. Cache::forget('px:profile:followers-v1.3:'.$target->id);
  102. Cache::forget('px:profile:following-v1.3:'.$user->id);
  103. Cache::forget('px:profile:following-v1.3:'.$target->id);
  104. Cache::forget('profile:follower_count:'.$target->id);
  105. Cache::forget('profile:follower_count:'.$user->id);
  106. Cache::forget('profile:following_count:'.$target->id);
  107. Cache::forget('profile:following_count:'.$user->id);
  108. return $target->url();
  109. }
  110. public function sendFollow($user, $target)
  111. {
  112. if($target->domain == null || $user->domain != null) {
  113. return;
  114. }
  115. $payload = [
  116. '@context' => 'https://www.w3.org/ns/activitystreams',
  117. 'id' => $user->permalink('#follow/'.$target->id),
  118. 'type' => 'Follow',
  119. 'actor' => $user->permalink(),
  120. 'object' => $target->permalink()
  121. ];
  122. $inbox = $target->sharedInbox ?? $target->inbox_url;
  123. Helpers::sendSignedObject($user, $inbox, $payload);
  124. }
  125. public function sendUndoFollow($user, $target)
  126. {
  127. if($target->domain == null || $user->domain != null) {
  128. return;
  129. }
  130. $payload = [
  131. '@context' => 'https://www.w3.org/ns/activitystreams',
  132. 'id' => $user->permalink('#follow/'.$target->id.'/undo'),
  133. 'type' => 'Undo',
  134. 'actor' => $user->permalink(),
  135. 'object' => [
  136. 'id' => $user->permalink('#follows/'.$target->id),
  137. 'actor' => $user->permalink(),
  138. 'object' => $target->permalink(),
  139. 'type' => 'Follow'
  140. ]
  141. ];
  142. $inbox = $target->sharedInbox ?? $target->inbox_url;
  143. Helpers::sendSignedObject($user, $inbox, $payload);
  144. }
  145. }