*/ public static function profile(Profile $profile): array { return self::authored($profile->toArray(), $profile); } /** * @return array|null */ public static function user(?User $user): ?array { return $user instanceof User ? self::dated($user->toArray(), $user) : null; } /** * @param iterable $users * @return list> */ public static function users(iterable $users): array { $shown = []; foreach ($users as $user) { $shown[] = self::dated($user->toArray(), $user); } return $shown; } /** * @return array|null */ public static function commit(?Commit $commit, bool $withGuildMentions = true): ?array { if (! $commit instanceof Commit) { return null; } $shown = $commit->toArray(); if ($commit->trashed()) { return $shown; } $shown['guild_handle'] = $commit->relationLoaded('guild') ? $commit->guild?->handle : null; $shown['guild'] = $commit->relationLoaded('guild') && $commit->guild !== null ? [ 'handle' => $commit->guild->handle, 'name' => $commit->guild->name, 'emoji' => $commit->guild->emoji, 'icon_url' => $commit->guild->iconUrl(), ] : null; if (is_array($shown['author'] ?? null) && $commit->relationLoaded('author') && $commit->author instanceof Profile) { $shown['author'] = self::authored($shown['author'], $commit->author); } if (is_array($shown['quoted_commit'] ?? null) && $commit->relationLoaded('quotedCommit')) { $shown['quoted_commit'] = self::commit($commit->quotedCommit, false); } $preview = $commit->hasAttribute('reply_preview') ? $commit->getAttribute('reply_preview') : null; if (is_array($preview) && $preview['data'] instanceof Collection) { $shown['reply_preview'] = [...$preview, 'data' => self::commits($preview['data'], false)]; } return $withGuildMentions ? ShownGuildMentions::of($shown, request()->user()) : $shown; } /** * @param iterable $commits * @return list> */ public static function commits(iterable $commits, bool $withGuildMentions = true): array { $shown = []; foreach ($commits as $commit) { $shown[] = self::commit($commit, false); } $shown = array_values(array_filter($shown)); return $withGuildMentions ? ShownGuildMentions::many($shown, request()->user()) : $shown; } /** * The page of commit cards, paginated as before. * * @param CursorPaginator $page * @return CursorPaginator */ public static function page(CursorPaginator $page, ?User $joinedGuildsFor = null): CursorPaginator { $guilds = $joinedGuildsFor instanceof User ? resolve(GuildCommitsQuery::class)->joinedFor($page->items(), $joinedGuildsFor) : []; $cards = self::commits($page->items()); $shown = clone $page; $shown->setCollection($page->getCollection()->values()->map(fn (Commit $commit, int $index): ShownCommitResource => new ShownCommitResource($commit, $guilds[$commit->id] ?? null, $cards[$index]))); return $shown; } /** * @param CursorPaginator $page * @return CursorPaginator */ public static function notifications(CursorPaginator $page): CursorPaginator { $cards = ShownGuildMentions::many($page->getCollection()->map(fn (Notification $notification): array => new ShownNotificationResource($notification)->toArray())->all(), request()->user()); $shown = clone $page; $shown->setCollection($page->getCollection()->values()->map(function (Notification $notification, int $index) use ($cards): ShownNotificationResource { $resource = new ShownNotificationResource($notification); $resource->shown = $cards[$index]; return $resource; })); return $shown; } /** * Reports with the commit each is about, paginated as before. * * @param CursorPaginator $reports * @return CursorPaginator> */ public static function reports(CursorPaginator $reports): CursorPaginator { $shown = clone $reports; $shown->setCollection(collect(ShownGuildMentions::many($reports->getCollection()->map(fn (CommitReport $report): array => $report->relationLoaded('commit') ? [...$report->toArray(), 'commit' => self::commit($report->commit, false)] : $report->toArray())->all(), request()->user()))); return $shown; } /** * @return array{data: list>, position: int, length: int, is_capped: bool, next_id: string|null} */ public static function continuation(ThreadContinuation $continuation): array { return [...$continuation->toArray(), 'data' => self::commits($continuation->commits)]; } /** * @param array $shown * @return array */ private static function authored(array $shown, Profile $author): array { return $author->relationLoaded('user') && $author->user instanceof User ? self::dated($shown, $author->user) : $shown; } /** * @param array $shown * @return array */ private static function dated(array $shown, User $user): array { return array_key_exists('live', $shown) ? [...$shown, 'live' => LiveStreamResource::of($user)] : $shown; } }