FollowerController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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::whereProfileId($user->id)
  87. ->whereFollowingId($target->id)
  88. ->delete();
  89. }
  90. Cache::forget('profile:following:'.$target->id);
  91. Cache::forget('profile:followers:'.$target->id);
  92. Cache::forget('profile:following:'.$user->id);
  93. Cache::forget('profile:followers:'.$user->id);
  94. Cache::forget('api:local:exp:rec:'.$user->id);
  95. Cache::forget('user:account:id:'.$target->user_id);
  96. Cache::forget('user:account:id:'.$user->user_id);
  97. }
  98. protected function sendFollow($user, $target)
  99. {
  100. if($target->domain == null || $user->domain != null) {
  101. return;
  102. }
  103. $payload = [
  104. '@context' => 'https://www.w3.org/ns/activitystreams',
  105. 'type' => 'Follow',
  106. 'actor' => $user->permalink(),
  107. 'object' => $target->permalink()
  108. ];
  109. $inbox = $target->sharedInbox ?? $target->inbox_url;
  110. Helpers::sendSignedObject($user, $inbox, $payload);
  111. }
  112. protected function sendUndoFollow($user, $target)
  113. {
  114. if($target->domain == null || $user->domain != null) {
  115. return;
  116. }
  117. $payload = [
  118. '@context' => 'https://www.w3.org/ns/activitystreams',
  119. 'id' => $user->permalink('#follow/'.$target->id.'/undo'),
  120. 'type' => 'Undo',
  121. 'actor' => $user->permalink(),
  122. 'object' => [
  123. 'id' => $user->permalink('#follows/'.$target->id),
  124. 'actor' => $user->permalink(),
  125. 'object' => $target->permalink(),
  126. 'type' => 'Follow'
  127. ]
  128. ];
  129. $inbox = $target->sharedInbox ?? $target->inbox_url;
  130. Helpers::sendSignedObject($user, $inbox, $payload);
  131. }
  132. }