diff --git a/src/claimDefinitions/index.js b/src/claimDefinitions/index.js index 984400e..dcc7b81 100644 --- a/src/claimDefinitions/index.js +++ b/src/claimDefinitions/index.js @@ -32,7 +32,8 @@ const list = [ 'mastodon', 'pleroma', 'discourse', - 'owncast' + 'owncast', + 'stackexchange' ] const data = { @@ -54,7 +55,8 @@ const data = { mastodon: require('./mastodon'), pleroma: require('./pleroma'), discourse: require('./discourse'), - owncast: require('./owncast') + owncast: require('./owncast'), + stackexchange: require('./stackexchange') } exports.list = list diff --git a/src/claimDefinitions/stackexchange.js b/src/claimDefinitions/stackexchange.js new file mode 100644 index 0000000..949314d --- /dev/null +++ b/src/claimDefinitions/stackexchange.js @@ -0,0 +1,117 @@ +/* +Copyright 2022 Yarmo Mackenbach + +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. +*/ +const E = require('../enums') + +const reURI = /^https:\/\/(.*(?:askubuntu|mathoverflow|serverfault|stackapps|stackoverflow)|.+\.stackexchange)\.com\/users\/(\d+)/ +const reStackExchange = /\.stackexchange$/ + +const processURI = (uri) => { + const [, domain, id] = uri.match(reURI) + const site = domain.replace(reStackExchange, '') + + return { + serviceprovider: { + type: 'web', + name: 'stackexchange' + }, + match: { + regularExpression: reURI, + isAmbiguous: false + }, + profile: { + display: `${id}@${site}`, + uri: uri, + qr: null + }, + proof: { + uri: `https://${domain}.com/users/${id}?tab=profile`, + request: { + fetcher: E.Fetcher.HTTP, + access: E.ProofAccess.GENERIC, + format: E.ProofFormat.JSON, + data: { + url: `https://api.stackexchange.com/2.3/users/${id}?site=${site}&filter=!AH)b5JqVyImf`, + format: E.ProofFormat.JSON + } + } + }, + claim: { + format: E.ClaimFormat.MESSAGE, + relation: E.ClaimRelation.CONTAINS, + path: ['items', 'about_me'] + } + } +} + +const tests = [ + { + uri: 'https://stackoverflow.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://stackoverflow.com/users/1234/alice', + shouldMatch: true + }, + { + uri: 'https://stackoverflow.com/users/1234?tab=topactivity', + shouldMatch: true + }, + { + uri: 'https://stackoverflow.com/users/1234/alice?tab=profile', + shouldMatch: true + }, + { + uri: 'https://meta.stackoverflow.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://pt.stackoverflow.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://pt.meta.stackoverflow.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://serverfault.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://meta.stackexchange.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://gaming.meta.stackexchange.com/users/1234', + shouldMatch: true + }, + { + uri: 'https://stackexchange.com/users/1234', + shouldMatch: false + }, + { + uri: 'https://domain.com/users/1234', + shouldMatch: false + }, + { + uri: 'https://meta.domain.com/users/1234', + shouldMatch: false + } + +] + +exports.reURI = reURI +exports.processURI = processURI +exports.tests = tests