Skip to content

Commit

Permalink
feat(endpoint-webmention): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Nov 10, 2023
1 parent be3a25f commit 22653de
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 0 deletions.
1 change: 1 addition & 0 deletions indiekit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const config = {
plugins: [
"@indiekit-test/frontend",
"@indiekit/endpoint-json-feed",
"@indiekit/endpoint-webmention",
"@indiekit/preset-jekyll",
"@indiekit/store-github",
"@indiekit/syndicator-internet-archive",
Expand Down
206 changes: 206 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/endpoint-webmention/assets/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions packages/endpoint-webmention/index.js
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;
}
}
23 changes: 23 additions & 0 deletions packages/endpoint-webmention/lib/webmention-handler.js
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;
};
Loading

0 comments on commit 22653de

Please sign in to comment.