mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-27 08:43:17 +00:00
Merge pull request #3135 from pixelfed/staging
Update ApiV1Controller, fix account settings bug
This commit is contained in:
commit
a061e04bbb
4 changed files with 168 additions and 103 deletions
|
@ -411,34 +411,40 @@ class ApiV1Controller extends Controller
|
|||
public function accountFollowersById(Request $request, $id)
|
||||
{
|
||||
abort_if(!$request->user(), 403);
|
||||
$account = AccountService::get($id);
|
||||
abort_if(!$account, 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
$user = $request->user();
|
||||
$profile = Profile::whereNull('status')->findOrFail($id);
|
||||
$limit = $request->input('limit') ?? 40;
|
||||
if($pid != $account['id']) {
|
||||
if($account['locked']) {
|
||||
if(FollowerService::follows($pid, $account['id'])) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
if($profile->domain) {
|
||||
$res = [];
|
||||
} else {
|
||||
if($profile->id == $user->profile_id) {
|
||||
$followers = $profile->followers()->paginate($limit);
|
||||
$resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
|
||||
$res = $this->fractal->createData($resource)->toArray();
|
||||
} else {
|
||||
if($profile->is_private) {
|
||||
abort_if(!$profile->followedBy($user->profile), 403);
|
||||
}
|
||||
$settings = $profile->user->settings;
|
||||
if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
|
||||
$settings->show_profile_followers == false
|
||||
) {
|
||||
$res = [];
|
||||
} else {
|
||||
$followers = $profile->followers()->paginate($limit);
|
||||
$resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
|
||||
$res = $this->fractal->createData($resource)->toArray();
|
||||
if(AccountService::hiddenFollowers($id)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if($request->has('page') && $request->page >= 5) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
$res = DB::table('followers')
|
||||
->select('id', 'profile_id', 'following_id')
|
||||
->whereFollowingId($account['id'])
|
||||
->orderByDesc('id')
|
||||
->simplePaginate(10)
|
||||
->map(function($follower) {
|
||||
return AccountService::getMastodon($follower->profile_id);
|
||||
})
|
||||
->filter(function($account) {
|
||||
return $account && isset($account['id']);
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
return response()->json($res);
|
||||
}
|
||||
|
||||
|
@ -452,35 +458,39 @@ class ApiV1Controller extends Controller
|
|||
public function accountFollowingById(Request $request, $id)
|
||||
{
|
||||
abort_if(!$request->user(), 403);
|
||||
$account = AccountService::get($id);
|
||||
abort_if(!$account, 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
$user = $request->user();
|
||||
$profile = Profile::whereNull('status')->findOrFail($id);
|
||||
$limit = $request->input('limit') ?? 40;
|
||||
|
||||
if($profile->domain) {
|
||||
$res = [];
|
||||
} else {
|
||||
if($profile->id == $user->profile_id) {
|
||||
$following = $profile->following()->paginate($limit);
|
||||
$resource = new Fractal\Resource\Collection($following, new AccountTransformer());
|
||||
$res = $this->fractal->createData($resource)->toArray();
|
||||
} else {
|
||||
if($profile->is_private) {
|
||||
abort_if(!$profile->followedBy($user->profile), 403);
|
||||
}
|
||||
$settings = $profile->user->settings;
|
||||
if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
|
||||
$settings->show_profile_following == false
|
||||
) {
|
||||
$res = [];
|
||||
} else {
|
||||
$following = $profile->following()->paginate($limit);
|
||||
$resource = new Fractal\Resource\Collection($following, new AccountTransformer());
|
||||
$res = $this->fractal->createData($resource)->toArray();
|
||||
}
|
||||
if($pid != $account['id']) {
|
||||
if($account['locked']) {
|
||||
if(FollowerService::follows($pid, $account['id'])) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
if(AccountService::hiddenFollowing($id)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if($request->has('page') && $request->page >= 5) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
$res = DB::table('followers')
|
||||
->select('id', 'profile_id', 'following_id')
|
||||
->whereProfileId($account['id'])
|
||||
->orderByDesc('id')
|
||||
->simplePaginate(10)
|
||||
->map(function($follower) {
|
||||
return AccountService::get($follower->following_id);
|
||||
})
|
||||
->filter(function($account) {
|
||||
return $account && isset($account['id']);
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
return response()->json($res);
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use App\{
|
|||
StatusView,
|
||||
UserFilter
|
||||
};
|
||||
use Auth, Cache;
|
||||
use Auth, Cache, DB;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Carbon\Carbon;
|
||||
use League\Fractal;
|
||||
|
@ -651,27 +651,39 @@ class PublicApiController extends Controller
|
|||
|
||||
public function accountFollowers(Request $request, $id)
|
||||
{
|
||||
abort_unless(Auth::check(), 403);
|
||||
$profile = Profile::with('user')->whereNull('status')->findOrFail($id);
|
||||
$owner = Auth::id() == $profile->user_id;
|
||||
abort_if(!$request->user(), 403);
|
||||
$account = AccountService::get($id);
|
||||
abort_if(!$account, 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
if(Auth::id() != $profile->user_id && $profile->is_private) {
|
||||
return response()->json([]);
|
||||
if($pid != $account['id']) {
|
||||
if($account['locked']) {
|
||||
if(FollowerService::follows($pid, $account['id'])) {
|
||||
return [];
|
||||
}
|
||||
if(!$profile->domain && !$profile->user->settings->show_profile_followers) {
|
||||
return response()->json([]);
|
||||
}
|
||||
if(!$owner && $request->page > 5) {
|
||||
|
||||
if(AccountService::hiddenFollowers($id)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$res = Follower::select('id', 'profile_id', 'following_id')
|
||||
->whereFollowingId($profile->id)
|
||||
if($request->has('page') && $request->page >= 5) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
$res = DB::table('followers')
|
||||
->select('id', 'profile_id', 'following_id')
|
||||
->whereFollowingId($account['id'])
|
||||
->orderByDesc('id')
|
||||
->simplePaginate(10)
|
||||
->map(function($follower) {
|
||||
return ProfileService::get($follower['profile_id']);
|
||||
return AccountService::get($follower->profile_id);
|
||||
})
|
||||
->filter(function($account) {
|
||||
return $account && isset($account['id']);
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
return response()->json($res);
|
||||
|
@ -679,40 +691,40 @@ class PublicApiController extends Controller
|
|||
|
||||
public function accountFollowing(Request $request, $id)
|
||||
{
|
||||
abort_unless(Auth::check(), 403);
|
||||
abort_if(!$request->user(), 403);
|
||||
$account = AccountService::get($id);
|
||||
abort_if(!$account, 404);
|
||||
$pid = $request->user()->profile_id;
|
||||
|
||||
$profile = Profile::with('user')
|
||||
->whereNull('status')
|
||||
->findOrFail($id);
|
||||
|
||||
// filter by username
|
||||
$search = $request->input('fbu');
|
||||
$owner = Auth::id() == $profile->user_id;
|
||||
$filter = ($owner == true) && ($search != null);
|
||||
|
||||
abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
|
||||
|
||||
if(!$profile->domain) {
|
||||
abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
|
||||
if($pid != $account['id']) {
|
||||
if($account['locked']) {
|
||||
if(FollowerService::follows($pid, $account['id'])) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
if(!$owner && $request->page > 5) {
|
||||
if(AccountService::hiddenFollowing($id)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if($search) {
|
||||
abort_if(!$owner, 404);
|
||||
$following = $profile->following()
|
||||
->where('profiles.username', 'like', '%'.$search.'%')
|
||||
->orderByDesc('followers.created_at')
|
||||
->paginate(10);
|
||||
} else {
|
||||
$following = $profile->following()
|
||||
->orderByDesc('followers.created_at')
|
||||
->paginate(10);
|
||||
if($request->has('page') && $request->page >= 5) {
|
||||
return [];
|
||||
}
|
||||
$resource = new Fractal\Resource\Collection($following, new AccountTransformer());
|
||||
$res = $this->fractal->createData($resource)->toArray();
|
||||
}
|
||||
|
||||
$res = DB::table('followers')
|
||||
->select('id', 'profile_id', 'following_id')
|
||||
->whereProfileId($account['id'])
|
||||
->orderByDesc('id')
|
||||
->simplePaginate(10)
|
||||
->map(function($follower) {
|
||||
return AccountService::get($follower->following_id);
|
||||
})
|
||||
->filter(function($account) {
|
||||
return $account && isset($account['id']);
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
return response()->json($res);
|
||||
}
|
||||
|
|
|
@ -77,6 +77,8 @@ trait PrivacySettings
|
|||
Cache::forget('profile:follower_count:' . $profile->id);
|
||||
Cache::forget('profile:following_count:' . $profile->id);
|
||||
Cache::forget('profile:embed:' . $profile->id);
|
||||
Cache::forget('pf:acct:settings:hidden-followers:' . $profile->id);
|
||||
Cache::forget('pf:acct:settings:hidden-following:' . $profile->id);
|
||||
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Services;
|
|||
use Cache;
|
||||
use App\Profile;
|
||||
use App\Status;
|
||||
use App\User;
|
||||
use App\UserSetting;
|
||||
use App\Transformer\Api\AccountTransformer;
|
||||
use League\Fractal;
|
||||
|
@ -174,4 +175,44 @@ class AccountService
|
|||
return (string) $profile->id;
|
||||
});
|
||||
}
|
||||
|
||||
public static function hiddenFollowers($id)
|
||||
{
|
||||
$account = self::get($id, true);
|
||||
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Cache::remember('pf:acct:settings:hidden-followers:' . $id, 43200, function() use($id) {
|
||||
$user = User::whereProfileId($id)->first();
|
||||
if(!$user) {
|
||||
return false;
|
||||
}
|
||||
$settings = UserSetting::whereUserId($user->id)->first();
|
||||
if($settings) {
|
||||
return $settings->show_profile_follower_count == false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public static function hiddenFollowing($id)
|
||||
{
|
||||
$account = self::get($id, true);
|
||||
if(!$account || !isset($account['local']) || $account['local'] == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Cache::remember('pf:acct:settings:hidden-following:' . $id, 43200, function() use($id) {
|
||||
$user = User::whereProfileId($id)->first();
|
||||
if(!$user) {
|
||||
return false;
|
||||
}
|
||||
$settings = UserSetting::whereUserId($user->id)->first();
|
||||
if($settings) {
|
||||
return $settings->show_profile_following_count == false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue