-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be3a25f
commit 22653de
Showing
7 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import express from "express"; | ||
import { webmentionHandler } from "./lib/webmention-handler.js"; | ||
|
||
const defaults = { mountPath: "/webmention" }; | ||
const router = express.Router(); // eslint-disable-line new-cap | ||
|
||
export default class MicropubEndpoint { | ||
constructor(options = {}) { | ||
this.id = "endpoint-webmention"; | ||
this.meta = import.meta; | ||
this.name = "Webmention endpoint"; | ||
this.options = { ...defaults, ...options }; | ||
this.mountPath = this.options.mountPath; | ||
} | ||
|
||
_routes(indiekitConfig) { | ||
const { me } = indiekitConfig.publication; | ||
const { mongodbUrl } = indiekitConfig.application; | ||
|
||
router.post("/", async function (request, response) { | ||
const hander = webmentionHandler(me, mongodbUrl); | ||
const { source, target } = request.body; | ||
|
||
try { | ||
const mentionResponse = await hander.addPendingMention(source, target); | ||
|
||
response.status(mentionResponse.code).send("Accepted"); | ||
} catch (error) { | ||
response.status(400).send(error.message); | ||
} | ||
}); | ||
|
||
router.post("/", async function (request, response) { | ||
const hander = webmentionHandler(me, mongodbUrl); | ||
const { source, target } = request.body; | ||
console.error("source", source); | ||
|
||
try { | ||
const mentionResponse = await hander.addPendingMention(source, target); | ||
|
||
response.status(mentionResponse.code).send("Accepted"); | ||
} catch (error) { | ||
console.error(error); | ||
response.status(400).send(error.message); | ||
} | ||
}); | ||
|
||
router.get("/process", async function (request, response) { | ||
const hander = webmentionHandler(me, mongodbUrl); | ||
|
||
try { | ||
setInterval(() => hander.processPendingMentions(), 0); | ||
|
||
response.status(200).send("Done"); | ||
} catch (error) { | ||
response.status(500).send(error.message); | ||
} | ||
}); | ||
|
||
return router; | ||
} | ||
|
||
init(Indiekit) { | ||
Indiekit.addEndpoint(this); | ||
|
||
// Use private value to register Webmention endpoint path | ||
Indiekit.config.application._webmentionEndpointPath = | ||
this.options.mountPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { WebMentionHandler } from "webmention-handler"; | ||
import { MongoWebMentionStorage } from "webmention-handler-mongodb"; | ||
|
||
export const webmentionHandler = (me, mongodbUrl) => { | ||
const { host } = new URL(me); | ||
|
||
const storage = new MongoWebMentionStorage({ | ||
databaseUri: mongodbUrl, | ||
dbName: "indiekit", | ||
mentionCollection: "mentions", | ||
pendingCollection: "mentions-pending", | ||
maxPendingFetch: 100, | ||
limitMentionsPerPageFetch: 50, | ||
}); | ||
|
||
const handler = new WebMentionHandler({ | ||
supportedHosts: [host], | ||
storageHandler: storage, | ||
// requiredProtocol: "https", | ||
}); | ||
|
||
return handler; | ||
}; |
Oops, something went wrong.