From b43eaa347c4122671469859eafe2b9273a68251e Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Fri, 16 Apr 2021 16:32:25 +0200 Subject: [PATCH] simplify webmention sending thanks to new server impl --- README.md | 8 +++---- package.json | 2 +- src/webmention/send.js | 25 ++------------------- test/webmention/send.test.js | 43 +++++------------------------------- 4 files changed, 12 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index ddb3eff..e2bc886 100644 --- a/README.md +++ b/README.md @@ -158,15 +158,13 @@ In cooperation with https://github.com/wgroeneveld/serve-my-jams #### 5.1 `getWebmentions` -Calls the get webmention endpoint, sorts by date, adds metadata such as relative date (`x days ago`, property `publishedFromNow`), and returns data. Could be written in a `data` folder for Hugo to parse, for example. +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`. #### 5.1 `send` -Calls the set webmention endpoint using a `PUT`. Based on the RSS feed located at `/index.xml`, see the [serve-my-jams](github.com/wgroeneveld/serve-my-jams) README. +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. -Parameters: just two: +Parameters: just one, the `domain`. -1. `domain` (see 5.1) -2. `configfile` location where an ISO-formatted datetime property is kept. diff --git a/package.json b/package.json index f4be790..7acfe7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.15", + "version": "1.0.16", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" diff --git a/src/webmention/send.js b/src/webmention/send.js index 0251f63..927a5b0 100644 --- a/src/webmention/send.js +++ b/src/webmention/send.js @@ -5,32 +5,11 @@ const fsp = require('fs').promises const dayjs = require('dayjs') -async function getSince(configfile) { - let since = '' - try { - const fileContent = await fsp.readFile(configfile, 'utf8') - since = JSON.parse(fileContent.toString()).since - } catch(err) { - // console.log(err) - // we assume the file doesn't exist. See https://nodejs.org/api/fs.html#fs_fs_access_path_mode_callback - } - return since -} - -async function updateSince(configfile) { - const since = new Date().toISOString() - await fsp.writeFile(configfile, JSON.stringify({ since }, null, 2), 'utf-8') - return since -} - -async function sendWebmentions(domain, configfile) { - const since = await getSince(configfile) - const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}?since=${since}` +async function sendWebmentions(domain) { + const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}` // this is an async call and will return 202 to say "started sending them out". const result = await got.put(url) - const updatedSince = await updateSince(configfile) - return updatedSince } module.exports = { diff --git a/test/webmention/send.test.js b/test/webmention/send.test.js index c286784..835bd55 100644 --- a/test/webmention/send.test.js +++ b/test/webmention/send.test.js @@ -1,52 +1,21 @@ -const MockDate = require('mockdate') -const dayjs = require('dayjs') - describe("webmention send serve-my-jam tests", () => { - const fs = require('fs'); - const fsp = require('fs').promises; - const { rmdir } = require('./../utils') - const got = require('got') - const { sendWebmentions } = require('./../../src/webmention/send') - const domain = "brainbaking.com" - const dumpdir = `${__dirname}/dump` + let calledPut = "" beforeEach(() => { - MockDate.set(dayjs('2021-03-11T19:00:00').toDate()) - got.put = jest.fn() - - if(fs.existsSync(dumpdir)) { - rmdir(dumpdir) + got.put = function(url) { + calledPut = url } - fs.mkdirSync(dumpdir) }); - test("sendWebmentions without a config creates a file with current date as since", async() => { - await sendWebmentions('brainbaking.com', `${dumpdir}/send.json`) - - const config = (await fsp.readFile(`${dumpdir}/send.json`)).toString() - const since = JSON.parse(config).since - - expect(got.put).toHaveBeenCalledWith("https://jam.brainbaking.com/webmention/brainbaking.com/miauwkes?since=") - expect(since).toBe(dayjs('2021-03-11T19:00:00').toDate().toISOString()) - }) - - test("sendWebmentions with a previous since sets that since as a query parameter", async() => { - const sinceSetup = dayjs('2020-01-01T20:00:00').toDate().toISOString() - await fsp.writeFile(`${dumpdir}/send.json`, JSON.stringify({ since: sinceSetup }), 'utf-8') - - await sendWebmentions('jefklakscodex.com', `${dumpdir}/send.json`) - - const config = (await fsp.readFile(`${dumpdir}/send.json`)).toString() - const since = JSON.parse(config).since - - expect(got.put).toHaveBeenCalledWith(`https://jam.brainbaking.com/webmention/jefklakscodex.com/miauwkes?since=${sinceSetup}`) - expect(since).toBe(dayjs('2021-03-11T19:00:00').toDate().toISOString()) + test("sendWebmentions", async() => { + await sendWebmentions('brainbaking.com') + expect(calledPut).toBe("https://jam.brainbaking.com/webmention/brainbaking.com/miauwkes") }) })