From db182d6517f6a8086bd7e6bc6950f13f404adde2 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Thu, 22 Jul 2021 00:05:01 +0000 Subject: [PATCH] Create display name service and update constants Signed-off-by: Christopher Ng --- .../src/constants/AccountPropertyConstants.js | 46 ++++++++++--- .../PersonalInfo/DisplayNameService.js | 68 +++++++++++++++++++ .../EmailService.js} | 14 ++-- 3 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 apps/settings/src/service/PersonalInfo/DisplayNameService.js rename apps/settings/src/service/{PersonalInfoService.js => PersonalInfo/EmailService.js} (94%) diff --git a/apps/settings/src/constants/AccountPropertyConstants.js b/apps/settings/src/constants/AccountPropertyConstants.js index 61e45be9b22..a1c2b4814ca 100644 --- a/apps/settings/src/constants/AccountPropertyConstants.js +++ b/apps/settings/src/constants/AccountPropertyConstants.js @@ -26,24 +26,48 @@ /** Enum of account properties */ export const ACCOUNT_PROPERTY_ENUM = Object.freeze({ + ADDRESS: 'address', AVATAR: 'avatar', DISPLAYNAME: 'displayname', - PHONE: 'phone', EMAIL: 'email', - WEBSITE: 'website', - ADDRESS: 'address', - TWITTER: 'twitter', EMAIL_COLLECTION: 'additional_mail', + PHONE: 'phone', + TWITTER: 'twitter', + WEBSITE: 'website', +}) + +/** Enum of account properties to human readable account properties */ +export const ACCOUNT_PROPERTY_READABLE_ENUM = Object.freeze({ + ADDRESS: 'Address', + AVATAR: 'Avatar', + DISPLAYNAME: 'Full name', + EMAIL: 'Email', + EMAIL_COLLECTION: 'Additional Email', + PHONE: 'Phone', + TWITTER: 'Twitter', + WEBSITE: 'Website', }) /** Enum of scopes */ export const SCOPE_ENUM = Object.freeze({ - PRIVATE: 'v2-private', LOCAL: 'v2-local', + PRIVATE: 'v2-private', FEDERATED: 'v2-federated', PUBLISHED: 'v2-published', }) +/** Enum of readable account properties to supported scopes */ +export const PROPERTY_READABLE_SUPPORTED_SCOPES_ENUM = Object.freeze({ + [ACCOUNT_PROPERTY_READABLE_ENUM.ADDRESS]: [SCOPE_ENUM.LOCAL, SCOPE_ENUM.PRIVATE], + [ACCOUNT_PROPERTY_READABLE_ENUM.AVATAR]: [SCOPE_ENUM.LOCAL, SCOPE_ENUM.PRIVATE], + [ACCOUNT_PROPERTY_READABLE_ENUM.DISPLAYNAME]: [SCOPE_ENUM.LOCAL], + [ACCOUNT_PROPERTY_READABLE_ENUM.EMAIL]: [SCOPE_ENUM.LOCAL], + [ACCOUNT_PROPERTY_READABLE_ENUM.EMAIL_COLLECTION]: [SCOPE_ENUM.LOCAL], + [ACCOUNT_PROPERTY_READABLE_ENUM.PHONE]: [SCOPE_ENUM.LOCAL, SCOPE_ENUM.PRIVATE], + [ACCOUNT_PROPERTY_READABLE_ENUM.TWITTER]: [SCOPE_ENUM.LOCAL, SCOPE_ENUM.PRIVATE], + [ACCOUNT_PROPERTY_READABLE_ENUM.WEBSITE]: [SCOPE_ENUM.LOCAL, SCOPE_ENUM.PRIVATE], +}) + /** Scope suffix */ export const SCOPE_SUFFIX = 'Scope' @@ -56,18 +80,18 @@ export const DEFAULT_ADDITIONAL_EMAIL_SCOPE = SCOPE_ENUM.LOCAL * *Used for federation control* */ export const SCOPE_PROPERTY_ENUM = Object.freeze({ - [SCOPE_ENUM.PRIVATE]: { - name: SCOPE_ENUM.PRIVATE, - displayName: t('settings', 'Private'), - tooltip: t('settings', 'Only visible to people matched via phone number integration through Talk on mobile'), - iconClass: 'icon-phone', - }, [SCOPE_ENUM.LOCAL]: { name: SCOPE_ENUM.LOCAL, displayName: t('settings', 'Local'), tooltip: t('settings', 'Only visible to people on this instance and guests'), iconClass: 'icon-password', }, + [SCOPE_ENUM.PRIVATE]: { + name: SCOPE_ENUM.PRIVATE, + displayName: t('settings', 'Private'), + tooltip: t('settings', 'Only visible to people matched via phone number integration through Talk on mobile'), + iconClass: 'icon-phone', + }, [SCOPE_ENUM.FEDERATED]: { name: SCOPE_ENUM.FEDERATED, displayName: t('settings', 'Federated'), diff --git a/apps/settings/src/service/PersonalInfo/DisplayNameService.js b/apps/settings/src/service/PersonalInfo/DisplayNameService.js new file mode 100644 index 00000000000..f0e98c015de --- /dev/null +++ b/apps/settings/src/service/PersonalInfo/DisplayNameService.js @@ -0,0 +1,68 @@ +/** + * @copyright 2021, Christopher Ng + * + * @author Christopher Ng + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import axios from '@nextcloud/axios' +import { getCurrentUser } from '@nextcloud/auth' +import { generateOcsUrl } from '@nextcloud/router' +import confirmPassword from '@nextcloud/password-confirmation' + +import { ACCOUNT_PROPERTY_ENUM, SCOPE_SUFFIX } from '../../constants/AccountPropertyConstants' + +/** + * Save the primary display name of the user + * + * @param {string} displayName the primary display name + * @returns {object} + */ +export const savePrimaryDisplayName = async(displayName) => { + const userId = getCurrentUser().uid + const url = generateOcsUrl('cloud/users/{userId}', { userId }) + + await confirmPassword() + + const res = await axios.put(url, { + key: ACCOUNT_PROPERTY_ENUM.DISPLAYNAME, + value: displayName, + }) + + return res.data +} + +/** + * Save the federation scope for the primary display name of the user + * + * @param {string} scope the federation scope + * @returns {object} + */ +export const savePrimaryDisplayNameScope = async(scope) => { + const userId = getCurrentUser().uid + const url = generateOcsUrl('cloud/users/{userId}', { userId }) + + await confirmPassword() + + const res = await axios.put(url, { + key: `${ACCOUNT_PROPERTY_ENUM.DISPLAYNAME}${SCOPE_SUFFIX}`, + value: scope, + }) + + return res.data +} diff --git a/apps/settings/src/service/PersonalInfoService.js b/apps/settings/src/service/PersonalInfo/EmailService.js similarity index 94% rename from apps/settings/src/service/PersonalInfoService.js rename to apps/settings/src/service/PersonalInfo/EmailService.js index f5c010f5377..00e2373736c 100644 --- a/apps/settings/src/service/PersonalInfoService.js +++ b/apps/settings/src/service/PersonalInfo/EmailService.js @@ -25,13 +25,13 @@ import { getCurrentUser } from '@nextcloud/auth' import { generateOcsUrl } from '@nextcloud/router' import confirmPassword from '@nextcloud/password-confirmation' -import { ACCOUNT_PROPERTY_ENUM, SCOPE_SUFFIX } from '../constants/AccountPropertyConstants' +import { ACCOUNT_PROPERTY_ENUM, SCOPE_SUFFIX } from '../../constants/AccountPropertyConstants' /** * Save the primary email of the user * * @param {string} email the primary email - * @returns {Object} + * @returns {object} */ export const savePrimaryEmail = async(email) => { const userId = getCurrentUser().uid @@ -53,7 +53,7 @@ export const savePrimaryEmail = async(email) => { * *Will be appended to the user's additional emails* * * @param {string} email the additional email - * @returns {Object} + * @returns {object} */ export const saveAdditionalEmail = async(email) => { const userId = getCurrentUser().uid @@ -73,7 +73,7 @@ export const saveAdditionalEmail = async(email) => { * Remove an additional email of the user * * @param {string} email the additional email - * @returns {Object} + * @returns {object} */ export const removeAdditionalEmail = async(email) => { const userId = getCurrentUser().uid @@ -94,7 +94,7 @@ export const removeAdditionalEmail = async(email) => { * * @param {string} prevEmail the additional email to be updated * @param {string} newEmail the new additional email - * @returns {Object} + * @returns {object} */ export const updateAdditionalEmail = async(prevEmail, newEmail) => { const userId = getCurrentUser().uid @@ -114,7 +114,7 @@ export const updateAdditionalEmail = async(prevEmail, newEmail) => { * Save the federation scope for the primary email of the user * * @param {string} scope the federation scope - * @returns {Object} + * @returns {object} */ export const savePrimaryEmailScope = async(scope) => { const userId = getCurrentUser().uid @@ -135,7 +135,7 @@ export const savePrimaryEmailScope = async(scope) => { * * @param {string} email the additional email * @param {string} scope the federation scope - * @returns {Object} + * @returns {object} */ export const saveAdditionalEmailScope = async(email, scope) => { const userId = getCurrentUser().uid