Commit workspace changes
This commit is contained in:
@@ -14,6 +14,8 @@ class ArtworkResource extends JsonResource
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
$this->resource->loadMissing(['group', 'uploadedBy.profile', 'primaryAuthor.profile', 'contributors.user.profile']);
|
||||
|
||||
$md = ThumbnailPresenter::present($this->resource, 'md');
|
||||
$lg = ThumbnailPresenter::present($this->resource, 'lg');
|
||||
$xl = ThumbnailPresenter::present($this->resource, 'xl');
|
||||
@@ -46,6 +48,7 @@ class ArtworkResource extends JsonResource
|
||||
$isFavorited = false;
|
||||
$isBookmarked = false;
|
||||
$isFollowing = false;
|
||||
$isFollowingGroup = false;
|
||||
$viewerAward = null;
|
||||
|
||||
$bookmarksCount = Schema::hasTable('artwork_bookmarks')
|
||||
@@ -87,6 +90,13 @@ class ArtworkResource extends JsonResource
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->group?->id) && Schema::hasTable('group_follows')) {
|
||||
$isFollowingGroup = DB::table('group_follows')
|
||||
->where('group_id', (int) $this->group->id)
|
||||
->where('user_id', $viewerId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
if (Schema::hasTable('artwork_awards')) {
|
||||
$viewerAward = DB::table('artwork_awards')
|
||||
->where('user_id', $viewerId)
|
||||
@@ -96,6 +106,62 @@ class ArtworkResource extends JsonResource
|
||||
}
|
||||
|
||||
$decode = static fn (?string $v): string => html_entity_decode((string) ($v ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$mapUser = static function ($user): ?array {
|
||||
if (! $user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (int) ($user->id ?? 0),
|
||||
'name' => html_entity_decode((string) ($user->name ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8'),
|
||||
'username' => (string) ($user->username ?? ''),
|
||||
'profile_url' => ! empty($user->username) ? '/@' . $user->username : null,
|
||||
'avatar_url' => $user->profile?->avatar_url,
|
||||
];
|
||||
};
|
||||
|
||||
$publisher = $this->group
|
||||
? [
|
||||
'type' => 'group',
|
||||
'id' => (int) $this->group->id,
|
||||
'name' => (string) $this->group->name,
|
||||
'slug' => (string) $this->group->slug,
|
||||
'headline' => (string) ($this->group->headline ?? ''),
|
||||
'avatar_url' => $this->group->avatarUrl(),
|
||||
'profile_url' => $this->group->publicUrl(),
|
||||
'followers_count' => (int) ($this->group->followers_count ?? 0),
|
||||
'follow_url' => route('groups.follow', ['group' => $this->group]),
|
||||
'unfollow_url' => route('groups.unfollow', ['group' => $this->group]),
|
||||
]
|
||||
: [
|
||||
'type' => 'user',
|
||||
'id' => (int) ($this->user?->id ?? 0),
|
||||
'name' => html_entity_decode((string) ($this->user?->name ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8'),
|
||||
'slug' => (string) ($this->user?->username ?? ''),
|
||||
'headline' => '',
|
||||
'avatar_url' => $this->user?->profile?->avatar_url,
|
||||
'profile_url' => $this->user?->username ? '/@' . $this->user->username : null,
|
||||
'followers_count' => $followerCount,
|
||||
'follow_url' => null,
|
||||
'unfollow_url' => null,
|
||||
];
|
||||
|
||||
$primaryAuthor = $mapUser($this->primaryAuthor ?: $this->user);
|
||||
$uploadedBy = $mapUser($this->uploadedBy ?: $this->user);
|
||||
$contributors = $this->contributors
|
||||
->map(function ($contributor) use ($mapUser): ?array {
|
||||
$user = $mapUser($contributor->user);
|
||||
if (! $user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array_merge($user, [
|
||||
'credit_role' => $contributor->credit_role,
|
||||
'is_primary' => (bool) $contributor->is_primary,
|
||||
]);
|
||||
})
|
||||
->filter()
|
||||
->values();
|
||||
|
||||
return [
|
||||
'id' => (int) $this->id,
|
||||
@@ -131,11 +197,19 @@ class ArtworkResource extends JsonResource
|
||||
'rank' => (string) ($this->user?->rank ?? 'Newbie'),
|
||||
'followers_count' => $followerCount,
|
||||
],
|
||||
'publisher' => $publisher,
|
||||
'credits' => [
|
||||
'uploaded_by' => $uploadedBy,
|
||||
'primary_author' => $primaryAuthor,
|
||||
'contributors' => $contributors,
|
||||
],
|
||||
'viewer' => [
|
||||
'is_bookmarked' => $isBookmarked,
|
||||
'is_liked' => $isLiked,
|
||||
'is_favorited' => $isFavorited,
|
||||
'is_following_author' => $isFollowing,
|
||||
'is_following_group' => $isFollowingGroup,
|
||||
'is_following_publisher' => $publisher['type'] === 'group' ? $isFollowingGroup : $isFollowing,
|
||||
'is_authenticated' => $viewerId > 0,
|
||||
'id' => $viewerId > 0 ? $viewerId : null,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user