-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (55 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict";
import functions from "@google-cloud/functions-framework";
import openAIClientPool from "./src/lib/openAIClientPool.js";
import RateLimiting from "./src/lib/rateLimiting.js";
import Nostr, { REPORT_KIND } from "./src/lib/nostr.js";
import Slack from "./src/lib/slack.js";
import DuplicationHandling from "./src/lib/duplicationHandling.js";
import ReportRequest from "./src/lib/reportRequest.js";
functions.cloudEvent("nostrEventsPubSub", async (cloudEvent) => {
//The nostr event can either directly be the object or be encapsulated within
//a reportedEvent key, if present. A reportedEvent key indicates a
//user-initiated manual report request originating from the reportinator
//server. These events require Slack-based verification, except when they get
//auto-flagged. They also include a test and a pubkey of the reporter user.
const reportRequest = ReportRequest.fromCloudEvent(cloudEvent);
const nostrEvent = Nostr.getVerifiedEvent(reportRequest.reportedEvent);
if (!nostrEvent) {
return;
}
await RateLimiting.handleRateLimit(async function () {
await DuplicationHandling.processIfNotDuplicate(
reportRequest.canBeManualVerified(),
nostrEvent,
async (event, onlySlack) => {
if (onlySlack) {
await Slack.postManualVerification(reportRequest);
return;
}
let eventToModerate = event;
let skipMessage = `Nostr Event ${event.id} passed moderation. Skipping`;
if (event.kind === REPORT_KIND) {
eventToModerate = await Nostr.getReportedNostrEvent(event);
if (!eventToModerate) {
return console.log(
`Couldn't find event reported by report ${event.id}`
);
}
skipMessage = `Nostr Event ${eventToModerate.id} reported by ${event.id} passed moderation. Skipping`;
}
const moderation = await openAIClientPool.getModeration(
eventToModerate
);
if (!moderation) {
if (!reportRequest.canBeManualVerified()) {
console.log(skipMessage);
return;
}
await Slack.postManualVerification(reportRequest);
return;
}
await Nostr.publishModeration(eventToModerate, moderation);
}
);
});
});