-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathuserStatus.js
More file actions
71 lines (66 loc) · 1.74 KB
/
userStatus.js
File metadata and controls
71 lines (66 loc) · 1.74 KB
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
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getCurrentUser } from '@nextcloud/auth'
import axios from '@nextcloud/axios'
import { getCapabilities } from '@nextcloud/capabilities'
import { generateOcsUrl } from '@nextcloud/router'
import { logger } from '../utils/logger.ts'
export default {
data() {
return {
hasStatus: false,
userStatus: {
status: null,
message: null,
icon: null,
},
}
},
methods: {
/**
* Fetches the user-status from the server
*
* @param {string} userId UserId of the user to fetch the status for
*
* @return {Promise<void>}
*/
async fetchUserStatus(userId) {
if (!userId) {
return
}
const capabilities = getCapabilities()
if (!Object.hasOwn(capabilities, 'user_status') || !capabilities.user_status.enabled) {
return
}
// User status endpoint is not available for guests.
if (!getCurrentUser()) {
return
}
try {
const { data } = await axios.get(generateOcsUrl('apps/user_status/api/v1/statuses/{userId}', { userId }))
this.setUserStatus(data.ocs.data)
} catch (e) {
if (e.response.status === 404 && e.response.data.ocs?.data?.length === 0) {
// User just has no status set, so don't log it
return
}
logger.error('Failed to fetch user status', { error: e })
}
},
/**
* Sets the user status
*
* @param {string} status user's status
* @param {string} message user's message
* @param {string} icon user's icon
*/
setUserStatus({ status, message, icon }) {
this.userStatus.status = status || ''
this.userStatus.message = message || ''
this.userStatus.icon = icon || ''
this.hasStatus = !!status
},
},
}