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>
This commit is contained in:
Ricardo
2026-02-12 22:30:40 +01:00
parent 1009a2d6f6
commit 02bfff7ce9
9 changed files with 167 additions and 31 deletions

View File

@@ -74,8 +74,8 @@ export async function createChannel(application, { name, userId }) {
excludeTypes: [],
excludeRegex: undefined,
},
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
await collection.insertOne(channel);
@@ -185,7 +185,7 @@ export async function updateChannel(application, uid, updates, userId) {
{
$set: {
...updates,
updatedAt: new Date(),
updatedAt: new Date().toISOString(),
},
},
{ returnDocument: "after" },
@@ -242,7 +242,7 @@ export async function reorderChannels(application, channelUids, userId) {
const operations = channelUids.map((uid, index) => ({
updateOne: {
filter: userId ? { uid, userId } : { uid },
update: { $set: { order: index, updatedAt: new Date() } },
update: { $set: { order: index, updatedAt: new Date().toISOString() } },
},
}));
@@ -298,8 +298,8 @@ export async function ensureNotificationsChannel(application, userId) {
excludeTypes: [],
excludeRegex: undefined,
},
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
await collection.insertOne(channel);

View File

@@ -45,11 +45,11 @@ export async function createFeed(
photo: photo || undefined,
tier: 1, // Start at tier 1 (2 minutes)
unmodified: 0,
nextFetchAt: new Date(), // Fetch immediately
nextFetchAt: new Date(), // Fetch immediately (kept as Date for query compatibility)
lastFetchedAt: undefined,
websub: undefined, // Will be populated if hub is discovered
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
await collection.insertOne(feed);
@@ -114,7 +114,7 @@ export async function updateFeed(application, id, updates) {
{
$set: {
...updates,
updatedAt: new Date(),
updatedAt: new Date().toISOString(),
},
},
{ returnDocument: "after" },
@@ -227,15 +227,15 @@ export async function updateFeedAfterFetch(
updateData = {
tier,
unmodified,
nextFetchAt,
lastFetchedAt: new Date(),
updatedAt: new Date(),
nextFetchAt, // Kept as Date for query compatibility
lastFetchedAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
} else {
updateData = {
...extra,
lastFetchedAt: new Date(),
updatedAt: new Date(),
lastFetchedAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
}
@@ -280,7 +280,7 @@ export async function updateFeedWebsub(application, id, websub) {
{
$set: {
websub: websubData,
updatedAt: new Date(),
updatedAt: new Date().toISOString(),
},
},
{ returnDocument: "after" },
@@ -314,12 +314,12 @@ export async function updateFeedStatus(application, id, status) {
const objectId = typeof id === "string" ? new ObjectId(id) : id;
const updateFields = {
updatedAt: new Date(),
updatedAt: new Date().toISOString(),
};
if (status.success) {
updateFields.status = "active";
updateFields.lastSuccessAt = new Date();
updateFields.lastSuccessAt = new Date().toISOString();
updateFields.consecutiveErrors = 0;
updateFields.lastError = undefined;
updateFields.lastErrorAt = undefined;
@@ -330,7 +330,7 @@ export async function updateFeedStatus(application, id, status) {
} else {
updateFields.status = "error";
updateFields.lastError = status.error;
updateFields.lastErrorAt = new Date();
updateFields.lastErrorAt = new Date().toISOString();
}
// Use $set for most fields, $inc for consecutiveErrors on failure

View File

@@ -49,8 +49,8 @@ export async function addItem(application, { channelId, feedId, uid, item }) {
name: item.name || undefined,
content: item.content || undefined,
summary: item.summary || undefined,
published: item.published ? new Date(item.published) : new Date(),
updated: item.updated ? new Date(item.updated) : undefined,
published: item.published ? new Date(item.published) : new Date(), // Keep as Date for query compatibility
updated: item.updated ? new Date(item.updated) : undefined, // Keep as Date for query compatibility
author: item.author || undefined,
category: item.category || [],
photo: item.photo || [],
@@ -62,7 +62,7 @@ export async function addItem(application, { channelId, feedId, uid, item }) {
inReplyTo: item["in-reply-to"] || item.inReplyTo || [],
source: item._source || undefined,
readBy: [], // Array of user IDs who have read this item
createdAt: new Date(),
createdAt: new Date().toISOString(),
};
await collection.insertOne(document);
@@ -182,7 +182,7 @@ function transformToJf2(item, userId) {
type: item.type,
uid: item.uid,
url: item.url,
published: item.published?.toISOString(),
published: item.published?.toISOString(), // Convert Date to ISO string
_id: item._id.toString(),
_is_read: userId ? item.readBy?.includes(userId) : false,
};
@@ -191,7 +191,7 @@ function transformToJf2(item, userId) {
if (item.name) jf2.name = item.name;
if (item.content) jf2.content = item.content;
if (item.summary) jf2.summary = item.summary;
if (item.updated) jf2.updated = item.updated.toISOString();
if (item.updated) jf2.updated = item.updated.toISOString(); // Convert Date to ISO string
if (item.author) jf2.author = normalizeAuthor(item.author);
if (item.category?.length > 0) jf2.category = item.category;