tangled
alpha
login
or
join now
baileytownsend.dev
/
label-watcher
35
fork
atom
PDS Admin tool make it easier to moderate your PDS with labels
35
fork
atom
overview
issues
pulls
pipelines
manual upserts
baileytownsend.dev
2 weeks ago
30912d3f
7cf1c92d
+39
-11
1 changed file
expand all
collapse all
unified
split
src
handlers
handleNewLabel.ts
+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
6
-
import { count, eq } from "drizzle-orm";
6
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
37
-
{ action: config.labels[label.val]?.action },
37
37
+
{ action: labelConfig.action },
38
38
`Listed label: ${label.val} found. Performing the action against: ${label.uri}`,
39
39
);
40
40
41
41
-
await db.insert(schema.labelsApplied).values({
42
42
-
did: label.uri,
43
43
-
label: label.val,
44
44
-
labeler: config.host,
45
45
-
action: labelConfig.action,
46
46
-
negated: label.neg ?? false,
47
47
-
dateApplied: labledDate,
48
48
-
});
41
41
+
const existing = await db
42
42
+
.select()
43
43
+
.from(schema.labelsApplied)
44
44
+
.where(
45
45
+
and(
46
46
+
eq(schema.labelsApplied.did, label.uri),
47
47
+
eq(schema.labelsApplied.label, label.val),
48
48
+
eq(schema.labelsApplied.labeler, config.host),
49
49
+
),
50
50
+
)
51
51
+
.limit(1);
52
52
+
53
53
+
const [existingRecord] = existing;
54
54
+
if (existingRecord) {
55
55
+
await db
56
56
+
.update(schema.labelsApplied)
57
57
+
.set({
58
58
+
action: labelConfig.action,
59
59
+
negated: label.neg ?? false,
60
60
+
dateApplied: labledDate,
61
61
+
})
62
62
+
.where(eq(schema.labelsApplied.id, existingRecord.id));
63
63
+
logger.debug(
64
64
+
{ did: label.uri, label: label.val },
65
65
+
"Updated existing label record",
66
66
+
);
67
67
+
} else {
68
68
+
await db.insert(schema.labelsApplied).values({
69
69
+
did: label.uri,
70
70
+
label: label.val,
71
71
+
labeler: config.host,
72
72
+
action: labelConfig.action,
73
73
+
negated: label.neg ?? false,
74
74
+
dateApplied: labledDate,
75
75
+
});
76
76
+
}
49
77
50
78
return;
51
79
}
52
80
logger.warn(
53
53
-
{ action: config.labels[label.val]?.action },
81
81
+
{ action: labelConfig.action },
54
82
"Listed label found but repo is not watched. Skipping",
55
83
);
56
84
}