From 689117ac982a1989d7522d6fe980a40c063b15d2 Mon Sep 17 00:00:00 2001 From: Bram Hagens Date: Thu, 8 Feb 2024 00:23:58 +0100 Subject: [PATCH 1/5] feat: add discord support --- src/serviceProviders/discord.js | 96 +++++++++++++++++++++++++++++++++ src/serviceProviders/index.js | 4 +- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/serviceProviders/discord.js diff --git a/src/serviceProviders/discord.js b/src/serviceProviders/discord.js new file mode 100644 index 0000000..c83ae04 --- /dev/null +++ b/src/serviceProviders/discord.js @@ -0,0 +1,96 @@ +/* +Copyright 2024 Bram Hagens + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +/** + * Discord service provider + * @module serviceProviders/discord + * @example + * import { ServiceProviderDefinitions } from 'doipjs'; + * const sp = ServiceProviderDefinitions.data.discord.processURI('https://discord.com/invite/AbCdEf'); + */ + +import * as E from '../enums.js' +import { ServiceProvider } from '../serviceProvider.js' + +export const reURI = /^https:\/\/discord\.com\/invite\/(.+)/ + +/** + * @function + * @param {string} uri - Claim URI to process + * @returns {ServiceProvider} The service provider information based on the claim URI + */ +export function processURI (uri) { + const match = uri.match(reURI) + + return new ServiceProvider({ + about: { + id: 'discord', + name: 'Discord', + homepage: 'https://discord.com' + }, + profile: { + display: null, + uri: null, + qr: null + }, + claim: { + uriRegularExpression: reURI.toString(), + uriIsAmbiguous: false + }, + proof: { + request: { + uri: `https://discord.com/api/v9/invites/${match[1]}`, + fetcher: E.Fetcher.HTTP, + accessRestriction: E.ProofAccessRestriction.NOCORS, + data: { + url: `https://discord.com/api/v9/invites/${match[1]}`, + format: E.ProofFormat.JSON + } + }, + response: { + format: E.ProofFormat.JSON + }, + target: [{ + format: E.ClaimFormat.FINGERPRINT, + encoding: E.EntityEncodingFormat.PLAIN, + relation: E.ClaimRelation.EQUALS, + path: ['guild', 'description'] + }] + } + }) +} + +export const functions = { + postprocess: async (claimData, proofData, opts) => { + claimData.profile.display = proofData.result.inviter.username + + return { claimData, proofData } + } +} + +export const tests = [ + { + uri: 'https://discord.com/invite/AbCdEf', + shouldMatch: true + }, + { + uri: 'https://discord.com/invite/AbCdEfGh', + shouldMatch: true + }, + { + uri: 'https://domain.com/invite/AbCdEf', + shouldMatch: false + } +] diff --git a/src/serviceProviders/index.js b/src/serviceProviders/index.js index 51b01df..986aa80 100644 --- a/src/serviceProviders/index.js +++ b/src/serviceProviders/index.js @@ -38,6 +38,7 @@ import * as stackexchange from './stackexchange.js' import * as keybase from './keybase.js' import * as opencollective from './opencollective.js' import * as orcid from './orcid.js' +import * as discord from './discord.js' const _data = { aspe, @@ -64,7 +65,8 @@ const _data = { stackexchange, keybase, opencollective, - orcid + orcid, + discord } export const list = Object.keys(_data) From 9b1a5d4d26a0cb651f19b5c1dad2629513bf8d69 Mon Sep 17 00:00:00 2001 From: Bram Hagens Date: Thu, 8 Feb 2024 10:11:52 +0100 Subject: [PATCH 2/5] bump discord API version to v10 --- src/serviceProviders/discord.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serviceProviders/discord.js b/src/serviceProviders/discord.js index c83ae04..b995af5 100644 --- a/src/serviceProviders/discord.js +++ b/src/serviceProviders/discord.js @@ -51,11 +51,11 @@ export function processURI (uri) { }, proof: { request: { - uri: `https://discord.com/api/v9/invites/${match[1]}`, + uri: `https://discord.com/api/v10/invites/${match[1]}`, fetcher: E.Fetcher.HTTP, accessRestriction: E.ProofAccessRestriction.NOCORS, data: { - url: `https://discord.com/api/v9/invites/${match[1]}`, + url: `https://discord.com/api/v10/invites/${match[1]}`, format: E.ProofFormat.JSON } }, From 6d464176dff964dab62ba02833814effecec4910 Mon Sep 17 00:00:00 2001 From: Bram Hagens Date: Thu, 8 Feb 2024 10:22:16 +0100 Subject: [PATCH 3/5] accept discord.gg urls --- src/serviceProviders/discord.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/serviceProviders/discord.js b/src/serviceProviders/discord.js index b995af5..f15f86c 100644 --- a/src/serviceProviders/discord.js +++ b/src/serviceProviders/discord.js @@ -24,7 +24,7 @@ limitations under the License. import * as E from '../enums.js' import { ServiceProvider } from '../serviceProvider.js' -export const reURI = /^https:\/\/discord\.com\/invite\/(.+)/ +export const reURI = /^https:\/\/(?:discord\.gg|discord\.com\/invite)\/(.+)/ /** * @function @@ -89,8 +89,28 @@ export const tests = [ uri: 'https://discord.com/invite/AbCdEfGh', shouldMatch: true }, + { + uri: 'https://discord.gg/AbCdEf', + shouldMatch: true + }, + { + uri: 'https://discord.gg/AbCdEfGh', + shouldMatch: true + }, { uri: 'https://domain.com/invite/AbCdEf', shouldMatch: false + }, + { + uri: 'https://domain.gg/AbCdEf', + shouldMatch: false + }, + { + uri: 'https://discord.com/invite/', + shouldMatch: false + } + { + uri: 'https://discord.gg/', + shouldMatch: false } ] From 041e22c52d7279ae350bae37b6eeaf5e3df5565f Mon Sep 17 00:00:00 2001 From: Bram Hagens Date: Thu, 8 Feb 2024 10:28:36 +0100 Subject: [PATCH 4/5] allow proof in guild name --- src/serviceProviders/discord.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/serviceProviders/discord.js b/src/serviceProviders/discord.js index f15f86c..0eec06a 100644 --- a/src/serviceProviders/discord.js +++ b/src/serviceProviders/discord.js @@ -62,12 +62,20 @@ export function processURI (uri) { response: { format: E.ProofFormat.JSON }, - target: [{ - format: E.ClaimFormat.FINGERPRINT, - encoding: E.EntityEncodingFormat.PLAIN, - relation: E.ClaimRelation.EQUALS, - path: ['guild', 'description'] - }] + target: [ + { + format: E.ClaimFormat.URI, + encoding: E.EntityEncodingFormat.PLAIN, + relation: E.ClaimRelation.CONTAINS, + path: ['guild', 'description'] + }, + { + format: E.ClaimFormat.URI, + encoding: E.EntityEncodingFormat.PLAIN, + relation: E.ClaimRelation.CONTAINS, + path: ['guild', 'name'] + } + ] } }) } @@ -108,7 +116,7 @@ export const tests = [ { uri: 'https://discord.com/invite/', shouldMatch: false - } + }, { uri: 'https://discord.gg/', shouldMatch: false From 758255f65284df9d8e8d3e8563d481321a2a967c Mon Sep 17 00:00:00 2001 From: Bram Hagens Date: Mon, 8 Apr 2024 00:19:32 +0200 Subject: [PATCH 5/5] add comments --- src/serviceProviders/discord.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/serviceProviders/discord.js b/src/serviceProviders/discord.js index 0eec06a..fbc32d9 100644 --- a/src/serviceProviders/discord.js +++ b/src/serviceProviders/discord.js @@ -49,6 +49,8 @@ export function processURI (uri) { uriRegularExpression: reURI.toString(), uriIsAmbiguous: false }, + // Get proof from invites (https://discord.com/developers/docs/resources/invite#get-invite) + // See https://discord.com/developers/docs/reference#api-versioning for Discord's API versioning proof: { request: { uri: `https://discord.com/api/v10/invites/${match[1]}`, @@ -82,6 +84,7 @@ export function processURI (uri) { export const functions = { postprocess: async (claimData, proofData, opts) => { + // Extract inviter's username from https://discord.com/developers/docs/resources/invite#invite-object claimData.profile.display = proofData.result.inviter.username return { claimData, proofData }