aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_status/src/services
diff options
context:
space:
mode:
Diffstat (limited to 'apps/user_status/src/services')
-rw-r--r--apps/user_status/src/services/clearAtOptionsService.js52
-rw-r--r--apps/user_status/src/services/clearAtService.js47
-rw-r--r--apps/user_status/src/services/dateService.js12
-rw-r--r--apps/user_status/src/services/heartbeatService.js25
-rw-r--r--apps/user_status/src/services/predefinedStatusService.js23
-rw-r--r--apps/user_status/src/services/statusOptionsService.js36
-rw-r--r--apps/user_status/src/services/statusService.js110
7 files changed, 305 insertions, 0 deletions
diff --git a/apps/user_status/src/services/clearAtOptionsService.js b/apps/user_status/src/services/clearAtOptionsService.js
new file mode 100644
index 00000000000..af0059bfb7f
--- /dev/null
+++ b/apps/user_status/src/services/clearAtOptionsService.js
@@ -0,0 +1,52 @@
+/**
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import { translate as t } from '@nextcloud/l10n'
+
+/**
+ * Returns an array
+ *
+ * @return {object[]}
+ */
+const getAllClearAtOptions = () => {
+ return [{
+ label: t('user_status', 'Don\'t clear'),
+ clearAt: null,
+ }, {
+ label: t('user_status', '30 minutes'),
+ clearAt: {
+ type: 'period',
+ time: 1800,
+ },
+ }, {
+ label: t('user_status', '1 hour'),
+ clearAt: {
+ type: 'period',
+ time: 3600,
+ },
+ }, {
+ label: t('user_status', '4 hours'),
+ clearAt: {
+ type: 'period',
+ time: 14400,
+ },
+ }, {
+ label: t('user_status', 'Today'),
+ clearAt: {
+ type: 'end-of',
+ time: 'day',
+ },
+ }, {
+ label: t('user_status', 'This week'),
+ clearAt: {
+ type: 'end-of',
+ time: 'week',
+ },
+ }]
+}
+
+export {
+ getAllClearAtOptions,
+}
diff --git a/apps/user_status/src/services/clearAtService.js b/apps/user_status/src/services/clearAtService.js
new file mode 100644
index 00000000000..f23d267ad02
--- /dev/null
+++ b/apps/user_status/src/services/clearAtService.js
@@ -0,0 +1,47 @@
+/**
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import {
+ dateFactory,
+} from './dateService.js'
+import moment from '@nextcloud/moment'
+
+/**
+ * Calculates the actual clearAt timestamp
+ *
+ * @param {object | null} clearAt The clear-at config
+ * @return {number | null}
+ */
+const getTimestampForClearAt = (clearAt) => {
+ if (clearAt === null) {
+ return null
+ }
+
+ const date = dateFactory()
+
+ if (clearAt.type === 'period') {
+ date.setSeconds(date.getSeconds() + clearAt.time)
+ return Math.floor(date.getTime() / 1000)
+ }
+ if (clearAt.type === 'end-of') {
+ switch (clearAt.time) {
+ case 'day':
+ case 'week':
+ return Number(moment(date).endOf(clearAt.time).format('X'))
+ }
+ }
+ // This is not an officially supported type
+ // but only used internally to show the remaining time
+ // in the Set Status Modal
+ if (clearAt.type === '_time') {
+ return clearAt.time
+ }
+
+ return null
+}
+
+export {
+ getTimestampForClearAt,
+}
diff --git a/apps/user_status/src/services/dateService.js b/apps/user_status/src/services/dateService.js
new file mode 100644
index 00000000000..26a61d4a3e2
--- /dev/null
+++ b/apps/user_status/src/services/dateService.js
@@ -0,0 +1,12 @@
+/**
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+const dateFactory = () => {
+ return new Date()
+}
+
+export {
+ dateFactory,
+}
diff --git a/apps/user_status/src/services/heartbeatService.js b/apps/user_status/src/services/heartbeatService.js
new file mode 100644
index 00000000000..fda1a1ffc9f
--- /dev/null
+++ b/apps/user_status/src/services/heartbeatService.js
@@ -0,0 +1,25 @@
+/**
+ * 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'
+
+/**
+ * Sends a heartbeat
+ *
+ * @param {boolean} isAway Whether or not the user is active
+ * @return {Promise<void>}
+ */
+const sendHeartbeat = async (isAway) => {
+ const url = generateOcsUrl('apps/user_status/api/v1/heartbeat?format=json')
+ const response = await HttpClient.put(url, {
+ status: isAway ? 'away' : 'online',
+ })
+ return response.data.ocs.data
+}
+
+export {
+ sendHeartbeat,
+}
diff --git a/apps/user_status/src/services/predefinedStatusService.js b/apps/user_status/src/services/predefinedStatusService.js
new file mode 100644
index 00000000000..b423c6e0cc4
--- /dev/null
+++ b/apps/user_status/src/services/predefinedStatusService.js
@@ -0,0 +1,23 @@
+/**
+ * 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 all predefined statuses from the server
+ *
+ * @return {Promise<void>}
+ */
+const fetchAllPredefinedStatuses = async () => {
+ const url = generateOcsUrl('apps/user_status/api/v1/predefined_statuses?format=json')
+ const response = await HttpClient.get(url)
+
+ return response.data.ocs.data
+}
+
+export {
+ fetchAllPredefinedStatuses,
+}
diff --git a/apps/user_status/src/services/statusOptionsService.js b/apps/user_status/src/services/statusOptionsService.js
new file mode 100644
index 00000000000..6c23645e5be
--- /dev/null
+++ b/apps/user_status/src/services/statusOptionsService.js
@@ -0,0 +1,36 @@
+/**
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import { translate as t } from '@nextcloud/l10n'
+
+/**
+ * Returns a list of all user-definable statuses
+ *
+ * @return {object[]}
+ */
+const getAllStatusOptions = () => {
+ return [{
+ type: 'online',
+ label: t('user_status', 'Online'),
+ }, {
+ type: 'away',
+ label: t('user_status', 'Away'),
+ }, {
+ type: 'busy',
+ label: t('user_status', 'Busy'),
+ }, {
+ type: 'dnd',
+ label: t('user_status', 'Do not disturb'),
+ subline: t('user_status', 'Mute all notifications'),
+ }, {
+ type: 'invisible',
+ label: t('user_status', 'Invisible'),
+ subline: t('user_status', 'Appear offline'),
+ }]
+}
+
+export {
+ getAllStatusOptions,
+}
diff --git a/apps/user_status/src/services/statusService.js b/apps/user_status/src/services/statusService.js
new file mode 100644
index 00000000000..6504411c996
--- /dev/null
+++ b/apps/user_status/src/services/statusService.js
@@ -0,0 +1,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 Id of the user to fetch the status
+ * @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 ID of the message to revert
+ * @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,
+}