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: 10,
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 scrobbles: ${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: Scrobble[] }, Error> => {
37 return Effect.tryPromise({
38 try: () =>
39 ctx.analytics.post("library.getScrobbles", {
40 user_did: params.did,
41 pagination: {
42 skip: params.offset,
43 take: params.limit,
44 },
45 }),
46 catch: (error) => new Error(`Failed to retrieve scrobles: ${error}`),
47 });
48};
49
50const presentation = ({
51 data,
52}: {
53 data: Scrobble[];
54}): Effect.Effect<{ scrobbles: ScrobbleViewBasic[] }, never> => {
55 return Effect.sync(() => ({ scrobbles: deepCamelCaseKeys(data) }));
56};
57
58type Scrobble = {
59 id: string;
60 track_id: string;
61 title: string;
62 artist: string;
63 album_artist: string;
64 album_art: string;
65 album: string;
66 handle: string;
67 did: string;
68 avatar: string | null;
69 uri: string;
70 track_uri: string;
71 artist_uri: string;
72 album_uri: string;
73 created_at: string;
74};
75
76export interface ScrobbleViewBasic {
77 /** The unique identifier of the scrobble. */
78 id?: string;
79 /** The handle of the user who created the scrobble. */
80 user?: string;
81 /** The display name of the user who created the scrobble. */
82 userDisplayName?: string;
83 /** The avatar URL of the user who created the scrobble. */
84 userAvatar?: string;
85 /** The title of the scrobble. */
86 title?: string;
87 /** The artist of the song. */
88 artist?: string;
89 /** The URI of the artist. */
90 artistUri?: string;
91 /** The album of the song. */
92 album?: string;
93 /** The URI of the album. */
94 albumUri?: string;
95 /** The album art URL of the song. */
96 cover?: string;
97 /** The timestamp when the scrobble was created. */
98 date?: string;
99 /** The URI of the scrobble. */
100 uri?: string;
101 /** The SHA256 hash of the scrobble data. */
102 sha256?: string;
103 [k: string]: unknown;
104}