Skip to content

Commit

Permalink
Updating unreadCount function
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Oct 1, 2020
1 parent 571134c commit 3bdafbf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,34 +1097,12 @@ export class Channel<
}

/**
* countUnread - Count the number of messages with a date thats newer than the last read timestamp
*
* @param {Date | Immutable.ImmutableDate | null} [lastRead] lastRead the time that the user read a message, defaults to current user's read state
* countUnread - Count of unread messages
*
* @return {number} Unread count
*/
countUnread(lastRead?: Date | Immutable.ImmutableDate | null) {
if (lastRead == null) {
lastRead = this.lastRead();
}
let count = 0;
for (const m of this.state.messages.asMutable()) {
const message = m.asMutable({ deep: true });
if (this.getClient().userID === message.user?.id) {
continue;
}
if (m.silent) {
continue;
}
if (lastRead == null) {
count++;
continue;
}
if (m.created_at > lastRead) {
count++;
}
}
return count;
countUnread() {
return this.state.unreadCount;
}

/**
Expand Down Expand Up @@ -1423,6 +1401,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 +1441,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;
}
}
break;
case 'user.watching.start':
Expand All @@ -1476,6 +1459,11 @@ export class Channel<
}
break;
case 'message.new':
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 +1562,7 @@ export class Channel<
}
}

// eslint-disable-next-line sonarjs/cognitive-complexity
_initializeState(
state: ChannelAPIResponse<
AttachmentType,
Expand Down Expand Up @@ -1642,6 +1631,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

0 comments on commit 3bdafbf

Please sign in to comment.