Skip to content

Commit

Permalink
fix: include unread_messages for all message events (#871)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Deme <[email protected]>
  • Loading branch information
madsroskar and peterdeme authored Jan 24, 2022
1 parent 381b0f7 commit f26ceed
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ export class Channel<
// because in client.ts the handleEvent call that flows to this sets this `event.received_at = new Date();`
last_read: event.received_at as Date,
user: event.user,
unread_messages: 0,
};

if (event.user?.id === this.getClient().user?.id) {
Expand Down Expand Up @@ -1308,6 +1309,7 @@ export class Channel<
channelState.read[event.user.id] = {
last_read: new Date(event.created_at as string),
user: event.user,
unread_messages: 0,
};
} else if (this._countMessageAsUnread(event.message)) {
channelState.unreadCount = channelState.unreadCount + 1;
Expand Down Expand Up @@ -1472,17 +1474,22 @@ export class Channel<
this.state.read[user.id] = {
user,
last_read,
unread_messages: 0,
};
}
}

// apply read state if part of the state
if (state.read) {
for (const read of state.read) {
const parsedRead = { ...read, last_read: new Date(read.last_read) };
this.state.read[read.user.id] = parsedRead;
if (read.user.id === user?.id && typeof parsedRead.unread_messages === 'number') {
this.state.unreadCount = parsedRead.unread_messages;
this.state.read[read.user.id] = {
last_read: new Date(read.last_read),
unread_messages: read.unread_messages ?? 0,
user: read.user,
};

if (read.user.id === user?.id) {
this.state.unreadCount = this.state.read[read.user.id].unread_messages;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
UserResponse,
} from './types';

type ChannelReadStatus<UserType> = Record<
string,
{ last_read: Date; unread_messages: number; user: UserResponse<UserType> }
>;

/**
* ChannelState - A container class for the channel state.
*/
Expand All @@ -29,7 +34,7 @@ export class ChannelState<
string,
Event<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType>
>;
read: Record<string, { last_read: Date; user: UserResponse<UserType> }>;
read: ChannelReadStatus<UserType>;
messages: Array<
ReturnType<
ChannelState<
Expand Down
62 changes: 62 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,68 @@ describe('Channel _handleChannelEvent', function () {

expect(channel.state.messages.find((msg) => msg.id === quotingMessage.id).quoted_message.deleted_at).to.be.ok;
});

it('should include unread_messages for message events from another user', () => {
const channel = client.channel('messaging', 'id');
channel.initialized = true;
channel.state.read['id'] = {
unread_messages: 2,
};

const message = generateMsg();

const events = [
'message.read',
'message.deleted',
'message.new',
'message.updated',
'member.added',
'member.updated',
'member.removed',
];

for (const event of events) {
channel.state.read['id'].unread_messages = 2;
channel._handleChannelEvent({
type: event,
user: { id: 'id' },
message,
});
expect(channel.state.read['id'].unread_messages, `${event} should not be undefined`).not.to.be.undefined;
}
});

it('should include unread_messages for message events from the current user', () => {
const channel = client.channel('messaging', 'id');

channel.initialized = true;
channel.state.read[client.user.id] = {
unread_messages: 2,
};

const message = generateMsg({ user: { id: client.userID } });

const events = [
'message.read',
'message.deleted',
'message.new',
'message.updated',
'member.added',
'member.updated',
'member.removed',
];

for (const event of events) {
channel.state.read['id'].unread_messages = 2;
channel._handleChannelEvent({
type: event,
user: { id: client.user.id },
message,
});
expect(channel.state.read[client.user.id].unread_messages, `${event} should not be undefined`).not.to.be
.undefined;
}
});
});

describe('Channels - Constructor', function () {
Expand Down

0 comments on commit f26ceed

Please sign in to comment.