Skip to content

Commit 6d831c0

Browse files
authored
Merge pull request #5873 from nextcloud/bugfix/5872/fix-unread-messages-initial-scroll
Fix initial scroll of unread messages marker
2 parents 5f71df8 + 6ebddc1 commit 6d831c0

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/components/MessagesList/MessagesList.vue

+24-6
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ export default {
447447
token: this.token,
448448
id: this.conversation.lastReadMessage,
449449
})
450+
450451
if (this.$store.getters.getFirstKnownMessageId(this.token) === null) {
451452
// first time load, initialize important properties
452453
this.$store.dispatch('setFirstKnownMessageId', {
@@ -458,18 +459,31 @@ export default {
458459
id: this.conversation.lastReadMessage,
459460
})
460461

461-
// get history + new messages
462+
// get history before last read message
462463
await this.getOldMessages(true)
464+
// at this stage, the read marker will appear at the bottom of the view port since
465+
// we haven't fetched the messages that come after it yet
466+
// TODO: should we still show a spinner at this stage ?
463467
}
464468

465-
// focus on next tick to make sure the DOM elements
466-
// for known messages are already rendered
467-
this.$nextTick(() => {
468-
this.scrollToFocussedMessage()
469-
})
469+
let hasScrolled = false
470+
// if lookForNewMessages will long poll instead of returning existing messages,
471+
// scroll right away to avoid delays
472+
if (this.$store.getters.getLastKnownMessageId(this.token) === this.conversation.lastMessage.id) {
473+
hasScrolled = true
474+
await this.$nextTick(() => {
475+
this.scrollToFocussedMessage()
476+
})
477+
}
470478

471479
// get new messages
472480
await this.lookForNewMessages()
481+
482+
// don't scroll if lookForNewMessages was polling as we don't want
483+
// to scroll back to the read marker after receiving new messages later
484+
if (!hasScrolled) {
485+
this.scrollToFocussedMessage()
486+
}
473487
} else {
474488
this.$store.dispatch('cancelLookForNewMessages', { requestId: this.chatIdentifier })
475489
}
@@ -644,6 +658,7 @@ export default {
644658
* Also see updateReadMarkerPosition() for the backend update.
645659
*/
646660
refreshReadMarkerPosition() {
661+
console.debug('setVisualLastReadMessageId token=' + this.token + ' id=' + this.conversation.lastReadMessage)
647662
this.$store.dispatch('setVisualLastReadMessageId', {
648663
token: this.token,
649664
id: this.conversation.lastReadMessage,
@@ -669,6 +684,7 @@ export default {
669684

670685
// to fix issues, this scenario should not happen
671686
if (this.conversation.lastReadMessage === 0) {
687+
console.debug('clearLastReadMessage because lastReadMessage was 0 token=' + this.token)
672688
this.$store.dispatch('clearLastReadMessage', { token: this.token, updateVisually: true })
673689
return
674690
}
@@ -685,6 +701,7 @@ export default {
685701

686702
// if we're at bottom of the chat, then simply clear the marker
687703
if (this.isSticky) {
704+
console.debug('clearLastReadMessage because of isSticky token=' + this.token)
688705
this.$store.dispatch('clearLastReadMessage', { token: this.token })
689706
return
690707
}
@@ -708,6 +725,7 @@ export default {
708725

709726
// we don't update visually here, it will update the next time the
710727
// user focusses back on the conversation. See refreshReadMarkerPosition().
728+
console.debug('updateLastReadMessage token=' + this.token + ' messageId=' + messageId)
711729
this.$store.dispatch('updateLastReadMessage', { token: this.token, id: messageId, updateVisually: false })
712730
},
713731

0 commit comments

Comments
 (0)