Skip to content

Commit

Permalink
Merge pull request #540 from GetStream/vishal/isUpToDate-flag
Browse files Browse the repository at this point in the history
isUpToDate flag on channel state
  • Loading branch information
vishalnarkhede committed Dec 31, 2020
2 parents 5786775 + 6d0f702 commit 588c0e9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/channel.ts
Expand Up @@ -1562,13 +1562,21 @@ export class Channel<
if (event.message) {
/* if message belongs to current user, always assume timestamp is changed to filter it out and add again to avoid duplication */
const ownMessage = event.user?.id === this.getClient().user?.id;
s.addMessageSorted(event.message, ownMessage);
const isThreadMessage =
event.message.parent_id && !event.message.show_in_channel;

if (this.state.isUpToDate || isThreadMessage) {
s.addMessageSorted(event.message, ownMessage);
}

if (ownMessage && event.user?.id) {
s.unreadCount = 0;
s.read = s.read.set(
event.user.id,
Immutable({ user: { ...event.user }, last_read: event.created_at }),
Immutable({
user: { ...event.user },
last_read: new Date(event.created_at as string),
}),
);
} else if (this._countMessageAsUnread(event.message)) {
s.unreadCount = s.unreadCount + 1;
Expand Down
27 changes: 26 additions & 1 deletion src/channel_state.ts
Expand Up @@ -87,7 +87,13 @@ export class ChannelState<
unreadCount: number;
membership: Immutable.ImmutableObject<ChannelMembership<UserType>>;
last_message_at: Date | null;

/**
* Flag which indicates if channel state contain latest/recent messages or no.
* This flag should be managed by UI sdks using a setter - setIsUpToDate.
* When false, any new message (received by websocket event - message.new) will not
* be pushed on to message list.
*/
isUpToDate: boolean;
constructor(
channel: Channel<
AttachmentType,
Expand Down Expand Up @@ -146,6 +152,13 @@ export class ChannelState<
}>({});
this.membership = Immutable<ChannelMembership<UserType>>({});
this.unreadCount = 0;
/**
* Flag which indicates if channel state contain latest/recent messages or no.
* This flag should be managed by UI sdks using a setter - setIsUpToDate.
* When false, any new message (received by websocket event - message.new) will not
* be pushed on to message list.
*/
this.isUpToDate = true;
this.last_message_at =
channel?.state?.last_message_at != null
? new Date(channel.state.last_message_at)
Expand Down Expand Up @@ -448,6 +461,18 @@ export class ChannelState<
}
}

/**
* Setter for isUpToDate.
*
* @param isUpToDate Flag which indicates if channel state contain latest/recent messages or no.
* This flag should be managed by UI sdks using a setter - setIsUpToDate.
* When false, any new message (received by websocket event - message.new) will not
* be pushed on to message list.
*/
setIsUpToDate = (isUpToDate: boolean) => {
this.isUpToDate = isUpToDate;
};

/**
* _addToMessageList - Adds a message to a list of messages, tries to update first, appends if message isn't found
*
Expand Down
41 changes: 41 additions & 0 deletions test/integration/channels.js
Expand Up @@ -2679,3 +2679,44 @@ describe('Quote messages', () => {
});
});
});

describe.only('Channel - isUpToDate', async () => {
it('new message should skip state, if channel is not upToDate', async () => {
const userIdVish = 'vishal';
const userIdAmin = 'amin';
await createUsers([userIdVish, userIdAmin]);

const clientVish = await getTestClientForUser(userIdVish);
const channelId = uuidv4();
const channelVish = clientVish.channel('messaging', channelId, {
members: [userIdAmin, userIdVish],
});
await channelVish.watch();

const serverClient = await getServerTestClient();
const channelAmin = serverClient.channel('messaging', channelId);

// First lets try with upToDate list.
let waiter = createEventWaiter(channelVish, 'message.new');
const { message: message1 } = await channelAmin.sendMessage({
text: uuidv4(),
user_id: userIdAmin,
});
await waiter;
expect(
channelVish.state.messages.findIndex((m) => m.id === message1.id),
).to.be.equal(0);

// Now lets check not upToDate list.
channelVish.state.setIsUpToDate(false);
waiter = createEventWaiter(channelVish, 'message.new');
const { message: message2 } = await channelAmin.sendMessage({
text: uuidv4(),
user_id: userIdAmin,
});
await waiter;
expect(
channelVish.state.messages.findIndex((m) => m.id === message2.id),
).to.be.equal(-1);
});
});

0 comments on commit 588c0e9

Please sign in to comment.