From 2ae527c0f37947dde03663266544d180e94674c5 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 3 Nov 2021 23:29:12 -0600 Subject: [PATCH] Update Status model, use AccountService to generate urls instead of loading profile relation --- app/Services/AccountService.php | 12 +++++++++--- app/Status.php | 8 ++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index d046b8c85..7042b3c94 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -15,7 +15,7 @@ class AccountService { const CACHE_KEY = 'pf:services:account:'; - public static function get($id) + public static function get($id, $softFail = false) { if($id > PHP_INT_MAX || $id < 1) { return []; @@ -24,10 +24,16 @@ class AccountService $key = self::CACHE_KEY . $id; $ttl = now()->addHours(12); - return Cache::remember($key, $ttl, function() use($id) { + return Cache::remember($key, $ttl, function() use($id, $softFail) { $fractal = new Fractal\Manager(); $fractal->setSerializer(new ArraySerializer()); - $profile = Profile::findOrFail($id); + $profile = Profile::find($id); + if(!$profile) { + if($softFail) { + return null; + } + abort(404); + } $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); return $fractal->createData($resource)->toArray(); }); diff --git a/app/Status.php b/app/Status.php index 35c5820bf..67fcffb6a 100644 --- a/app/Status.php +++ b/app/Status.php @@ -8,6 +8,7 @@ use App\HasSnowflakePrimary; use App\Http\Controllers\StatusController; use Illuminate\Database\Eloquent\SoftDeletes; use App\Models\Poll; +use App\Services\AccountService; class Status extends Model { @@ -108,8 +109,11 @@ class Status extends Model return $forceLocal ? "/i/web/post/_/{$this->profile_id}/{$this->id}" : $this->uri; } else { $id = $this->id; - $username = $this->profile->username; - $path = url(config('app.url')."/p/{$username}/{$id}"); + $account = AccountService::get($this->profile_id, true); + if(!$account || !isset($account['username'])) { + return '/404'; + } + $path = url(config('app.url')."/p/{$account['username']}/{$id}"); return $path; } }