From 5ddf7d7d1dfccb22ccc69a54e59ffe255211bce5 Mon Sep 17 00:00:00 2001 From: "Grigorii K. Shartsev" Date: Wed, 21 Jun 2023 21:05:37 +0200 Subject: [PATCH] perf(conversations): update in store only changed conversation on fetch Signed-off-by: Grigorii K. Shartsev --- src/store/conversationsStore.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/store/conversationsStore.js b/src/store/conversationsStore.js index 066119c4554e..ba1ebe2bd50b 100644 --- a/src/store/conversationsStore.js +++ b/src/store/conversationsStore.js @@ -203,7 +203,7 @@ const mutations = { const actions = { /** - * Add a conversation to the store and index the displayName. + * Add a conversation to the store and index the displayName * * @param {object} context default store context; * @param {object} conversation the conversation; @@ -211,6 +211,35 @@ const actions = { addConversation(context, conversation) { context.commit('addConversation', conversation) + context.dispatch('postAddConversation', conversation) + }, + + /** + * Add conversation to the store only, if it was changed + * + * @param {object} context dispatch context + * @param {object} payload mutation payload + * @param {object} payload.conversation the conversation + * @param {number|0} payload.modifiedSince timestamp of last state or 0 if unknown + */ + addConversationIfChanged(context, { conversation, modifiedSince }) { + // console.log(conversation.lastActivity, modifiedSince, modifiedSince && conversation.lastActivity >= modifiedSince) + if (conversation.lastActivity >= modifiedSince) { + context.commit('addConversation', conversation) + } + + context.dispatch('postAddConversation', conversation) + }, + + /** + * Post-actions after adding a conversation: + * - Get user status from 1-1 conversations + * - Add current user to the new conversation's participants + * + * @param {object} context dispatch context + * @param {object} conversation the conversation + */ + postAddConversation(context, conversation) { if (conversation.type === CONVERSATION.TYPE.ONE_TO_ONE && conversation.status) { emit('user_status:status.updated', { status: conversation.status, @@ -649,7 +678,7 @@ const actions = { dispatch('purgeConversationsStore') } response.data.ocs.data.forEach(conversation => { - dispatch('addConversation', conversation) + dispatch('addConversationIfChanged', { conversation, modifiedSince }) }) return response } catch (error) {