From 17eb4c745b1a42f29c2961ac5940bf7ed54f94fe Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Tue, 10 Dec 2024 12:45:46 +0100 Subject: [PATCH] fix: migrate settingsService to TS Signed-off-by: Maksim Sukharev --- src/components/AdminSettings/SIPBridge.vue | 10 +- .../SettingsDialog/SettingsDialog.vue | 2 +- src/services/settingsService.js | 107 ----------------- src/services/settingsService.ts | 111 ++++++++++++++++++ src/store/fileUploadStore.js | 2 +- src/store/fileUploadStore.spec.js | 2 +- src/stores/__tests__/settings.spec.js | 2 +- src/stores/settings.js | 2 +- src/stores/sounds.js | 2 +- src/types/index.ts | 10 ++ 10 files changed, 134 insertions(+), 116 deletions(-) delete mode 100644 src/services/settingsService.js create mode 100644 src/services/settingsService.ts diff --git a/src/components/AdminSettings/SIPBridge.vue b/src/components/AdminSettings/SIPBridge.vue index cf7cb4affd0..93084994eac 100644 --- a/src/components/AdminSettings/SIPBridge.vue +++ b/src/components/AdminSettings/SIPBridge.vue @@ -95,7 +95,7 @@ import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js' import NcTextArea from '@nextcloud/vue/dist/Components/NcTextArea.js' import { EventBus } from '../../services/EventBus.ts' -import { setSIPSettings } from '../../services/settingsService.js' +import { setSIPSettings } from '../../services/settingsService.ts' import { getWelcomeMessage } from '../../services/signalingService.js' export default { @@ -194,11 +194,15 @@ export default { this.loading = true this.saveLabel = t('spreed', 'Saving …') - const groups = this.sipGroups.map(group => { + const sipGroups = this.sipGroups.map(group => { return group.id }) - await setSIPSettings(groups, this.sharedSecret, this.dialInInfo) + await setSIPSettings({ + sipGroups, + sharedSecret: this.sharedSecret, + dialInInfo: this.dialInInfo, + }) if (this.currentSetup.dialOutEnabled !== this.dialOutEnabled) { await OCP.AppConfig.setValue('spreed', 'sip_dialout', this.dialOutEnabled ? 'yes' : 'no') } diff --git a/src/components/SettingsDialog/SettingsDialog.vue b/src/components/SettingsDialog/SettingsDialog.vue index efb01fa1b1b..5512d8cb099 100644 --- a/src/components/SettingsDialog/SettingsDialog.vue +++ b/src/components/SettingsDialog/SettingsDialog.vue @@ -216,7 +216,7 @@ import { PRIVACY } from '../../constants.js' import BrowserStorage from '../../services/BrowserStorage.js' import { getTalkConfig } from '../../services/CapabilitiesManager.ts' import { useCustomSettings } from '../../services/SettingsAPI.ts' -import { setUserConfig } from '../../services/settingsService.js' +import { setUserConfig } from '../../services/settingsService.ts' import { useSettingsStore } from '../../stores/settings.js' import { useSoundsStore } from '../../stores/sounds.js' import { isMac } from '../../utils/browserCheck.ts' diff --git a/src/services/settingsService.js b/src/services/settingsService.js deleted file mode 100644 index b0f92149f16..00000000000 --- a/src/services/settingsService.js +++ /dev/null @@ -1,107 +0,0 @@ -/** - * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -import axios from '@nextcloud/axios' -import { generateOcsUrl } from '@nextcloud/router' - -import BrowserStorage from './BrowserStorage.js' - -/** - * Sets the attachment folder setting for the user - * - * @param {string} path The name of the folder - * @return {object} The axios response - */ -const setAttachmentFolder = async function(path) { - return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { - key: 'attachment_folder', - value: path, - }) -} - -/** - * Sets the read status privacy setting for the user - * - * @param {number} privacy The selected value, either 0 or 1 - * @return {object} The axios response - */ -const setReadStatusPrivacy = async function(privacy) { - return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { - key: 'read_status_privacy', - value: privacy, - }) -} - -/** - * Sets the typing status privacy setting for the user - * - * @param {number} privacy The selected value, either 0 or 1 - * @return {object} The axios response - */ -const setTypingStatusPrivacy = async function(privacy) { - return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { - key: 'typing_privacy', - value: privacy, - }) -} - -/** - * Save the SIP settings - * - * @param {Array} sipGroups The groups allowed to enable SIP on a conversation - * @param {string} sharedSecret The shared secret which is used by the SIP server to authenticate - * @param {string} dialInInfo The dial-in Information displayed in the email and sidebar - * @return {object} The axios response - */ -const setSIPSettings = async function(sipGroups, sharedSecret, dialInInfo) { - return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/sip'), { - sipGroups, - sharedSecret, - dialInInfo, - }) -} - -const setPlaySounds = async function(hasUserAccount, value) { - if (hasUserAccount) { - await axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { - key: 'play_sounds', - value, - }) - } else { - BrowserStorage.setItem('play_sounds', value) - } -} - -const setStartWithoutMedia = async function(value) { - await setUserConfig('spreed', 'calls_start_without_media', value ? 'yes' : 'no') -} - -const setBlurVirtualBackground = async function(value) { - await setUserConfig('spreed', 'blur_virtual_background', value ? 'yes' : 'no') -} - -/** - * Set user config using provisioning API - * - * @param {string} appId - app id - * @param {string} configKey - key of the config to set - * @param {string} configValue - value to set - */ -const setUserConfig = async function(appId, configKey, configValue) { - await axios.post(generateOcsUrl('apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', { appId, configKey }), { - configValue, - }) -} - -export { - setAttachmentFolder, - setBlurVirtualBackground, - setReadStatusPrivacy, - setTypingStatusPrivacy, - setSIPSettings, - setPlaySounds, - setStartWithoutMedia, - setUserConfig, -} diff --git a/src/services/settingsService.ts b/src/services/settingsService.ts new file mode 100644 index 00000000000..174cadcbacb --- /dev/null +++ b/src/services/settingsService.ts @@ -0,0 +1,111 @@ +/** + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import axios from '@nextcloud/axios' +import { generateOcsUrl } from '@nextcloud/router' + +import BrowserStorage from './BrowserStorage.js' +import type { + setSipSettingsParams, + setSipSettingsResponse, + setUserSettingsParams, + setUserSettingsResponse, + UserPreferencesResponse, +} from '../types/index.ts' + +/** + * Sets the attachment folder setting for the user + * + * @param path The name of the folder + */ +const setAttachmentFolder = async function(path: string): setUserSettingsResponse { + return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { + key: 'attachment_folder', + value: path, + } as setUserSettingsParams) +} + +/** + * Sets the read status privacy setting for the user + * + * @param privacy The selected value, either 0 or 1 + */ +const setReadStatusPrivacy = async function(privacy: number): setUserSettingsResponse { + return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { + key: 'read_status_privacy', + value: privacy, + } as setUserSettingsParams) +} + +/** + * Sets the typing status privacy setting for the user + * + * @param privacy The selected value, either 0 or 1 + */ +const setTypingStatusPrivacy = async function(privacy: number): setUserSettingsResponse { + return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { + key: 'typing_privacy', + value: privacy, + } as setUserSettingsParams) +} + +/** + * Save the SIP settings + * + * @param payload payload + * @param payload.sipGroups The groups allowed to enable SIP on a conversation + * @param payload.sharedSecret The shared secret which is used by the SIP server to authenticate + * @param payload.dialInInfo The dial-in Information displayed in the email and sidebar + */ +const setSIPSettings = async function({ sipGroups, sharedSecret, dialInInfo }: setSipSettingsParams): setSipSettingsResponse { + return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/sip'), { + sipGroups, + sharedSecret, + dialInInfo, + } as setSipSettingsParams) +} + +const setPlaySounds = async function(hasUserAccount: boolean, value: 'yes'|'no') { + if (hasUserAccount) { + return axios.post(generateOcsUrl('apps/spreed/api/v1/settings/user'), { + key: 'play_sounds', + value, + }) + } else { + BrowserStorage.setItem('play_sounds', value) + } +} + +const setStartWithoutMedia = async function(value: boolean) { + return setUserConfig('spreed', 'calls_start_without_media', value ? 'yes' : 'no') +} + +const setBlurVirtualBackground = async function(value: boolean) { + return setUserConfig('spreed', 'blur_virtual_background', value ? 'yes' : 'no') +} + +/** + * Set user config using provisioning API + * + * @param appId - app id + * @param configKey - key of the config to set + * @param configValue - value to set + */ +const setUserConfig = async function(appId: string, configKey: string, configValue: string): UserPreferencesResponse { + return axios.post(generateOcsUrl('apps/provisioning_api/api/v1/config/users/{appId}/{configKey}', { appId, configKey }), { + configValue, + }) +} + +export { + setAttachmentFolder, + setBlurVirtualBackground, + setReadStatusPrivacy, + setTypingStatusPrivacy, + setSIPSettings, + setPlaySounds, + setStartWithoutMedia, + setUserConfig, +} diff --git a/src/store/fileUploadStore.js b/src/store/fileUploadStore.js index 754d65cd37a..6f6063bd876 100644 --- a/src/store/fileUploadStore.js +++ b/src/store/fileUploadStore.js @@ -19,7 +19,7 @@ import { getFileTemplates, shareFile, } from '../services/filesSharingServices.js' -import { setAttachmentFolder } from '../services/settingsService.js' +import { setAttachmentFolder } from '../services/settingsService.ts' import { useChatExtrasStore } from '../stores/chatExtras.js' import { hasDuplicateUploadNames, diff --git a/src/store/fileUploadStore.spec.js b/src/store/fileUploadStore.spec.js index 37e75ab8332..735e626b0eb 100644 --- a/src/store/fileUploadStore.spec.js +++ b/src/store/fileUploadStore.spec.js @@ -17,7 +17,7 @@ import storeConfig from './storeConfig.js' import fileUploadStore from './fileUploadStore.js' import { getDavClient } from '../services/DavClient.js' import { shareFile } from '../services/filesSharingServices.js' -import { setAttachmentFolder } from '../services/settingsService.js' +import { setAttachmentFolder } from '../services/settingsService.ts' import { findUniquePath } from '../utils/fileUpload.js' jest.mock('../services/DavClient', () => ({ diff --git a/src/stores/__tests__/settings.spec.js b/src/stores/__tests__/settings.spec.js index f0a2eee7c68..7556b5e2e08 100644 --- a/src/stores/__tests__/settings.spec.js +++ b/src/stores/__tests__/settings.spec.js @@ -8,7 +8,7 @@ import { loadState } from '@nextcloud/initial-state' import { PRIVACY } from '../../constants.js' import BrowserStorage from '../../services/BrowserStorage.js' -import { setReadStatusPrivacy, setTypingStatusPrivacy } from '../../services/settingsService.js' +import { setReadStatusPrivacy, setTypingStatusPrivacy } from '../../services/settingsService.ts' import { generateOCSResponse } from '../../test-helpers.js' import { useSettingsStore } from '../settings.js' diff --git a/src/stores/settings.js b/src/stores/settings.js index 9c3e37aaf63..dc8e9e4ee0b 100644 --- a/src/stores/settings.js +++ b/src/stores/settings.js @@ -16,7 +16,7 @@ import { setTypingStatusPrivacy, setStartWithoutMedia, setBlurVirtualBackground, -} from '../services/settingsService.js' +} from '../services/settingsService.ts' /** * @typedef {string} Token diff --git a/src/stores/sounds.js b/src/stores/sounds.js index e7599499754..e97cee048f3 100644 --- a/src/stores/sounds.js +++ b/src/stores/sounds.js @@ -11,7 +11,7 @@ import { loadState } from '@nextcloud/initial-state' import { generateFilePath } from '@nextcloud/router' import BrowserStorage from '../services/BrowserStorage.js' -import { setPlaySounds } from '../services/settingsService.js' +import { setPlaySounds } from '../services/settingsService.ts' const hasUserAccount = Boolean(getCurrentUser()?.uid) /** diff --git a/src/types/index.ts b/src/types/index.ts index 7e3f6178b61..37f7269ab12 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -245,3 +245,13 @@ export type OutOfOfficeResponse = ApiResponseUnwrapped<{ replacementUserDisplayName?: string|null, } }> + +// User preferences response +// from https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-user-preferences-api.html +export type UserPreferencesResponse = ApiResponseUnwrapped + +// Settings +export type setSipSettingsParams = Required['requestBody']['content']['application/json'] +export type setSipSettingsResponse = ApiResponse +export type setUserSettingsParams = Required['requestBody']['content']['application/json'] +export type setUserSettingsResponse = ApiResponse