Update ApiV1Controller

This commit is contained in:
Daniel Supernault 2020-02-03 23:30:21 -07:00
parent bd45728615
commit e2828f4b85
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -367,7 +367,7 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
$target = Profile::where('id', '!=', $user->id) $target = Profile::where('id', '!=', $user->profile_id)
->whereNull('status') ->whereNull('status')
->findOrFail($id); ->findOrFail($id);
@ -375,7 +375,7 @@ class ApiV1Controller extends Controller
$remote = (bool) $target->domain; $remote = (bool) $target->domain;
$blocked = UserFilter::whereUserId($target->id) $blocked = UserFilter::whereUserId($target->id)
->whereFilterType('block') ->whereFilterType('block')
->whereFilterableId($user->id) ->whereFilterableId($user->profile_id)
->whereFilterableType('App\Profile') ->whereFilterableType('App\Profile')
->exists(); ->exists();
@ -383,7 +383,7 @@ class ApiV1Controller extends Controller
abort(400, 'You cannot follow this user.'); abort(400, 'You cannot follow this user.');
} }
$isFollowing = Follower::whereProfileId($user->id) $isFollowing = Follower::whereProfileId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->exists(); ->exists();
@ -396,42 +396,42 @@ class ApiV1Controller extends Controller
} }
// Rate limits, max 7500 followers per account // Rate limits, max 7500 followers per account
if($user->following()->count() >= Follower::MAX_FOLLOWING) { if($user->profile->following()->count() >= Follower::MAX_FOLLOWING) {
abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts'); abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
} }
// Rate limits, follow 30 accounts per hour max // Rate limits, follow 30 accounts per hour max
if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) { if($user->profile->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour'); abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
} }
if($private == true) { if($private == true) {
$follow = FollowRequest::firstOrCreate([ $follow = FollowRequest::firstOrCreate([
'follower_id' => $user->id, 'follower_id' => $user->profile_id,
'following_id' => $target->id 'following_id' => $target->id
]); ]);
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendFollow($user, $target); (new FollowerController())->sendFollow($user->profile, $target);
} }
} else { } else {
$follower = new Follower(); $follower = new Follower();
$follower->profile_id = $user->id; $follower->profile_id = $user->profile_id;
$follower->following_id = $target->id; $follower->following_id = $target->id;
$follower->save(); $follower->save();
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendFollow($user, $target); (new FollowerController())->sendFollow($user->profile, $target);
} }
FollowPipeline::dispatch($follower); FollowPipeline::dispatch($follower);
} }
Cache::forget('profile:following:'.$target->id); Cache::forget('profile:following:'.$target->id);
Cache::forget('profile:followers:'.$target->id); Cache::forget('profile:followers:'.$target->id);
Cache::forget('profile:following:'.$user->id); Cache::forget('profile:following:'.$user->profile_id);
Cache::forget('profile:followers:'.$user->id); Cache::forget('profile:followers:'.$user->profile_id);
Cache::forget('api:local:exp:rec:'.$user->id); Cache::forget('api:local:exp:rec:'.$user->profile_id);
Cache::forget('user:account:id:'.$target->user_id); Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->user_id); Cache::forget('user:account:id:'.$user->id);
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer()); $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -452,14 +452,14 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
$target = Profile::where('id', '!=', $user->id) $target = Profile::where('id', '!=', $user->profile_id)
->whereNull('status') ->whereNull('status')
->findOrFail($id); ->findOrFail($id);
$private = (bool) $target->is_private; $private = (bool) $target->is_private;
$remote = (bool) $target->domain; $remote = (bool) $target->domain;
$isFollowing = Follower::whereProfileId($user->id) $isFollowing = Follower::whereProfileId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->exists(); ->exists();
@ -471,29 +471,29 @@ class ApiV1Controller extends Controller
} }
// Rate limits, follow 30 accounts per hour max // Rate limits, follow 30 accounts per hour max
if($user->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) { if($user->profile->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour'); abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
} }
FollowRequest::whereFollowerId($user->id) FollowRequest::whereFollowerId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->delete(); ->delete();
Follower::whereProfileId($user->id) Follower::whereProfileId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->delete(); ->delete();
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendUndoFollow($user, $target); (new FollowerController())->sendUndoFollow($user->profile, $target);
} }
Cache::forget('profile:following:'.$target->id); Cache::forget('profile:following:'.$target->id);
Cache::forget('profile:followers:'.$target->id); Cache::forget('profile:followers:'.$target->id);
Cache::forget('profile:following:'.$user->id); Cache::forget('profile:following:'.$user->profile_id);
Cache::forget('profile:followers:'.$user->id); Cache::forget('profile:followers:'.$user->profile_id);
Cache::forget('api:local:exp:rec:'.$user->id); Cache::forget('api:local:exp:rec:'.$user->profile_id);
Cache::forget('user:account:id:'.$target->user_id); Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->user_id); Cache::forget('user:account:id:'.$user->id);
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer()); $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();