PDS Admin tool make it easier to moderate your PDS with labels

manual upserts

+39 -11
+39 -11
src/handlers/handleNewLabel.ts
··· 3 3 import { logger } from "../logger.js"; 4 4 import type { LibSQLDatabase } from "drizzle-orm/libsql"; 5 5 import * as schema from "../db/schema.js"; 6 - import { count, eq } from "drizzle-orm"; 6 + import { and, count, eq } from "drizzle-orm"; 7 7 8 8 export const handleNewLabel = async ( 9 9 config: LabelerConfig, ··· 34 34 35 35 if (isRepoWatched.length > 0) { 36 36 logger.info( 37 - { action: config.labels[label.val]?.action }, 37 + { action: labelConfig.action }, 38 38 `Listed label: ${label.val} found. Performing the action against: ${label.uri}`, 39 39 ); 40 40 41 - await db.insert(schema.labelsApplied).values({ 42 - did: label.uri, 43 - label: label.val, 44 - labeler: config.host, 45 - action: labelConfig.action, 46 - negated: label.neg ?? false, 47 - dateApplied: labledDate, 48 - }); 41 + const existing = await db 42 + .select() 43 + .from(schema.labelsApplied) 44 + .where( 45 + and( 46 + eq(schema.labelsApplied.did, label.uri), 47 + eq(schema.labelsApplied.label, label.val), 48 + eq(schema.labelsApplied.labeler, config.host), 49 + ), 50 + ) 51 + .limit(1); 52 + 53 + const [existingRecord] = existing; 54 + if (existingRecord) { 55 + await db 56 + .update(schema.labelsApplied) 57 + .set({ 58 + action: labelConfig.action, 59 + negated: label.neg ?? false, 60 + dateApplied: labledDate, 61 + }) 62 + .where(eq(schema.labelsApplied.id, existingRecord.id)); 63 + logger.debug( 64 + { did: label.uri, label: label.val }, 65 + "Updated existing label record", 66 + ); 67 + } else { 68 + await db.insert(schema.labelsApplied).values({ 69 + did: label.uri, 70 + label: label.val, 71 + labeler: config.host, 72 + action: labelConfig.action, 73 + negated: label.neg ?? false, 74 + dateApplied: labledDate, 75 + }); 76 + } 49 77 50 78 return; 51 79 } 52 80 logger.warn( 53 - { action: config.labels[label.val]?.action }, 81 + { action: labelConfig.action }, 54 82 "Listed label found but repo is not watched. Skipping", 55 83 ); 56 84 }