Skip to content

Commit

Permalink
Refactor and use same schema as reportinator server
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Mar 27, 2024
1 parent c0ba361 commit 31f8943
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 93 deletions.
31 changes: 9 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ 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 nostrEvent key, if present. A nostrEvent 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."
const [nostrEventJson, userReportRequest] = getJSONFromCloudEvent(cloudEvent);
const nostrEvent = Nostr.getVerifiedEvent(nostrEventJson);
//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;
Expand Down Expand Up @@ -41,15 +43,12 @@ functions.cloudEvent("nostrEventsPubSub", async (cloudEvent) => {
);

if (!moderation) {
if (!userReportRequest) {
if (!reportRequest.reporterPubkey) {
console.log(skipMessage);
return;
}

await Slack.postManualVerification(nostrEventJson, userReportRequest);
console.log(
`Event ${eventToModerate.id} reported by ${userReportRequest.pubkey} not flagged. Sending to Slack`
);
await Slack.postManualVerification(reportRequest);
return;
}

Expand All @@ -58,15 +57,3 @@ functions.cloudEvent("nostrEventsPubSub", async (cloudEvent) => {
);
});
});

function getJSONFromCloudEvent(cloudEvent) {
const data = cloudEvent.data.message.data;
const jsonString = data ? Buffer.from(data, "base64").toString() : "{}";
const json = JSON.parse(jsonString);

if (json?.userReportRequest) {
return [json?.nostrEvent, json.userReportRequest];
}

return [json, null];
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": "~20"
},
"scripts": {
"test": "c8 --reporter=lcov --check-coverage --lines 80 --functions 80 --branches 80 --statements 80 mocha --timeout=5000 --exit",
"test": "c8 --reporter=lcov --check-coverage --lines 80 --functions 80 --branches 80 --statements 80 mocha --timeout=10000 --exit",
"test:integration": "c8 mocha -j 2 test/integration.test.js --timeout=5000",
"test:grep": "c8 mocha -j 2 --timeout=5000 --grep",
"test:rateLimit": "node runArtilleryTest.mjs",
Expand All @@ -33,7 +33,7 @@
"c8": "^8.0.0",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"mocha": "^10.0.0",
"mocha": "^10.4.0",
"sinon": "^15.0.0",
"supertest": "^6.0.0",
"uuid": "^9.0.0"
Expand Down
29 changes: 11 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/lib/reportRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default class ReportRequest {
constructor(reportedEvent, reporterPubkey, reporterText) {
this.reportedEvent = reportedEvent;
this.reporterPubkey = reporterPubkey;
this.reporterText = reporterText;
}

static fromCloudEvent(cloudEvent) {
const data = cloudEvent.data.message.data;
const jsonString = data ? Buffer.from(data, "base64").toString() : "{}";
const json = JSON.parse(jsonString);

if (json?.reportedEvent) {
return new ReportRequest(
json.reportedEvent,
json?.reporterPubkey,
json?.reporterText
);
}

return new ReportRequest(json, null, null);
}

canBeManualVerified() {
return Boolean(this.reporterPubkey);
}
}
9 changes: 7 additions & 2 deletions src/lib/slack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export default class Slack {
static async postManualVerification(nostrEventJson, userReportRequest) {
console.log(`userReportRequest: ${userReportRequest}`);
static async postManualVerification(reportRequest) {
console.log(
`TODO: Implement Slack.postManualVerification.\n
Event payload: ${JSON.stringify(reportRequest.reportedEvent)}\n
Reporter pubkey: ${reportRequest.reporterPubkey}\n
Reporter text: ${reportRequest.reporterText}`
);
}
}
Loading

0 comments on commit 31f8943

Please sign in to comment.