Files
indiekit-server/scripts/patch-webmention-sender-reset-stale.mjs
Sven d501d70e1d fix(patches): restore livefetch patch so webmentions use live page HTML
The webmention sender was using stored post content (just the body text)
instead of the live page, missing template-rendered links like
u-in-reply-to, u-like-of, u-bookmark-of. This caused reply/like/bookmark
posts to be marked as sent with 0 webmentions. Bump reset-stale migration
to v4 so affected posts are retried.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 09:56:35 +01:00

69 lines
2.1 KiB
JavaScript

/**
* One-time migration: reset posts that were incorrectly marked as webmention-sent
* with empty results because the live page was not yet deployed when the poller fired.
*
* Runs at startup but only executes once (guarded by a migrations collection entry).
* After running, the patch-webmention-sender-retry.mjs code fix prevents recurrence.
*/
import { MongoClient } from "mongodb";
import config from "../indiekit.config.mjs";
const MIGRATION_ID = "webmention-sender-reset-stale-v4";
const mongodbUrl = config.application?.mongodbUrl;
if (!mongodbUrl) {
console.log("[patch] webmention-sender-reset-stale: no MongoDB URL, skipping");
process.exit(0);
}
const client = new MongoClient(mongodbUrl, { connectTimeoutMS: 5000 });
try {
await client.connect();
const db = client.db();
// Check if this migration has already run
const migrations = db.collection("migrations");
const alreadyRun = await migrations.findOne({ _id: MIGRATION_ID });
if (alreadyRun) {
console.log("[patch] webmention-sender-reset-stale: already run, skipping");
process.exit(0);
}
// Find posts marked as webmention-sent with all-zero results.
// These were silently marked by the bug (failed fetch → empty results).
const posts = db.collection("posts");
const result = await posts.updateMany(
{
"properties.webmention-sent": true,
"properties.webmention-results.sent": 0,
"properties.webmention-results.failed": 0,
"properties.webmention-results.skipped": 0,
},
{
$unset: {
"properties.webmention-sent": "",
"properties.webmention-results": "",
},
},
);
console.log(
`[patch] webmention-sender-reset-stale: reset ${result.modifiedCount} post(s) for retry`,
);
// Record that this migration has run
await migrations.insertOne({
_id: MIGRATION_ID,
ranAt: new Date().toISOString(),
modifiedCount: result.modifiedCount,
});
} catch (error) {
console.error(`[patch] webmention-sender-reset-stale: error — ${error.message}`);
// Non-fatal: don't block startup
} finally {
await client.close().catch(() => {});
}