diff --git a/src/sentry-api/endpoints/delete.js b/src/sentry-api/endpoints/delete.js new file mode 100644 index 0000000..0a8acf2 --- /dev/null +++ b/src/sentry-api/endpoints/delete.js @@ -0,0 +1,9 @@ +const schema = require("../../models/sentrySchema"); + +module.exports = async (req, res) => { + if(!await schema.exists({ _id: req.params.secret })) return res.status(400).json({ "message": "Invalid capture ID.", "code": "INVALID_ID" }); + + await schema.findOneAndDelete({ _id: req.params.secret }); + + res.status(200).json({ "success": true }); +} diff --git a/src/sentry-api/endpoints/post.js b/src/sentry-api/endpoints/post.js index 02d809a..d44bec0 100644 --- a/src/sentry-api/endpoints/post.js +++ b/src/sentry-api/endpoints/post.js @@ -1,8 +1,7 @@ const Discord = require("discord.js"); -const cap = require("../../util/cap"); -const getColor = require("../../util/sentry/color"); -const parser = require("../../util/sentry/parser"); +const cap = require("../util/cap"); +const parser = require("../util/parser"); const schema = require("../../models/sentrySchema"); @@ -14,27 +13,21 @@ module.exports = async (req, res, client) => { const event = req.body; const embed = new Discord.EmbedBuilder() - .setAuthor({ name: event.project_name, iconURL: "attachment://sentry-glyph-light-400x367.png", url: parser.getProjectLink(event) }) - - const logo = new Discord.AttachmentBuilder("src/assets/sentry-api/sentry-glyph-light-400x367.png", { name: "sentry-glyph-light-400x367.png" }); + .setColor(parser.getColor(parser.getLevel(event))) + .setAuthor({ name: event.project_name, iconURL: "https://gc-sentry-api.wdh.gg/assets/sentry-glyph-light-400x367.png", url: parser.getProjectLink(event) }) + .setTimestamp(parser.getTime(event)) const projectName = parser.getProject(event); const eventTitle = parser.getTitle(event); - if(projectName) { - const embedTitle = `[${projectName}] ${eventTitle}`; - embed.setTitle(cap(embedTitle, 250)); - } else { - embed.setTitle(cap(eventTitle, 250)); - } + embed.setTitle(cap(eventTitle, 250)); + + if(projectName) embed.setTitle(cap(`[${projectName}] ${eventTitle}`, 250)); const link = parser.getLink(event); if(link.startsWith("https://") || link.startsWith("http://")) embed.setURL(parser.getLink(event)); - embed.setTimestamp(parser.getTime(event)); - embed.setColor(getColor(parser.getLevel(event))); - const fileLocation = parser.getFileLocation(event); const snippet = cap(parser.getErrorCodeSnippet(event), 3900); @@ -52,7 +45,7 @@ module.exports = async (req, res, client) => { fields.push({ name: "Stack", value: `\`\`\`${cap(location.join("\n"), 1000)}\n\`\`\`` - }); + }) } const user = parser.getUser(event); @@ -62,7 +55,7 @@ module.exports = async (req, res, client) => { name: "User", value: cap(`${user.username} ${user.id ? `(${user.id})` : ""}`, 1024), inline: true - }); + }) } const tags = parser.getTags(event); @@ -72,7 +65,7 @@ module.exports = async (req, res, client) => { name: "Tags", value: cap(tags.map(([key, value]) => `**${key}**: ${value}`).join("\n"), 1024), inline: true - }); + }) } const extras = parser.getExtras(event); @@ -82,7 +75,7 @@ module.exports = async (req, res, client) => { name: "Extras", value: cap(extras.join("\n"), 1024), inline: true - }); + }) } const contexts = parser.getContexts(event); @@ -92,7 +85,7 @@ module.exports = async (req, res, client) => { name: "Contexts", value: cap(contexts.join("\n"), 1024), inline: true - }); + }) } const release = parser.getRelease(event); @@ -102,7 +95,7 @@ module.exports = async (req, res, client) => { name: "Release", value: cap(release, 1024), inline: true - }); + }) } embed.addFields(fields); @@ -127,7 +120,11 @@ module.exports = async (req, res, client) => { const channel = client.channels.cache.get(data.channel); - channel.send({ embeds: [embed], components: [actions], files: [logo] }); - - res.status(200).json({ "message": "The event has been received.", "code": "EVENT_RECEIVED" }); + try { + await channel.send({ embeds: [embed], components: [actions] }); + res.status(200).json({ "success": true }); + } catch(err) { + client.sentry.captureException(err); + res.status(500).json({ "success": false }); + } } diff --git a/src/sentry-api/index.js b/src/sentry-api/index.js index 1231899..a87b6be 100644 --- a/src/sentry-api/index.js +++ b/src/sentry-api/index.js @@ -27,7 +27,14 @@ module.exports = async (client) => { app.use(bodyParser.urlencoded({ extended: true })); app.use(express.json()); - const routes = require("./util/routes"); + // Host public files + app.use(express.static(__dirname + "/public")); + + const routes = require("./routes"); + + app.delete("/:secret", async (req, res) => { + routes.delete(req, res); + }) app.get("/:secret", async (req, res) => { routes.get(req, res); diff --git a/src/assets/sentry-api/sentry-glyph-light-400x367.png b/src/sentry-api/public/assets/sentry-glyph-light-400x367.png similarity index 100% rename from src/assets/sentry-api/sentry-glyph-light-400x367.png rename to src/sentry-api/public/assets/sentry-glyph-light-400x367.png diff --git a/src/sentry-api/routes.js b/src/sentry-api/routes.js new file mode 100644 index 0000000..9f7ccd9 --- /dev/null +++ b/src/sentry-api/routes.js @@ -0,0 +1,5 @@ +module.exports = { + delete: require("./endpoints/delete"), + get: require("./endpoints/get"), + post: require("./endpoints/post") +} diff --git a/src/util/cap.js b/src/sentry-api/util/cap.js similarity index 100% rename from src/util/cap.js rename to src/sentry-api/util/cap.js diff --git a/src/util/sentry/parser.js b/src/sentry-api/util/parser.js similarity index 89% rename from src/util/sentry/parser.js rename to src/sentry-api/util/parser.js index 35a2f18..5321cee 100644 --- a/src/util/sentry/parser.js +++ b/src/sentry-api/util/parser.js @@ -1,5 +1,21 @@ // https://github.com/IanMitchell/sentrydiscord.dev/blob/867b889e15c6b101d619610d07b4663c6a73fe6a/lib/message.ts +module.exports.getColor = function (level) { + switch (level) { + case "debug": + return parseInt("fbe14f", 16); + case "info": + return parseInt("2788ce", 16); + case "warning": + return parseInt("f18500", 16); + case "fatal": + return parseInt("d20f2a", 16); + case "error": + default: + return parseInt("e03e2f", 16); + } +} + module.exports.getContexts = function (issue) { const contexts = this.getEvent(issue)?.contexts ?? {}; diff --git a/src/sentry-api/util/routes.js b/src/sentry-api/util/routes.js deleted file mode 100644 index d38cbd6..0000000 --- a/src/sentry-api/util/routes.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - get: require("../endpoints/get"), - post: require("../endpoints/post") -} diff --git a/src/util/sentry/color.js b/src/util/sentry/color.js deleted file mode 100644 index 6f2bbf3..0000000 --- a/src/util/sentry/color.js +++ /dev/null @@ -1,15 +0,0 @@ -module.exports = function (level) { - switch (level) { - case "debug": - return parseInt("fbe14f", 16); - case "info": - return parseInt("2788ce", 16); - case "warning": - return parseInt("f18500", 16); - case "fatal": - return parseInt("d20f2a", 16); - case "error": - default: - return parseInt("e03e2f", 16); - } -}