Update ProfileController

This commit is contained in:
Daniel Supernault 2019-09-11 00:22:03 -06:00
parent 01352e04ea
commit a74b3ea798
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -20,80 +20,93 @@ class ProfileController extends Controller
{ {
public function show(Request $request, $username) public function show(Request $request, $username)
{ {
$user = Profile::whereUsername($username)->firstOrFail(); $user = Profile::whereNull('domain')
if($user->domain) { ->whereNull('status')
return redirect($user->remote_url); ->whereUsername($username)
} ->firstOrFail();
if($user->status != null) { if($request->wantsJson() && config('federation.activitypub.enabled')) {
return $this->accountCheck($user); return $this->showActivityPub($request, $user);
} else {
return $this->buildProfile($request, $user);
} }
return $this->buildProfile($request, $user);
} }
// TODO: refactor this mess
protected function buildProfile(Request $request, $user) protected function buildProfile(Request $request, $user)
{ {
$username = $user->username; $username = $user->username;
$loggedIn = Auth::check(); $loggedIn = Auth::check();
$isPrivate = false; $isPrivate = false;
$isBlocked = false; $isBlocked = false;
if(!$loggedIn) {
$key = 'profile:settings:' . $user->id;
$ttl = now()->addHours(6);
$settings = Cache::remember($key, $ttl, function() use($user) {
return $user->user->settings;
});
if($user->status != null) { if ($user->is_private == true) {
return ProfileController::accountCheck($user); abort(404);
} }
if ($user->remote_url) { $owner = false;
$settings = new \StdClass; $is_following = false;
$settings->crawlable = false;
$settings->show_profile_follower_count = true; $is_admin = $user->user->is_admin;
$settings->show_profile_following_count = true; $profile = $user;
$settings = [
'crawlable' => $settings->crawlable,
'following' => [
'count' => $settings->show_profile_following_count,
'list' => $settings->show_profile_following
],
'followers' => [
'count' => $settings->show_profile_follower_count,
'list' => $settings->show_profile_followers
]
];
return view('profile.show', compact('profile', 'settings'));
} else { } else {
$settings = $user->user->settings; $key = 'profile:settings:' . $user->id;
} $ttl = now()->addHours(6);
$settings = Cache::remember($key, $ttl, function() use($user) {
return $user->user->settings;
});
if ($request->wantsJson() && config('federation.activitypub.enabled')) { if ($user->is_private == true) {
return $this->showActivityPub($request, $user); $isPrivate = $this->privateProfileCheck($user, $loggedIn);
} }
if ($user->is_private == true) {
$isPrivate = $this->privateProfileCheck($user, $loggedIn);
}
if ($loggedIn == true) {
$isBlocked = $this->blockedProfileCheck($user); $isBlocked = $this->blockedProfileCheck($user);
$owner = $loggedIn && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
if ($isPrivate == true || $isBlocked == true) {
$requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
->whereFollowingId($user->id)
->exists() : false;
return view('profile.private', compact('user', 'is_following', 'requested'));
}
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
$profile = $user;
$settings = [
'crawlable' => $settings->crawlable,
'following' => [
'count' => $settings->show_profile_following_count,
'list' => $settings->show_profile_following
],
'followers' => [
'count' => $settings->show_profile_follower_count,
'list' => $settings->show_profile_followers
]
];
return view('profile.show', compact('profile', 'settings'));
} }
$owner = $loggedIn && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
if ($isPrivate == true || $isBlocked == true) {
$requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
->whereFollowingId($user->id)
->exists() : false;
return view('profile.private', compact('user', 'is_following', 'requested'));
}
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
$profile = $user;
$settings = [
'crawlable' => $settings->crawlable,
'following' => [
'count' => $settings->show_profile_following_count,
'list' => $settings->show_profile_following
],
'followers' => [
'count' => $settings->show_profile_follower_count,
'list' => $settings->show_profile_followers
]
];
return view('profile.show', compact('user', 'profile', 'settings', 'owner', 'is_following', 'is_admin'));
} }
public function permalinkRedirect(Request $request, $username) public function permalinkRedirect(Request $request, $username)
{ {
$user = Profile::whereUsername($username)->firstOrFail(); $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
$settings = User::whereUsername($username)->firstOrFail()->settings;
if ($request->wantsJson() && config('federation.activitypub.enabled')) { if ($request->wantsJson() && config('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $user); return $this->showActivityPub($request, $user);
@ -136,34 +149,19 @@ class ProfileController extends Controller
return false; return false;
} }
public static function accountCheck(Profile $profile)
{
switch ($profile->status) {
case 'disabled':
case 'suspended':
case 'delete':
return view('profile.disabled');
break;
default:
# code...
break;
}
return abort(404);
}
public function showActivityPub(Request $request, $user) public function showActivityPub(Request $request, $user)
{ {
abort_if(!config('federation.activitypub.enabled'), 404); abort_if(!config('federation.activitypub.enabled'), 404);
abort_if($user->domain, 404);
if($user->status != null) { $key = 'profile:ap:' . $user->id;
return ProfileController::accountCheck($user); $ttl = now()->addHours(6);
}
$fractal = new Fractal\Manager(); return Cache::remember($key, $ttl, function() use($user) {
$resource = new Fractal\Resource\Item($user, new ProfileTransformer); $fractal = new Fractal\Manager();
$res = $fractal->createData($resource)->toArray(); $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json'); $res = $fractal->createData($resource)->toArray();
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
});
} }
public function showAtomFeed(Request $request, $user) public function showAtomFeed(Request $request, $user)