Merge pull request #418 from pixelfed/frontend-ui-refactor

Update ProfileController
This commit is contained in:
daniel 2018-08-28 12:55:57 -06:00 committed by GitHub
commit c084064f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,21 +2,36 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Cache;
use App\Follower; use App\Follower;
use App\Profile; use App\Profile;
use App\Transformer\ActivityPub\ProfileTransformer;
use App\User; use App\User;
use Auth; use App\UserFilter;
use Illuminate\Http\Request;
use League\Fractal; use League\Fractal;
use App\Util\Lexer\Nickname;
use App\Util\Webfinger\Webfinger;
use App\Transformer\ActivityPub\ProfileOutbox;
use App\Transformer\ActivityPub\ProfileTransformer;
class ProfileController extends Controller class ProfileController extends Controller
{ {
public function show(Request $request, $username) public function show(Request $request, $username)
{ {
$user = Profile::whereUsername($username)->firstOrFail(); $user = Profile::whereUsername($username)->firstOrFail();
return $this->buildProfile($request, $user);
}
// TODO: refactor this mess
protected function buildProfile(Request $request, $user)
{
$username = $user->username;
$loggedIn = Auth::check();
$isPrivate = false;
if ($user->remote_url) { if ($user->remote_url) {
$settings = new \StdClass(); $settings = new \StdClass;
$settings->crawlable = false; $settings->crawlable = false;
} else { } else {
$settings = User::whereUsername($username)->firstOrFail()->settings; $settings = User::whereUsername($username)->firstOrFail()->settings;
@ -27,22 +42,27 @@ class ProfileController extends Controller
} }
if ($user->is_private == true) { if ($user->is_private == true) {
$can_access = $this->privateProfileCheck($user); $isPrivate = $this->privateProfileCheck($user);
if ($can_access !== true) {
abort(403);
}
} }
// TODO: refactor this mess
$owner = Auth::check() && Auth::id() === $user->user_id; if ($loggedIn == true) {
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $isPrivate = $this->blockedProfileCheck($user);
$is_admin = is_null($user->domain) ? $user->user->is_admin : false; }
$timeline = $user->statuses()
if ($isPrivate == true) {
return view('profile.private', compact('user'));
} else {
$owner = $loggedIn && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
$timeline = $user->statuses()
->whereHas('media') ->whereHas('media')
->whereNull('in_reply_to_id') ->whereNull('in_reply_to_id')
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->withCount(['comments', 'likes']) ->withCount(['comments', 'likes'])
->simplePaginate(21); ->simplePaginate(21);
}
return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline')); return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline'));
} }
@ -64,9 +84,8 @@ class ProfileController extends Controller
if (Auth::check() === false) { if (Auth::check() === false) {
return false; return false;
} }
$follower_ids = (array) $profile->followers()->pluck('followers.profile_id');
$pid = Auth::user()->profile->id; $pid = Auth::user()->profile->id;
$follower_ids = $profile->followers()->pluck('followers.profile_id')->toArray();
if (!in_array($pid, $follower_ids) && $pid !== $profile->id) { if (!in_array($pid, $follower_ids) && $pid !== $profile->id) {
return false; return false;
} }
@ -74,12 +93,26 @@ class ProfileController extends Controller
return true; return true;
} }
protected function blockedProfileCheck(Profile $profile)
{
$pid = Auth::user()->profile->id;
$blocks = UserFilter::whereUserId($profile->id)
->whereFilterType('block')
->whereFilterableType('App\Profile')
->pluck('filterable_id')
->toArray();
if (in_array($pid, $blocks)) {
return true;
}
return false;
}
public function showActivityPub(Request $request, $user) public function showActivityPub(Request $request, $user)
{ {
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$resource = new Fractal\Resource\Item($user, new ProfileTransformer()); $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
$res = $fractal->createData($resource)->toArray(); $res = $fractal->createData($resource)->toArray();
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json'); return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
} }
@ -87,22 +120,25 @@ class ProfileController extends Controller
{ {
$profile = Profile::whereUsername($user)->firstOrFail(); $profile = Profile::whereUsername($user)->firstOrFail();
$items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get(); $items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get();
return response()->view('atom.user', compact('profile', 'items')) return response()->view('atom.user', compact('profile', 'items'))
->header('Content-Type', 'application/atom+xml'); ->header('Content-Type', 'application/atom+xml');
} }
public function followers(Request $request, $username) public function followers(Request $request, $username)
{ {
$profile = Profile::whereUsername($username)->firstOrFail(); $profile = $user = Profile::whereUsername($username)->firstOrFail();
// TODO: fix $profile/$user mismatch in profile & follower templates // TODO: fix $profile/$user mismatch in profile & follower templates
$user = $profile;
$owner = Auth::check() && Auth::id() === $user->user_id; $owner = Auth::check() && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$followers = $profile->followers()->orderBy('created_at', 'desc')->simplePaginate(12); $followers = $profile->followers()->orderBy('created_at', 'desc')->simplePaginate(12);
$is_admin = is_null($user->domain) ? $user->user->is_admin : false; $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
if ($user->remote_url) {
return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin')); $settings = new \StdClass;
$settings->crawlable = false;
} else {
$settings = User::whereUsername($username)->firstOrFail()->settings;
}
return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin', 'settings'));
} }
public function following(Request $request, $username) public function following(Request $request, $username)
@ -114,8 +150,13 @@ class ProfileController extends Controller
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$following = $profile->following()->orderBy('created_at', 'desc')->simplePaginate(12); $following = $profile->following()->orderBy('created_at', 'desc')->simplePaginate(12);
$is_admin = is_null($user->domain) ? $user->user->is_admin : false; $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
if ($user->remote_url) {
return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin')); $settings = new \StdClass;
$settings->crawlable = false;
} else {
$settings = User::whereUsername($username)->firstOrFail()->settings;
}
return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin', 'settings'));
} }
public function savedBookmarks(Request $request, $username) public function savedBookmarks(Request $request, $username)
@ -127,10 +168,9 @@ class ProfileController extends Controller
$settings = User::whereUsername($username)->firstOrFail()->settings; $settings = User::whereUsername($username)->firstOrFail()->settings;
$owner = true; $owner = true;
$following = false; $following = false;
$timeline = $user->bookmarks()->withCount(['likes', 'comments'])->orderBy('created_at', 'desc')->simplePaginate(10); $timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at', 'desc')->simplePaginate(10);
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$is_admin = is_null($user->domain) ? $user->user->is_admin : false; $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin')); return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
} }
} }