Skip to content

Commit

Permalink
fix: prevent channel unread count reset to 0 when sending message and…
Browse files Browse the repository at this point in the history
… on new own or thread messages
  • Loading branch information
MartinCupela committed Jan 16, 2024
1 parent 4d73838 commit 525e6e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
32 changes: 15 additions & 17 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,10 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
* @return {Promise<SendMessageAPIResponse<StreamChatGenerics>>} The Server Response
*/
async sendMessage(message: Message<StreamChatGenerics>, options?: SendMessageOptions) {
const sendMessageResponse = await this.getClient().post<SendMessageAPIResponse<StreamChatGenerics>>(
this._channelURL() + '/message',
{
message,
...options,
},
);

// Reset unreadCount to 0.
this.state.unreadCount = 0;

return sendMessageResponse;
return await this.getClient().post<SendMessageAPIResponse<StreamChatGenerics>>(this._channelURL() + '/message', {
message,
...options,
});
}

sendFile(
Expand Down Expand Up @@ -1289,6 +1281,12 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
channelState.addPinnedMessage(event.message);
}

// do not increase the unread count - the back-end does not increase the count neither in the following cases:
// 1. the message is mine
// 2. the message is a thread reply from any user
const preventUnreadCountUpdate = ownMessage || isThreadMessage;
if (preventUnreadCountUpdate) break;

if (event.user?.id) {
for (const userId in channelState.read) {
if (userId === event.user.id) {
Expand All @@ -1303,9 +1301,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
}
}

if (ownMessage) {
channelState.unreadCount = 0;
} else if (this._countMessageAsUnread(event.message)) {
if (this._countMessageAsUnread(event.message)) {
channelState.unreadCount = channelState.unreadCount + 1;
}
}
Expand Down Expand Up @@ -1363,15 +1359,17 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
const ownMessage = event.user?.id === this.getClient().user?.id;
if (!(ownMessage && event.user)) break;

const unreadCount = event.unread_messages ?? 0;

channelState.read[event.user.id] = {
first_unread_message_id: event.first_unread_message_id,
last_read: new Date(event.last_read_at as string),
last_read_message_id: event.last_read_message_id,
user: event.user,
unread_messages: event.unread_messages ?? 0,
unread_messages: unreadCount,
};

channelState.unreadCount = event.unread_messages ?? 0;
channelState.unreadCount = unreadCount;
break;
}
case 'channel.updated':
Expand Down
34 changes: 32 additions & 2 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,45 @@ describe('Channel _handleChannelEvent', function () {
channel.initialized = true;
});

it('message.new reset the unreadCount for current user messages', function () {
it('message.new does not reset the unreadCount for current user messages', function () {
channel.state.unreadCount = 100;
channel._handleChannelEvent({
type: 'message.new',
user,
message: generateMsg(),
});

expect(channel.state.unreadCount).to.be.equal(0);
expect(channel.state.unreadCount).to.be.equal(100);
});

it('message.new does not reset the unreadCount for own thread replies', function () {
channel.state.unreadCount = 100;
channel._handleChannelEvent({
type: 'message.new',
user,
message: generateMsg({
parent_id: 'parentId',
type: 'reply',
user,
}),
});

expect(channel.state.unreadCount).to.be.equal(100);
});

it('message.new does not reset the unreadCount for others thread replies', function () {
channel.state.unreadCount = 100;
channel._handleChannelEvent({
type: 'message.new',
user: { id: 'id' },
message: generateMsg({
parent_id: 'parentId',
type: 'reply',
user: { id: 'id' },
}),
});

expect(channel.state.unreadCount).to.be.equal(100);
});

it('message.new increment unreadCount properly', function () {
Expand Down

0 comments on commit 525e6e0

Please sign in to comment.