Skip to content

Commit

Permalink
runfix: Trigger a last read timestamp update when scroll reaches bott…
Browse files Browse the repository at this point in the history
…om of conversation (#6065)
  • Loading branch information
atomrc committed Mar 12, 2019
1 parent e48417b commit 1ee04df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
7 changes: 2 additions & 5 deletions src/script/view_model/bindings/MessageListBindings.js
Expand Up @@ -83,9 +83,7 @@ ko.bindingHandlers.infinite_scroll = {
const {onHitTop, onHitBottom, onInit} = params();
onInit(scrollingElement);

const onScroll = event => {
const element = event.target;

const onScroll = ({target: element}) => {
// On some HiDPI screens scrollTop returns a floating point number instead of an integer
// https://github.com/jquery/api.jquery.com/issues/608
const scrollPosition = Math.ceil(element.scrollTop);
Expand All @@ -100,8 +98,7 @@ ko.bindingHandlers.infinite_scroll = {
}
};

const onMouseWheel = event => {
const element = event.currentTarget;
const onMouseWheel = ({currentTarget: element}) => {
const isScrollable = element.scrollHeight > element.clientHeight;
if (isScrollable) {
// if the element is scrollable, the scroll event will take the relay
Expand Down
49 changes: 27 additions & 22 deletions src/script/view_model/content/MessageListViewModel.js
Expand Up @@ -193,14 +193,8 @@ class MessageListViewModel {
});
}

_conversationHasExtraMessages(conversationEntity) {
const lastMessageEntity = conversationEntity.getLastMessage();
if (!lastMessageEntity) {
return false;
}

const isLastConversationEvent = lastMessageEntity.timestamp() >= this.conversation().last_event_timestamp();
return !isLastConversationEvent && lastMessageEntity.timestamp();
_isLastReceivedMessage(messageEntity, conversationEntity) {
return messageEntity.timestamp() && messageEntity.timestamp() >= conversationEntity.last_event_timestamp();
}

getMessagesContainer() {
Expand Down Expand Up @@ -332,10 +326,15 @@ class MessageListViewModel {
* @returns {Promise<any>} A promise that resolves when the loading is done
*/
loadFollowingMessages() {
const last_message = this.conversation().getLastMessage();
const lastMessage = this.conversation().getLastMessage();

if (last_message && this._conversationHasExtraMessages(this.conversation())) {
return this.conversation_repository.getSubsequentMessages(this.conversation(), last_message, false);
if (lastMessage) {
if (!this._isLastReceivedMessage(lastMessage, this.conversation())) {
// if the last loaded message is not the last of the conversation, we load the subsequent messages
return this.conversation_repository.getSubsequentMessages(this.conversation(), lastMessage, false);
}
// is the message is the last of the conversation, then we update the last read timestamp of the conversation
this.updateConversationLastRead(this.conversation(), lastMessage);
}
return Promise.resolve();
}
Expand Down Expand Up @@ -513,7 +512,6 @@ class MessageListViewModel {
* @returns {Function|null} Callback or null
*/
getInViewportCallback(conversationEntity, messageEntity) {
const conversationTimestamp = conversationEntity.last_read_timestamp();
const messageTimestamp = messageEntity.timestamp();
const callbacks = [];

Expand All @@ -531,15 +529,6 @@ class MessageListViewModel {

const updateLastRead = () => {
conversationEntity.setTimestamp(messageEntity.timestamp(), Conversation.TIMESTAMP_TYPE.LAST_READ);
const hasNoUnread = conversationEntity.unreadState().allMessages.length === 0;

if (hasNoUnread) {
const lastKnowTimestamp = conversationEntity.get_last_known_timestamp(
this.serverTimeRepository.toServerTimestamp()
);
conversationEntity.setTimestamp(lastKnowTimestamp, Conversation.TIMESTAMP_TYPE.LAST_READ);
this.conversation_repository.markAsRead(conversationEntity);
}
};

const startTimer = () => {
Expand All @@ -552,7 +541,7 @@ class MessageListViewModel {
callbacks.push(startTimer);
}

const isUnreadMessage = messageTimestamp > conversationTimestamp;
const isUnreadMessage = messageTimestamp > conversationEntity.last_read_timestamp();
const isNotOwnMessage = !messageEntity.user().is_me;

let shouldSendReadReceipt = false;
Expand All @@ -565,6 +554,10 @@ class MessageListViewModel {
}
}

if (this._isLastReceivedMessage(messageEntity, conversationEntity)) {
callbacks.push(() => this.updateConversationLastRead(conversationEntity, messageEntity));
}

if (isUnreadMessage && isNotOwnMessage) {
callbacks.push(updateLastRead);
if (shouldSendReadReceipt) {
Expand All @@ -582,6 +575,18 @@ class MessageListViewModel {
};
}

updateConversationLastRead(conversationEntity, messageEntity) {
const conversationLastRead = conversationEntity.last_read_timestamp();
const lastKnownTimestamp = conversationEntity.get_last_known_timestamp(
this.serverTimeRepository.toServerTimestamp()
);
const needsUpdate = conversationLastRead < lastKnownTimestamp;
if (needsUpdate && this._isLastReceivedMessage(messageEntity, conversationEntity)) {
conversationEntity.setTimestamp(lastKnownTimestamp, Conversation.TIMESTAMP_TYPE.LAST_READ);
this.conversation_repository.markAsRead(conversationEntity);
}
}

handleClickOnMessage(messageEntity, event) {
const hasMentions = messageEntity.mentions().length;
const mentionElement = hasMentions && event.target.closest('.message-mention');
Expand Down

0 comments on commit 1ee04df

Please sign in to comment.