Skip to content
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
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
39 changes: 34 additions & 5 deletions test/unit/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1687,11 +1687,16 @@ describe('Channel search', async () => {
});

describe('Channel lastMessage', async () => {
const client = await getClientWithUser();
const channel = client.channel('messaging', uuidv4());
let channel;
let client;
beforeEach(async () => {
client = await getClientWithUser();
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 +1710,7 @@ describe('Channel lastMessage', async () => {
});

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 +1724,7 @@ describe('Channel lastMessage', async () => {
});

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 +1742,30 @@ describe('Channel lastMessage', async () => {
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.state.last_message_at.getTime()).toBe(
new Date(latestMessages[1].created_at).getTime(),
);
});
});

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