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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/**
* @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@pm.me>
*
* @author Marco Ambrosini <marcoambrosini@pm.me>
*
* @license AGPL-3.0-or-later
*
* 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 axios from '@nextcloud/axios'
import { loadState } from '@nextcloud/initial-state'
import { generateOcsUrl } from '@nextcloud/router'
import { CONVERSATION, SHARE } from '../constants.js'
/**
* Fetches the conversations from the server.
*
* @param {object} options options
*/
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)
}
/**
* Fetches a conversation from the server.
*
* @param {string} token The token of the conversation to be fetched.
*/
const fetchConversation = async function(token) {
return axios.get(generateOcsUrl('apps/spreed/api/v4/room/{token}', { token }))
}
/**
* Fetch listed conversations
*
* @param {string} searchText The string that will be used in the search query.
* @param {object} options options
*/
const searchListedConversations = async function({ searchText }, options) {
return axios.get(generateOcsUrl('apps/spreed/api/v4/listed-room'), Object.assign(options, {
params: {
searchTerm: searchText,
},
}))
}
/**
* Fetch possible conversations
*
* @param {object} data the wrapping object;
* @param {string} data.searchText The string that will be used in the search query.
* @param {string} [data.token] The token of the conversation (if any), or "new" for a new one
* @param {boolean} [data.onlyUsers] Only return users
* @param {object} options options
*/
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, {
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.
*/
const createOneToOneConversation = async function(userId) {
try {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { roomType: CONVERSATION.TYPE.ONE_TO_ONE, invite: userId })
return response
} catch (error) {
console.debug('Error creating new one to one conversation: ', error)
}
}
/**
* Create a new group conversation.
*
* @param {string} invite The group/circle ID
* @param {string} source The source of the invite ID (defaults to groups)
*/
const createGroupConversation = async function(invite, source) {
try {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { roomType: CONVERSATION.TYPE.GROUP, invite, source: source || 'groups' })
return response
} catch (error) {
console.debug('Error creating new group conversation: ', error)
}
}
/**
* Create a new private conversation.
*
* @param {string} conversationName The name for the new conversation
*/
const createPrivateConversation = async function(conversationName) {
try {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { roomType: CONVERSATION.TYPE.GROUP, roomName: conversationName })
return response
} catch (error) {
console.debug('Error creating new private conversation: ', error)
}
}
/**
* Create a new private conversation.
*
* @param {string} conversationName The name for the new conversation
*/
const createPublicConversation = async function(conversationName) {
try {
const response = await axios.post(generateOcsUrl('apps/spreed/api/v4/room'), { roomType: CONVERSATION.TYPE.PUBLIC, roomName: conversationName })
return response
} catch (error) {
console.debug('Error creating new public conversation: ', error)
}
}
/**
* Set a conversation's password
*
* @param {string} token the conversation's token
* @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 }), {
password,
})
return response
}
/**
* Set a conversation's name
*
* @param {string} token the conversation's token
* @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 }), {
roomName: name,
})
return response
}
/**
* Delete a conversation.
*
* @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)
}
}
/**
* Clears the conversation history
*
* @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
}
/**
* Add a conversation to the favorites
*
* @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)
}
}
/**
* Remove a conversation from the favorites
*
* @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)
}
}
/**
* Set notification level
*
* @param {string} token The token of the conversation to change the notification level
* @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)
}
}
/**
* Set call notifications
*
* @param {string} token The token of the conversation to change the call notification 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)
}
}
/**
* Make the conversation public
*
* @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)
}
}
/**
* Make the conversation private
*
* @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)
}
}
/**
* Change the SIP enabled
*
* @param {string} token The token of the conversation to be modified
* @param {number} newState The new SIP state to set
*/
const setSIPEnabled = async function(token, newState) {
return axios.put(generateOcsUrl('apps/spreed/api/v4/room/{token}/webinar/sip', { token }), {
state: newState,
})
}
/**
* Change the lobby state
*
* @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
*/
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)
}
}
/**
* Change the read-only state
*
* @param {string} token The token of the conversation to be modified
* @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)
}
}
/**
* Change the listable scope
*
* @param {string} token The token of the conversation to be modified
* @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 }), {
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
}
/**
* Set the default permissions for participants in a conversation.
*
* @param {string} token conversation token
* @param {number} permissions the type of permission to be granted. Valid values are
* any sums of 'DEFAULT', 'CUSTOM', 'CALL_START', 'CALL_JOIN', 'LOBBY_IGNORE',
* '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,
})
}
/**
* Set the default permissions for participants in a call. These will be reset
* to default once the call has ended.
*
* @param {string} token conversation token
* @param {number} permissions the type of permission to be granted. Valid values are
* any sums of 'DEFAULT', 'CUSTOM', 'CALL_START', 'CALL_JOIN', 'LOBBY_IGNORE',
* '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,
})
}
const validatePassword = async (password) => {
return await axios.post(generateOcsUrl('apps/password_policy/api/v1/validate'), {
password,
})
}
export {
fetchConversations,
fetchConversation,
searchListedConversations,
searchPossibleConversations,
createOneToOneConversation,
createGroupConversation,
createPrivateConversation,
createPublicConversation,
deleteConversation,
addToFavorites,
removeFromFavorites,
setNotificationLevel,
setNotificationCalls,
makePublic,
makePrivate,
setSIPEnabled,
changeLobbyState,
changeReadOnlyState,
changeListable,
setConversationPassword,
setConversationName,
setConversationDescription,
clearConversationHistory,
setConversationPermissions,
setCallPermissions,
validatePassword,
}
|