FollowerController.php 5.2 KB

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