From fa5dee8505ce9be414b4e7dc14c6c1ff8141936a Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Sat, 24 Oct 2020 15:50:14 +0200 Subject: [PATCH] Add Hackernews service provider --- src/serviceproviders.js | 2 ++ src/serviceproviders/hackernews.js | 53 ++++++++++++++++++++++++++++++ test/verify.test.js | 6 ++++ 3 files changed, 61 insertions(+) create mode 100644 src/serviceproviders/hackernews.js diff --git a/src/serviceproviders.js b/src/serviceproviders.js index 72fee94..c2c3e56 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -17,10 +17,12 @@ exports.serviceprovidersList = [ 'dns', 'xmpp', 'twitter', + 'hackernews', ] exports.serviceproviders = { dns: require('./serviceproviders/dns'), xmpp: require('./serviceproviders/xmpp'), twitter: require('./serviceproviders/twitter'), + hackernews: require('./serviceproviders/hackernews'), } diff --git a/src/serviceproviders/hackernews.js b/src/serviceproviders/hackernews.js new file mode 100644 index 0000000..129d0d7 --- /dev/null +++ b/src/serviceproviders/hackernews.js @@ -0,0 +1,53 @@ +/* +Copyright 2020 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 reURI = /^https:\/\/news.ycombinator.com\/user\?id=(.*)\/?/ + +const processURI = (uri, opts) => { + if (!opts) { opts = {} } + const match = uri.match(reURI) + + return { + type: "hackernews", + profile: { + display: match[1], + uri: uri + }, + proof: { + uri: `https://hacker-news.firebaseio.com/v0/user/${match[1]}.json`, + fetch: null + }, + qr: null + } +} + +const tests = [ + { + uri: 'https://news.ycombinator.com/user?id=Alice', + shouldMatch: true + }, + { + uri: 'https://news.ycombinator.com/user?id=Alice/', + shouldMatch: true + }, + { + uri: 'https://domain.org/user?id=Alice', + shouldMatch: false + } +] + +exports.reURI = reURI +exports.processURI = processURI +exports.tests = tests diff --git a/test/verify.test.js b/test/verify.test.js index 2126e0c..459a6a0 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -45,4 +45,10 @@ describe('verify', () => { expect(matches).to.be.length(1) expect(matches[0].type).to.be.equal('twitter') }) + it('should match "https://news.ycombinator.com/user?id=Alice" to the Hackernews service provider', () => { + const matches = doipjs.verify('https://news.ycombinator.com/user?id=Alice', null, {returnMatchesOnly: true}) + expect(matches).to.be.a('array') + expect(matches).to.be.length(1) + expect(matches[0].type).to.be.equal('hackernews') + }) })