From ae0d38314eeca0f0b89ec0dd7a19825632727ca3 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Tue, 22 Aug 2023 10:55:24 +0200 Subject: [PATCH] add store action to clear all messages before 'history_cleared' Signed-off-by: Maksim Sukharev --- src/store/messagesStore.js | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/store/messagesStore.js b/src/store/messagesStore.js index 49e5c3b0680..8c38b187b69 100644 --- a/src/store/messagesStore.js +++ b/src/store/messagesStore.js @@ -366,6 +366,31 @@ const mutations = { } }, + /** + * Clears the messages entry from the store for the given conversation token + * starting from defined id. + * + * @param {object} state current store state + * @param {object} payload payload; + * @param {string} payload.token the token of the conversation to be cleared; + * @param {number} payload.id the id of the message to be the first one after clear; + */ + clearMessagesHistory(state, { token, id }) { + Vue.set(state.firstKnown, token, id) + + if (state.visualLastReadMessageId[token] && state.visualLastReadMessageId[token] < id) { + Vue.set(state.visualLastReadMessageId, token, id) + } + + if (state.messages[token]) { + for (const messageId of Object.keys(state.messages[token])) { + if (messageId < id) { + Vue.delete(state.messages[token], messageId) + } + } + } + }, + // Increases reaction count for a particular reaction on a message addReactionToMessage(state, { token, messageId, reaction }) { if (!state.messages[token][messageId].reactions[reaction]) { @@ -486,6 +511,13 @@ const actions = { }) } + if (message.systemMessage === 'history_cleared') { + context.commit('clearMessagesHistory', { + token: message.token, + id: message.id, + }) + } + // Filter out some system messages if (message.systemMessage !== 'reaction' && message.systemMessage !== 'reaction_deleted' @@ -673,6 +705,18 @@ const actions = { context.commit('deleteMessages', token) }, + /** + * Clear all messages before defined id from the store only. + * + * @param {object} context default store context; + * @param {object} payload payload; + * @param {string} payload.token the token of the conversation to be cleared; + * @param {number} payload.id the id of the message to be the first one after clear; + */ + clearMessagesHistory(context, { token, id }) { + context.commit('clearMessagesHistory', { token, id }) + }, + /** * Clears the last read message marker by moving it to the last message * in the conversation.