···11import { TealContext } from "@/ctx";
22-import { db, plays, profiles } from "@teal/db";
22+import { db, mvTopReleasesPerUser30Days, profiles } from "@teal/db";
33import { OutputSchema } from "@teal/lexicons/src/types/fm/teal/alpha/actor/getTopAlbums";
44import { eq } from "drizzle-orm";
55···35353636 profile = profile[0];
37373838- const playsQuery = await db
3838+ const topReleases30Days = await db
3939 .select()
4040- .from(plays)
4141- .where(eq(plays.did, profile.did))
4242-4343- .limit(100);
4444-4545- const albums = playsQuery.map((play) => {
4646- return {
4747- albumName: play.releaseName,
4848- // TODO: okay so this isn't in the db ?!
4949- albumArtist: play.releaseName,
5050- // TODO: see how its implemented on frontend
5151- // albumArt: play.,
5252- albumReleaseMBID: play.releaseMbid,
5353- };
5454- });
5555-5656- // TODO: idk this probably sucks, i'm going to bed im tired
5757- const albumCounts = albums.reduce((acc: Record<string, number>, album) => {
5858- if (album.albumName && album.albumArtist) {
5959- acc[album.albumName] = (acc[album.albumName] || 0) + 1;
6060- }
6161- return acc;
6262- }, {});
6363- const sortedAlbums = Object.entries(albumCounts).sort((a, b) => b[1] - a[1]);
6464- const topAlbums = sortedAlbums.slice(0, Number(params.limit) || 10);
4040+ .from(mvTopReleasesPerUser30Days)
4141+ .where(eq(mvTopReleasesPerUser30Days.userDid, profile.did))
4242+ .limit(Number(params.limit) ?? 10);
65436644 const res: OutputSchema = {
6745 actor: {
···7452 createdAt: profile.createdAt?.toISOString(),
7553 },
7654 // TODO: actually implement this
7777- albums: topAlbums.map(([albumName]) => ({
7878- albumName,
7979- albumArtist: "", // TODO: Get actual artist name
5555+ albums: topReleases30Days.map((release) => ({
5656+ albumName: release.releaseName,
5757+ albumArtist: release.releaseName,
8058 albumArt: undefined,
8181- albumReleaseMBID: undefined,
5959+ albumReleaseMBID: release.releaseMbid,
8260 })),
8361 };
8462
+1
packages/db/.drizzle/0006_stale_romulus.sql
···11+CREATE MATERIALIZED VIEW "public"."mv_top_releases_per_user_30days" AS (select "plays"."did", "releases"."mbid", "releases"."name", count("plays"."uri") as "play_count" from "releases" inner join "plays" on "plays"."release_mbid" = "releases"."mbid" where "plays"."played_time" >= NOW() - INTERVAL '30 days' group by "plays"."did", "releases"."mbid", "releases"."name" order by count("plays"."uri") DESC);