From 4fe80bb150b1adf96635c33911efd2d2f17e1035 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Mon, 27 Apr 2020 08:39:57 -0500 Subject: [PATCH] Get data for [Discord] badges via OVH server proxies (#4956) --- config/custom-environment-variables.yml | 2 ++ config/default.yml | 2 ++ core/server/server.js | 3 +++ services/discord/discord.service.js | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/config/custom-environment-variables.yml b/config/custom-environment-variables.yml index f509c63566..45dba2c19b 100644 --- a/config/custom-environment-variables.yml +++ b/config/custom-environment-variables.yml @@ -64,6 +64,8 @@ public: fetchLimit: 'FETCH_LIMIT' + shieldsProductionHerokuHacks: 'SHIELDS_PRODUCTION_HEROKU_HACKS' + private: azure_devops_token: 'AZURE_DEVOPS_TOKEN' bintray_user: 'BINTRAY_USER' diff --git a/config/default.yml b/config/default.yml index e782ca0857..e27a3cbc6d 100644 --- a/config/default.yml +++ b/config/default.yml @@ -36,4 +36,6 @@ public: fetchLimit: '10MB' + shieldsProductionHerokuHacks: false + private: {} diff --git a/core/server/server.js b/core/server/server.js index 33fa141ca7..767322cb16 100644 --- a/core/server/server.js +++ b/core/server/server.js @@ -159,6 +159,7 @@ const publicConfigSchema = Joi.object({ rateLimit: Joi.boolean().required(), handleInternalErrors: Joi.boolean().required(), fetchLimit: Joi.string().regex(/^[0-9]+(b|kb|mb|gb|tb)$/i), + shieldsProductionHerokuHacks: Joi.boolean(), }).required() const privateConfigSchema = Joi.object({ @@ -401,6 +402,8 @@ class Server { rasterUrl: config.public.rasterUrl, private: config.private, public: config.public, + shieldsProductionHerokuHacks: + config.public.shieldsProductionHerokuHacks, } ) ) diff --git a/services/discord/discord.service.js b/services/discord/discord.service.js index e43d94fa86..8a3d418577 100644 --- a/services/discord/discord.service.js +++ b/services/discord/discord.service.js @@ -8,6 +8,11 @@ const discordSchema = Joi.object({ presence_count: nonNegativeInteger, }).required() +const proxySchema = Joi.object({ + message: Joi.string().required(), + color: Joi.string().required(), +}).required() + const documentation = `

The Discord badge requires the SERVER ID in order access the Discord JSON API. @@ -62,6 +67,11 @@ module.exports = class Discord extends BaseJsonService { } } + constructor(context, config) { + super(context, config) + this._shieldsProductionHerokuHacks = config.shieldsProductionHerokuHacks + } + async fetch({ serverId }) { const url = `https://discordapp.com/api/guilds/${serverId}/widget.json` return this._requestJson({ @@ -74,7 +84,19 @@ module.exports = class Discord extends BaseJsonService { }) } + async fetchOvhProxy({ serverId }) { + return this._requestJson({ + url: `https://legacy-img.shields.io/discord/${serverId}.json`, + schema: proxySchema, + }) + } + async handle({ serverId }) { + if (this._shieldsProductionHerokuHacks) { + const { message, color } = await this.fetchOvhProxy({ serverId }) + return { message, color } + } + const data = await this.fetch({ serverId }) return this.constructor.render({ members: data.presence_count }) }