1
0
Fork 0
pixelfed/app/Http/Controllers/ProfileController.php

249 lines
8.4 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;
2019-12-05 04:21:08 +00:00
use View;
2018-08-28 03:07:36 +00:00
use App\Follower;
use App\FollowRequest;
2018-08-28 03:07:36 +00:00
use App\Profile;
2020-01-03 21:06:07 +00:00
use App\Story;
2018-08-28 03:07:36 +00:00
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)
{
2019-09-11 06:22:03 +00:00
$user = Profile::whereNull('domain')
->whereNull('status')
->whereUsername($username)
->firstOrFail();
2019-09-11 06:22:03 +00:00
if($request->wantsJson() && config('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $user);
2018-12-24 05:00:54 +00:00
}
2019-09-11 06:22:03 +00:00
return $this->buildProfile($request, $user);
2018-08-28 18:54:37 +00:00
}
protected function buildProfile(Request $request, $user)
{
$username = $user->username;
$loggedIn = Auth::check();
$isPrivate = false;
2018-09-02 07:37:25 +00:00
$isBlocked = false;
2019-09-11 06:22:03 +00:00
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->is_private == true) {
abort(404);
}
$owner = false;
$is_following = false;
$is_admin = $user->user->is_admin;
$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
]
];
2020-12-10 05:22:56 +00:00
$ui = $request->has('ui') && $request->input('ui') == 'memory' ? 'profile.memory' : 'profile.show';
return view($ui, compact('profile', 'settings'));
2018-08-28 03:07:36 +00:00
} else {
2019-09-11 06:22:03 +00:00
$key = 'profile:settings:' . $user->id;
$ttl = now()->addHours(6);
$settings = Cache::remember($key, $ttl, function() use($user) {
return $user->user->settings;
});
2018-06-01 02:40:50 +00:00
2019-09-11 06:22:03 +00:00
if ($user->is_private == true) {
$isPrivate = $this->privateProfileCheck($user, $loggedIn);
}
2018-08-14 01:31:18 +00:00
$isBlocked = $this->blockedProfileCheck($user);
2018-08-28 18:54:37 +00:00
2019-09-11 06:22:03 +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) {
$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
]
];
2020-12-10 05:22:56 +00:00
$ui = $request->has('ui') && $request->input('ui') == 'memory' ? 'profile.memory' : 'profile.show';
return view($ui, compact('profile', 'settings'));
2019-09-11 06:22:03 +00:00
}
}
2018-08-14 00:10:47 +00:00
public function permalinkRedirect(Request $request, $username)
{
2019-09-11 06:22:03 +00:00
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
2018-08-14 00:10:47 +00:00
2019-06-09 23:22:37 +00:00
if ($request->wantsJson() && config('federation.activitypub.enabled')) {
2018-08-28 03:07:36 +00:00
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
}
2020-01-03 21:06:07 +00:00
public static function accountCheck(Profile $profile)
{
switch ($profile->status) {
case 'disabled':
case 'suspended':
case 'delete':
return view('profile.disabled');
break;
default:
break;
}
return abort(404);
}
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-06-01 02:40:50 +00:00
public function showActivityPub(Request $request, $user)
{
2019-06-09 23:21:06 +00:00
abort_if(!config('federation.activitypub.enabled'), 404);
2019-09-11 06:22:03 +00:00
abort_if($user->domain, 404);
$fractal = new Fractal\Manager();
$resource = new Fractal\Resource\Item($user, new ProfileTransformer);
$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)
{
2019-06-09 23:21:06 +00:00
abort_if(!config('federation.atom.enabled'), 404);
2019-03-11 09:05:15 +00:00
$profile = $user = Profile::whereNull('status')->whereNull('domain')->whereUsername($user)->whereIsPrivate(false)->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 meRedirect()
{
abort_if(!Auth::check(), 404);
return redirect(Auth::user()->url());
}
2019-12-05 04:21:08 +00:00
public function embed(Request $request, $username)
{
$res = view('profile.embed-removed');
if(strlen($username) > 15 || strlen($username) < 2) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
$profile = Profile::whereUsername($username)
->whereIsPrivate(false)
->whereNull('status')
->whereNull('domain')
->first();
if(!$profile) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
$content = Cache::remember('profile:embed:'.$profile->id, now()->addHours(12), function() use($profile) {
return View::make('profile.embed')->with(compact('profile'))->render();
});
return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
}
2020-01-03 21:06:07 +00:00
public function stories(Request $request, $username)
{
abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
$profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
$pid = $profile->id;
2020-01-06 04:52:40 +00:00
$authed = Auth::user()->profile;
abort_if($pid != $authed->id && $profile->followedBy($authed) == false, 404);
2020-01-03 21:06:07 +00:00
$exists = Story::whereProfileId($pid)
->where('expires_at', '>', now())
->count();
2020-01-06 04:52:40 +00:00
abort_unless($exists > 0, 404);
return view('profile.story', compact('pid', 'profile'));
2020-01-03 21:06:07 +00:00
}
}