diff --git a/README.md b/README.md index 24ef9be..34bc3f3 100644 --- a/README.md +++ b/README.md @@ -160,3 +160,12 @@ In cooperation with https://github.com/wgroeneveld/serve-my-jams 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. 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. + +Parameters: just two: + +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 429b4d3..04c3d0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.11", + "version": "1.0.12", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" diff --git a/src/index.js b/src/index.js index 95c436c..5bd8a18 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,9 @@ const { parseMastoFeed } = require('./mastodon/feed-parser') const { widgetify } = require('./goodreads/widgetify.js') const { buildIndex } = require('./lunr/index-builder.js') const { howlong } = require('./howlongtobeat/howlong.js') + const { getWebmentions } = require('./webmention/get.js') +const { sendWebmentions } = require('./webmention/send.js') module.exports = { mastodon: { @@ -18,6 +20,7 @@ module.exports = { howlong: howlong }, webmention: { - getWebmentions: getWebmentions + getWebmentions: getWebmentions, + send: sendWebmentions } }; diff --git a/src/webmention/send.js b/src/webmention/send.js new file mode 100644 index 0000000..f994abb --- /dev/null +++ b/src/webmention/send.js @@ -0,0 +1,36 @@ + +const got = require('got') +const config = require('../config') +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') +} + +async function sendWebmentions(domain, configfile) { + const since = await getSince(configfile) + const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}?since=${since}` + + // this is an async call and will return 202 to say "started sending them out". + const result = await got.put(url) + await updateSince(configfile) +} + +module.exports = { + sendWebmentions +} diff --git a/test/__mocks__/got.js b/test/__mocks__/got.js index e39c26f..99ed3ba 100644 --- a/test/__mocks__/got.js +++ b/test/__mocks__/got.js @@ -14,4 +14,10 @@ async function got(url) { return result } +async function gotPutMock(url, opts) { + +} + +got.put = gotPutMock + module.exports = got diff --git a/test/webmention/send.test.js b/test/webmention/send.test.js new file mode 100644 index 0000000..c286784 --- /dev/null +++ b/test/webmention/send.test.js @@ -0,0 +1,52 @@ +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` + + beforeEach(() => { + MockDate.set(dayjs('2021-03-11T19:00:00').toDate()) + got.put = jest.fn() + + if(fs.existsSync(dumpdir)) { + rmdir(dumpdir) + } + 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()) + }) + +})