forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import type { Context } from "../context.ts";
2import { Effect, pipe } from "effect";
3import { deepCamelCaseKeys } from "../lib/deepCamelKeys.ts";
4
5export default function (ctx: Context, did: string) {
6 return Effect.runPromise(
7 pipe(
8 retrieve({
9 ctx,
10 params: {
11 did,
12 offset: 0,
13 limit: 12,
14 },
15 }),
16 Effect.flatMap(presentation),
17 Effect.retry({ times: 3 }),
18 Effect.timeout("120 seconds"),
19 Effect.catchAll((error) =>
20 Effect.fail(new Error(`Failed to retrieve albums: ${error}`)),
21 ),
22 ),
23 );
24}
25
26const retrieve = ({
27 params,
28 ctx,
29}: {
30 params: {
31 did: string;
32 offset: number;
33 limit: number;
34 };
35 ctx: Context;
36}): Effect.Effect<{ data: Album[] }, Error> => {
37 return Effect.tryPromise({
38 try: () =>
39 ctx.analytics.post("library.getTopAlbums", {
40 user_did: params.did,
41 pagination: {
42 skip: params.offset || 0,
43 take: params.limit || 10,
44 },
45 }),
46 catch: (error) => new Error(`Failed to retrieve artists: ${error}`),
47 });
48};
49
50const presentation = ({
51 data,
52}: {
53 data: Album[];
54}): Effect.Effect<{ albums: AlbumViewBasic[] }, never> => {
55 return Effect.sync(() => ({ albums: deepCamelCaseKeys(data) }));
56};
57
58type Album = {
59 id: string;
60 uri: string;
61 title: string;
62 artist: string;
63 artist_uri: string;
64 year: number;
65 album_art: string;
66 release_date: string;
67 sha256: string;
68 play_count: number;
69 unique_listeners: number;
70};
71
72export interface AlbumViewBasic {
73 /** The unique identifier of the album. */
74 id?: string;
75 /** The URI of the album. */
76 uri?: string;
77 /** The title of the album. */
78 title?: string;
79 /** The artist of the album. */
80 artist?: string;
81 /** The URI of the album's artist. */
82 artistUri?: string;
83 /** The year the album was released. */
84 year?: number;
85 /** The URL of the album art image. */
86 albumArt?: string;
87 /** The release date of the album. */
88 releaseDate?: string;
89 /** The SHA256 hash of the album. */
90 sha256?: string;
91 /** The number of times the album has been played. */
92 playCount?: number;
93 /** The number of unique listeners who have played the album. */
94 uniqueListeners?: number;
95 [k: string]: unknown;
96}