Skip to content

Commit

Permalink
Update Status model, improve thumb logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Sep 25, 2023
1 parent 439638f commit d969a97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 0 additions & 2 deletions app/Services/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ public static function del($id, $purge = false)
}
Cache::forget('status:transformer:media:attachments:' . $id);
MediaService::del($id);
Cache::forget('status:thumb:nsfw0' . $id);
Cache::forget('status:thumb:nsfw1' . $id);
Cache::forget('pf:services:sh:id:' . $id);
PublicTimelineService::rem($id);
NetworkTimelineService::rem($id);
Expand Down
30 changes: 21 additions & 9 deletions app/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Poll;
use App\Services\AccountService;
use App\Services\StatusService;
use App\Models\StatusEdit;
use Illuminate\Support\Str;

class Status extends Model
{
Expand Down Expand Up @@ -95,16 +97,26 @@ public function setType()

public function thumb($showNsfw = false)
{
$key = $showNsfw ? 'status:thumb:nsfw1'.$this->id : 'status:thumb:nsfw0'.$this->id;
return Cache::remember($key, now()->addMinutes(15), function() use ($showNsfw) {
$type = $this->type ?? $this->setType();
$is_nsfw = !$showNsfw ? $this->is_nsfw : false;
if ($this->media->count() == 0 || $is_nsfw || !in_array($type,['photo', 'photo:album', 'video'])) {
return url(Storage::url('public/no-preview.png'));
}
$entity = StatusService::get($this->id);

if(!$entity || !isset($entity['media_attachments']) || empty($entity['media_attachments'])) {
return url(Storage::url('public/no-preview.png'));
}

if((!isset($entity['sensitive']) || $entity['sensitive']) && !$showNsfw) {
return url(Storage::url('public/no-preview.png'));
}

return collect($entity['media_attachments'])
->filter(fn($media) => $media['type'] == 'image' && in_array($media['mime'], ['image/jpeg', 'image/png']))
->map(function($media) {
if(!Str::endsWith($media['preview_url'], ['no-preview.png', 'no-preview.jpg'])) {
return $media['preview_url'];
}

return url(Storage::url($this->firstMedia()->thumbnail_path));
});
return $media['url'];
})
->first() ?? url(Storage::url('public/no-preview.png'));
}

public function url($forceLocal = false)
Expand Down

0 comments on commit d969a97

Please sign in to comment.