pixelfed/app/Http/Controllers/ProfileController.php

253 lines
9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-08-28 18:54:37 +00:00
use Illuminate\Http\Request;
use Auth;
use Cache;
2018-08-28 03:07:36 +00:00
use App\Follower;
use App\Profile;
use App\User;
2018-08-28 18:54:37 +00:00
use App\UserFilter;
2018-06-01 02:40:50 +00:00
use League\Fractal;
2018-08-28 18:54:37 +00:00
use App\Util\Lexer\Nickname;
use App\Util\Webfinger\Webfinger;
use App\Transformer\ActivityPub\ProfileOutbox;
use App\Transformer\ActivityPub\ProfileTransformer;
class ProfileController extends Controller
{
2018-04-19 05:56:33 +00:00
public function show(Request $request, $username)
{
2018-08-28 03:07:36 +00:00
$user = Profile::whereUsername($username)->firstOrFail();
2018-12-24 05:00:54 +00:00
if($user->status != null) {
return $this->accountCheck($user);
} else {
return $this->buildProfile($request, $user);
}
2018-08-28 18:54:37 +00:00
}
// TODO: refactor this mess
protected function buildProfile(Request $request, $user)
{
$username = $user->username;
$loggedIn = Auth::check();
$isPrivate = false;
2018-09-02 07:37:25 +00:00
$isBlocked = false;
2018-12-24 05:00:54 +00:00
if($user->status != null) {
return ProfileController::accountCheck($user);
}
2018-08-28 03:07:36 +00:00
if ($user->remote_url) {
2018-08-28 18:54:37 +00:00
$settings = new \StdClass;
2018-08-28 03:07:36 +00:00
$settings->crawlable = false;
2018-11-09 01:26:49 +00:00
$settings->show_profile_follower_count = true;
$settings->show_profile_following_count = true;
2018-08-28 03:07:36 +00:00
} else {
2018-11-27 08:42:08 +00:00
$settings = $user->user->settings;
2018-08-28 03:07:36 +00:00
}
2018-06-01 02:40:50 +00:00
2018-08-28 03:07:36 +00:00
if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
return $this->showActivityPub($request, $user);
}
2018-08-14 01:31:18 +00:00
2018-08-28 03:07:36 +00:00
if ($user->is_private == true) {
$isPrivate = $this->privateProfileCheck($user, $loggedIn);
}
2018-08-28 18:54:37 +00:00
if ($loggedIn == true) {
$isBlocked = $this->blockedProfileCheck($user);
2018-08-28 18:54:37 +00:00
}
2018-09-03 03:59:54 +00:00
$owner = $loggedIn && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
if ($isPrivate == true || $isBlocked == true) {
2018-09-03 03:59:54 +00:00
return view('profile.private', compact('user', 'is_following'));
}
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
2019-02-11 19:30:56 +00:00
$profile = $user;
2019-02-13 02:48:28 +00:00
return view('profile.show', compact('user', 'profile', 'settings', 'owner', 'is_following', 'is_admin'));
}
2018-08-14 00:10:47 +00:00
public function permalinkRedirect(Request $request, $username)
{
2018-08-28 03:07:36 +00:00
$user = Profile::whereUsername($username)->firstOrFail();
$settings = User::whereUsername($username)->firstOrFail()->settings;
2018-08-14 00:10:47 +00:00
2018-08-28 03:07:36 +00:00
if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
return $this->showActivityPub($request, $user);
}
2018-08-14 00:10:47 +00:00
2018-08-28 03:07:36 +00:00
return redirect($user->url());
2018-08-14 00:10:47 +00:00
}
protected function privateProfileCheck(Profile $profile, $loggedIn)
{
if (!Auth::check()) {
return true;
2018-08-28 03:07:36 +00:00
}
$user = Auth::user()->profile;
2018-09-02 05:47:08 +00:00
if($user->id == $profile->id || !$profile->is_private) {
2018-08-28 03:07:36 +00:00
return false;
}
$follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
if ($follows == false) {
return true;
}
return false;
2018-06-01 02:40:50 +00:00
}
2018-08-28 18:54:37 +00:00
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;
}
2018-12-24 05:00:54 +00:00
public static function accountCheck(Profile $profile)
{
switch ($profile->status) {
case 'disabled':
2019-02-11 19:30:56 +00:00
case 'suspended':
2018-12-24 05:00:54 +00:00
case 'delete':
return view('profile.disabled');
break;
default:
# code...
break;
}
return abort(404);
}
2018-06-01 02:40:50 +00:00
public function showActivityPub(Request $request, $user)
{
2018-12-24 05:00:54 +00:00
if($user->status != null) {
return ProfileController::accountCheck($user);
}
2018-08-28 03:07:36 +00:00
$fractal = new Fractal\Manager();
2018-08-28 18:54:37 +00:00
$resource = new Fractal\Resource\Item($user, new ProfileTransformer);
2018-08-28 03:07:36 +00:00
$res = $fractal->createData($resource)->toArray();
return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
2018-06-01 02:40:50 +00:00
}
public function showAtomFeed(Request $request, $user)
{
2018-09-02 05:47:08 +00:00
$profile = $user = Profile::whereUsername($user)->firstOrFail();
2018-12-24 05:00:54 +00:00
if($profile->status != null) {
return $this->accountCheck($profile);
}
2018-09-02 05:47:08 +00:00
if($profile->is_private || Auth::check()) {
$blocked = $this->blockedProfileCheck($profile);
$check = $this->privateProfileCheck($profile, null);
if($check || $blocked) {
2018-09-09 03:22:19 +00:00
return redirect($profile->url());
2018-09-02 05:47:08 +00:00
}
}
2018-12-16 01:39:52 +00:00
$items = $profile->statuses()->whereHas('media')->whereIn('visibility',['public', 'unlisted'])->orderBy('created_at', 'desc')->take(10)->get();
2018-08-28 03:07:36 +00:00
return response()->view('atom.user', compact('profile', 'items'))
2018-06-01 02:40:50 +00:00
->header('Content-Type', 'application/atom+xml');
}
public function followers(Request $request, $username)
{
2018-08-28 18:54:37 +00:00
$profile = $user = Profile::whereUsername($username)->firstOrFail();
2018-12-24 05:00:54 +00:00
if($profile->status != null) {
return $this->accountCheck($profile);
}
2018-08-28 03:07:36 +00:00
// TODO: fix $profile/$user mismatch in profile & follower templates
2018-09-03 03:59:54 +00:00
$owner = Auth::check() && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
2018-09-02 05:47:08 +00:00
if($profile->is_private || Auth::check()) {
$blocked = $this->blockedProfileCheck($profile);
$check = $this->privateProfileCheck($profile, null);
if($check || $blocked) {
2018-11-20 06:44:02 +00:00
return view('profile.private', compact('user', 'is_following'));
2018-09-02 05:47:08 +00:00
}
}
2019-01-17 20:27:14 +00:00
$followers = $profile->followers()->whereNull('status')->orderBy('followers.created_at', 'desc')->simplePaginate(12);
2018-08-28 03:07:36 +00:00
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
2018-08-28 18:54:37 +00:00
if ($user->remote_url) {
$settings = new \StdClass;
$settings->crawlable = false;
} else {
2018-11-27 08:42:08 +00:00
$settings = $profile->user->settings;
if(!$settings->show_profile_follower_count && !$owner) {
abort(403);
}
2018-08-28 18:54:37 +00:00
}
return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin', 'settings'));
2018-06-01 02:40:50 +00:00
}
public function following(Request $request, $username)
{
2018-09-02 05:47:08 +00:00
$profile = $user = Profile::whereUsername($username)->firstOrFail();
2018-12-24 05:00:54 +00:00
if($profile->status != null) {
return $this->accountCheck($profile);
}
2018-08-28 03:07:36 +00:00
// TODO: fix $profile/$user mismatch in profile & follower templates
2018-09-03 03:59:54 +00:00
$owner = Auth::check() && Auth::id() === $user->user_id;
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
2018-09-02 05:47:08 +00:00
if($profile->is_private || Auth::check()) {
$blocked = $this->blockedProfileCheck($profile);
$check = $this->privateProfileCheck($profile, null);
if($check || $blocked) {
2018-11-20 06:44:02 +00:00
return view('profile.private', compact('user', 'is_following'));
2018-09-02 05:47:08 +00:00
}
}
2019-01-17 20:27:14 +00:00
$following = $profile->following()->whereNull('status')->orderBy('followers.created_at', 'desc')->simplePaginate(12);
2018-08-28 03:07:36 +00:00
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
2018-08-28 18:54:37 +00:00
if ($user->remote_url) {
$settings = new \StdClass;
$settings->crawlable = false;
} else {
2018-11-27 08:42:08 +00:00
$settings = $profile->user->settings;
if(!$settings->show_profile_follower_count && !$owner) {
abort(403);
}
2018-08-28 18:54:37 +00:00
}
return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin', 'settings'));
2018-06-01 02:40:50 +00:00
}
public function savedBookmarks(Request $request, $username)
{
2018-08-28 03:07:36 +00:00
if (Auth::check() === false || $username !== Auth::user()->username) {
abort(403);
}
2018-12-24 05:00:54 +00:00
$user = $profile = Auth::user()->profile;
if($profile->status != null) {
return $this->accountCheck($profile);
}
2018-08-28 03:07:36 +00:00
$settings = User::whereUsername($username)->firstOrFail()->settings;
$owner = true;
$following = false;
2018-08-28 18:54:37 +00:00
$timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at', 'desc')->simplePaginate(10);
2018-08-28 03:07:36 +00:00
$is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$is_admin = is_null($user->domain) ? $user->user->is_admin : false;
2019-02-13 04:14:33 +00:00
return view('profile.bookmarks', compact('user', 'profile', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
2018-04-19 05:56:33 +00:00
}
2019-02-11 19:30:56 +00:00
public function createCollection(Request $request)
{
}
public function collections(Request $request)
{
}
}