Skip to content

Commit 6d75f15

Browse files
committed
fix: fetch messages sent to active channel while user were offline
1 parent 9410181 commit 6d75f15

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,16 +1942,26 @@ describe('ChannelService', () => {
19421942
await init();
19431943
let channels!: Channel<DefaultStreamChatGenerics>[];
19441944
service.channels$.subscribe((c) => (channels = c!));
1945+
const activeChannel = channels[0];
19451946
const spy = jasmine.createSpy();
19461947
service.activeChannel$.subscribe(spy);
19471948
spy.calls.reset();
1949+
const messagesSpy = jasmine.createSpy();
1950+
service.activeChannelMessages$.subscribe(messagesSpy);
1951+
messagesSpy.calls.reset();
19481952
mockChatClient.queryChannels.and.resolveTo(channels);
19491953
spyOn(service, 'deselectActiveChannel').and.callThrough();
1954+
const newMessage = generateMockMessages()[0];
1955+
newMessage.text = 'new message received while offline';
1956+
activeChannel.state.messages.push(newMessage);
19501957
events$.next({ eventType: 'connection.recovered' } as ClientEvent);
19511958
tick();
19521959

19531960
expect(spy).not.toHaveBeenCalled();
19541961
expect(service.deselectActiveChannel).not.toHaveBeenCalled();
1962+
expect(messagesSpy).toHaveBeenCalledWith(
1963+
jasmine.arrayContaining([newMessage])
1964+
);
19551965
}));
19561966

19571967
it('should add new channel to channel list', () => {

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -412,33 +412,7 @@ export class ChannelService<
412412
this.watchForActiveChannelEvents(channel);
413413
this.addChannel(channel);
414414
this.activeChannelSubject.next(channel);
415-
channel.state.messages.forEach((m) => {
416-
m.readBy = getReadBy(m, channel);
417-
m.translation = getMessageTranslation(
418-
m,
419-
channel,
420-
this.chatClientService.chatClient.user
421-
);
422-
if (m.quoted_message) {
423-
m.quoted_message.translation = getMessageTranslation(
424-
m.quoted_message,
425-
channel,
426-
this.chatClientService.chatClient.user
427-
);
428-
}
429-
});
430-
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
431-
void channel.markRead();
432-
}
433-
this.activeChannelMessagesSubject.next([...channel.state.messages]);
434-
this.activeChannelPinnedMessagesSubject.next([
435-
...channel.state.pinnedMessages,
436-
]);
437-
this.activeParentMessageIdSubject.next(undefined);
438-
this.activeThreadMessagesSubject.next([]);
439-
this.messageToQuoteSubject.next(undefined);
440-
this.usersTypingInChannelSubject.next([]);
441-
this.usersTypingInThreadSubject.next([]);
415+
this.setChannelState(channel);
442416
}
443417

444418
/**
@@ -1015,8 +989,23 @@ export class ChannelService<
1015989
this.shouldSetActiveChannel &&
1016990
!this.activeChannelSubject.getValue();
1017991
await this.queryChannels(shoulSetActiveChannel || false, true);
1018-
// Thread messages are not refetched so active thread gets deselected to avoid displaying stale messages
1019-
void this.setAsActiveParentMessage(undefined);
992+
if (this.activeChannelSubject.getValue()) {
993+
// Thread messages are not refetched so active thread gets deselected to avoid displaying stale messages
994+
void this.setAsActiveParentMessage(undefined);
995+
// Update and reselect message to quote
996+
const messageToQuote = this.messageToQuoteSubject.getValue();
997+
this.setChannelState(this.activeChannelSubject.getValue()!);
998+
let messages!: StreamMessage<T>[];
999+
this.activeChannelMessages$
1000+
.pipe(take(1))
1001+
.subscribe((m) => (messages = m));
1002+
const updatedMessageToQuote = messages.find(
1003+
(m) => m.id === messageToQuote?.id
1004+
);
1005+
if (updatedMessageToQuote) {
1006+
this.selectMessageToQuote(updatedMessageToQuote);
1007+
}
1008+
}
10201009
this.isStateRecoveryInProgress = false;
10211010
} catch {
10221011
this.isStateRecoveryInProgress = false;
@@ -1678,4 +1667,34 @@ export class ChannelService<
16781667
});
16791668
}
16801669
}
1670+
1671+
private setChannelState(channel: Channel<T>) {
1672+
channel.state.messages.forEach((m) => {
1673+
m.readBy = getReadBy(m, channel);
1674+
m.translation = getMessageTranslation(
1675+
m,
1676+
channel,
1677+
this.chatClientService.chatClient.user
1678+
);
1679+
if (m.quoted_message) {
1680+
m.quoted_message.translation = getMessageTranslation(
1681+
m.quoted_message,
1682+
channel,
1683+
this.chatClientService.chatClient.user
1684+
);
1685+
}
1686+
});
1687+
if (this.canSendReadEvents && this.shouldMarkActiveChannelAsRead) {
1688+
void channel.markRead();
1689+
}
1690+
this.activeChannelMessagesSubject.next([...channel.state.messages]);
1691+
this.activeChannelPinnedMessagesSubject.next([
1692+
...channel.state.pinnedMessages,
1693+
]);
1694+
this.activeParentMessageIdSubject.next(undefined);
1695+
this.activeThreadMessagesSubject.next([]);
1696+
this.messageToQuoteSubject.next(undefined);
1697+
this.usersTypingInChannelSubject.next([]);
1698+
this.usersTypingInThreadSubject.next([]);
1699+
}
16811700
}

0 commit comments

Comments
 (0)