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 tables from "../schema/mod.ts";
4import { eq, desc } from "drizzle-orm";
5
6export default function (ctx: Context, offset?: number, limit?: number) {
7 return Effect.runPromise(
8 pipe(
9 retrieve({
10 ctx,
11 params: {
12 offset: offset || 0,
13 limit: limit || 20,
14 },
15 }),
16 Effect.retry({ times: 3 }),
17 Effect.timeout("120 seconds"),
18 Effect.catchAll((error) =>
19 Effect.fail(new Error(`Failed to retrieve scrobbles: ${error}`)),
20 ),
21 ),
22 );
23}
24
25const retrieve = ({
26 ctx,
27 params,
28}: {
29 ctx: Context;
30 params: { offset?: number; limit?: number };
31}) => {
32 return Effect.tryPromise({
33 try: () =>
34 ctx.db
35 .select()
36 .from(tables.scrobbles)
37 .leftJoin(tables.tracks, eq(tables.scrobbles.trackId, tables.tracks.id))
38 .leftJoin(tables.users, eq(tables.scrobbles.userId, tables.users.id))
39 .orderBy(desc(tables.scrobbles.timestamp))
40 .offset(params.offset || 0)
41 .limit(params.limit || 20)
42 .execute(),
43
44 catch: (error) => new Error(`Failed to retrieve scrobbles: ${error}`),
45 });
46};