A decentralized music tracking and discovery platform built on AT Protocol 馃幍
at fff48ea3213bb11efcfcb7db85be1dfcd2bebc5e 52 lines 1.6 kB view raw
1import chalk from "chalk"; 2import { consola } from "consola"; 3import type { Context } from "context"; 4import { eq } from "drizzle-orm"; 5import _ from "lodash"; 6import { StringCodec } from "nats"; 7import tables from "schema"; 8 9export function onNewTrack(ctx: Context) { 10 const sc = StringCodec(); 11 const sub = ctx.nc.subscribe("rocksky.track"); 12 (async () => { 13 for await (const m of sub) { 14 const payload: { 15 track: { xata_id: string }; 16 artist_album: { 17 artist_id: { xata_id: string }; 18 album_id: { xata_id: string }; 19 }; 20 } = JSON.parse(sc.decode(m.data)); 21 22 const [tracks, artists, albums] = await Promise.all([ 23 ctx.db 24 .select() 25 .from(tables.tracks) 26 .where(eq(tables.tracks.id, payload.track.xata_id)) 27 .execute(), 28 ctx.db 29 .select() 30 .from(tables.artists) 31 .where(eq(tables.artists.id, payload.artist_album.artist_id.xata_id)) 32 .execute(), 33 ctx.db 34 .select() 35 .from(tables.albums) 36 .where(eq(tables.albums.id, payload.artist_album.album_id.xata_id)) 37 .execute(), 38 ]); 39 40 consola.info(`New track: ${chalk.cyan(_.get(tracks, "0.title"))}`); 41 42 await Promise.all([ 43 ctx.meilisearch.post(`indexes/albums/documents?primaryKey=id`, albums), 44 ctx.meilisearch.post( 45 `indexes/artists/documents?primaryKey=id`, 46 artists, 47 ), 48 ctx.meilisearch.post(`indexes/tracks/documents?primaryKey=id`, tracks), 49 ]); 50 } 51 })(); 52}