mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-26 00:03:16 +00:00
Update StatusTransformers, remove includes and use cached services
This commit is contained in:
parent
8001ad998c
commit
09d5198c55
4 changed files with 70 additions and 62 deletions
40
app/Services/MediaService.php
Normal file
40
app/Services/MediaService.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Cache;
|
||||||
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
use App\Media;
|
||||||
|
use App\Status;
|
||||||
|
use League\Fractal;
|
||||||
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
|
use App\Transformer\Api\MediaTransformer;
|
||||||
|
|
||||||
|
class MediaService
|
||||||
|
{
|
||||||
|
const CACHE_KEY = 'status:transformer:media:attachments:';
|
||||||
|
|
||||||
|
public static function get($statusId)
|
||||||
|
{
|
||||||
|
return Cache::remember(self::CACHE_KEY.$statusId, 129600, function() use($statusId) {
|
||||||
|
$status = Status::find($statusId);
|
||||||
|
if(!$status) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if(in_array($status->type, ['photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) {
|
||||||
|
$media = Media::whereStatusId($status->id)->orderBy('order')->get();
|
||||||
|
$fractal = new Fractal\Manager();
|
||||||
|
$fractal->setSerializer(new ArraySerializer());
|
||||||
|
$resource = new Fractal\Resource\Collection($media, new MediaTransformer());
|
||||||
|
return $fractal->createData($resource)->toArray();
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function del($statusId)
|
||||||
|
{
|
||||||
|
return Cache::forget(self::CACHE_KEY . $statusId);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ use Cache;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use App\{Status, StatusHashtag};
|
use App\{Status, StatusHashtag};
|
||||||
use App\Transformer\Api\StatusHashtagTransformer;
|
use App\Transformer\Api\StatusHashtagTransformer;
|
||||||
|
use App\Transformer\Api\HashtagTransformer;
|
||||||
use League\Fractal;
|
use League\Fractal;
|
||||||
use League\Fractal\Serializer\ArraySerializer;
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
|
@ -78,4 +79,21 @@ class StatusHashtagService {
|
||||||
{
|
{
|
||||||
return ['status' => StatusService::get($statusId)];
|
return ['status' => StatusService::get($statusId)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function statusTags($statusId)
|
||||||
|
{
|
||||||
|
$key = 'pf:services:sh:id:' . $statusId;
|
||||||
|
|
||||||
|
return Cache::remember($key, 604800, function() use($statusId) {
|
||||||
|
$status = Status::find($statusId);
|
||||||
|
if(!$status) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$fractal = new Fractal\Manager();
|
||||||
|
$fractal->setSerializer(new ArraySerializer());
|
||||||
|
$resource = new Fractal\Resource\Collection($status->hashtags, new HashtagTransformer());
|
||||||
|
return $fractal->createData($resource)->toArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,18 +7,14 @@ use League\Fractal;
|
||||||
use Cache;
|
use Cache;
|
||||||
use App\Services\HashidService;
|
use App\Services\HashidService;
|
||||||
use App\Services\LikeService;
|
use App\Services\LikeService;
|
||||||
|
use App\Services\MediaService;
|
||||||
use App\Services\MediaTagService;
|
use App\Services\MediaTagService;
|
||||||
|
use App\Services\StatusHashtagService;
|
||||||
use App\Services\StatusLabelService;
|
use App\Services\StatusLabelService;
|
||||||
use App\Services\ProfileService;
|
use App\Services\ProfileService;
|
||||||
|
|
||||||
class StatusStatelessTransformer extends Fractal\TransformerAbstract
|
class StatusStatelessTransformer extends Fractal\TransformerAbstract
|
||||||
{
|
{
|
||||||
protected $defaultIncludes = [
|
|
||||||
'account',
|
|
||||||
'tags',
|
|
||||||
'media_attachments',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function transform(Status $status)
|
public function transform(Status $status)
|
||||||
{
|
{
|
||||||
$taggedPeople = MediaTagService::get($status->id);
|
$taggedPeople = MediaTagService::get($status->id);
|
||||||
|
@ -62,31 +58,10 @@ class StatusStatelessTransformer extends Fractal\TransformerAbstract
|
||||||
'local' => (bool) $status->local,
|
'local' => (bool) $status->local,
|
||||||
'taggedPeople' => $taggedPeople,
|
'taggedPeople' => $taggedPeople,
|
||||||
'label' => StatusLabelService::get($status),
|
'label' => StatusLabelService::get($status),
|
||||||
'liked_by' => LikeService::likedBy($status)
|
'liked_by' => LikeService::likedBy($status),
|
||||||
|
'media_attachments' => MediaService::get($status->id),
|
||||||
|
'account' => ProfileService::get($status->profile_id),
|
||||||
|
'tags' => StatusHashtagService::statusTags($status->id)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function includeAccount(Status $status)
|
|
||||||
{
|
|
||||||
$account = $status->profile;
|
|
||||||
|
|
||||||
return $this->item($account, new AccountTransformer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function includeTags(Status $status)
|
|
||||||
{
|
|
||||||
$tags = $status->hashtags;
|
|
||||||
|
|
||||||
return $this->collection($tags, new HashtagTransformer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function includeMediaAttachments(Status $status)
|
|
||||||
{
|
|
||||||
return Cache::remember('status:transformer:media:attachments:'.$status->id, now()->addMinutes(3), function() use($status) {
|
|
||||||
if(in_array($status->type, ['photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) {
|
|
||||||
$media = $status->media()->orderBy('order')->get();
|
|
||||||
return $this->collection($media, new MediaTransformer());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,19 +8,15 @@ use League\Fractal;
|
||||||
use Cache;
|
use Cache;
|
||||||
use App\Services\HashidService;
|
use App\Services\HashidService;
|
||||||
use App\Services\LikeService;
|
use App\Services\LikeService;
|
||||||
|
use App\Services\MediaService;
|
||||||
use App\Services\MediaTagService;
|
use App\Services\MediaTagService;
|
||||||
|
use App\Services\StatusHashtagService;
|
||||||
use App\Services\StatusLabelService;
|
use App\Services\StatusLabelService;
|
||||||
use App\Services\ProfileService;
|
use App\Services\ProfileService;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class StatusTransformer extends Fractal\TransformerAbstract
|
class StatusTransformer extends Fractal\TransformerAbstract
|
||||||
{
|
{
|
||||||
protected $defaultIncludes = [
|
|
||||||
'account',
|
|
||||||
'tags',
|
|
||||||
'media_attachments',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function transform(Status $status)
|
public function transform(Status $status)
|
||||||
{
|
{
|
||||||
$taggedPeople = MediaTagService::get($status->id);
|
$taggedPeople = MediaTagService::get($status->id);
|
||||||
|
@ -64,31 +60,10 @@ class StatusTransformer extends Fractal\TransformerAbstract
|
||||||
'local' => (bool) $status->local,
|
'local' => (bool) $status->local,
|
||||||
'taggedPeople' => $taggedPeople,
|
'taggedPeople' => $taggedPeople,
|
||||||
'label' => StatusLabelService::get($status),
|
'label' => StatusLabelService::get($status),
|
||||||
'liked_by' => LikeService::likedBy($status)
|
'liked_by' => LikeService::likedBy($status),
|
||||||
|
'media_attachments' => MediaService::get($status->id),
|
||||||
|
'account' => ProfileService::get($status->profile_id),
|
||||||
|
'tags' => StatusHashtagService::statusTags($status->id)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function includeAccount(Status $status)
|
|
||||||
{
|
|
||||||
$account = $status->profile;
|
|
||||||
|
|
||||||
return $this->item($account, new AccountTransformer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function includeTags(Status $status)
|
|
||||||
{
|
|
||||||
$tags = $status->hashtags;
|
|
||||||
|
|
||||||
return $this->collection($tags, new HashtagTransformer());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function includeMediaAttachments(Status $status)
|
|
||||||
{
|
|
||||||
return Cache::remember('status:transformer:media:attachments:'.$status->id, now()->addMinutes(14), function() use($status) {
|
|
||||||
if(in_array($status->type, ['photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) {
|
|
||||||
$media = $status->media()->orderBy('order')->get();
|
|
||||||
return $this->collection($media, new MediaTransformer());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue