Skip to content

Commit

Permalink
Merge pull request #474 from GetStream/reconnect-issue
Browse files Browse the repository at this point in the history
fix: fetch messages sent to active channel while user were offline
  • Loading branch information
szuperaz authored Sep 21, 2023
2 parents 9410181 + 6d75f15 commit cb25a03
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
10 changes: 10 additions & 0 deletions projects/stream-chat-angular/src/lib/channel.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1942,16 +1942,26 @@ describe('ChannelService', () => {
await init();
let channels!: Channel<DefaultStreamChatGenerics>[];
service.channels$.subscribe((c) => (channels = c!));
const activeChannel = channels[0];
const spy = jasmine.createSpy();
service.activeChannel$.subscribe(spy);
spy.calls.reset();
const messagesSpy = jasmine.createSpy();
service.activeChannelMessages$.subscribe(messagesSpy);
messagesSpy.calls.reset();
mockChatClient.queryChannels.and.resolveTo(channels);
spyOn(service, 'deselectActiveChannel').and.callThrough();
const newMessage = generateMockMessages()[0];
newMessage.text = 'new message received while offline';
activeChannel.state.messages.push(newMessage);
events$.next({ eventType: 'connection.recovered' } as ClientEvent);
tick();

expect(spy).not.toHaveBeenCalled();
expect(service.deselectActiveChannel).not.toHaveBeenCalled();
expect(messagesSpy).toHaveBeenCalledWith(
jasmine.arrayContaining([newMessage])
);
}));

it('should add new channel to channel list', () => {
Expand Down
77 changes: 48 additions & 29 deletions projects/stream-chat-angular/src/lib/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,33 +412,7 @@ export class ChannelService<
this.watchForActiveChannelEvents(channel);
this.addChannel(channel);
this.activeChannelSubject.next(channel);
channel.state.messages.forEach((m) => {
m.readBy = getReadBy(m, channel);
m.translation = getMessageTranslation(
m,
channel,
this.chatClientService.chatClient.user
);
if (m.quoted_message) {
m.quoted_message.translation = getMessageTranslation(
m.quoted_message,
channel,
this.chatClientService.chatClient.user
);
}
});
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
void channel.markRead();
}
this.activeChannelMessagesSubject.next([...channel.state.messages]);
this.activeChannelPinnedMessagesSubject.next([
...channel.state.pinnedMessages,
]);
this.activeParentMessageIdSubject.next(undefined);
this.activeThreadMessagesSubject.next([]);
this.messageToQuoteSubject.next(undefined);
this.usersTypingInChannelSubject.next([]);
this.usersTypingInThreadSubject.next([]);
this.setChannelState(channel);
}

/**
Expand Down Expand Up @@ -1015,8 +989,23 @@ export class ChannelService<
this.shouldSetActiveChannel &&
!this.activeChannelSubject.getValue();
await this.queryChannels(shoulSetActiveChannel || false, true);
// Thread messages are not refetched so active thread gets deselected to avoid displaying stale messages
void this.setAsActiveParentMessage(undefined);
if (this.activeChannelSubject.getValue()) {
// Thread messages are not refetched so active thread gets deselected to avoid displaying stale messages
void this.setAsActiveParentMessage(undefined);
// Update and reselect message to quote
const messageToQuote = this.messageToQuoteSubject.getValue();
this.setChannelState(this.activeChannelSubject.getValue()!);
let messages!: StreamMessage<T>[];
this.activeChannelMessages$
.pipe(take(1))
.subscribe((m) => (messages = m));
const updatedMessageToQuote = messages.find(
(m) => m.id === messageToQuote?.id
);
if (updatedMessageToQuote) {
this.selectMessageToQuote(updatedMessageToQuote);
}
}
this.isStateRecoveryInProgress = false;
} catch {
this.isStateRecoveryInProgress = false;
Expand Down Expand Up @@ -1678,4 +1667,34 @@ export class ChannelService<
});
}
}

private setChannelState(channel: Channel<T>) {
channel.state.messages.forEach((m) => {
m.readBy = getReadBy(m, channel);
m.translation = getMessageTranslation(
m,
channel,
this.chatClientService.chatClient.user
);
if (m.quoted_message) {
m.quoted_message.translation = getMessageTranslation(
m.quoted_message,
channel,
this.chatClientService.chatClient.user
);
}
});
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
void channel.markRead();
}
this.activeChannelMessagesSubject.next([...channel.state.messages]);
this.activeChannelPinnedMessagesSubject.next([
...channel.state.pinnedMessages,
]);
this.activeParentMessageIdSubject.next(undefined);
this.activeThreadMessagesSubject.next([]);
this.messageToQuoteSubject.next(undefined);
this.usersTypingInChannelSubject.next([]);
this.usersTypingInThreadSubject.next([]);
}
}

0 comments on commit cb25a03

Please sign in to comment.