From 4fa07de2997b29b0519873885fa387ebc6257550 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 20 Aug 2020 22:25:41 -0700 Subject: [PATCH] support logs for manual color changes it writes to our discord server, owo! --- src/server/index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++- src/server/util.js | 27 +++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/server/index.js b/src/server/index.js index 809869e..e368d57 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -10,6 +10,7 @@ const { getPoseFromPetData, getEmotion, getGenderPresentation, + logToDiscord, } = require("./util"); const typeDefs = gql` @@ -616,12 +617,14 @@ const resolvers = { setManualSpecialColor: async ( _, { itemId, colorId, supportSecret }, - { db } + { itemLoader, itemTranslationLoader, colorTranslationLoader, db } ) => { if (supportSecret !== process.env["SUPPORT_SECRET"]) { throw new Error(`Support secret is incorrect. Try setting up again?`); } + const item = await itemLoader.load(itemId); + const [ result, ] = await db.execute( @@ -635,6 +638,55 @@ const resolvers = { ); } + if (process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"]) { + try { + const [ + itemTranslation, + oldColorTranslation, + newColorTranslation, + ] = await Promise.all([ + itemTranslationLoader.load(itemId), + item.manualSpecialColorId + ? colorTranslationLoader.load(item.manualSpecialColorId) + : Promise.resolve(null), + colorId + ? colorTranslationLoader.load(colorId) + : Promise.resolve(null), + ]); + + const oldColorName = oldColorTranslation + ? capitalize(oldColorTranslation.name) + : "Auto-detect"; + const newColorName = newColorTranslation + ? capitalize(newColorTranslation.name) + : "Auto-detect"; + await logToDiscord({ + embeds: [ + { + title: `🛠 ${itemTranslation.name}`, + thumbnail: { + url: item.thumbnailUrl, + height: 80, + width: 80, + }, + fields: [ + { + name: "Special color", + value: `${oldColorName} → **${newColorName}**`, + }, + ], + timestamp: new Date().toISOString(), + url: `https://impress.openneo.net/items/${item.id}`, + }, + ], + }); + } catch (e) { + console.error("Error sending Discord support log", e); + } + } else { + console.warn("No Discord support webhook provided, skipping"); + } + return { id: itemId }; }, diff --git a/src/server/util.js b/src/server/util.js index 0bae7d8..e4740a7 100644 --- a/src/server/util.js +++ b/src/server/util.js @@ -1,3 +1,6 @@ +const beeline = require("honeycomb-beeline"); +const fetch = require("node-fetch"); + function capitalize(str) { return str[0].toUpperCase() + str.slice(1); } @@ -82,6 +85,29 @@ function getPoseFromPetData(petMetaData, petCustomData) { } } +async function logToDiscord(body) { + const span = beeline.startSpan({ name: "logToDiscord" }); + + try { + const res = await fetch(process.env["SUPPORT_TOOLS_DISCORD_WEBHOOK_URL"], { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }); + + if (!res.ok) { + const resText = await res.text(); + throw new Error( + `Discord returned ${res.status} ${res.statusText}: ` + `${resText}` + ); + } + } finally { + beeline.finishSpan(span); + } +} + function normalizeRow(row) { const normalizedRow = {}; for (let [key, value] of Object.entries(row)) { @@ -100,5 +126,6 @@ module.exports = { getGenderPresentation, getPoseFromPetState, getPoseFromPetData, + logToDiscord, normalizeRow, };