webmention sending endpoint client

This commit is contained in:
Wouter Groeneveld 2021-03-17 22:15:43 +01:00
parent 7f4773c276
commit 7b10426429
6 changed files with 108 additions and 2 deletions

View File

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

View File

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

View File

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

36
src/webmention/send.js Normal file
View File

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

View File

@ -14,4 +14,10 @@ async function got(url) {
return result
}
async function gotPutMock(url, opts) {
}
got.put = gotPutMock
module.exports = got

View File

@ -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())
})
})