Skip to content
Snippets Groups Projects
Unverified Commit c3962ec0 authored by Guilherme Gazzo's avatar Guilherme Gazzo Committed by GitHub
Browse files

[FIX] Jump to message (#20265)

parent 0872682d
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......@@ -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);
}
......
......@@ -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;
}
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment