Update Status model, use AccountService to generate urls instead of loading profile relation

This commit is contained in:
Daniel Supernault 2021-11-03 23:29:12 -06:00
parent 694ce6ff3d
commit 2ae527c0f3
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 15 additions and 5 deletions

View file

@ -15,7 +15,7 @@ class AccountService
{ {
const CACHE_KEY = 'pf:services:account:'; 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) { if($id > PHP_INT_MAX || $id < 1) {
return []; return [];
@ -24,10 +24,16 @@ class AccountService
$key = self::CACHE_KEY . $id; $key = self::CACHE_KEY . $id;
$ttl = now()->addHours(12); $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 = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $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()); $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
return $fractal->createData($resource)->toArray(); return $fractal->createData($resource)->toArray();
}); });

View file

@ -8,6 +8,7 @@ use App\HasSnowflakePrimary;
use App\Http\Controllers\StatusController; use App\Http\Controllers\StatusController;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Poll; use App\Models\Poll;
use App\Services\AccountService;
class Status extends Model class Status extends Model
{ {
@ -108,8 +109,11 @@ class Status extends Model
return $forceLocal ? "/i/web/post/_/{$this->profile_id}/{$this->id}" : $this->uri; return $forceLocal ? "/i/web/post/_/{$this->profile_id}/{$this->id}" : $this->uri;
} else { } else {
$id = $this->id; $id = $this->id;
$username = $this->profile->username; $account = AccountService::get($this->profile_id, true);
$path = url(config('app.url')."/p/{$username}/{$id}"); if(!$account || !isset($account['username'])) {
return '/404';
}
$path = url(config('app.url')."/p/{$account['username']}/{$id}");
return $path; return $path;
} }
} }