FollowerController.php 5.0 KB

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