Wire admin studio SSR and search infrastructure
This commit is contained in:
@@ -22,7 +22,10 @@ class PostFeedService
|
||||
?int $viewerId,
|
||||
int $page = 1,
|
||||
): array {
|
||||
$baseQuery = Post::with($this->eagerLoads())
|
||||
$baseQuery = $this->applyViewerSaveState(
|
||||
Post::with($this->eagerLoads()),
|
||||
$viewerId,
|
||||
)
|
||||
->where('user_id', $profileUser->id)
|
||||
->visibleTo($viewerId);
|
||||
|
||||
@@ -80,6 +83,8 @@ class PostFeedService
|
||||
->visibleTo($viewer->id)
|
||||
->orderByDesc('created_at');
|
||||
|
||||
$query = $this->applyViewerSaveState($query, $viewer->id);
|
||||
|
||||
if ($filter === 'shares') $query->where('type', Post::TYPE_ARTWORK_SHARE);
|
||||
elseif ($filter === 'text') $query->where('type', Post::TYPE_TEXT);
|
||||
elseif ($filter === 'uploads') $query->where('type', Post::TYPE_UPLOAD);
|
||||
@@ -109,7 +114,10 @@ class PostFeedService
|
||||
): array {
|
||||
$tag = mb_strtolower($tag);
|
||||
|
||||
$paginated = Post::with($this->eagerLoads())
|
||||
$paginated = $this->applyViewerSaveState(
|
||||
Post::with($this->eagerLoads()),
|
||||
$viewerId,
|
||||
)
|
||||
->whereHas('hashtags', fn ($q) => $q->where('tag', $tag))
|
||||
->visibleTo($viewerId)
|
||||
->orderByDesc('created_at')
|
||||
@@ -132,7 +140,10 @@ class PostFeedService
|
||||
|
||||
public function getSavedFeed(User $viewer, int $page = 1): array
|
||||
{
|
||||
$paginated = Post::with($this->eagerLoads())
|
||||
$paginated = $this->applyViewerSaveState(
|
||||
Post::with($this->eagerLoads()),
|
||||
$viewer->id,
|
||||
)
|
||||
->whereHas('saves', fn ($q) => $q->where('user_id', $viewer->id))
|
||||
->where('status', Post::STATUS_PUBLISHED)
|
||||
->orderByDesc('created_at')
|
||||
@@ -174,6 +185,17 @@ class PostFeedService
|
||||
return $this->eagerLoads();
|
||||
}
|
||||
|
||||
private function applyViewerSaveState($query, ?int $viewerId)
|
||||
{
|
||||
if (! $viewerId) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->withExists([
|
||||
'saves as viewer_saved' => fn ($saveQuery) => $saveQuery->where('user_id', $viewerId),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Penalize runs of 5+ posts from the same author by deferring them to the end.
|
||||
*/
|
||||
@@ -223,8 +245,9 @@ class PostFeedService
|
||||
$viewerLiked = $viewerSaved = false;
|
||||
if ($viewerId) {
|
||||
$viewerLiked = $post->reactions->where('user_id', $viewerId)->where('reaction', 'like')->isNotEmpty();
|
||||
// saves are lazy-loaded only when needed; check if relation is loaded
|
||||
if ($post->relationLoaded('saves')) {
|
||||
if (array_key_exists('viewer_saved', $post->getAttributes())) {
|
||||
$viewerSaved = (bool) $post->getAttribute('viewer_saved');
|
||||
} elseif ($post->relationLoaded('saves')) {
|
||||
$viewerSaved = $post->saves->where('user_id', $viewerId)->isNotEmpty();
|
||||
} else {
|
||||
$viewerSaved = $post->saves()->where('user_id', $viewerId)->exists();
|
||||
|
||||
Reference in New Issue
Block a user