Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent channel unread count reset to 0 when sending message and on new own or thread messages #1210

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading