Merge pull request 'Add Stack Exchange service provider' (#23) from cherryblossom/doipjs:stack-exchange into main

Reviewed-on: https://codeberg.org/keyoxide/doipjs/pulls/23
This commit is contained in:
Yarmo Mackenbach 2022-08-16 21:34:11 +02:00
commit 24b06d5d4c
2 changed files with 121 additions and 2 deletions

View file

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

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