A discord bot for teal.fm
discord tealfm music

include saving cid, uri and rkey to database

besaid.zone e4a8259a e6c7419f

verified
Changed files
+62 -4
apps
packages
+13 -3
apps/tapper/index.ts
··· 1 1 import { SimpleIndexer, Tap } from "@atproto/tap"; 2 2 import { env } from "@tealfmbot/common/constants"; 3 - // import { db } from "./kysely/db.ts" 3 + // import { isTealRecord } from "./utils"; 4 + // import { db } from "@tealfmbot/database/db" 4 5 5 6 const tap = new Tap("https://tap.xero.systems", { 6 7 adminPassword: env.TAP_ADMIN_PASSWORD, ··· 10 11 11 12 indexer.record(async (evt, opts) => { 12 13 const uri = `at://${evt.did}/${evt.collection}/${evt.rkey}`; 13 - if (evt.action === "create" || evt.action === "update") { 14 + if (evt.action === "create") { 15 + // if (isTealRecord(evt.record) && typeof evt !== "undefined" && typeof evt.record !== 'undefined') { 16 + // await db.insertInto("plays").values({ 17 + // release_name: evt?.record?.releaseName, 18 + // played_time: evt?.record?.playedTime, 19 + // track_name: evt?.record?.trackName, 20 + // indexed_at: new Date().toJSON(), 21 + // live: evt.live 22 + // }).execute() 23 + // } 14 24 // await db.insertInto("plays").values({ 15 25 // played_time: evt?.record?.playedTime, 16 26 // release_name: evt?.record?.releaseName, 17 27 // track_name: evt?.record?.trackName, 18 28 // user_id: 4 19 - // }).execute() 29 + // }).execute 20 30 console.log(evt.record); 21 31 } else { 22 32 console.log(`deleted: ${uri}`);
+2 -1
apps/tapper/package.json
··· 10 10 }, 11 11 "dependencies": { 12 12 "@atproto/tap": "^0.0.2", 13 - "@tealfmbot/common": "workspace:*" 13 + "@tealfmbot/common": "workspace:*", 14 + "@tealfmbot/database": "workspace:*" 14 15 }, 15 16 "devDependencies": { 16 17 "@tealfmbot/tsconfig": "workspace:*",
+24
apps/tapper/utils.ts
··· 1 + type TealRecord = { 2 + $type: "fm.teal.alpha.feed.play"; 3 + trackName: string; 4 + trackMbId?: string; 5 + recordingMbId?: string; 6 + duration?: number; 7 + releaseName?: string; 8 + releaseMbId?: string; 9 + isrc?: string; 10 + originUrl?: string; 11 + musicServiceBaseDomain?: string; 12 + submissionClientAgent?: string; 13 + playedTime?: Date; 14 + artists: Artist[]; 15 + }; 16 + 17 + type Artist = { 18 + artistMbId?: string; 19 + artistName?: string; 20 + }; 21 + 22 + export function isTealRecord(record: unknown): record is TealRecord { 23 + return (record as TealRecord).$type === "fm.teal.alpha.feed.play"; 24 + }
+3
packages/database/database.d.ts
··· 27 27 } 28 28 29 29 export interface Plays { 30 + cid: string | null; 30 31 id: Generated<number>; 31 32 indexed_at: Generated<Timestamp>; 32 33 live: boolean | null; 33 34 played_time: Timestamp; 34 35 release_name: string; 36 + rkey: string | null; 35 37 track_name: string; 38 + uri: string | null; 36 39 user_id: number; 37 40 } 38 41
+17
packages/database/migrations/1767553973566_add_cid_uri_rkey.ts
··· 1 + import type { Kysely } from 'kysely' 2 + 3 + // `any` is required here since migrations should be frozen in time. alternatively, keep a "snapshot" db interface. 4 + export async function up(db: Kysely<any>): Promise<void> { 5 + // up migration code goes here... 6 + // note: up migrations are mandatory. you must implement this function. 7 + // For more info, see: https://kysely.dev/docs/migrations 8 + await db.schema.alterTable("plays").addColumn("cid", "varchar").addColumn("uri", "varchar").addColumn("rkey", "varchar").execute() 9 + } 10 + 11 + // `any` is required here since migrations should be frozen in time. alternatively, keep a "snapshot" db interface. 12 + export async function down(db: Kysely<any>): Promise<void> { 13 + // down migration code goes here... 14 + // note: down migrations are optional. you can safely delete this function. 15 + // For more info, see: https://kysely.dev/docs/migrations 16 + await db.schema.alterTable("plays").dropColumn("cid").dropColumn("uri").dropColumn("rkey").execute() 17 + }
+3
pnpm-lock.yaml
··· 55 55 '@tealfmbot/common': 56 56 specifier: workspace:* 57 57 version: link:../../packages/common 58 + '@tealfmbot/database': 59 + specifier: workspace:* 60 + version: link:../../packages/database 58 61 devDependencies: 59 62 '@tealfmbot/tsconfig': 60 63 specifier: workspace:*