fix webmention get as a string

This commit is contained in:
Wouter Groeneveld 2021-03-12 19:03:56 +01:00
parent 90773a2521
commit 4d45134e1f
4 changed files with 25 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{
"name": "jam-my-stack",
"version": "1.0.6",
"version": "1.0.7",
"repository": {
"url": "https://github.com/wgroeneveld/jam-my-stack",
"type": "git"

View File

@ -10,12 +10,17 @@ async function getWebmentions(domain) {
const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}`
const result = await got(url)
result.body.json.forEach(mention => {
if(!result.body || !result.body) {
return []
}
const json = JSON.parse(result.body).json
json.forEach(mention => {
mention.publishedFromNow = dayjs(mention.published).fromNow()
mention.relativeTarget = mention.target.substring(mention.target.indexOf(domain) + domain.length, mention.target.length)
})
return result.body.json.sort((a, b) => dayjs(b.published).toDate() - dayjs(a.published).toDate())
return json.sort((a, b) => dayjs(b.published).toDate() - dayjs(a.published).toDate())
}
module.exports = {

View File

@ -5,7 +5,8 @@ async function got(url) {
if(url.indexOf('/webmention') >= 0) {
const result = await fs.readFile(`./test/__mocks__/get-sample.json`, 'utf8');
return {
body: JSON.parse(result)
// WHY not a JSON.parse here? The body is a STRING IRL!
body: result
}
}

View File

@ -0,0 +1,15 @@
jest.disableAutomock()
jest.unmock('got')
const { getWebmentions } = require('./../../src/webmention/get')
const domain = "brainbaking.com"
describe("webmention receive scenario test", () => {
test("getWebmentions fetches anything at all", async () => {
const result = await getWebmentions(domain)
expect(result.length).toBeGreaterThan(-1)
})
})