mirror of
https://github.com/svemagie/indiekit-endpoint-microsub.git
synced 2026-04-02 15:35:00 +02:00
fix: Only count unread items within 30-day retention period
- Add date filter to channel unread counts (UNREAD_RETENTION_DAYS = 30) - Update getUnreadCount to also filter by date - Prevents inflated counts from old accumulated items Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user