1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
import { showError } from '@nextcloud/dialogs'
import Vue from 'vue'
import {
acceptMember,
createCircle,
deleteCircle,
deleteMember,
getCircleMembers,
getCircle,
getCircles,
leaveCircle,
addMembers,
editCircleSetting,
} from '../services/circles.ts'
import Member from '../models/member.ts'
import Circle from '../models/circle.ts'
import logger from '../services/logger.js'
const state = {
/** @type {Object<string>} Circle */
circles: {},
}
const mutations = {
/**
* Add a circle into state
*
* @param {object} state the store data
* @param {Circle} circle the circle to add
*/
addCircle(state, circle) {
if (circle.constructor.name !== Circle.name) {
throw new Error('circle must be a Circle type')
}
Vue.set(state.circles, circle.id, circle)
},
/**
* Delete circle
*
* @param {object} state the store data
* @param {Circle} circle the circle to delete
*/
deleteCircle(state, circle) {
if (!(circle.id in state.circles)) {
logger.warn('Skipping deletion of unknown circle', { circle })
}
Vue.delete(state.circles, circle.id)
},
/**
* Append a list of members to a circle
* and remove duplicates
*
* @param {object} state the store data
* @param {Members[]} members array of members to append
*/
appendMembersToCircle(state, members) {
members.forEach(member => member.circle.addMember(member))
},
/**
* Add a member to a circle and overwrite if duplicate uid
*
* @param {object} state the store data
* @param {object} data destructuring object
* @param {string} data.circleId the circle to add the members to
* @param {Member} data.member array of contacts to append
*/
addMemberToCircle(state, { circleId, member }) {
const circle = state.circles[circleId]
circle.addMember(member)
},
/**
* Delete a contact in a specified circle
*
* @param {object} state the store data
* @param {Member} member the member to add
*/
deleteMemberFromCircle(state, member) {
// Circles dependencies are managed directly from the model
member.delete()
},
setCircleSettings(state, { circleId, settings }) {
Vue.set(state.circles[circleId]._data, 'settings', settings)
},
}
const getters = {
getCircles: state => Object.values(state.circles),
getCircle: state => (id) => state.circles[id],
}
const actions = {
/**
* Retrieve and commit circles
*
* @param {object} context the store mutations
* @return {object[]} the circles
*/
async getCircles(context) {
const circles = await getCircles()
logger.debug(`Retrieved ${circles.length} circle(s)`, { circles })
let failure = false
circles.forEach(circle => {
try {
const newCircle = new Circle(circle)
context.commit('addCircle', newCircle)
} catch (error) {
failure = true
logger.error('This circle failed to be processed', { circle, error })
}
})
if (failure) {
showError(t('contacts', 'Some circle(s) an error occurred. Check the console for more details.'))
}
return circles
},
/**
* Retrieve and commit circles
*
* @param {object} context the store mutations
* @param {string} circleId the circle id
* @return {object[]} the circles
*/
async getCircle(context, circleId) {
const circle = await getCircle(circleId)
logger.debug('Retrieved 1 circle', { circle })
try {
const newCircle = new Circle(circle)
context.commit('addCircle', newCircle)
} catch (error) {
logger.error('This circle failed to be processed', { circle, error })
}
return circle
},
/**
* Retrieve and commit circle members
*
* @param {object} context the store mutations
* @param {string} circleId the circle id
*/
async getCircleMembers(context, circleId) {
const circle = context.getters.getCircle(circleId)
const members = await getCircleMembers(circleId)
logger.debug(`${circleId} have ${members.length} member(s)`, { members })
context.commit('appendMembersToCircle', members.map(member => new Member(member, circle)))
},
/**
* Create circle
*
* @param {object} context the store mutations Current context
* @param {object} data destructuring object
* @param {string} data.circleName the circle name
* @param {boolean} data.isPersonal the circle is a personal one
* @param {boolean} data.isLocal the circle is not distributed to the GlobalScale
* @return {Circle} the new circle
*/
async createCircle(context, { circleName, isPersonal, isLocal }) {
try {
const response = await createCircle(circleName, isPersonal, isLocal)
const circle = new Circle(response)
context.commit('addCircle', circle)
logger.debug('Created circle', { circleName, circle })
return circle
} catch (error) {
console.error(error)
showError(t('contacts', 'Unable to create circle {circleName}', { circleName }))
}
},
/**
* Delete circle
*
* @param {object} context the store mutations Current context
* @param {Circle} circleId the circle to delete
*/
async deleteCircle(context, circleId) {
const circle = context.getters.getCircle(circleId)
try {
await deleteCircle(circleId)
context.commit('deleteCircle', circle)
logger.debug('Deleted circle', { circleId })
} catch (error) {
console.error(error)
showError(t('contacts', 'Unable to delete circle {circleId}', circleId))
}
},
/**
* Add members to a circle
*
* @param {object} context the store mutations Current context
* @param {object} data destructuring object
* @param {string} data.circleId the circle to manage
* @param {Array} data.selection the members to add, see addMembers service
* @return {Member[]}
*/
async addMembersToCircle(context, { circleId, selection }) {
const circle = context.getters.getCircle(circleId)
const results = await addMembers(circleId, selection)
const members = results.map(member => new Member(member, circle))
logger.debug('Added members to circle', { circle, members })
context.commit('appendMembersToCircle', members)
return members
},
/**
* Delete a member from a circle
*
* @param {object} context the store mutations Current context
* @param {Member} member the member to remove
* @param {boolean} [leave=false] leave the circle instead of removing the member
*/
async deleteMemberFromCircle(context, { member, leave = false }) {
const circleId = member.circle.id
const memberId = member.id
if (leave) {
const circle = await leaveCircle(circleId)
member.circle.updateData(circle)
// If the circle is not visible, we remove it from the list
if (!member.circle.isVisible && !member.circle.isMember) {
await context.commit('deleteCircle', circle)
logger.debug('Deleted circle', { circleId, memberId })
}
} else {
await deleteMember(circleId, memberId)
}
|