Skip to content

Commit d969a97

Browse files
committed
Update Status model, improve thumb logic
1 parent 439638f commit d969a97

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

app/Services/StatusService.php

-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ public static function del($id, $purge = false)
161161
}
162162
Cache::forget('status:transformer:media:attachments:' . $id);
163163
MediaService::del($id);
164-
Cache::forget('status:thumb:nsfw0' . $id);
165-
Cache::forget('status:thumb:nsfw1' . $id);
166164
Cache::forget('pf:services:sh:id:' . $id);
167165
PublicTimelineService::rem($id);
168166
NetworkTimelineService::rem($id);

app/Status.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Illuminate\Database\Eloquent\SoftDeletes;
1010
use App\Models\Poll;
1111
use App\Services\AccountService;
12+
use App\Services\StatusService;
1213
use App\Models\StatusEdit;
14+
use Illuminate\Support\Str;
1315

1416
class Status extends Model
1517
{
@@ -95,16 +97,26 @@ public function setType()
9597

9698
public function thumb($showNsfw = false)
9799
{
98-
$key = $showNsfw ? 'status:thumb:nsfw1'.$this->id : 'status:thumb:nsfw0'.$this->id;
99-
return Cache::remember($key, now()->addMinutes(15), function() use ($showNsfw) {
100-
$type = $this->type ?? $this->setType();
101-
$is_nsfw = !$showNsfw ? $this->is_nsfw : false;
102-
if ($this->media->count() == 0 || $is_nsfw || !in_array($type,['photo', 'photo:album', 'video'])) {
103-
return url(Storage::url('public/no-preview.png'));
104-
}
100+
$entity = StatusService::get($this->id);
101+
102+
if(!$entity || !isset($entity['media_attachments']) || empty($entity['media_attachments'])) {
103+
return url(Storage::url('public/no-preview.png'));
104+
}
105+
106+
if((!isset($entity['sensitive']) || $entity['sensitive']) && !$showNsfw) {
107+
return url(Storage::url('public/no-preview.png'));
108+
}
109+
110+
return collect($entity['media_attachments'])
111+
->filter(fn($media) => $media['type'] == 'image' && in_array($media['mime'], ['image/jpeg', 'image/png']))
112+
->map(function($media) {
113+
if(!Str::endsWith($media['preview_url'], ['no-preview.png', 'no-preview.jpg'])) {
114+
return $media['preview_url'];
115+
}
105116

106-
return url(Storage::url($this->firstMedia()->thumbnail_path));
107-
});
117+
return $media['url'];
118+
})
119+
->first() ?? url(Storage::url('public/no-preview.png'));
108120
}
109121

110122
public function url($forceLocal = false)

0 commit comments

Comments
 (0)