Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get data for [Discord] badges via OVH server proxies #4956

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/custom-environment-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public:

fetchLimit: 'FETCH_LIMIT'

shieldsProductionHerokuHacks: 'SHIELDS_PRODUCTION_HEROKU_HACKS'

private:
azure_devops_token: 'AZURE_DEVOPS_TOKEN'
bintray_user: 'BINTRAY_USER'
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ public:

fetchLimit: '10MB'

shieldsProductionHerokuHacks: false

private: {}
3 changes: 3 additions & 0 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -401,6 +402,8 @@ class Server {
rasterUrl: config.public.rasterUrl,
private: config.private,
public: config.public,
shieldsProductionHerokuHacks:
config.public.shieldsProductionHerokuHacks,
}
)
)
Expand Down
22 changes: 22 additions & 0 deletions services/discord/discord.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<p>
The Discord badge requires the <code>SERVER ID</code> in order access the Discord JSON API.
Expand Down Expand Up @@ -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({
Expand All @@ -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 })
}
Expand Down