Skip to content

Commit

Permalink
fix: calculate new first unread message id only if the last_read valu…
Browse files Browse the repository at this point in the history
…e changed
  • Loading branch information
MartinCupela committed Jan 19, 2024
1 parent a561748 commit 4cd5698
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,15 +1528,19 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
// apply read state if part of the state
if (state.read) {
for (const read of state.read) {
this.state.read[read.user.id] = {
const previousReadState = this.state.read[read.user.id];
const newReadState = {
last_read: new Date(read.last_read),
last_read_message_id: read.last_read_message_id,
unread_messages: read.unread_messages ?? 0,
user: read.user,
};
this.state.read[read.user.id] = newReadState;

if (read.user.id === user?.id) {
if (this.state.read[user.id].last_read) {
const lastReadChanged =
newReadState.last_read && newReadState.last_read.getTime() !== previousReadState?.last_read.getTime();
if (lastReadChanged) {
for (let i = 0; i < messageSet.messages.length; i++) {
const msg = messageSet.messages[i];
const isOwnMsg = msg.user?.id === user.id;
Expand All @@ -1545,6 +1549,8 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
this.state.read[read.user.id].first_unread_message_id = msg.id;
break;
}
} else {
this.state.read[read.user.id].first_unread_message_id = previousReadState.first_unread_message_id;
}

this.state.unreadCount = this.state.read[read.user.id].unread_messages;
Expand Down
34 changes: 34 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,4 +1259,38 @@ describe('Channel _initializeState', () => {
channel._initializeState(state);
expect(channel.state.read[client.user.id].first_unread_message_id).to.be.undefined;
});

it('should update first unread message id if last_read has not changed', async () => {
const createdAtMsg3 = new Date();
const createdAtMsg2 = new Date(new Date(createdAtMsg3).getTime() - 500);
const createdAtMsg1 = new Date(new Date(createdAtMsg3).getTime() - 1000);
const messages = [
generateMsg({ date: createdAtMsg1.toISOString() }),
generateMsg({ date: createdAtMsg2.toISOString() }),
generateMsg({ date: createdAtMsg3.toISOString() }),
];
let newState = { messages, read: [{ last_read: createdAtMsg1.toISOString(), user: client.user }] };
channel._initializeState(newState);
expect(channel.state.read[client.user.id].first_unread_message_id).to.be.equal(messages[1].id);
newState = { messages, read: [{ last_read: createdAtMsg2.toISOString(), user: client.user }] };
channel._initializeState(newState);
expect(channel.state.read[client.user.id].first_unread_message_id).to.be.equal(messages[2].id);
});

it('should not update first unread message id if last_read has not changed', async () => {
const createdAtMsg3 = new Date();
const createdAtMsg2 = new Date(new Date(createdAtMsg3).getTime() - 500);
const createdAtMsg1 = new Date(new Date(createdAtMsg3).getTime() - 1000);
const messages = [
generateMsg({ date: createdAtMsg1.toISOString() }),
generateMsg({ date: createdAtMsg2.toISOString() }),
generateMsg({ date: createdAtMsg3.toISOString() }),
];
let newState = { messages, read: [{ last_read: createdAtMsg1.toISOString(), user: client.user }] };
channel._initializeState(newState);
expect(channel.state.read[client.user.id].first_unread_message_id).to.be.equal(messages[1].id);
newState = { messages, read: [{ last_read: createdAtMsg1.toISOString(), user: client.user }] };
channel._initializeState(newState);
expect(channel.state.read[client.user.id].first_unread_message_id).to.be.equal(messages[1].id);
});
});

0 comments on commit 4cd5698

Please sign in to comment.