From 8d6e0043417bcfc0d0b95f732c1ef1c65c579c84 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Wed, 28 Feb 2024 10:04:35 +0100 Subject: [PATCH] refactor(conversationsService): optimize functions, move error handling to store Signed-off-by: Maksim Sukharev --- src/components/LeftSidebar/LeftSidebar.vue | 10 +- .../NewConversationDialog.vue | 38 +-- src/services/conversationsService.js | 233 ++++++----------- src/services/conversationsService.spec.js | 5 + src/store/conversationsStore.js | 240 +++++++++++------- src/store/conversationsStore.spec.js | 16 +- 6 files changed, 263 insertions(+), 279 deletions(-) diff --git a/src/components/LeftSidebar/LeftSidebar.vue b/src/components/LeftSidebar/LeftSidebar.vue index d42421100e9..9113f201628 100644 --- a/src/components/LeftSidebar/LeftSidebar.vue +++ b/src/components/LeftSidebar/LeftSidebar.vue @@ -777,9 +777,13 @@ export default { }, async createConversation(name) { - const response = await createPrivateConversation(name) - const conversation = response.data.ocs.data - this.switchToConversation(conversation) + try { + const response = await createPrivateConversation(name) + const conversation = response.data.ocs.data + this.switchToConversation(conversation) + } catch (error) { + console.error('Error creating new private conversation: ', error) + } }, async restoreNoteToSelfConversation() { diff --git a/src/components/NewConversationDialog/NewConversationDialog.vue b/src/components/NewConversationDialog/NewConversationDialog.vue index 9812c91658c..2a8a81c08bc 100644 --- a/src/components/NewConversationDialog/NewConversationDialog.vue +++ b/src/components/NewConversationDialog/NewConversationDialog.vue @@ -308,7 +308,7 @@ export default { await this.createConversation(PRIVACY.PRIVATE) } } catch (exception) { - console.debug(exception) + console.error(exception) this.isLoading = false this.error = true // Stop the execution of the method on exceptions. @@ -321,7 +321,7 @@ export default { listable: this.listable, }) } catch (exception) { - console.debug(exception) + console.error(exception) this.isLoading = false this.error = true // Stop the execution of the method on exceptions. @@ -332,7 +332,7 @@ export default { try { await addParticipant(this.newConversation.token, participant.id, participant.source) } catch (exception) { - console.debug(exception) + console.error(exception) this.isLoading = false this.error = true // Stop the execution of the method on exceptions. @@ -360,20 +360,24 @@ export default { * @param {number} flag choose to send a request with private or public flag */ async createConversation(flag) { - let response - if (flag === PRIVACY.PRIVATE) { - response = await createPrivateConversation(this.conversationName) - } else if (flag === PRIVACY.PUBLIC) { - response = await createPublicConversation(this.conversationName) - } - const conversation = response.data.ocs.data - this.$store.dispatch('addConversation', conversation) - this.newConversation.token = conversation.token - if (this.isAvatarEdited) { - this.$refs.setupPage.$refs.conversationAvatar.saveAvatar() - } - if (this.newConversation.description) { - this.handleUpdateDescription() + try { + let response + if (flag === PRIVACY.PRIVATE) { + response = await createPrivateConversation(this.conversationName) + } else if (flag === PRIVACY.PUBLIC) { + response = await createPublicConversation(this.conversationName) + } + const conversation = response.data.ocs.data + this.$store.dispatch('addConversation', conversation) + this.newConversation.token = conversation.token + if (this.isAvatarEdited) { + this.$refs.setupPage.$refs.conversationAvatar.saveAvatar() + } + if (this.newConversation.description) { + this.handleUpdateDescription() + } + } catch (error) { + console.error('Error creating new conversation: ', error) } }, pushNewRoute() { diff --git a/src/services/conversationsService.js b/src/services/conversationsService.js index 4ae3dbdc513..2cd16af3311 100644 --- a/src/services/conversationsService.js +++ b/src/services/conversationsService.js @@ -35,7 +35,7 @@ const fetchConversations = async function(options) { options = options || {} options.params = options.params || {} options.params.includeStatus = true - return await axios.get(generateOcsUrl('apps/spreed/api/v4/room'), options) + return axios.get(generateOcsUrl('apps/spreed/api/v4/room'), options) } /** @@ -54,11 +54,12 @@ const fetchConversation = async function(token) { * @param {object} options options */ const searchListedConversations = async function({ searchText }, options) { - return axios.get(generateOcsUrl('apps/spreed/api/v4/listed-room'), Object.assign(options, { + return axios.get(generateOcsUrl('apps/spreed/api/v4/listed-room'), { + ...options, params: { searchTerm: searchText, }, - })) + }) } /** @@ -81,46 +82,36 @@ const fetchNoteToSelfConversation = async function() { const searchPossibleConversations = async function({ searchText, token, onlyUsers }, options) { token = token || 'new' onlyUsers = !!onlyUsers + const shareTypes = [ SHARE.TYPE.USER, - ] - - if (!onlyUsers) { - shareTypes.push(SHARE.TYPE.GROUP) - shareTypes.push(SHARE.TYPE.CIRCLE) - if (token !== 'new') { - shareTypes.push(SHARE.TYPE.EMAIL) - - if (loadState('spreed', 'federation_enabled')) { - shareTypes.push(SHARE.TYPE.REMOTE) - } - } - } - - return axios.get(generateOcsUrl('core/autocomplete/get'), Object.assign(options, { + !onlyUsers ? SHARE.TYPE.GROUP : null, + !onlyUsers ? SHARE.TYPE.CIRCLE : null, + (!onlyUsers && token !== 'new') ? SHARE.TYPE.EMAIL : null, + (!onlyUsers && token !== 'new' && loadState('spreed', 'federation_enabled')) ? SHARE.TYPE.REMOTE : null, + ].filter(type => type !== null) + + return axios.get(generateOcsUrl('core/autocomplete/get'), { + ...options, params: { search: searchText, itemType: 'call', itemId: token, shareTypes, }, - })) + }) } /** * Create a new one to one conversation with the specified user. * - * @param {string} userId The ID of the user with wich the new conversation will be opened. + * @param {string} userId The ID of the user with which the new conversation will be opened. */ const createOneToOneConversation = async function(userId) { - try { - return await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { - roomType: CONVERSATION.TYPE.ONE_TO_ONE, - invite: userId - }) - } catch (error) { - console.debug('Error creating new one to one conversation: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { + roomType: CONVERSATION.TYPE.ONE_TO_ONE, + invite: userId, + }) } /** @@ -130,15 +121,11 @@ const createOneToOneConversation = async function(userId) { * @param {string} source The source of the invite ID (defaults to groups) */ const createGroupConversation = async function(invite, source) { - try { - return await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { - roomType: CONVERSATION.TYPE.GROUP, - invite, - source: source || ATTENDEE.ACTOR_TYPE.GROUPS - }) - } catch (error) { - console.debug('Error creating new group conversation: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { + roomType: CONVERSATION.TYPE.GROUP, + invite, + source: source || ATTENDEE.ACTOR_TYPE.GROUPS, + }) } /** @@ -148,15 +135,11 @@ const createGroupConversation = async function(invite, source) { * @param {string} [objectType] The conversation object type */ const createPrivateConversation = async function(conversationName, objectType) { - try { - return await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { - roomType: CONVERSATION.TYPE.GROUP, - roomName: conversationName, - objectType, - }) - } catch (error) { - console.debug('Error creating new private conversation: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { + roomType: CONVERSATION.TYPE.GROUP, + roomName: conversationName, + objectType, + }) } /** @@ -166,15 +149,11 @@ const createPrivateConversation = async function(conversationName, objectType) { * @param {string} [objectType] The conversation object type */ const createPublicConversation = async function(conversationName, objectType) { - try { - return await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { - roomType: CONVERSATION.TYPE.PUBLIC, - roomName: conversationName, - objectType, - }) - } catch (error) { - console.debug('Error creating new public conversation: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { + roomType: CONVERSATION.TYPE.PUBLIC, + roomName: conversationName, + objectType, + }) } /** @@ -184,10 +163,9 @@ const createPublicConversation = async function(conversationName, objectType) { * @param {string} password the password to be set */ const setConversationPassword = async function(token, password) { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/password', { token }), { + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/password', { token }), { password, }) - return response } /** @@ -197,10 +175,21 @@ const setConversationPassword = async function(token, password) { * @param {string} name the name to be set */ const setConversationName = async function(token, name) { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}', { token }), { + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}', { token }), { roomName: name, }) - return response +} + +/** + * Set a conversation's description + * + * @param {string} token the conversation's token + * @param {string} description the description to be set + */ +const setConversationDescription = async function(token, description) { + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/description', { token }), { + description, + }) } /** @@ -209,12 +198,7 @@ const setConversationName = async function(token, name) { * @param {string} token The token of the conversation to be deleted. */ const deleteConversation = async function(token) { - try { - const response = await axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}', { token })) - return response - } catch (error) { - console.debug('Error while deleting the conversation: ', error) - } + return axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}', { token })) } /** @@ -223,8 +207,7 @@ const deleteConversation = async function(token) { * @param {string} token The token of the conversation to be deleted. */ const clearConversationHistory = async function(token) { - const response = await axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token })) - return response + return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}', { token })) } /** @@ -233,12 +216,7 @@ const clearConversationHistory = async function(token) { * @param {string} token The token of the conversation to be set as unread */ const setConversationUnread = async function(token) { - try { - const response = axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}/read', { token })) - return response - } catch (error) { - console.debug('Error while setting the conversation as unread: ', error) - } + return axios.delete(generateOcsUrl('apps/spreed/api/v1/chat/{token}/read', { token })) } /** @@ -247,12 +225,7 @@ const setConversationUnread = async function(token) { * @param {string} token The token of the conversation to be favorites */ const addToFavorites = async function(token) { - try { - const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/favorite', { token })) - return response - } catch (error) { - console.debug('Error while adding the conversation to favorites: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/favorite', { token })) } /** @@ -261,12 +234,7 @@ const addToFavorites = async function(token) { * @param {string} token The token of the conversation to be removed from favorites */ const removeFromFavorites = async function(token) { - try { - const response = await axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}/favorite', { token })) - return response - } catch (error) { - console.debug('Error while removing the conversation from favorites: ', error) - } + return axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}/favorite', { token })) } /** @@ -276,12 +244,7 @@ const removeFromFavorites = async function(token) { * @param {number} level The notification level to set. */ const setNotificationLevel = async function(token, level) { - try { - const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/notify', { token }), { level }) - return response - } catch (error) { - console.debug('Error while setting the notification level: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/notify', { token }), { level }) } /** @@ -291,12 +254,7 @@ const setNotificationLevel = async function(token, level) { * @param {number} level The call notification level. */ const setNotificationCalls = async function(token, level) { - try { - const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/notify-calls', { token }), { level }) - return response - } catch (error) { - console.debug('Error while setting the call notification level: ', error) - } + return axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/notify-calls', { token }), { level }) } /** @@ -304,13 +262,8 @@ const setNotificationCalls = async function(token, level) { * * @param {string} token The token of the conversation to be removed from favorites */ -const makePublic = async function(token) { - try { - const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/public', { token })) - return response - } catch (error) { - console.debug('Error while making the conversation public: ', error) - } +const makeConversationPublic = async function(token) { + return axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/public', { token })) } /** @@ -318,13 +271,8 @@ const makePublic = async function(token) { * * @param {string} token The token of the conversation to be removed from favorites */ -const makePrivate = async function(token) { - try { - const response = await axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}/public', { token })) - return response - } catch (error) { - console.debug('Error while making the conversation private: ', error) - } +const makeConversationPrivate = async function(token) { + return axios.delete(generateOcsUrl('apps/spreed/api/v4/room/{token}/public', { token })) } /** @@ -356,18 +304,13 @@ const setRecordingConsent = async function(token, newState) { * * @param {string} token The token of the conversation to be modified * @param {number} newState The new lobby state to set - * @param {number} timestamp The UNIX timestamp (in seconds) to set, if any + * @param {number} [timestamp] The UNIX timestamp (in seconds) to set, if any */ const changeLobbyState = async function(token, newState, timestamp) { - try { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/webinar/lobby', { token }), { - state: newState, - timer: timestamp, - }) - return response - } catch (error) { - console.debug('Error while updating webinar lobby: ', error) - } + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/webinar/lobby', { token }), { + state: newState, + timer: timestamp, + }) } /** @@ -377,14 +320,9 @@ const changeLobbyState = async function(token, newState, timestamp) { * @param {number} readOnly The new read-only state to set */ const changeReadOnlyState = async function(token, readOnly) { - try { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/read-only', { token }), { - state: readOnly, - }) - return response - } catch (error) { - console.debug('Error while updating read-only state: ', error) - } + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/read-only', { token }), { + state: readOnly, + }) } /** @@ -394,17 +332,9 @@ const changeReadOnlyState = async function(token, readOnly) { * @param {number} listable The new listable scope to set */ const changeListable = async function(token, listable) { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/listable', { token }), { + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/listable', { token }), { scope: listable, }) - return response -} - -const setConversationDescription = async function(token, description) { - const response = await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/description', { token }), { - description, - }) - return response } /** @@ -416,10 +346,9 @@ const setConversationDescription = async function(token, description) { * 'PUBLISH_AUDIO', 'PUBLISH_VIDEO', 'PUBLISH_SCREEN'. */ const setConversationPermissions = async (token, permissions) => { - await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/permissions/default', { token }), - { - permissions, - }) + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/permissions/default', { token }), { + permissions, + }) } /** @@ -432,10 +361,9 @@ const setConversationPermissions = async (token, permissions) => { * 'PUBLISH_AUDIO', 'PUBLISH_VIDEO', 'PUBLISH_SCREEN'. */ const setCallPermissions = async (token, permissions) => { - await axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/permissions/call', { token }), - { - permissions, - }) + return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/permissions/call', { token }), { + permissions, + }) } /** @@ -445,17 +373,11 @@ const setCallPermissions = async (token, permissions) => { * @param {number} seconds the seconds for the message expiration, 0 to disable */ const setMessageExpiration = async (token, seconds) => { - return await axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/message-expiration', { token }), { + return axios.post(generateOcsUrl('apps/spreed/api/v4/room/{token}/message-expiration', { token }), { seconds, }) } -const validatePassword = async (password) => { - return await axios.post(generateOcsUrl('apps/password_policy/api/v1/validate'), { - password, - }) -} - const setConversationAvatar = async function(token, file) { return axios.post(generateOcsUrl('apps/spreed/api/v1/room/{token}/avatar', { token }), file,) } @@ -483,8 +405,8 @@ export { removeFromFavorites, setNotificationLevel, setNotificationCalls, - makePublic, - makePrivate, + makeConversationPublic, + makeConversationPrivate, setSIPEnabled, setRecordingConsent, changeLobbyState, @@ -498,7 +420,6 @@ export { setConversationPermissions, setCallPermissions, setMessageExpiration, - validatePassword, setConversationAvatar, setConversationEmojiAvatar, deleteConversationAvatar, diff --git a/src/services/conversationsService.spec.js b/src/services/conversationsService.spec.js index 987b6b60dcc..8d2bfa8fc5b 100644 --- a/src/services/conversationsService.spec.js +++ b/src/services/conversationsService.spec.js @@ -71,6 +71,10 @@ describe('conversationsService', () => { }) test('searchPossibleConversations with other share types', () => { + loadStateSettings = { + federation_enabled: true, + } + testSearchPossibleConversations( 'conversation-token', false, @@ -79,6 +83,7 @@ describe('conversationsService', () => { SHARE.TYPE.GROUP, SHARE.TYPE.CIRCLE, SHARE.TYPE.EMAIL, + SHARE.TYPE.REMOTE, ], ) }) diff --git a/src/store/conversationsStore.js b/src/store/conversationsStore.js index f7a968c7627..2449da768c1 100644 --- a/src/store/conversationsStore.js +++ b/src/store/conversationsStore.js @@ -34,8 +34,8 @@ import { } from '../constants.js' import BrowserStorage from '../services/BrowserStorage.js' import { - makePublic, - makePrivate, + makeConversationPublic, + makeConversationPrivate, setSIPEnabled, setRecordingConsent, changeLobbyState, @@ -443,10 +443,14 @@ const actions = { * @param {string} data.token the token of the conversation to be deleted; */ async deleteConversationFromServer(context, { token }) { - await deleteConversation(token) - // upon success, also delete from store - await context.dispatch('deleteConversation', token) - talkBroadcastChannel.postMessage({ message: 'force-fetch-all-conversations' }) + try { + await deleteConversation(token) + // upon success, also delete from store + await context.dispatch('deleteConversation', token) + talkBroadcastChannel.postMessage({ message: 'force-fetch-all-conversations' }) + } catch (error) { + console.error('Error while deleting the conversation: ', error) + } }, /** @@ -467,9 +471,7 @@ const actions = { context.dispatch('purgeMessagesStore', token) return response } catch (error) { - console.debug( - t('spreed', 'Error while clearing conversation history'), - error) + console.error(t('spreed', 'Error while clearing conversation history'), error) } }, @@ -478,16 +480,19 @@ const actions = { return } - const conversation = Object.assign({}, getters.conversations[token]) - if (allowGuests) { - await makePublic(token) - conversation.type = CONVERSATION.TYPE.PUBLIC - } else { - await makePrivate(token) - conversation.type = CONVERSATION.TYPE.GROUP + try { + const conversation = Object.assign({}, getters.conversations[token]) + if (allowGuests) { + await makeConversationPublic(token) + conversation.type = CONVERSATION.TYPE.PUBLIC + } else { + await makeConversationPrivate(token) + conversation.type = CONVERSATION.TYPE.GROUP + } + commit('addConversation', conversation) + } catch (error) { + console.error('Error while changing the conversation public status: ', error) } - - commit('addConversation', conversation) }, async toggleFavorite({ commit, getters }, { token, isFavorite }) { @@ -495,16 +500,19 @@ const actions = { return } - // FIXME: logic is reversed - if (isFavorite) { - await removeFromFavorites(token) - } else { - await addToFavorites(token) - } + try { + if (isFavorite) { + await removeFromFavorites(token) + } else { + await addToFavorites(token) + } - const conversation = Object.assign({}, getters.conversations[token], { isFavorite: !isFavorite }) + const conversation = Object.assign({}, getters.conversations[token], { isFavorite: !isFavorite }) - commit('addConversation', conversation) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while changing the conversation favorite status: ', error) + } }, async toggleLobby({ commit, getters }, { token, enableLobby }) { @@ -512,16 +520,19 @@ const actions = { return } - const conversation = Object.assign({}, getters.conversations[token]) - if (enableLobby) { - await changeLobbyState(token, WEBINAR.LOBBY.NON_MODERATORS) - conversation.lobbyState = WEBINAR.LOBBY.NON_MODERATORS - } else { - await changeLobbyState(token, WEBINAR.LOBBY.NONE) - conversation.lobbyState = WEBINAR.LOBBY.NONE + try { + const conversation = Object.assign({}, getters.conversations[token]) + if (enableLobby) { + await changeLobbyState(token, WEBINAR.LOBBY.NON_MODERATORS) + conversation.lobbyState = WEBINAR.LOBBY.NON_MODERATORS + } else { + await changeLobbyState(token, WEBINAR.LOBBY.NONE) + conversation.lobbyState = WEBINAR.LOBBY.NONE + } + commit('addConversation', conversation) + } catch (error) { + console.error('Error while updating webinar lobby: ', error) } - - commit('addConversation', conversation) }, async setConversationName({ commit, getters }, { token, name }) { @@ -529,37 +540,44 @@ const actions = { return } - await setConversationName(token, name) - - const conversation = Object.assign({}, getters.conversations[token], { displayName: name }) - - commit('addConversation', conversation) + try { + await setConversationName(token, name) + const conversation = Object.assign({}, getters.conversations[token], { displayName: name }) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while setting a name for conversation: ', error) + } }, async setConversationDescription({ commit }, { token, description }) { - await setConversationDescription(token, description) - commit('setConversationDescription', { token, description }) + try { + await setConversationDescription(token, description) + commit('setConversationDescription', { token, description }) + } catch (error) { + console.error('Error while setting a description for conversation: ', error) + } }, async setConversationPassword({ commit }, { token, newPassword }) { - await setConversationPassword(token, newPassword) - - commit('setConversationHasPassword', { - token, - hasPassword: !!newPassword, - }) + try { + await setConversationPassword(token, newPassword) + commit('setConversationHasPassword', { token, hasPassword: !!newPassword }) + } catch (error) { + console.error('Error while setting a password for conversation: ', error) + } }, async setReadOnlyState({ commit, getters }, { token, readOnly }) { if (!getters.conversations[token]) { return } - - await changeReadOnlyState(token, readOnly) - - const conversation = Object.assign({}, getters.conversations[token], { readOnly }) - - commit('addConversation', conversation) + try { + await changeReadOnlyState(token, readOnly) + const conversation = Object.assign({}, getters.conversations[token], { readOnly }) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while updating read-only state: ', error) + } }, async setListable({ commit, getters }, { token, listable }) { @@ -567,11 +585,13 @@ const actions = { return } - await changeListable(token, listable) - - const conversation = Object.assign({}, getters.conversations[token], { listable }) - - commit('addConversation', conversation) + try { + await changeListable(token, listable) + const conversation = Object.assign({}, getters.conversations[token], { listable }) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while updating listable state: ', error) + } }, async setLobbyTimer({ commit, getters }, { token, timestamp }) { @@ -579,12 +599,14 @@ const actions = { return } - const conversation = Object.assign({}, getters.conversations[token], { lobbyTimer: timestamp }) - - // The backend requires the state and timestamp to be set together. - await changeLobbyState(token, conversation.lobbyState, timestamp) - - commit('addConversation', conversation) + try { + const conversation = Object.assign({}, getters.conversations[token], { lobbyTimer: timestamp }) + // The backend requires the state and timestamp to be set together. + await changeLobbyState(token, conversation.lobbyState, timestamp) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while updating webinar lobby: ', error) + } }, async setSIPEnabled({ commit, getters }, { token, state }) { @@ -592,11 +614,13 @@ const actions = { return } - await setSIPEnabled(token, state) - - const conversation = Object.assign({}, getters.conversations[token], { sipEnabled: state }) - - commit('addConversation', conversation) + try { + await setSIPEnabled(token, state) + const conversation = Object.assign({}, getters.conversations[token], { sipEnabled: state }) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while changing the SIP state for conversation: ', error) + } }, async setRecordingConsent({ commit, getters }, { token, state }) { @@ -604,11 +628,13 @@ const actions = { return } - await setRecordingConsent(token, state) - - const conversation = Object.assign({}, getters.conversations[token], { recordingConsent: state }) - - commit('addConversation', conversation) + try { + await setRecordingConsent(token, state) + const conversation = Object.assign({}, getters.conversations[token], { recordingConsent: state }) + commit('addConversation', conversation) + } catch (error) { + console.error('Error while changing the recording consent state for conversation: ', error) + } }, async setConversationProperties({ commit, getters }, { token, properties }) { @@ -634,9 +660,13 @@ const actions = { return } - await setConversationUnread(token) - commit('updateUnreadMessages', { token, unreadMessages: 1 }) - await dispatch('fetchConversation', { token }) + try { + await setConversationUnread(token) + commit('updateUnreadMessages', { token, unreadMessages: 1 }) + await dispatch('fetchConversation', { token }) + } catch (error) { + console.error('Error while setting the conversation as unread: ', error) + } }, async updateLastCommonReadMessage({ commit, getters }, { token, lastCommonReadMessage }) { @@ -866,15 +896,21 @@ const actions = { }, async setNotificationLevel({ commit }, { token, notificationLevel }) { - await setNotificationLevel(token, notificationLevel) - - commit('setNotificationLevel', { token, notificationLevel }) + try { + await setNotificationLevel(token, notificationLevel) + commit('setNotificationLevel', { token, notificationLevel }) + } catch (error) { + console.error('Error while setting the notification level: ', error) + } }, async setNotificationCalls({ commit }, { token, notificationCalls }) { - await setNotificationCalls(token, notificationCalls) - - commit('setNotificationCalls', { token, notificationCalls }) + try { + await setNotificationCalls(token, notificationCalls) + commit('setNotificationCalls', { token, notificationCalls }) + } catch (error) { + console.error('Error while setting the call notification level: ', error) + } }, /** @@ -885,26 +921,40 @@ const actions = { * @param {string} actorId actor id; */ async createOneToOneConversation(context, actorId) { - const response = await createOneToOneConversation(actorId) - const conversation = response.data.ocs.data - context.dispatch('addConversation', conversation) - - return conversation + try { + const response = await createOneToOneConversation(actorId) + await context.dispatch('addConversation', response.data.ocs.data) + return response.data.ocs.data + } catch (error) { + console.error('Error creating new one to one conversation: ', error) + } }, async setConversationPermissions(context, { token, permissions }) { - await setConversationPermissions(token, permissions) - context.commit('setConversationPermissions', { token, permissions }) + try { + await setConversationPermissions(token, permissions) + context.commit('setConversationPermissions', { token, permissions }) + } catch (error) { + console.error('Error while updating conversation permissions: ', error) + } }, async setMessageExpiration({ commit }, { token, seconds }) { - await setMessageExpiration(token, seconds) - commit('setMessageExpiration', { token, seconds }) + try { + await setMessageExpiration(token, seconds) + commit('setMessageExpiration', { token, seconds }) + } catch (error) { + console.error('Error while setting conversation message expiration: ', error) + } }, async setCallPermissions(context, { token, permissions }) { - await setCallPermissions(token, permissions) - context.commit('setCallPermissions', { token, permissions }) + try { + await setCallPermissions(token, permissions) + context.commit('setCallPermissions', { token, permissions }) + } catch (error) { + console.error('Error while updating call permissions: ', error) + } }, async startCallRecording(context, { token, callRecording }) { diff --git a/src/store/conversationsStore.spec.js b/src/store/conversationsStore.spec.js index 40977145ba3..80f56048bdd 100644 --- a/src/store/conversationsStore.spec.js +++ b/src/store/conversationsStore.spec.js @@ -15,8 +15,8 @@ import { } from '../constants.js' import BrowserStorage from '../services/BrowserStorage.js' import { - makePublic, - makePrivate, + makeConversationPublic, + makeConversationPrivate, addToFavorites, removeFromFavorites, changeLobbyState, @@ -38,8 +38,8 @@ import { useTalkHashStore } from '../stores/talkHash.js' import { generateOCSErrorResponse, generateOCSResponse } from '../test-helpers.js' jest.mock('../services/conversationsService', () => ({ - makePublic: jest.fn(), - makePrivate: jest.fn(), + makeConversationPublic: jest.fn(), + makeConversationPrivate: jest.fn(), addToFavorites: jest.fn(), removeFromFavorites: jest.fn(), changeLobbyState: jest.fn(), @@ -643,14 +643,14 @@ describe('conversationsStore', () => { store.dispatch('addConversation', testConversation) - makePublic.mockResolvedValue() + makeConversationPublic.mockResolvedValue() await store.dispatch('toggleGuests', { token: testToken, allowGuests: true, }) - expect(makePublic).toHaveBeenCalledWith(testToken) + expect(makeConversationPublic).toHaveBeenCalledWith(testToken) const changedConversation = store.getters.conversation(testToken) expect(changedConversation.type).toEqual(CONVERSATION.TYPE.PUBLIC) @@ -661,14 +661,14 @@ describe('conversationsStore', () => { store.dispatch('addConversation', testConversation) - makePrivate.mockResolvedValue() + makeConversationPrivate.mockResolvedValue() await store.dispatch('toggleGuests', { token: testToken, allowGuests: false, }) - expect(makePrivate).toHaveBeenCalledWith(testToken) + expect(makeConversationPrivate).toHaveBeenCalledWith(testToken) const changedConversation = store.getters.conversation(testToken) expect(changedConversation.type).toEqual(CONVERSATION.TYPE.GROUP)