simplify webmention sending thanks to new server impl
parent
7bdd377b43
commit
b43eaa347c
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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")
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue