tangled
alpha
login
or
join now
baileytownsend.dev
/
label-watcher
36
fork
atom
PDS Admin tool make it easier to moderate your PDS with labels
36
fork
atom
overview
issues
pulls
pipelines
manual upserts
baileytownsend.dev
3 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
import { logger } from "../logger.js";
4
import type { LibSQLDatabase } from "drizzle-orm/libsql";
5
import * as schema from "../db/schema.js";
6
-
import { count, eq } from "drizzle-orm";
7
8
export const handleNewLabel = async (
9
config: LabelerConfig,
···
34
35
if (isRepoWatched.length > 0) {
36
logger.info(
37
-
{ action: config.labels[label.val]?.action },
38
`Listed label: ${label.val} found. Performing the action against: ${label.uri}`,
39
);
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
-
});
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
49
50
return;
51
}
52
logger.warn(
53
-
{ action: config.labels[label.val]?.action },
54
"Listed label found but repo is not watched. Skipping",
55
);
56
}
···
3
import { logger } from "../logger.js";
4
import type { LibSQLDatabase } from "drizzle-orm/libsql";
5
import * as schema from "../db/schema.js";
6
+
import { and, count, eq } from "drizzle-orm";
7
8
export const handleNewLabel = async (
9
config: LabelerConfig,
···
34
35
if (isRepoWatched.length > 0) {
36
logger.info(
37
+
{ action: labelConfig.action },
38
`Listed label: ${label.val} found. Performing the action against: ${label.uri}`,
39
);
40
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
+
}
77
78
return;
79
}
80
logger.warn(
81
+
{ action: labelConfig.action },
82
"Listed label found but repo is not watched. Skipping",
83
);
84
}