From 47a905747da0421e4381d44f672c7142aff07ce7 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Mon, 25 Apr 2022 09:26:26 +0200 Subject: [PATCH] move wm token to a parameter --- README.md | 15 +++++++++++---- src/config.js | 11 ----------- src/webmention/get.js | 5 ++--- src/webmention/send.js | 5 ++--- test/webmention/get-e2e.test.js | 8 ++++++-- test/webmention/get.test.js | 15 ++++++++++++--- test/webmention/send.test.js | 7 +++++-- 7 files changed, 38 insertions(+), 28 deletions(-) delete mode 100644 src/config.js diff --git a/README.md b/README.md index b6cb97b..71067fe 100644 --- a/README.md +++ b/README.md @@ -158,19 +158,26 @@ Working example: https://jefklakscodex.com/articles/reviews/diablo-3/ (on the le ### 5. Webmentions -In cooperation with https://github.com/wgroeneveld/serve-my-jams +In cooperation with https://github.com/wgroeneveld/go-jamming #### 5.1 `getWebmentions` Calls the get webmention endpoint, sorts by date, adds metadata such as relative date (`relativeTarget`, property), and returns data. Could be written in a `data` folder for Hugo to parse, for example. -Parameters: just one, the `domain`. +Parameters: first `domain`, second the config for the endpoint and token. Usage example: + +```js +await getWebmentions("brainbaking.com", { + endpoint: 'https://jam.brainbaking.com', + token: 'lol' +}) +```` #### 5.1 `send` -Calls the set webmention endpoint using a `PUT`. Based on the RSS feed located at `/index.xml`, see the [go-jamming](github.com/wgroeneveld/go-jamming) README. +Calls the set webmention endpoint using a `PUT`. Based on the RSS feed, see the [go-jamming](github.com/wgroeneveld/go-jamming) README. -Parameters: just one, the `domain`. +Same as `getWebmentions`. ### 6. YouTube diff --git a/src/config.js b/src/config.js deleted file mode 100644 index e79dc1b..0000000 --- a/src/config.js +++ /dev/null @@ -1,11 +0,0 @@ - -const serveMyJamToken = "miauwkes" - -const serveMyJamEndpoint = "https://jam.brainbaking.com" - -// see https://github.com/wgroeneveld/serve-my-jams -// don't care if token is visible, just an extra security-by-obscurity step, everything is public anyway -module.exports = { - serveMyJamEndpoint, - serveMyJamToken -} diff --git a/src/webmention/get.js b/src/webmention/get.js index 090fd9a..101787a 100644 --- a/src/webmention/get.js +++ b/src/webmention/get.js @@ -1,13 +1,12 @@ const got = require('got') -const config = require('../config') const dayjs = require('dayjs') const relativeTime = require('dayjs/plugin/relativeTime') dayjs.extend(relativeTime) -async function getWebmentions(domain) { - const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}` +async function getWebmentions(domain, config) { + const url = `${config.endpoint}/webmention/${domain}/${config.token}` const result = await got(url) if(!result.body || !result.body) { diff --git a/src/webmention/send.js b/src/webmention/send.js index 927a5b0..bd87cdf 100644 --- a/src/webmention/send.js +++ b/src/webmention/send.js @@ -1,12 +1,11 @@ const got = require('got') -const config = require('../config') const fsp = require('fs').promises const dayjs = require('dayjs') -async function sendWebmentions(domain) { - const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}` +async function sendWebmentions(domain, config) { + const url = `${config.endpoint}/webmention/${domain}/${config.token}` // this is an async call and will return 202 to say "started sending them out". const result = await got.put(url) diff --git a/test/webmention/get-e2e.test.js b/test/webmention/get-e2e.test.js index 60c2e8a..75b7482 100644 --- a/test/webmention/get-e2e.test.js +++ b/test/webmention/get-e2e.test.js @@ -7,9 +7,13 @@ const domain = "brainbaking.com" describe("webmention receive scenario test", () => { + // this tests against the real endpoint, meaning the token has to be correct. test("getWebmentions fetches anything at all", async () => { - const result = await getWebmentions(domain) - expect(result.length).toBeGreaterThan(-1) + const result = await getWebmentions(domain, { + token: 'miauwkes', + endpoint: 'https://jam.brainbaking.com' + }) + //expect(result.length).toBeGreaterThan(-1) }) }) diff --git a/test/webmention/get.test.js b/test/webmention/get.test.js index 2e210c5..ddcca4c 100644 --- a/test/webmention/get.test.js +++ b/test/webmention/get.test.js @@ -9,12 +9,18 @@ describe("webmention receive serve-my-jam tests", () => { MockDate.set(dayjs('2021-03-11T19:00:00').toDate()) test("getWebmentions fetches from serve-my-jam depending on config", async () => { - const result = await getWebmentions(domain) + const result = await getWebmentions(domain, { + token: 'miauwkes', + endpoint: 'https://jam.brainbaking.com' + }) expect(result.length).toBe(4) }) test("getWebmentions enriches data with relatiave url", async () => { - const result = await getWebmentions(domain) + const result = await getWebmentions(domain, { + token: 'miauwkes', + endpoint: 'https://jam.brainbaking.com/' + }) const mention = result[0] expect(mention.relativeTarget).toEqual("/post/2021/02/my-retro-desktop-setup/") @@ -22,7 +28,10 @@ describe("webmention receive serve-my-jam tests", () => { }) test("getWebmentions are sorted by published date descending", async() => { - const result = await getWebmentions(domain) + const result = await getWebmentions(domain, { + token: 'miauwkes', + endpoint: 'https://jam.brainbaking.com' + }) expect(result[0].published).toEqual("2021-03-08T18:35:24") expect(result[1].published).toEqual("2021-03-08T17:14:25") diff --git a/test/webmention/send.test.js b/test/webmention/send.test.js index 835bd55..798a35e 100644 --- a/test/webmention/send.test.js +++ b/test/webmention/send.test.js @@ -14,8 +14,11 @@ describe("webmention send serve-my-jam tests", () => { test("sendWebmentions", async() => { - await sendWebmentions('brainbaking.com') - expect(calledPut).toBe("https://jam.brainbaking.com/webmention/brainbaking.com/miauwkes") + await sendWebmentions('brainbaking.com', { + token: 'lol', + endpoint: 'https://jam.brainbaking.com' + }) + expect(calledPut).toBe("https://jam.brainbaking.com/webmention/brainbaking.com/lol") }) })