diff --git a/README.md b/README.md index 7d45af0..224d458 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Cleanstr is a Google Cloud Function node application designed to filter and mode - If you don't want to host your own deployment you can use our relay. Add `wss://relay.nos.social` to your Nostr client's relay list. - Content flagged by Cleanstr is managed by this relay and marked as kind 1984 for prompt handling. - Following the [Reportinator](https://njump.me/nprofile1qqs2m4gep0jxwdmg23kp3dt9mgaxnyjp7rsx5a0zm0qr7xrx85dhkfcpzemhxue69uhhyetvv9ujumn0wvh8xmmrd9skcl8vqu6) bot in your Nostr client is essential for proper integration with Cleanstr's moderation reports. - - Your client should properly process kind 1984 in a similar way that https://nos.social does. + - Your client should properly process kind 1984 in a similar way that https://nos.social does. ## Support diff --git a/src/lib/nostr.js b/src/lib/nostr.js index 77b8215..f2f048c 100644 --- a/src/lib/nostr.js +++ b/src/lib/nostr.js @@ -2,6 +2,7 @@ import NDK, { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import { validateEvent, verifySignature } from "nostr-tools"; import OPENAI_CATEGORIES from "./openAICategories.js"; import { WebSocket } from "ws"; +import { nip19 } from "nostr-tools"; if (!process.env.NOSTR_PRIVATE_KEY) { throw new Error("NOSTR_PRIVATE_KEY environment variable is required"); @@ -31,13 +32,21 @@ export const REPORT_KIND = 1984; export default class Nostr { static async updateNjump(reportRequest, hexpubkey, fieldToUpdate) { + const profile = await this.fetchProfile(hexpubkey); + + const njumpPath = profile?.nip05 + ? profile.nip05 + : nip19.npubEncode(hexpubkey); + console.log("njumpPath", njumpPath, hexpubkey); + + const njump = `https://njump.me/${njumpPath}`; + reportRequest[fieldToUpdate] = njump; + } + + static async fetchProfile(hexpubkey) { await connectedPromise; const user = ndk.getUser({ hexpubkey }); - const profile = await user.fetchProfile(); - if (profile?.nip05) { - const njump = `https://njump.me/${profile.nip05}`; - reportRequest[fieldToUpdate] = njump; - } + return await user.fetchProfile(); } static async maybeFetchNip05(reportRequest) { diff --git a/src/lib/reportRequest.js b/src/lib/reportRequest.js index 61d43b8..ffa33cb 100644 --- a/src/lib/reportRequest.js +++ b/src/lib/reportRequest.js @@ -22,15 +22,6 @@ export default class ReportRequest { return new ReportRequest(json, null, null); } - reporterNpub() { - console.log("this.reporterPubkey", this.reporterPubkey); - return nip19.npubEncode(this.reporterPubkey); - } - - reportedNpub() { - return nip19.npubEncode(this.reportedEvent.pubkey); - } - nevent() { return nip19.neventEncode(this.reportedEvent.id); } diff --git a/src/lib/slack.js b/src/lib/slack.js index 36f0aa2..744563e 100644 --- a/src/lib/slack.js +++ b/src/lib/slack.js @@ -1,5 +1,4 @@ import { WebClient } from "@slack/web-api"; -import OPENAI_CATEGORIES from "./openAICategories.js"; import Nostr from "./nostr.js"; if (!process.env.SLACK_TOKEN) { @@ -44,11 +43,7 @@ export default class Slack { } static createSlackMessagePayload(reportRequest) { - let text = `New Nostr Event to moderate requested by ${ - reportRequest.njump || code(reportRequest.reporterNpub()) - } reporting an event published by ${ - reportRequest.reportedUserNjump || code(reportRequest.reportedNpub()) - }`; + let text = `New Nostr Event to moderate requested by ${reportRequest.njump} reporting an event published by ${reportRequest.reportedUserNjump}`; const elements = nip56_report_type.map((category) => { return { diff --git a/test/moderationFunction.test.js b/test/moderationFunction.test.js index acf7390..82f383f 100644 --- a/test/moderationFunction.test.js +++ b/test/moderationFunction.test.js @@ -60,7 +60,7 @@ describe("Moderation Cloud Function", async () => { sinon.spy(console, "error"); sinon.spy(console, "log"); sinon.stub(Nostr, "publishNostrEvent").returns(Promise.resolve()); - sinon.stub(Nostr, "updateNjump").returns(Promise.resolve()); + sinon.stub(Nostr, "fetchProfile").returns(Promise.resolve()); sinon.stub(Slack, "postManualVerification").returns(Promise.resolve()); sinon.stub(Datastore.prototype, "get").resolves([]); sinon.stub(Datastore.prototype, "save").resolves(); diff --git a/test/slack.test.js b/test/slack.test.js index 562915d..01f6a37 100644 --- a/test/slack.test.js +++ b/test/slack.test.js @@ -3,19 +3,21 @@ import sinon from "sinon"; import Slack from "../src/lib/slack.js"; import ReportRequest from "../src/lib/reportRequest.js"; import fs from "fs"; +import Nostr from "../src/lib/nostr.js"; describe("Slack", () => { beforeEach(async () => { sinon.spy(console, "error"); sinon.spy(console, "log"); sinon.stub(Slack, "postManualVerification").returns(Promise.resolve()); + sinon.stub(Nostr, "fetchProfile").returns(Promise.resolve()); }); afterEach(async () => { sinon.restore(); }); - it("createSlackMessagePayload", () => { + it("createSlackMessagePayload", async () => { const pubkey = "56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65"; const nostrEvent = { @@ -45,12 +47,12 @@ describe("Slack", () => { }; const reportRequest = ReportRequest.fromCloudEvent(cloudEvent); - console.log(reportRequest); + await Nostr.maybeFetchNip05(reportRequest); const slackMessagePayload = Slack.createSlackMessagePayload(reportRequest); expect(slackMessagePayload).to.be.eql({ channel: "something", - text: "New Nostr Event to moderate requested by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j` reporting an event published by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j`", + text: "New Nostr Event to moderate requested by https://njump.me/npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j reporting an event published by https://njump.me/npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j", unfurl_links: false, unfurl_media: false, blocks: [ @@ -58,7 +60,7 @@ describe("Slack", () => { type: "section", text: { type: "mrkdwn", - text: "New Nostr Event to moderate requested by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j` reporting an event published by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j`", + text: "New Nostr Event to moderate requested by https://njump.me/npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j reporting an event published by https://njump.me/npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j", }, }, {