Skip to content

Commit

Permalink
Include unread count in subscription API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil94058 committed Jan 14, 2025
1 parent 88be133 commit f1fc11f
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions apps/meteor/app/api/server/v1/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ API.v1.addRoute(

const result = await Meteor.callAsync('subscriptions/get', updatedSinceDate);

// Include unread count in the response
const subscriptionsWithUnread = Array.isArray(result)
? result.map((subscription) => ({
...subscription,
unread: subscription.unread || 0, // Ensure the unread count is included
}))
: result;

return API.v1.success(
Array.isArray(result)
Array.isArray(subscriptionsWithUnread)
? {
update: result,
update: subscriptionsWithUnread,
remove: [],
}
: result,
: subscriptionsWithUnread,
);
},
},
Expand All @@ -56,8 +64,18 @@ API.v1.addRoute(
return API.v1.failure("The 'roomId' param is required");
}

// Fetch the subscription and include unread count
const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId);

if (!subscription) {
return API.v1.failure("Subscription not found for the given roomId.");
}

return API.v1.success({
subscription: await Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId),
subscription: {
...subscription,
unread: subscription.unread || 0, // Add unread count
},
});
},
},
Expand Down Expand Up @@ -85,7 +103,12 @@ API.v1.addRoute(

await readMessages(roomId, this.userId, readThreads);

return API.v1.success();
// Fetch updated unread count after marking as read
const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId);

return API.v1.success({
unread: subscription?.unread || 0, // Updated unread count
});
},
},
);
Expand All @@ -98,9 +121,16 @@ API.v1.addRoute(
},
{
async post() {
await Meteor.callAsync('unreadMessages', (this.bodyParams as any).firstUnreadMessage, (this.bodyParams as any).roomId);
const { firstUnreadMessage, roomId } = this.bodyParams;

await Meteor.callAsync('unreadMessages', firstUnreadMessage, roomId);

// Fetch updated unread count after marking as unread
const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId);

return API.v1.success();
return API.v1.success({
unread: subscription?.unread || 0, // Updated unread count
});
},
},
);

0 comments on commit f1fc11f

Please sign in to comment.