Update ProfileController, preserve deleted actor objects for federated account deletion and use more efficient account cache lookup

This commit is contained in:
Daniel Supernault 2024-03-08 05:00:56 -07:00
parent e742d595a6
commit 853a729f76
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1
1 changed files with 317 additions and 288 deletions

View File

@ -2,65 +2,63 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Cache;
use DB;
use View;
use App\AccountInterstitial; use App\AccountInterstitial;
use App\Follower; use App\Follower;
use App\FollowRequest; use App\FollowRequest;
use App\Profile; use App\Profile;
use App\Story;
use App\Status;
use App\User;
use App\UserSetting;
use App\UserFilter;
use League\Fractal;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Util\Lexer\Nickname; use App\Status;
use App\Util\Webfinger\Webfinger; use App\Story;
use App\Transformer\ActivityPub\ProfileOutbox;
use App\Transformer\ActivityPub\ProfileTransformer; use App\Transformer\ActivityPub\ProfileTransformer;
use App\User;
use App\UserFilter;
use App\UserSetting;
use Auth;
use Cache;
use Illuminate\Http\Request;
use League\Fractal;
use View;
class ProfileController extends Controller class ProfileController extends Controller
{ {
public function show(Request $request, $username) public function show(Request $request, $username)
{ {
// redirect authed users to Metro 2.0 if ($request->wantsJson() && (bool) config_cache('federation.activitypub.enabled')) {
if($request->user()) { $user = $this->getCachedUser($username, true);
// unless they force static view abort_if(! $user, 404, 'Not found');
if(!$request->has('fs') || $request->input('fs') != '1') {
$pid = AccountService::usernameToId($username);
if($pid) {
return redirect('/i/web/profile/' . $pid);
}
}
}
$user = Profile::whereNull('domain')
->whereNull('status')
->whereUsername($username)
->firstOrFail();
if($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $user); return $this->showActivityPub($request, $user);
} }
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $user->id, 86400, function() use($user) { // redirect authed users to Metro 2.0
if ($request->user()) {
// unless they force static view
if (! $request->has('fs') || $request->input('fs') != '1') {
$pid = AccountService::usernameToId($username);
if ($pid) {
return redirect('/i/web/profile/'.$pid);
}
}
}
$user = $this->getCachedUser($username);
abort_unless($user, 404);
$aiCheck = Cache::remember('profile:ai-check:spam-login:'.$user->id, 3600, function () use ($user) {
$exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count(); $exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count();
if($exists) { if ($exists) {
return true; return true;
} }
return false; return false;
}); });
if($aiCheck) { if ($aiCheck) {
return redirect('/login'); return redirect('/login');
} }
return $this->buildProfile($request, $user); return $this->buildProfile($request, $user);
} }
@ -70,15 +68,16 @@ class ProfileController extends Controller
$loggedIn = Auth::check(); $loggedIn = Auth::check();
$isPrivate = false; $isPrivate = false;
$isBlocked = false; $isBlocked = false;
if(!$loggedIn) { if (! $loggedIn) {
$key = 'profile:settings:' . $user->id; $key = 'profile:settings:'.$user->id;
$ttl = now()->addHours(6); $ttl = now()->addHours(6);
$settings = Cache::remember($key, $ttl, function() use($user) { $settings = Cache::remember($key, $ttl, function () use ($user) {
return $user->user->settings; return $user->user->settings;
}); });
if ($user->is_private == true) { if ($user->is_private == true) {
$profile = null; $profile = null;
return view('profile.private', compact('user')); return view('profile.private', compact('user'));
} }
@ -90,18 +89,19 @@ class ProfileController extends Controller
'crawlable' => $settings->crawlable, 'crawlable' => $settings->crawlable,
'following' => [ 'following' => [
'count' => $settings->show_profile_following_count, 'count' => $settings->show_profile_following_count,
'list' => $settings->show_profile_following 'list' => $settings->show_profile_following,
], ],
'followers' => [ 'followers' => [
'count' => $settings->show_profile_follower_count, 'count' => $settings->show_profile_follower_count,
'list' => $settings->show_profile_followers 'list' => $settings->show_profile_followers,
] ],
]; ];
return view('profile.show', compact('profile', 'settings')); return view('profile.show', compact('profile', 'settings'));
} else { } else {
$key = 'profile:settings:' . $user->id; $key = 'profile:settings:'.$user->id;
$ttl = now()->addHours(6); $ttl = now()->addHours(6);
$settings = Cache::remember($key, $ttl, function() use($user) { $settings = Cache::remember($key, $ttl, function () use ($user) {
return $user->user->settings; return $user->user->settings;
}); });
@ -118,6 +118,7 @@ class ProfileController extends Controller
$requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id) $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
->whereFollowingId($user->id) ->whereFollowingId($user->id)
->exists() : false; ->exists() : false;
return view('profile.private', compact('user', 'is_following', 'requested')); return view('profile.private', compact('user', 'is_following', 'requested'));
} }
@ -127,36 +128,61 @@ class ProfileController extends Controller
'crawlable' => $settings->crawlable, 'crawlable' => $settings->crawlable,
'following' => [ 'following' => [
'count' => $settings->show_profile_following_count, 'count' => $settings->show_profile_following_count,
'list' => $settings->show_profile_following 'list' => $settings->show_profile_following,
], ],
'followers' => [ 'followers' => [
'count' => $settings->show_profile_follower_count, 'count' => $settings->show_profile_follower_count,
'list' => $settings->show_profile_followers 'list' => $settings->show_profile_followers,
] ],
]; ];
return view('profile.show', compact('profile', 'settings')); return view('profile.show', compact('profile', 'settings'));
} }
} }
protected function getCachedUser($username, $withTrashed = false)
{
$val = str_replace(['_', '.', '-'], '', $username);
if (! ctype_alnum($val)) {
return;
}
$hash = ($withTrashed ? 'wt:' : 'wot:').strtolower($username);
return Cache::remember('pfc:cached-user:'.$hash, ($withTrashed ? 14400 : 900), function () use ($username, $withTrashed) {
if (! $withTrashed) {
return Profile::whereNull(['domain', 'status'])
->whereUsername($username)
->first();
} else {
return Profile::withTrashed()
->whereNull('domain')
->whereUsername($username)
->first();
}
});
}
public function permalinkRedirect(Request $request, $username) public function permalinkRedirect(Request $request, $username)
{ {
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail(); if ($request->wantsJson() && (bool) config_cache('federation.activitypub.enabled')) {
$user = $this->getCachedUser($username, true);
if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $user); return $this->showActivityPub($request, $user);
} }
$user = $this->getCachedUser($username);
return redirect($user->url()); return redirect($user->url());
} }
protected function privateProfileCheck(Profile $profile, $loggedIn) protected function privateProfileCheck(Profile $profile, $loggedIn)
{ {
if (!Auth::check()) { if (! Auth::check()) {
return true; return true;
} }
$user = Auth::user()->profile; $user = Auth::user()->profile;
if($user->id == $profile->id || !$profile->is_private) { if ($user->id == $profile->id || ! $profile->is_private) {
return false; return false;
} }
@ -180,6 +206,7 @@ class ProfileController extends Controller
default: default:
break; break;
} }
return abort(404); return abort(404);
} }
@ -200,36 +227,38 @@ class ProfileController extends Controller
public function showActivityPub(Request $request, $user) public function showActivityPub(Request $request, $user)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! config_cache('federation.activitypub.enabled'), 404);
abort_if(! $user, 404, 'Not found');
abort_if($user->domain, 404); abort_if($user->domain, 404);
return Cache::remember('pf:activitypub:user-object:by-id:' . $user->id, 3600, function() use($user) { return Cache::remember('pf:activitypub:user-object:by-id:'.$user->id, 1800, function () use ($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');
}); });
} }
public function showAtomFeed(Request $request, $user) public function showAtomFeed(Request $request, $user)
{ {
abort_if(!config('federation.atom.enabled'), 404); abort_if(! config('federation.atom.enabled'), 404);
$pid = AccountService::usernameToId($user); $pid = AccountService::usernameToId($user);
abort_if(!$pid, 404); abort_if(! $pid, 404);
$profile = AccountService::get($pid, true); $profile = AccountService::get($pid, true);
abort_if(!$profile || $profile['locked'] || !$profile['local'], 404); abort_if(! $profile || $profile['locked'] || ! $profile['local'], 404);
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile['id'], 86400, function() use($profile) { $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 86400, function () use ($profile) {
$uid = User::whereProfileId($profile['id'])->first(); $uid = User::whereProfileId($profile['id'])->first();
if(!$uid) { if (! $uid) {
return true; return true;
} }
$exists = AccountInterstitial::whereUserId($uid->id)->where('is_spam', 1)->count(); $exists = AccountInterstitial::whereUserId($uid->id)->where('is_spam', 1)->count();
if($exists) { if ($exists) {
return true; return true;
} }
@ -238,54 +267,55 @@ class ProfileController extends Controller
abort_if($aiCheck, 404); abort_if($aiCheck, 404);
$enabled = Cache::remember('profile:atom:enabled:' . $profile['id'], 84600, function() use ($profile) { $enabled = Cache::remember('profile:atom:enabled:'.$profile['id'], 84600, function () use ($profile) {
$uid = User::whereProfileId($profile['id'])->first(); $uid = User::whereProfileId($profile['id'])->first();
if(!$uid) { if (! $uid) {
return false; return false;
} }
$settings = UserSetting::whereUserId($uid->id)->first(); $settings = UserSetting::whereUserId($uid->id)->first();
if(!$settings) { if (! $settings) {
return false; return false;
} }
return $settings->show_atom; return $settings->show_atom;
}); });
abort_if(!$enabled, 404); abort_if(! $enabled, 404);
$data = Cache::remember('pf:atom:user-feed:by-id:' . $profile['id'], 900, function() use($pid, $profile) { $data = Cache::remember('pf:atom:user-feed:by-id:'.$profile['id'], 14400, function () use ($pid, $profile) {
$items = Status::whereProfileId($pid) $items = Status::whereProfileId($pid)
->whereScope('public') ->whereScope('public')
->whereIn('type', ['photo', 'photo:album']) ->whereIn('type', ['photo', 'photo:album'])
->orderByDesc('id') ->orderByDesc('id')
->take(10) ->take(10)
->get() ->get()
->map(function($status) { ->map(function ($status) {
return StatusService::get($status->id, true); return StatusService::get($status->id, true);
}) })
->filter(function($status) { ->filter(function ($status) {
return $status && return $status &&
isset($status['account']) && isset($status['account']) &&
isset($status['media_attachments']) && isset($status['media_attachments']) &&
count($status['media_attachments']); count($status['media_attachments']);
}) })
->values(); ->values();
$permalink = config('app.url') . "/users/{$profile['username']}.atom"; $permalink = config('app.url')."/users/{$profile['username']}.atom";
$headers = ['Content-Type' => 'application/atom+xml']; $headers = ['Content-Type' => 'application/atom+xml'];
if($items && $items->count()) { if ($items && $items->count()) {
$headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String(); $headers['Last-Modified'] = now()->parse($items->first()['created_at'])->toRfc7231String();
} }
return compact('items', 'permalink', 'headers'); return compact('items', 'permalink', 'headers');
}); });
abort_if(!$data || !isset($data['items']) || !isset($data['permalink']), 404); abort_if(! $data || ! isset($data['items']) || ! isset($data['permalink']), 404);
return response() return response()
->view('atom.user', ->view('atom.user',
[ [
'profile' => $profile, 'profile' => $profile,
'items' => $data['items'], 'items' => $data['items'],
'permalink' => $data['permalink'] 'permalink' => $data['permalink'],
] ]
) )
->withHeaders($data['headers']); ->withHeaders($data['headers']);
@ -293,7 +323,8 @@ class ProfileController extends Controller
public function meRedirect() public function meRedirect()
{ {
abort_if(!Auth::check(), 404); abort_if(! Auth::check(), 404);
return redirect(Auth::user()->url()); return redirect(Auth::user()->url());
} }
@ -301,57 +332,55 @@ class ProfileController extends Controller
{ {
$res = view('profile.embed-removed'); $res = view('profile.embed-removed');
if(!config('instance.embed.profile')) { if (! config('instance.embed.profile')) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
if(strlen($username) > 15 || strlen($username) < 2) { if (strlen($username) > 15 || strlen($username) < 2) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$profile = Profile::whereUsername($username) $profile = $this->getCachedUser($username);
->whereIsPrivate(false)
->whereNull('status')
->whereNull('domain')
->first();
if(!$profile) { if (! $profile || $profile->is_private) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile->id, 86400, function() use($profile) { $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile->id, 86400, function () use ($profile) {
$exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count(); $exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
if($exists) { if ($exists) {
return true; return true;
} }
return false; return false;
}); });
if($aiCheck) { if ($aiCheck) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
if(AccountService::canEmbed($profile->user_id) == false) { if (AccountService::canEmbed($profile->user_id) == false) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$profile = AccountService::get($profile->id); $profile = AccountService::get($profile->id);
$res = view('profile.embed', compact('profile')); $res = view('profile.embed', compact('profile'));
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
public function stories(Request $request, $username) public function stories(Request $request, $username)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! config_cache('instance.stories.enabled') || ! $request->user(), 404);
$profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail(); $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
$pid = $profile->id; $pid = $profile->id;
$authed = Auth::user()->profile_id; $authed = Auth::user()->profile_id;
abort_if($pid != $authed && !FollowerService::follows($authed, $pid), 404); abort_if($pid != $authed && ! FollowerService::follows($authed, $pid), 404);
$exists = Story::whereProfileId($pid) $exists = Story::whereProfileId($pid)
->whereActive(true) ->whereActive(true)
->exists(); ->exists();
abort_unless($exists, 404); abort_unless($exists, 404);
return view('profile.story', compact('pid', 'profile')); return view('profile.story', compact('pid', 'profile'));
} }
} }