Add Stack Exchange service provider

This commit is contained in:
cherryblossom 2022-05-22 17:37:40 +10:00
parent a630693b08
commit ccad9d8894
No known key found for this signature in database
GPG key ID: 03B34648D6DEB639
2 changed files with 121 additions and 2 deletions

View file

@ -31,7 +31,8 @@ const list = [
'mastodon',
'pleroma',
'discourse',
'owncast'
'owncast',
'stackexchange'
]
const data = {
@ -52,7 +53,8 @@ const data = {
mastodon: require('./mastodon'),
pleroma: require('./pleroma'),
discourse: require('./discourse'),
owncast: require('./owncast')
owncast: require('./owncast'),
stackexchange: require('./stackexchange')
}
exports.list = list

View file

@ -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