From 5f716b9fbaa9e83917a361e65604489b42b8c65e Mon Sep 17 00:00:00 2001 From: David Germain Date: Mon, 3 Jun 2024 19:59:10 +0200 Subject: [PATCH 1/5] feat: make a webhook for when a question is accepted --- functions/src/Integrations/firebase-slack.ts | 35 ++++++++++++++++++++ functions/src/Integrations/index.ts | 1 + 2 files changed, 36 insertions(+) diff --git a/functions/src/Integrations/firebase-slack.ts b/functions/src/Integrations/firebase-slack.ts index 8c618a5a48..e7885e2bbb 100644 --- a/functions/src/Integrations/firebase-slack.ts +++ b/functions/src/Integrations/firebase-slack.ts @@ -64,3 +64,38 @@ export const notifyNewHowTo = functions }, ) }) + +export const notifyAcceptedQuestion = functions + .runWith({ memory: '512MB' }) + .firestore.document('questions_rev20230926/{id}') + .onCreate((snapshot) => { + const info = snapshot.data() + console.log(info) + + const username = info._createdBy + const title = info.title + const slug = info.slug + + const moderation = info.moderation + + if (moderation !== IModerationStatus.ACCEPTED) { + return + } + + request.post( + SLACK_WEBHOOK_URL, + { + json: { + text: `❓ ${username} has a new question: ${title}\n Help them out and answer here: ${SITE_URL}/questions/${slug}`, + }, + }, + (error, response) => { + if (error) { + console.error(error) + return + } else { + return response + } + }, + ) + }) diff --git a/functions/src/Integrations/index.ts b/functions/src/Integrations/index.ts index 2cbf2a710e..00bec4de05 100644 --- a/functions/src/Integrations/index.ts +++ b/functions/src/Integrations/index.ts @@ -4,6 +4,7 @@ import * as IntegrationsPatreon from './patreon' exports.notifyNewPin = IntegrationsSlack.notifyNewPin exports.notifyNewHowTo = IntegrationsSlack.notifyNewHowTo +exports.notifyAcceptedQuestion = IntegrationsSlack.notifyAcceptedQuestion exports.notifyPinAccepted = IntegrationsDiscord.notifyPinAccepted exports.notifyHowToAccepted = IntegrationsDiscord.notifyHowToAccepted exports.patreonAuth = IntegrationsPatreon.patreonAuth From f1821ac13d383c1f74104b5bcc1eb239a4f2b7c7 Mon Sep 17 00:00:00 2001 From: David Germain Date: Fri, 14 Jun 2024 18:07:22 +0200 Subject: [PATCH 2/5] refactor: group exports by topic for easier understanding --- functions/src/Integrations/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/functions/src/Integrations/index.ts b/functions/src/Integrations/index.ts index 00bec4de05..a8d2bce4c8 100644 --- a/functions/src/Integrations/index.ts +++ b/functions/src/Integrations/index.ts @@ -3,8 +3,11 @@ import * as IntegrationsDiscord from './firebase-discord' import * as IntegrationsPatreon from './patreon' exports.notifyNewPin = IntegrationsSlack.notifyNewPin -exports.notifyNewHowTo = IntegrationsSlack.notifyNewHowTo -exports.notifyAcceptedQuestion = IntegrationsSlack.notifyAcceptedQuestion exports.notifyPinAccepted = IntegrationsDiscord.notifyPinAccepted + +exports.notifyNewHowTo = IntegrationsSlack.notifyNewHowTo exports.notifyHowToAccepted = IntegrationsDiscord.notifyHowToAccepted + +exports.notifyAcceptedQuestion = IntegrationsSlack.notifyAcceptedQuestion + exports.patreonAuth = IntegrationsPatreon.patreonAuth From 25eaad320988c37539984733bfde511aafe430e3 Mon Sep 17 00:00:00 2001 From: David Germain Date: Fri, 14 Jun 2024 18:53:48 +0200 Subject: [PATCH 3/5] chore: cleanup code because questions are not moderated --- functions/src/Integrations/firebase-slack.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/functions/src/Integrations/firebase-slack.ts b/functions/src/Integrations/firebase-slack.ts index e7885e2bbb..af8aa4e0fa 100644 --- a/functions/src/Integrations/firebase-slack.ts +++ b/functions/src/Integrations/firebase-slack.ts @@ -68,6 +68,8 @@ export const notifyNewHowTo = functions export const notifyAcceptedQuestion = functions .runWith({ memory: '512MB' }) .firestore.document('questions_rev20230926/{id}') + // currently, questions are immediately posted with no review. + // if that changes, this code will need to change. .onCreate((snapshot) => { const info = snapshot.data() console.log(info) @@ -76,12 +78,6 @@ export const notifyAcceptedQuestion = functions const title = info.title const slug = info.slug - const moderation = info.moderation - - if (moderation !== IModerationStatus.ACCEPTED) { - return - } - request.post( SLACK_WEBHOOK_URL, { From 3894a822676fc523f154b3070c98238c1d3ae22f Mon Sep 17 00:00:00 2001 From: David Germain Date: Fri, 14 Jun 2024 18:54:46 +0200 Subject: [PATCH 4/5] chore: use the discord webhook url in the slack file --- functions/src/Integrations/firebase-slack.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/src/Integrations/firebase-slack.ts b/functions/src/Integrations/firebase-slack.ts index af8aa4e0fa..1ea6c2d8e5 100644 --- a/functions/src/Integrations/firebase-slack.ts +++ b/functions/src/Integrations/firebase-slack.ts @@ -7,6 +7,7 @@ const SITE_URL = CONFIG.deployment.site_url // e.g. https://dev.onearmy.world or https://community.preciousplastic.com const SLACK_WEBHOOK_URL = CONFIG.integrations.slack_webhook +const DISCORD_WEBHOOK_URL = CONFIG.integrations.discord_webhook export const notifyNewPin = functions .runWith({ memory: '512MB' }) @@ -79,7 +80,7 @@ export const notifyAcceptedQuestion = functions const slug = info.slug request.post( - SLACK_WEBHOOK_URL, + DISCORD_WEBHOOK_URL, { json: { text: `❓ ${username} has a new question: ${title}\n Help them out and answer here: ${SITE_URL}/questions/${slug}`, From b9d2bf0dec40f77decc92eb900155dcda6d98d69 Mon Sep 17 00:00:00 2001 From: David Germain Date: Fri, 14 Jun 2024 19:09:40 +0200 Subject: [PATCH 5/5] chore: move code to discord file --- .../src/Integrations/firebase-discord.ts | 25 +++++++++++++++ functions/src/Integrations/firebase-slack.ts | 32 ------------------- functions/src/Integrations/index.ts | 2 +- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/functions/src/Integrations/firebase-discord.ts b/functions/src/Integrations/firebase-discord.ts index 40d6a029d8..9639adfff8 100644 --- a/functions/src/Integrations/firebase-discord.ts +++ b/functions/src/Integrations/firebase-discord.ts @@ -54,6 +54,31 @@ export const notifyHowToAccepted = functions .catch(handleErr) }) +export const notifyAcceptedQuestion = functions + .runWith({ memory: '512MB' }) + .firestore.document('questions_rev20230926/{id}') + // currently, questions are immediately posted with no review. + // if that changes, this code will need to be updated. + .onCreate(async (snapshot) => { + const info = snapshot.data() + console.log(info) + + const username = info._createdBy + const title = info.title + const slug = info.slug + + try { + const response = await axios.post(DISCORD_WEBHOOK_URL, { + json: { + text: `❓ ${username} has a new question: ${title}\n Help them out and answer here: ${SITE_URL}/questions/${slug}`, + }, + }) + handleResponse(response) + } catch (error) { + handleErr(error) + } + }) + const handleResponse = (res: AxiosResponse) => { console.log('post success') return res diff --git a/functions/src/Integrations/firebase-slack.ts b/functions/src/Integrations/firebase-slack.ts index 1ea6c2d8e5..8c618a5a48 100644 --- a/functions/src/Integrations/firebase-slack.ts +++ b/functions/src/Integrations/firebase-slack.ts @@ -7,7 +7,6 @@ const SITE_URL = CONFIG.deployment.site_url // e.g. https://dev.onearmy.world or https://community.preciousplastic.com const SLACK_WEBHOOK_URL = CONFIG.integrations.slack_webhook -const DISCORD_WEBHOOK_URL = CONFIG.integrations.discord_webhook export const notifyNewPin = functions .runWith({ memory: '512MB' }) @@ -65,34 +64,3 @@ export const notifyNewHowTo = functions }, ) }) - -export const notifyAcceptedQuestion = functions - .runWith({ memory: '512MB' }) - .firestore.document('questions_rev20230926/{id}') - // currently, questions are immediately posted with no review. - // if that changes, this code will need to change. - .onCreate((snapshot) => { - const info = snapshot.data() - console.log(info) - - const username = info._createdBy - const title = info.title - const slug = info.slug - - request.post( - DISCORD_WEBHOOK_URL, - { - json: { - text: `❓ ${username} has a new question: ${title}\n Help them out and answer here: ${SITE_URL}/questions/${slug}`, - }, - }, - (error, response) => { - if (error) { - console.error(error) - return - } else { - return response - } - }, - ) - }) diff --git a/functions/src/Integrations/index.ts b/functions/src/Integrations/index.ts index a8d2bce4c8..6cb475ffa6 100644 --- a/functions/src/Integrations/index.ts +++ b/functions/src/Integrations/index.ts @@ -8,6 +8,6 @@ exports.notifyPinAccepted = IntegrationsDiscord.notifyPinAccepted exports.notifyNewHowTo = IntegrationsSlack.notifyNewHowTo exports.notifyHowToAccepted = IntegrationsDiscord.notifyHowToAccepted -exports.notifyAcceptedQuestion = IntegrationsSlack.notifyAcceptedQuestion +exports.notifyAcceptedQuestion = IntegrationsDiscord.notifyAcceptedQuestion exports.patreonAuth = IntegrationsPatreon.patreonAuth