Skip to content

Commit

Permalink
Use npub if no njump available
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed May 30, 2024
1 parent abdebeb commit 571fe08
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
14 changes: 14 additions & 0 deletions src/lib/reportRequest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nip19 } from "nostr-tools";
export default class ReportRequest {
constructor(reportedEvent, reporterPubkey, reporterText) {
this.reportedEvent = reportedEvent;
Expand All @@ -21,6 +22,19 @@ 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);
}

canBeManualVerified() {
return Boolean(this.reporterPubkey);
}
Expand Down
15 changes: 6 additions & 9 deletions src/lib/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ export default class Slack {
await web.chat.postMessage(messagePayload);

console.log(
`Sent event ${reportRequest.reportedEvent.id} to Slack for manual evaluation.`
`Sent event ${reportRequest.reportedEvent.nevent()} to Slack for manual evaluation.`
);
} catch (error) {
console.error(error);
}
}

static createSlackMessagePayload(reportRequest) {
let text = `New Nostr Event to moderate requested by pubkey \`${reportRequest.reporterPubkey}\``;
if (reportRequest.njump) {
text = `New Nostr Event to moderate requested by ${reportRequest.njump}`;
}

if (reportRequest.reportedUserNjump) {
text += ` reporting an event published by ${reportRequest.reportedUserNjump}`;
}
let text = `New Nostr Event to moderate requested by \`${
reportRequest.njump || reportRequest.reporterNpub()
}\` reporting an event published by \`${
reportRequest.reportedUserNjump || reportRequest.reportedNpub()
}\``;

const elements = Object.entries(OPENAI_CATEGORIES).map(
([category, categoryData]) => {
Expand Down
49 changes: 32 additions & 17 deletions test/slack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ describe("Slack", () => {
});

it("createSlackMessagePayload", () => {
const pubkey =
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65";
const nostrEvent = {
id: "12",
pubkey: "34",
pubkey,
created_at: 1673347337,
kind: 1,
content: "Foobar",
Expand All @@ -35,27 +37,28 @@ describe("Slack", () => {
data: Buffer.from(
JSON.stringify({
reportedEvent: nostrEvent,
reporterPubkey: "npub123",
reporterPubkey: pubkey,
})
).toString("base64"),
},
},
};

const reportRequest = ReportRequest.fromCloudEvent(cloudEvent);
console.log(reportRequest);
const slackMessagePayload = Slack.createSlackMessagePayload(reportRequest);

expect(slackMessagePayload).to.be.eql({
channel: "something",
text: "New Nostr Event to moderate requested by pubkey `npub123`",
text: "New Nostr Event to moderate requested by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j` reporting an event published by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j`",
unfurl_links: false,
unfurl_media: false,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "New Nostr Event to moderate requested by pubkey `npub123`",
text: "New Nostr Event to moderate requested by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j` reporting an event published by `npub12m2t8433p7kmw22t0uzp426xn30lezv3kxcmxvvcrwt2y3hk4ejsvre68j`",
},
},
{
Expand Down Expand Up @@ -113,7 +116,7 @@ describe("Slack", () => {
{
type: "text",
style: { code: true },
text: '{\n "id": "12",\n "pubkey": "34",\n "created_at": 1673347337,\n "kind": 1,\n "content": "Foobar",\n "tags": [\n [\n "e",\n "56"\n ],\n [\n "p",\n "78"\n ]\n ],\n "sig": "91"\n}',
text: '{\n "id": "12",\n "pubkey": "56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",\n "created_at": 1673347337,\n "kind": 1,\n "content": "Foobar",\n "tags": [\n [\n "e",\n "56"\n ],\n [\n "p",\n "78"\n ]\n ],\n "sig": "91"\n}',
},
],
border: 0,
Expand All @@ -130,7 +133,8 @@ describe("Slack", () => {
type: "plain_text",
text: "Skip",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "skip",
},
{
Expand All @@ -139,7 +143,8 @@ describe("Slack", () => {
type: "plain_text",
text: "hate",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "hate",
},
{
Expand All @@ -148,7 +153,8 @@ describe("Slack", () => {
type: "plain_text",
text: "hate/threatening",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "hate/threatening",
},
{
Expand All @@ -157,7 +163,8 @@ describe("Slack", () => {
type: "plain_text",
text: "harassment",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "harassment",
},
{
Expand All @@ -166,7 +173,8 @@ describe("Slack", () => {
type: "plain_text",
text: "harassment/threatening",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "harassment/threatening",
},
{
Expand All @@ -175,7 +183,8 @@ describe("Slack", () => {
type: "plain_text",
text: "self-harm",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "self-harm",
},
{
Expand All @@ -184,7 +193,8 @@ describe("Slack", () => {
type: "plain_text",
text: "self-harm/intent",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "self-harm/intent",
},
{
Expand All @@ -193,7 +203,8 @@ describe("Slack", () => {
type: "plain_text",
text: "self-harm/instructions",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "self-harm/instructions",
},
{
Expand All @@ -202,7 +213,8 @@ describe("Slack", () => {
type: "plain_text",
text: "sexual",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "sexual",
},
{
Expand All @@ -211,7 +223,8 @@ describe("Slack", () => {
type: "plain_text",
text: "sexual/minors",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "sexual/minors",
},
{
Expand All @@ -220,7 +233,8 @@ describe("Slack", () => {
type: "plain_text",
text: "violence",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "violence",
},
{
Expand All @@ -229,7 +243,8 @@ describe("Slack", () => {
type: "plain_text",
text: "violence/graphic",
},
value: "npub123",
value:
"56d4b3d6310fadb7294b7f041aab469c5ffc8991b1b1b331981b96a246f6ae65",
action_id: "violence/graphic",
},
],
Expand Down

0 comments on commit 571fe08

Please sign in to comment.