Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,15 @@ export class ChannelState {
delete this.threads[message.id];
}

if (!this.last_message_at) {
this.last_message_at = new Date(message.created_at.getTime());
}

if (message.created_at.getTime() > this.last_message_at.getTime()) {
const shouldSkipLastMessageAtUpdate =
this._channel.getConfig()?.skip_last_msg_update_for_system_msgs &&
message.type === 'system';

if (
!shouldSkipLastMessageAtUpdate &&
(!this.last_message_at ||
message.created_at.getTime() > this.last_message_at.getTime())
) {
this.last_message_at = new Date(message.created_at.getTime());
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,7 @@ export type ChannelConfigFields = {
replies?: boolean;
search?: boolean;
shared_locations?: boolean;
skip_last_msg_update_for_system_msgs?: boolean;
count_messages?: boolean;
typing_events?: boolean;
uploads?: boolean;
Expand Down
34 changes: 31 additions & 3 deletions test/unit/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1689,9 +1689,10 @@
describe('Channel lastMessage', async () => {
const client = await getClientWithUser();
const channel = client.channel('messaging', uuidv4());
client._addChannelConfig({ cid: channel.cid, config: {} });

it('should return last message - messages are in order', () => {
channel.state = new ChannelState();
channel.state = new ChannelState(channel);
const latestMessageDate = '2018-01-01T00:13:24';
channel.state.addMessagesSorted([
generateMsg({ date: '2018-01-01T00:00:00' }),
Expand All @@ -1705,7 +1706,7 @@
});

it('should return last message - messages are out of order', () => {
channel.state = new ChannelState();
channel.state = new ChannelState(channel);
const latestMessageDate = '2018-01-01T00:13:24';
channel.state.addMessagesSorted([
generateMsg({ date: latestMessageDate }),
Expand All @@ -1719,7 +1720,7 @@
});

it('should return last message - state has more message sets loaded', () => {
channel.state = new ChannelState();
channel.state = new ChannelState(channel);
const latestMessageDate = '2018-01-01T00:13:24';
const latestMessages = [
generateMsg({ date: latestMessageDate }),
Expand All @@ -1737,6 +1738,33 @@
new Date(latestMessageDate).getTime(),
);
});

it('should return last message - system message is ignored when skip_last_msg_update_for_system_msgs: true', () => {
client._addChannelConfig({
cid: channel.cid,
config: { skip_last_msg_update_for_system_msgs: true },
});
channel.state = new ChannelState(channel);
const latestMessageDate = '2018-01-01T00:13:24';
const latestMessages = [
generateMsg({ date: latestMessageDate, type: 'system' }),
generateMsg({ date: '2018-01-01T00:02:00' }),
generateMsg({ date: '2018-01-01T00:00:00' }),
];
const otherMessages = [
generateMsg({ date: '2017-11-21T00:05:33' }),
generateMsg({ date: '2017-11-21T00:05:35' }),
];
channel.state.addMessagesSorted(latestMessages);
channel.state.addMessagesSorted(otherMessages, 'new');

expect(channel.lastMessage().created_at.getTime()).to.be.equal(
new Date(latestMessageDate).getTime(),
);
expect(channel.state.last_message_at.getTime()).toBe(

Check failure on line 1764 in test/unit/channel.test.js

View workflow job for this annotation

GitHub Actions / test

test/unit/channel.test.js > Channel lastMessage > should return last message - system message is ignored when skip_last_msg_update_for_system_msgs: true

AssertionError: expected 1514765604000 to be 1514764920000 // Object.is equality - Expected + Received - 1514764920000 + 1514765604000 ❯ test/unit/channel.test.js:1764:51
new Date(latestMessages[1].created_at).getTime(),
);
});
});

describe('Channel _initializeState', () => {
Expand Down
Loading
Loading