Skip to content

Commit 5d5ed6e

Browse files
committed
Create personal info service
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 94e469f commit 5d5ed6e

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @copyright 2021, Christopher Ng <chrng8@gmail.com>
3+
*
4+
* @author Christopher Ng <chrng8@gmail.com>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
/*
24+
* SYNC to be kept in sync with lib/public/Accounts/IAccountManager.php
25+
*/
26+
27+
/** Enum of account properties */
28+
export const ACCOUNT_PROPERTY_ENUM = Object.freeze({
29+
AVATAR: 'avatar',
30+
DISPLAYNAME: 'displayname',
31+
PHONE: 'phone',
32+
EMAIL: 'email',
33+
WEBSITE: 'website',
34+
ADDRESS: 'address',
35+
TWITTER: 'twitter',
36+
EMAIL_COLLECTION: 'additional_mail',
37+
})
38+
39+
/** Enum of scopes */
40+
export const SCOPE_ENUM = Object.freeze({
41+
PRIVATE: 'v2-private',
42+
LOCAL: 'v2-local',
43+
FEDERATED: 'v2-federated',
44+
PUBLISHED: 'v2-published',
45+
})
46+
47+
/** Scope suffix */
48+
export const SCOPE_SUFFIX = 'Scope'
49+
50+
/** Default additional email scope */
51+
export const DEFAULT_ADDITIONAL_EMAIL_SCOPE = SCOPE_ENUM.LOCAL
52+
53+
/**
54+
* Enum of scope names to properties
55+
*
56+
* *Used for federation control*
57+
*/
58+
export const SCOPE_PROPERTY_ENUM = Object.freeze({
59+
[SCOPE_ENUM.PRIVATE]: {
60+
name: SCOPE_ENUM.PRIVATE,
61+
displayName: t('settings', 'Private'),
62+
tooltip: t('settings', 'Only visible to people matched via phone number integration through Talk on mobile'),
63+
iconClass: 'icon-phone',
64+
},
65+
[SCOPE_ENUM.LOCAL]: {
66+
name: SCOPE_ENUM.LOCAL,
67+
displayName: t('settings', 'Local'),
68+
tooltip: t('settings', 'Only visible to people on this instance and guests'),
69+
iconClass: 'icon-password',
70+
},
71+
[SCOPE_ENUM.FEDERATED]: {
72+
name: SCOPE_ENUM.FEDERATED,
73+
displayName: t('settings', 'Federated'),
74+
tooltip: t('settings', 'Only synchronize to trusted servers'),
75+
iconClass: 'icon-contacts-dark',
76+
},
77+
[SCOPE_ENUM.PUBLISHED]: {
78+
name: SCOPE_ENUM.PUBLISHED,
79+
displayName: t('settings', 'Published'),
80+
tooltip: t('settings', 'Synchronize to trusted servers and the global and public address book'),
81+
iconClass: 'icon-link',
82+
},
83+
})
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* @copyright 2021, Christopher Ng <chrng8@gmail.com>
3+
*
4+
* @author Christopher Ng <chrng8@gmail.com>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
import axios from '@nextcloud/axios'
24+
import { getCurrentUser } from '@nextcloud/auth'
25+
import { generateOcsUrl } from '@nextcloud/router'
26+
import confirmPassword from '@nextcloud/password-confirmation'
27+
28+
import { ACCOUNT_PROPERTY_ENUM, SCOPE_SUFFIX } from '../constants/AccountPropertyConstants'
29+
30+
/**
31+
* Save the primary email of the user
32+
*
33+
* @param {string} email the primary email
34+
* @returns {Object}
35+
*/
36+
export const savePrimaryEmail = async(email) => {
37+
const userId = getCurrentUser().uid
38+
// TODO upgrade @nextcloud/router to v2.0 so we can remove the .slice() trailing slash hacks (same below)
39+
const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1)
40+
41+
await confirmPassword()
42+
43+
const res = await axios.put(url, {
44+
key: ACCOUNT_PROPERTY_ENUM.EMAIL,
45+
value: email,
46+
})
47+
48+
return res.data
49+
}
50+
51+
/**
52+
* Save an additional email of the user
53+
*
54+
* *Will be appended to the user's additional emails*
55+
*
56+
* @param {string} email the additional email
57+
* @returns {Object}
58+
*/
59+
export const saveAdditionalEmail = async(email) => {
60+
const userId = getCurrentUser().uid
61+
const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1)
62+
63+
await confirmPassword()
64+
65+
const res = await axios.put(url, {
66+
key: ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION,
67+
value: email,
68+
})
69+
70+
return res.data
71+
}
72+
73+
/**
74+
* Remove an additional email of the user
75+
*
76+
* @param {string} email the additional email
77+
* @returns {Object}
78+
*/
79+
export const removeAdditionalEmail = async(email) => {
80+
const userId = getCurrentUser().uid
81+
const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}`, 2).slice(0, -1)
82+
83+
await confirmPassword()
84+
85+
const res = await axios.put(url, {
86+
key: email,
87+
value: '',
88+
})
89+
90+
return res.data
91+
}
92+
93+
/**
94+
* Update an additional email of the user
95+
*
96+
* @param {string} prevEmail the additional email to be updated
97+
* @param {string} newEmail the new additional email
98+
* @returns {Object}
99+
*/
100+
export const updateAdditionalEmail = async(prevEmail, newEmail) => {
101+
const userId = getCurrentUser().uid
102+
const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}`, 2).slice(0, -1)
103+
104+
await confirmPassword()
105+
106+
const res = await axios.put(url, {
107+
key: prevEmail,
108+
value: newEmail,
109+
})
110+
111+
return res.data
112+
}
113+
114+
/**
115+
* Save the federation scope for the primary email of the user
116+
*
117+
* @param {string} scope the federation scope
118+
* @returns {Object}
119+
*/
120+
export const savePrimaryEmailScope = async(scope) => {
121+
const userId = getCurrentUser().uid
122+
const url = generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1)
123+
124+
await confirmPassword()
125+
126+
const res = await axios.put(url, {
127+
key: `${ACCOUNT_PROPERTY_ENUM.EMAIL}${SCOPE_SUFFIX}`,
128+
value: scope,
129+
})
130+
131+
return res.data
132+
}
133+
134+
/**
135+
* Save the federation scope for the additional email of the user
136+
*
137+
* @param {string} email the additional email
138+
* @param {string} scope the federation scope
139+
* @returns {Object}
140+
*/
141+
export const saveAdditionalEmailScope = async(email, scope) => {
142+
const userId = getCurrentUser().uid
143+
const url = generateOcsUrl(`cloud/users/${userId}/${ACCOUNT_PROPERTY_ENUM.EMAIL_COLLECTION}${SCOPE_SUFFIX}`, 2).slice(0, -1)
144+
145+
await confirmPassword()
146+
147+
const res = await axios.put(url, {
148+
key: email,
149+
value: scope,
150+
})
151+
152+
return res.data
153+
}

0 commit comments

Comments
 (0)