forked from wgroeneveld/go-jamming
use optional objects instead of if()
This commit is contained in:
parent
62e49c5c15
commit
1f794e5863
|
@ -39,17 +39,14 @@ async function discover(target) {
|
||||||
})
|
})
|
||||||
const webmention = format.rels?.webmention?.[0]
|
const webmention = format.rels?.webmention?.[0]
|
||||||
const pingback = format.rels?.pingback?.[0]
|
const pingback = format.rels?.pingback?.[0]
|
||||||
if(!webmention && !pingback) {
|
|
||||||
throw "no webmention and no pingback found?"
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
link: webmention ? webmention : pingback,
|
link: webmention ? webmention : (pingback ? pingback : ""),
|
||||||
type: webmention ? "webmention" : "pingback"
|
type: webmention ? "webmention" : (pingback ? "pingback" : "unknown")
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.warn(` -- whoops, failed to discover ${target}, why: ${err}`)
|
console.warn(` -- whoops, failed to discover ${target}, why: ${err}`)
|
||||||
return undefined
|
return { type: "unknown" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,10 +22,10 @@ async function sendWebmentionToEndpoint(endpoint, source, target) {
|
||||||
async function mention(opts) {
|
async function mention(opts) {
|
||||||
const { source, target } = opts
|
const { source, target } = opts
|
||||||
const endpoint = await discover(target)
|
const endpoint = await discover(target)
|
||||||
if(!endpoint) return
|
|
||||||
const sendMention = {
|
const sendMention = {
|
||||||
"webmention": sendWebmentionToEndpoint,
|
"webmention": sendWebmentionToEndpoint,
|
||||||
"pingback": sendPingbackToEndpoint
|
"pingback": sendPingbackToEndpoint,
|
||||||
|
"unknown": async function() {}
|
||||||
}
|
}
|
||||||
await sendMention[endpoint.type](endpoint.link, source, target)
|
await sendMention[endpoint.type](endpoint.link, source, target)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
<p>this one is a pingback-only <a href="https://brainbaking.com/pingback-discover-test-single.html">single</a> one. Not good!</p>
|
<p>this one is a pingback-only <a href="https://brainbaking.com/pingback-discover-test-single.html">single</a> one. Not good!</p>
|
||||||
|
|
||||||
<p>another cool link: <a href="https://brainbaking.com/link-discover-test-multiple.html">multiple</a></p>
|
<p>another cool link: <a href="https://brainbaking.com/link-discover-test-multiple.html">multiple</a></p>
|
||||||
|
|
||||||
|
<p>last but not least: <a href="https://brainbaking.com/link-discover-test-none.html">Nothin!</a> What a shame.</p>
|
||||||
|
|
||||||
]]>
|
]]>
|
||||||
</description>
|
</description>
|
||||||
|
|
|
@ -3,9 +3,11 @@ const { discover } = require('../src/linkdiscoverer')
|
||||||
|
|
||||||
describe("link discoverer", () => {
|
describe("link discoverer", () => {
|
||||||
|
|
||||||
test("discover nothing if no link is present", async() => {
|
test("discover 'unknown' if no link is present", async() => {
|
||||||
const result = await discover("https://brainbaking.com/link-discover-test-none.html")
|
const result = await discover("https://brainbaking.com/link-discover-test-none.html")
|
||||||
expect(result).toBeUndefined()
|
expect(result).toEqual({
|
||||||
|
type: "unknown"
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test("prefer webmentions over pingbacks if both links are present", async () => {
|
test("prefer webmentions over pingbacks if both links are present", async () => {
|
||||||
|
|
|
@ -6,13 +6,17 @@ const { send } = require('../../src/webmention/send')
|
||||||
|
|
||||||
describe("webmention send scenarios", () => {
|
describe("webmention send scenarios", () => {
|
||||||
test("webmention send integration test that can send both webmentions and pingbacks", async () => {
|
test("webmention send integration test that can send both webmentions and pingbacks", async () => {
|
||||||
got.post = jest.fn()
|
// jest.fn() gives unpredictable and unreadable output if unorderd calledWith... DIY!
|
||||||
|
let posts = {}
|
||||||
|
got.post = function(url, opts) {
|
||||||
|
posts[url] = opts
|
||||||
|
}
|
||||||
|
|
||||||
// fetches index.xml
|
// fetches index.xml
|
||||||
await send("brainbaking.com", '2021-03-16T16:00:00.000Z')
|
await send("brainbaking.com", '2021-03-16T16:00:00.000Z')
|
||||||
|
|
||||||
expect(got.post).toHaveBeenCalledTimes(3)
|
expect(Object.keys(posts).length).toBe(3)
|
||||||
expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/webmention-endpoint-header", {
|
expect(posts["http://aaronpk.example/webmention-endpoint-header"]).toEqual({
|
||||||
contentType: "x-www-form-urlencoded",
|
contentType: "x-www-form-urlencoded",
|
||||||
form: {
|
form: {
|
||||||
source: "https://brainbaking.com/notes/2021/03/16h17m07s14/",
|
source: "https://brainbaking.com/notes/2021/03/16h17m07s14/",
|
||||||
|
@ -23,7 +27,7 @@ describe("webmention send scenarios", () => {
|
||||||
methods: ["POST"]
|
methods: ["POST"]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/pingback-endpoint-body", {
|
expect(posts["http://aaronpk.example/pingback-endpoint-body"]).toEqual({
|
||||||
contentType: "text/xml",
|
contentType: "text/xml",
|
||||||
body: `<?xml version="1.0" encoding="UTF-8"?>
|
body: `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<methodCall>
|
<methodCall>
|
||||||
|
@ -42,7 +46,7 @@ describe("webmention send scenarios", () => {
|
||||||
methods: ["POST"]
|
methods: ["POST"]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/webmention-endpoint-body", {
|
expect(posts["http://aaronpk.example/webmention-endpoint-body"]).toEqual({
|
||||||
contentType: "x-www-form-urlencoded",
|
contentType: "x-www-form-urlencoded",
|
||||||
form: {
|
form: {
|
||||||
source: "https://brainbaking.com/notes/2021/03/16h17m07s14/",
|
source: "https://brainbaking.com/notes/2021/03/16h17m07s14/",
|
||||||
|
|
Loading…
Reference in New Issue