Files
indiekit-endpoint-microsub/lib/controllers/block.js
Ricardo 02bfff7ce9 fix: store dates as ISO strings instead of Date objects
Prevents dateString.split crash when Nunjucks | date filter receives
Date objects from MongoDB. Audit timestamps (createdAt, updatedAt,
lastFetchedAt, etc.) now use .toISOString(). Query-used fields
(published, nextFetchAt) kept as Date objects for MongoDB compatibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 22:30:40 +01:00

87 lines
2.1 KiB
JavaScript

/**
* Block controller
* @module controllers/block
*/
import { deleteItemsByAuthorUrl } from "../storage/items.js";
import { getUserId } from "../utils/auth.js";
import { validateUrl } from "../utils/validation.js";
/**
* Get blocked collection
* @param {object} application - Indiekit application
* @returns {object} MongoDB collection
*/
function getCollection(application) {
return application.collections.get("microsub_blocked");
}
/**
* List blocked URLs
* GET ?action=block
* @param {object} request - Express request
* @param {object} response - Express response
*/
export async function list(request, response) {
const { application } = request.app.locals;
const userId = getUserId(request);
const collection = getCollection(application);
const blocked = await collection.find({ userId }).toArray();
const items = blocked.map((b) => ({ url: b.url }));
response.json({ items });
}
/**
* Block a URL
* POST ?action=block
* @param {object} request - Express request
* @param {object} response - Express response
*/
export async function block(request, response) {
const { application } = request.app.locals;
const userId = getUserId(request);
const { url } = request.body;
validateUrl(url);
const collection = getCollection(application);
// Check if already blocked
const existing = await collection.findOne({ userId, url });
if (!existing) {
await collection.insertOne({
userId,
url,
createdAt: new Date().toISOString(),
});
}
// Remove past items from blocked URL
await deleteItemsByAuthorUrl(application, userId, url);
response.json({ result: "ok" });
}
/**
* Unblock a URL
* POST ?action=unblock
* @param {object} request - Express request
* @param {object} response - Express response
*/
export async function unblock(request, response) {
const { application } = request.app.locals;
const userId = getUserId(request);
const { url } = request.body;
validateUrl(url);
const collection = getCollection(application);
await collection.deleteOne({ userId, url });
response.json({ result: "ok" });
}
export const blockController = { list, block, unblock };