diff --git a/lib/storage/channels.js b/lib/storage/channels.js index d013855..a40173a 100644 --- a/lib/storage/channels.js +++ b/lib/storage/channels.js @@ -83,6 +83,9 @@ export async function createChannel(application, { name, userId }) { return channel; } +// Retention period for unread count (only count recent items) +const UNREAD_RETENTION_DAYS = 30; + /** * Get all channels for a user * @param {object} application - Indiekit application @@ -101,12 +104,17 @@ export async function getChannels(application, userId) { .sort({ order: 1 }) .toArray(); - // Get unread counts for each channel + // Calculate cutoff date for unread counts (only count recent items) + const cutoffDate = new Date(); + cutoffDate.setDate(cutoffDate.getDate() - UNREAD_RETENTION_DAYS); + + // Get unread counts for each channel (only recent items) const channelsWithCounts = await Promise.all( channels.map(async (channel) => { const unreadCount = await itemsCollection.countDocuments({ channelId: channel._id, readBy: { $ne: userId }, + published: { $gte: cutoffDate }, }); return { diff --git a/lib/storage/items.js b/lib/storage/items.js index ed6d7ab..6896807 100644 --- a/lib/storage/items.js +++ b/lib/storage/items.js @@ -562,6 +562,9 @@ export async function deleteItemsForFeed(application, feedId) { return result.deletedCount; } +// Retention period for unread count (only count recent items) +const UNREAD_RETENTION_DAYS = 30; + /** * Get unread count for a channel * @param {object} application - Indiekit application @@ -574,9 +577,14 @@ export async function getUnreadCount(application, channelId, userId) { const objectId = typeof channelId === "string" ? new ObjectId(channelId) : channelId; + // Only count items from the last UNREAD_RETENTION_DAYS + const cutoffDate = new Date(); + cutoffDate.setDate(cutoffDate.getDate() - UNREAD_RETENTION_DAYS); + return collection.countDocuments({ channelId: objectId, readBy: { $ne: userId }, + published: { $gte: cutoffDate }, }); } diff --git a/package.json b/package.json index 87f413f..0c36869 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rmdes/indiekit-endpoint-microsub", - "version": "1.0.25", + "version": "1.0.26", "description": "Microsub endpoint for Indiekit. Enables subscribing to feeds and reading content using the Microsub protocol.", "keywords": [ "indiekit",