aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_status/src/services/statusService.js
blob: fcaf2ef9902d97e24ded458d7cd0c6cc66327346 (plain)
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
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import HttpClient from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

/**
 * Fetches the current user-status
 *
 * @return {Promise<object>}
 */
const fetchCurrentStatus = async () => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status')
	const response = await HttpClient.get(url)

	return response.data.ocs.data
}

/**
 * Fetches the current user-status
 *
 * @param {string} userId
 * @return {Promise<object>}
 */
const fetchBackupStatus = async (userId) => {
	const url = generateOcsUrl('apps/user_status/api/v1/statuses/{userId}', { userId: '_' + userId })
	const response = await HttpClient.get(url)

	return response.data.ocs.data
}

/**
 * Sets the status
 *
 * @param {string} statusType The status (online / away / dnd / invisible)
 * @return {Promise<void>}
 */
const setStatus = async (statusType) => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status/status')
	await HttpClient.put(url, {
		statusType,
	})
}

/**
 * Sets a message based on our predefined statuses
 *
 * @param {string} messageId The id of the message, taken from predefined status service
 * @param {number | null} clearAt When to automatically clean the status
 * @return {Promise<void>}
 */
const setPredefinedMessage = async (messageId, clearAt = null) => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status/message/predefined?format=json')
	await HttpClient.put(url, {
		messageId,
		clearAt,
	})
}

/**
 * Sets a custom message
 *
 * @param {string} message The user-defined message
 * @param {string | null} statusIcon The user-defined icon
 * @param {number | null} clearAt When to automatically clean the status
 * @return {Promise<void>}
 */
const setCustomMessage = async (message, statusIcon = null, clearAt = null) => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status/message/custom?format=json')
	await HttpClient.put(url, {
		message,
		statusIcon,
		clearAt,
	})
}

/**
 * Clears the current status of the user
 *
 * @return {Promise<void>}
 */
const clearMessage = async () => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status/message?format=json')
	await HttpClient.delete(url)
}

/**
 * Revert the automated status
 *
 * @param {string} messageId
 * @return {Promise<object>}
 */
const revertToBackupStatus = async (messageId) => {
	const url = generateOcsUrl('apps/user_status/api/v1/user_status/revert/{messageId}', { messageId })
	const response = await HttpClient.delete(url)

	return response.data.ocs.data
}

export {
	fetchCurrentStatus,
	fetchBackupStatus,
	setStatus,
	setCustomMessage,
	setPredefinedMessage,
	clearMessage,
	revertToBackupStatus,
}