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

Updating countUnread function #452

Merged
merged 4 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 28 additions & 4 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class Channel<
* @return {Promise<SendMessageAPIResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType>>} The Server Response
*/
async sendMessage(message: Message<AttachmentType, MessageType, UserType>) {
return await this.getClient().post<
const sendMessageResponse = await this.getClient().post<
SendMessageAPIResponse<
AttachmentType,
ChannelType,
Expand All @@ -207,6 +207,11 @@ export class Channel<
>(this._channelURL() + '/message', {
message,
});

// Reset unreadCount to 0.
this.state.unreadCount = 0;

return sendMessageResponse;
}

sendFile(
Expand Down Expand Up @@ -1097,16 +1102,17 @@ export class Channel<
}

/**
* countUnread - Count the number of messages with a date thats newer than the last read timestamp
* countUnread - Count of unread messages
*
* @param {Date | Immutable.ImmutableDate | null} [lastRead] lastRead the time that the user read a message, defaults to current user's read state
*
* @return {number} Unread count
*/
countUnread(lastRead?: Date | Immutable.ImmutableDate | null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

countUnread function fails to return actual count of unread messages if [number of unread messages] > [number of messages loaded/queried in channel]. E.g., if user queries only first page of channel messages and user has 100 unread messages ---> channel.countUnread() will return only 25.

if (lastRead == null) {
lastRead = this.lastRead();
if (!lastRead) {
return this.state.unreadCount;
}

let count = 0;
for (const m of this.state.messages.asMutable()) {
const message = m.asMutable({ deep: true });
Expand Down Expand Up @@ -1423,6 +1429,7 @@ export class Channel<
this.listeners[key] = this.listeners[key].filter(value => value !== callback);
}

// eslint-disable-next-line sonarjs/cognitive-complexity
_handleChannelEvent(
event: Event<
AttachmentType,
Expand Down Expand Up @@ -1462,6 +1469,10 @@ export class Channel<
event.user.id,
Immutable({ user: { ...event.user }, last_read: event.received_at }),
);

if (event.user?.id === this.getClient().user?.id) {
s.unreadCount = 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set to 0 message.read event.

}
}
break;
case 'user.watching.start':
Expand All @@ -1476,6 +1487,15 @@ export class Channel<
}
break;
case 'message.new':
if (event.user?.id === this.getClient().user?.id) {
s.unreadCount = 0;
} else {
s.unreadCount = s.unreadCount + 1;
}
if (event.message) {
s.addMessageSorted(event.message);
}
break;
case 'message.updated':
case 'message.deleted':
if (event.message) {
Expand Down Expand Up @@ -1574,6 +1594,7 @@ export class Channel<
}
}

// eslint-disable-next-line sonarjs/cognitive-complexity
_initializeState(
state: ChannelAPIResponse<
AttachmentType,
Expand Down Expand Up @@ -1642,6 +1663,9 @@ export class Channel<
const parsedRead = Object.assign({ ...read });
parsedRead.last_read = new Date(read.last_read);
this.state.read = this.state.read.set(read.user.id, parsedRead);
if (read.user.id === this.getClient().user?.id) {
this.state.unreadCount = parsedRead.unread_messages;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class ChannelState<
members: Immutable.ImmutableObject<{
[key: string]: Immutable.Immutable<ChannelMemberResponse<UserType>>;
}>;
unreadCount: number;
membership: Immutable.ImmutableObject<ChannelMembership<UserType>>;
last_message_at: Date | null;

Expand Down Expand Up @@ -155,6 +156,7 @@ export class ChannelState<
[key: string]: Immutable.Immutable<ChannelMemberResponse<UserType>>;
}>({});
this.membership = Immutable<ChannelMembership<UserType>>({});
this.unreadCount = 0;
this.last_message_at =
channel?.state?.last_message_at != null
? new Date(channel.state.last_message_at)
Expand Down