Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/bright-dots-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/meteor': patch
'@rocket.chat/model-typings': patch
'@rocket.chat/models': patch
---

Fixes main channel scroll position changing when jumping to a thread message from search
10 changes: 9 additions & 1 deletion apps/meteor/app/ui-utils/client/lib/RoomHistoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ class RoomHistoryManagerClass extends Emitter {
}

public async getSurroundingMessages(message?: Pick<IMessage, '_id' | 'rid'> & { ts?: Date }) {
return this.loadSurroundingMessages(message, true);
}

public async getSurroundingChannelMessages(message?: Pick<IMessage, '_id' | 'rid'> & { ts?: Date }) {
return this.loadSurroundingMessages(message, false);
}

private async loadSurroundingMessages(message: (Pick<IMessage, '_id' | 'rid'> & { ts?: Date }) | undefined, showThreadMessages: boolean) {
if (!message?.rid) {
return;
}
Expand All @@ -309,7 +317,7 @@ class RoomHistoryManagerClass extends Emitter {
const room = this.getRoom(message.rid);

const subscription = Subscriptions.state.find((record) => record.rid === message.rid);
const result = await callWithErrorHandling('loadSurroundingMessages', message, defaultLimit);
const result = await callWithErrorHandling('loadSurroundingMessages', message, defaultLimit, showThreadMessages);

this.clear(message.rid);

Expand Down
12 changes: 9 additions & 3 deletions apps/meteor/client/lib/utils/legacyJumpToMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ export const legacyJumpToMessage = async (message: IMessage) => {
routeParamsOverrides: { tab: 'thread', context: message.tmid || message._id },
replace: RoomManager.opened === message.rid,
});
await RoomHistoryManager.getSurroundingMessages(message);

if (message.tcount) {
await RoomHistoryManager.getSurroundingChannelMessages(message);
} else if (!RoomHistoryManager.isLoaded(message.rid)) {
await RoomHistoryManager.getMore(message.rid);
}

return;
}

if (RoomManager.opened === message.rid) {
await RoomHistoryManager.getSurroundingMessages(message);
await RoomHistoryManager.getSurroundingChannelMessages(message);
return;
}

await goToRoomById(message.rid);

await RoomHistoryManager.getSurroundingMessages(message);
await RoomHistoryManager.getSurroundingChannelMessages(message);
};
24 changes: 14 additions & 10 deletions apps/meteor/client/views/room/body/hooks/useGetMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,36 @@ export const useGetMore = (rid: string, atBottomRef: MutableRefObject<boolean>)
return;
}

const { scrollTop, clientHeight, scrollHeight } = getBoundingClientRect(element);
if (jumpToRef.current) {
return;
}

if (RoomHistoryManager.isLoading(rid)) {
return;
}

if (msgIdRef.current && !RoomHistoryManager.isLoaded(rid)) {
return;
}

const { scrollTop, clientHeight, scrollHeight } = getBoundingClientRect(element);

const lastScrollTopRef = scrollTop;
const height = clientHeight;
const isLoading = RoomHistoryManager.isLoading(rid);
const hasMore = RoomHistoryManager.hasMore(rid);
const hasMoreNext = RoomHistoryManager.hasMoreNext(rid);

if (jumpToRef.current) {
return;
}

if (isLoading) {
return;
}

if (hasMore === true && lastScrollTopRef <= height / 3) {
await RoomHistoryManager.getMore(rid);

if (jumpToRef.current) {
return;
}

if (!element.isConnected) {
return;
}

flushSync(() => {
RoomHistoryManager.restoreScroll(rid);
});
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/loadMissedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Meteor.methods<ServerMethods>({
return false;
}

return Messages.findVisibleByRoomIdAfterTimestamp(rid, start, {
return Messages.findVisibleByRoomIdAfterTimestamp(rid, start, true, {
sort: {
ts: -1,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/loadNextMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Meteor.methods<ServerMethods>({

let records;
if (end) {
records = await Messages.findVisibleByRoomIdAfterTimestamp(rid, end, {
records = await Messages.findVisibleByRoomIdAfterTimestamp(rid, end, true, {
sort: {
ts: 1,
},
Expand Down
18 changes: 15 additions & 3 deletions apps/meteor/server/methods/loadSurroundingMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module '@rocket.chat/ddp-client' {
loadSurroundingMessages(
message: Pick<IMessage, '_id' | 'rid'> & { ts?: Date },
limit?: number,
showThreadMessages?: boolean,
):
| {
messages: IMessage[];
Expand All @@ -25,9 +26,10 @@ declare module '@rocket.chat/ddp-client' {
}

Meteor.methods<ServerMethods>({
Comment thread
aleksandernsilva marked this conversation as resolved.
async loadSurroundingMessages(message, limit = 50) {
async loadSurroundingMessages(message, limit = 50, showThreadMessages = true) {
check(message, Object);
check(limit, Number);
Comment thread
aleksandernsilva marked this conversation as resolved.
check(showThreadMessages, Boolean);

if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
Expand Down Expand Up @@ -60,7 +62,12 @@ Meteor.methods<ServerMethods>({
limit: Math.ceil(limit / 2),
};

const messages = await Messages.findVisibleByRoomIdBeforeTimestamp(mainMessage.rid, mainMessage.ts, options).toArray();
const messages = await Messages.findVisibleByRoomIdBeforeTimestamp(
mainMessage.rid,
mainMessage.ts,
showThreadMessages,
options,
).toArray();

const moreBefore = messages.length === options.limit;

Expand All @@ -72,7 +79,12 @@ Meteor.methods<ServerMethods>({

options.limit = Math.floor(limit / 2);

const afterMessages = await Messages.findVisibleByRoomIdAfterTimestamp(mainMessage.rid, mainMessage.ts, options).toArray();
const afterMessages = await Messages.findVisibleByRoomIdAfterTimestamp(
mainMessage.rid,
mainMessage.ts,
showThreadMessages,
options,
).toArray();

const moreAfter = afterMessages.length === options.limit;

Expand Down
Loading
Loading