@@ -267,7 +267,9 @@ import {
267267 searchListedConversations ,
268268} from ' ../../services/conversationsService.js'
269269import { EventBus } from ' ../../services/EventBus.js'
270+ import { talkBroadcastChannel } from ' ../../services/talkBroadcastChannel.js'
270271import CancelableRequest from ' ../../utils/cancelableRequest.js'
272+ import { requestTabLeadership } from ' ../../utils/requestTabLeadership.js'
271273
272274export default {
273275
@@ -339,6 +341,8 @@ export default {
339341 preventFindingUnread: false ,
340342 roomListModifiedBefore: 0 ,
341343 forceFullRoomListRefreshAfterXLoops: 0 ,
344+ isFetchingConversations: false ,
345+ isCurrentTabLeader: false ,
342346 isFocused: false ,
343347 isFiltered: null ,
344348 }
@@ -423,17 +427,44 @@ export default {
423427 this .abortSearch ()
424428 })
425429
426- this .fetchConversations ()
427- },
430+ // Restore last fetched conversations from browser storage,
431+ // before updated ones come from server
432+ this .restoreConversations ()
428433
429- mounted () {
430- // Refreshes the conversations every 30 seconds
431- this .refreshTimer = window .setInterval (() => {
432- if (! this .isFetchingConversations ) {
434+ requestTabLeadership ().then (() => {
435+ this .isCurrentTabLeader = true
436+ this .fetchConversations ()
437+ // Refreshes the conversations list every 30 seconds
438+ this .refreshTimer = window .setInterval (() => {
433439 this .fetchConversations ()
440+ }, 30000 )
441+ })
442+
443+ talkBroadcastChannel .addEventListener (' message' , (event ) => {
444+ if (this .isCurrentTabLeader ) {
445+ switch (event .data .message ) {
446+ case ' force-fetch-all-conversations' :
447+ this .roomListModifiedBefore = 0
448+ this .debounceFetchConversations ()
449+ break
450+ }
451+ } else {
452+ switch (event .data .message ) {
453+ case ' update-conversations' :
454+ this .$store .dispatch (' patchConversations' , {
455+ conversations: event .data .conversations ,
456+ withRemoving: event .data .withRemoving ,
457+ })
458+ break
459+ case ' update-nextcloud-talk-hash' :
460+ this .$store .dispatch (' setNextcloudTalkHash' , event .data .hash )
461+ break
462+ }
434463 }
435- }, 30000 )
464+ })
465+ },
436466
467+ mounted () {
437468 EventBus .$on (' should-refresh-conversations' , this .handleShouldRefreshConversations )
438469 EventBus .$once (' conversations-received' , this .handleUnreadMention )
439470 EventBus .$on (' route-change' , this .onRouteChange )
@@ -623,7 +654,13 @@ export default {
623654 */
624655 async handleShouldRefreshConversations (options ) {
625656 if (options? .all === true ) {
626- this .roomListModifiedBefore = 0
657+ if (this .isCurrentTabLeader ) {
658+ this .roomListModifiedBefore = 0
659+ } else {
660+ // Force leader tab to do a full fetch
661+ talkBroadcastChannel .postMessage ({ message: ' force-fetch-all-conversations' })
662+ return
663+ }
627664 } else if (options? .token && options? .properties ) {
628665 await this .$store .dispatch (' setConversationProperties' , {
629666 token: options .token ,
@@ -635,12 +672,14 @@ export default {
635672 },
636673
637674 debounceFetchConversations: debounce (function () {
638- if (! this .isFetchingConversations ) {
639- this .fetchConversations ()
640- }
675+ this .fetchConversations ()
641676 }, 3000 ),
642677
643678 async fetchConversations () {
679+ if (this .isFetchingConversations ) {
680+ return
681+ }
682+
644683 this .isFetchingConversations = true
645684 if (this .forceFullRoomListRefreshAfterXLoops === 0 ) {
646685 this .roomListModifiedBefore = 0
@@ -673,16 +712,24 @@ export default {
673712 * Emits a global event that is used in App.vue to update the page title once the
674713 * ( if the current route is a conversation and once the conversations are received)
675714 */
676- EventBus .$emit (' conversations-received' , {
677- singleConversation: false ,
678- })
715+ EventBus .$emit (' conversations-received' , { singleConversation: false })
679716 this .isFetchingConversations = false
680717 } catch (error) {
681718 console .debug (' Error while fetching conversations: ' , error)
682719 this .isFetchingConversations = false
683720 }
684721 },
685722
723+ async restoreConversations () {
724+ try {
725+ await this .$store .dispatch (' restoreConversations' )
726+ this .initialisedConversations = true
727+ EventBus .$emit (' conversations-received' , { singleConversation: false })
728+ } catch (error) {
729+ console .debug (' Error while restoring conversations: ' , error)
730+ }
731+ },
732+
686733 // Checks whether the conversations list is scrolled all the way to the top
687734 // or not
688735 handleScroll () {
0 commit comments