Skip to content

Commit

Permalink
[FIX] Jump to message (#20265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Jan 20, 2021
1 parent 0872682 commit c3962ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
32 changes: 22 additions & 10 deletions app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ export const normalizeThreadMessage = ({ ...message }) => {
}
};


const waitUntilWrapperExists = async () => document.querySelector('.messages-box .wrapper') || new Promise((resolve) => {
const observer = new MutationObserver(function(mutations, obs) {
const element = document.querySelector('.messages-box .wrapper');
if (element) {
obs.disconnect(); // stop observing
return resolve(element);
}
});
observer.observe(document, {
childList: true,
subtree: true,
});
});

export const upsertMessage = async ({ msg, subscription, uid = Tracker.nonreactive(() => Meteor.userId()) }, collection = ChatMessage) => {
const userId = msg.u && msg.u._id;

Expand Down Expand Up @@ -230,29 +245,26 @@ export const RoomHistoryManager = new class {
}
}

getSurroundingMessages(message, limit = defaultLimit) {
async getSurroundingMessages(message, limit = defaultLimit) {
if (!message || !message.rid) {
return;
}

const instance = Blaze.getView($('.messages-box .wrapper')[0]).templateInstance();
const w = await waitUntilWrapperExists();

const instance = Blaze.getView(w).templateInstance();

if (ChatMessage.findOne({ _id: message._id, _hidden: { $ne: true } })) {
const wrapper = $('.messages-box .wrapper');
const msgElement = $(`#${ message._id }`, wrapper);
const msgElement = $(`#${ message._id }`, w);
if (msgElement.length === 0) {
return;
}

const wrapper = $('.messages-box .wrapper');
const pos = (wrapper.scrollTop() + msgElement.offset().top) - (wrapper.height() / 2);
wrapper.animate({
scrollTop: pos,
}, 500);
msgElement.addClass('highlight');

setTimeout(function() {
const messages = wrapper[0];
instance.atBottom = messages.scrollTop >= (messages.scrollHeight - messages.clientHeight);
});

return setTimeout(() => msgElement.removeClass('highlight'), 500);
}
Expand Down
7 changes: 5 additions & 2 deletions app/ui-utils/client/lib/openRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const openRoom = async function(type, name) {
}


if (room._id === Session.get('openedRoom')) {
if (room._id === Session.get('openedRoom') && !FlowRouter.getQueryParam('msg')) {
return;
}

Expand Down Expand Up @@ -125,13 +125,16 @@ export const openRoom = async function(type, name) {
const messageId = FlowRouter.getQueryParam('msg');
const msg = { _id: messageId, rid: room._id };

const message = Messages.findOne({ ss_id: msg._id }) || (await call('getMessages', [msg._id]))[0];
const message = Messages.findOne({ _id: msg._id }) || (await call('getMessages', [msg._id]))[0];

if (message && (message.tmid || message.tcount)) {
return FlowRouter.setParams({ tab: 'thread', context: message.tmid || message._id });
}

RoomHistoryManager.getSurroundingMessages(msg);
FlowRouter.setQueryParams({
msg: undefined,
});
}

return callbacks.run('enter-room', sub);
Expand Down

0 comments on commit c3962ec

Please sign in to comment.