FollowerController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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)->count();
  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. FollowPipeline::dispatch($follower);
  74. } else {
  75. $follower = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->firstOrFail();
  76. $follower->delete();
  77. }
  78. Cache::forget('profile:following:'.$target->id);
  79. Cache::forget('profile:followers:'.$target->id);
  80. Cache::forget('profile:following:'.$user->id);
  81. Cache::forget('profile:followers:'.$user->id);
  82. Cache::forget('api:local:exp:rec:'.$user->id);
  83. Cache::forget('user:account:id:'.$target->user_id);
  84. Cache::forget('user:account:id:'.$user->user_id);
  85. }
  86. protected function sendFollow($user, $target)
  87. {
  88. if($target->domain == null || $user->domain != null) {
  89. return;
  90. }
  91. $payload = [
  92. '@context' => 'https://www.w3.org/ns/activitystreams',
  93. 'type' => 'Follow',
  94. 'actor' => $user->permalink(),
  95. 'object' => $target->permalink()
  96. ];
  97. $inbox = $target->sharedInbox ?? $target->inbox_url;
  98. Helpers::sendSignedObject($user, $inbox, $payload);
  99. }
  100. }