diff options
author | Joas Schilling <coding@schilljs.com> | 2023-02-24 16:38:32 +0100 |
---|---|---|
committer | Maksim Sukharev <antreesy.web@gmail.com> | 2023-04-18 14:57:44 +0200 |
commit | 64776ff042d880eac5d702d3cbc7af4fb0834be2 (patch) | |
tree | 5872ca565b5365a8c006169ffb8f621ddfdf723f | |
parent | 1a255379e85f02f3fb301f709720f3210f56b011 (diff) | |
download | nextcloud-server-64776ff042d880eac5d702d3cbc7af4fb0834be2.tar.gz nextcloud-server-64776ff042d880eac5d702d3cbc7af4fb0834be2.zip |
feat(user_status): Allow to manually revert an automated status
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | apps/user_status/appinfo/routes.php | 1 | ||||
-rw-r--r-- | apps/user_status/lib/Controller/UserStatusController.php | 13 | ||||
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 13 | ||||
-rw-r--r-- | apps/user_status/src/components/PredefinedStatus.vue | 2 | ||||
-rw-r--r-- | apps/user_status/src/components/PreviousStatus.vue | 120 | ||||
-rw-r--r-- | apps/user_status/src/components/SetStatusModal.vue | 81 | ||||
-rw-r--r-- | apps/user_status/src/services/statusService.js | 28 | ||||
-rw-r--r-- | apps/user_status/src/store/index.js | 2 | ||||
-rw-r--r-- | apps/user_status/src/store/userBackupStatus.js | 117 | ||||
-rw-r--r-- | apps/user_status/src/store/userStatus.js | 4 |
10 files changed, 369 insertions, 12 deletions
diff --git a/apps/user_status/appinfo/routes.php b/apps/user_status/appinfo/routes.php index 147d1927358..fe534098a58 100644 --- a/apps/user_status/appinfo/routes.php +++ b/apps/user_status/appinfo/routes.php @@ -34,6 +34,7 @@ return [ ['name' => 'UserStatus#setPredefinedMessage', 'url' => '/api/v1/user_status/message/predefined', 'verb' => 'PUT'], ['name' => 'UserStatus#setCustomMessage', 'url' => '/api/v1/user_status/message/custom', 'verb' => 'PUT'], ['name' => 'UserStatus#clearMessage', 'url' => '/api/v1/user_status/message', 'verb' => 'DELETE'], + ['name' => 'UserStatus#revertStatus', 'url' => '/api/v1/user_status/revert/{messageId}', 'verb' => 'DELETE'], // Routes for listing default routes ['name' => 'PredefinedStatus#findAll', 'url' => '/api/v1/predefined_statuses/', 'verb' => 'GET'], // Route for doing heartbeats diff --git a/apps/user_status/lib/Controller/UserStatusController.php b/apps/user_status/lib/Controller/UserStatusController.php index 214dc21f453..aded923d07f 100644 --- a/apps/user_status/lib/Controller/UserStatusController.php +++ b/apps/user_status/lib/Controller/UserStatusController.php @@ -185,6 +185,19 @@ class UserStatusController extends OCSController { } /** + * @NoAdminRequired + * + * @return DataResponse + */ + public function revertStatus(string $messageId): DataResponse { + $backupStatus = $this->service->revertUserStatus($this->userId, $messageId, true); + if ($backupStatus) { + return new DataResponse($this->formatStatus($backupStatus)); + } + return new DataResponse([]); + } + + /** * @param UserStatus $status * @return array */ diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 1b7b44d95d3..b5dd3cd361a 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -496,25 +496,32 @@ class StatusService { } } - public function revertUserStatus(string $userId, string $messageId): void { + public function revertUserStatus(string $userId, string $messageId, bool $revertedManually = false): ?UserStatus { try { /** @var UserStatus $userStatus */ $backupUserStatus = $this->mapper->findByUserId($userId, true); } catch (DoesNotExistException $ex) { // No user status to revert, do nothing - return; + return null; } $deleted = $this->mapper->deleteCurrentStatusToRestoreBackup($userId, $messageId); if (!$deleted) { // Another status is set automatically or no status, do nothing - return; + return null; + } + + if ($revertedManually && $backupUserStatus->getStatus() === IUserStatus::OFFLINE) { + // When the user reverts the status manually they are online + $backupUserStatus->setStatus(IUserStatus::ONLINE); } $backupUserStatus->setIsBackup(false); // Remove the underscore prefix added when creating the backup $backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1)); $this->mapper->update($backupUserStatus); + + return $backupUserStatus; } public function revertMultipleUserStatus(array $userIds, string $messageId): void { diff --git a/apps/user_status/src/components/PredefinedStatus.vue b/apps/user_status/src/components/PredefinedStatus.vue index b5eafaed30b..2775051439c 100644 --- a/apps/user_status/src/components/PredefinedStatus.vue +++ b/apps/user_status/src/components/PredefinedStatus.vue @@ -104,7 +104,7 @@ export default { } &__clear-at { - opacity: .7; + color: var(--color-text-maxcontrast); &::before { content: ' – '; diff --git a/apps/user_status/src/components/PreviousStatus.vue b/apps/user_status/src/components/PreviousStatus.vue new file mode 100644 index 00000000000..6364277bdfb --- /dev/null +++ b/apps/user_status/src/components/PreviousStatus.vue @@ -0,0 +1,120 @@ +<!-- + - @copyright Copyright (c) 2020 Georg Ehrke <oc.list@georgehrke.com> + - @author Georg Ehrke <oc.list@georgehrke.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/>. + - + --> +<template> + <div class="predefined-status backup-status" + tabindex="0" + @keyup.enter="select" + @keyup.space="select" + @click="select"> + <span class="predefined-status__icon"> + {{ icon }} + </span> + <span class="predefined-status__message"> + {{ message }} + </span> + <span class="predefined-status__clear-at"> + {{ $t('user_status', 'Previously set') }} + </span> + + <div class="backup-status__reset-button"> + <NcButton @click="select"> + {{ $t('user_status', 'Reset status') }} + </NcButton> + </div> + </div> +</template> + +<script> +import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' + +export default { + name: 'PreviousStatus', + + components: { + NcButton, + }, + + props: { + icon: { + type: [String, null], + required: true, + }, + message: { + type: String, + required: true, + }, + }, + methods: { + /** + * Emits an event when the user clicks the row + */ + select() { + this.$emit('select') + }, + }, +} +</script> + +<style lang="scss" scoped> +.predefined-status { + display: flex; + flex-wrap: nowrap; + justify-content: flex-start; + flex-basis: 100%; + border-radius: var(--border-radius); + align-items: center; + min-height: 44px; + + &:hover, + &:focus { + background-color: var(--color-background-hover); + } + + &:active{ + background-color: var(--color-background-dark); + } + + &__icon { + flex-basis: 40px; + text-align: center; + } + + &__message { + font-weight: bold; + padding: 0 6px; + } + + &__clear-at { + color: var(--color-text-maxcontrast); + + &::before { + content: ' – '; + } + } +} +.backup-status { + &__reset-button { + justify-content: flex-end; + display: flex; + flex-grow: 1; + } +} +</style> diff --git a/apps/user_status/src/components/SetStatusModal.vue b/apps/user_status/src/components/SetStatusModal.vue index c3f0a793df3..f4b528a5a92 100644 --- a/apps/user_status/src/components/SetStatusModal.vue +++ b/apps/user_status/src/components/SetStatusModal.vue @@ -48,6 +48,14 @@ @submit="saveStatus" @select-icon="setIcon" /> </div> + <div v-if="messageId" + class="set-status-modal__automation-hint"> + {{ $t('user_status', 'Your status was set automatically') }} + </div> + <PreviousStatus v-if="messageId" + :icon="backupIcon" + :message="backupMessage" + @select="revertBackupFromServer" /> <PredefinedStatusesList @select-status="selectPredefinedMessage" /> <ClearAtSelect :clear-at="clearAt" @select-clear-at="setClearAt" /> @@ -78,6 +86,7 @@ import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' import { getAllStatusOptions } from '../services/statusOptionsService.js' import OnlineStatusMixin from '../mixins/OnlineStatusMixin.js' import PredefinedStatusesList from './PredefinedStatusesList.vue' +import PreviousStatus from './PreviousStatus.vue' import CustomMessageInput from './CustomMessageInput.vue' import ClearAtSelect from './ClearAtSelect.vue' import OnlineStatusSelect from './OnlineStatusSelect.vue' @@ -91,6 +100,7 @@ export default { NcModal, OnlineStatusSelect, PredefinedStatusesList, + PreviousStatus, NcButton, }, mixins: [OnlineStatusMixin], @@ -98,21 +108,53 @@ export default { data() { return { clearAt: null, - icon: null, - message: '', - messageId: '', isSavingStatus: false, statuses: getAllStatusOptions(), } }, + computed: { + messageId() { + return this.$store.state.userStatus.messageId + }, + icon() { + return this.$store.state.userStatus.icon + }, + message() { + return this.$store.state.userStatus.message || '' + }, + backupIcon() { + return this.$store.state.userBackupStatus.icon || '' + }, + backupMessage() { + return this.$store.state.userBackupStatus.message || '' + }, + + resetButtonText() { + if (this.backupIcon && this.backupMessage) { + return this.$t('user_status', 'Reset status to "{icon} {message}"', { + icon: this.backupIcon, + message: this.backupMessage, + }) + } else if (this.backupMessage) { + return this.$t('user_status', 'Reset status to "{message}"', { + message: this.backupMessage, + }) + } else if (this.backupIcon) { + return this.$t('user_status', 'Reset status to "{icon}"', { + icon: this.backupIcon, + }) + } + + return this.$t('user_status', 'Reset status') + }, + }, + /** * Loads the current status when a user opens dialog */ mounted() { - this.messageId = this.$store.state.userStatus.messageId - this.icon = this.$store.state.userStatus.icon - this.message = this.$store.state.userStatus.message || '' + this.$store.dispatch('fetchBackupFromServer') if (this.$store.state.userStatus.clearAt !== null) { this.clearAt = { @@ -222,6 +264,26 @@ export default { this.isSavingStatus = false this.closeModal() }, + /** + * + * @return {Promise<void>} + */ + async revertBackupFromServer() { + try { + this.isSavingStatus = true + + await this.$store.dispatch('revertBackupFromServer', { + messageId: this.messageId, + }) + } catch (err) { + showError(this.$t('user_status', 'There was an error reverting the status')) + console.debug(err) + this.isSavingStatus = false + return + } + + this.isSavingStatus = false + }, }, } </script> @@ -248,6 +310,13 @@ export default { margin-bottom: 10px; } + &__automation-hint { + display: flex; + width: 100%; + margin-bottom: 10px; + color: var(--color-text-maxcontrast); + } + .status-buttons { display: flex; padding: 3px; diff --git a/apps/user_status/src/services/statusService.js b/apps/user_status/src/services/statusService.js index f4bda930303..fb77866a4f4 100644 --- a/apps/user_status/src/services/statusService.js +++ b/apps/user_status/src/services/statusService.js @@ -36,6 +36,19 @@ const fetchCurrentStatus = async () => { } /** + * 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) @@ -90,10 +103,25 @@ const clearMessage = async () => { 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, } diff --git a/apps/user_status/src/store/index.js b/apps/user_status/src/store/index.js index caf4eb5f072..386967fe638 100644 --- a/apps/user_status/src/store/index.js +++ b/apps/user_status/src/store/index.js @@ -24,6 +24,7 @@ import Vue from 'vue' import Vuex, { Store } from 'vuex' import predefinedStatuses from './predefinedStatuses.js' import userStatus from './userStatus.js' +import userBackupStatus from './userBackupStatus.js' Vue.use(Vuex) @@ -31,6 +32,7 @@ export default new Store({ modules: { predefinedStatuses, userStatus, + userBackupStatus, }, strict: true, }) diff --git a/apps/user_status/src/store/userBackupStatus.js b/apps/user_status/src/store/userBackupStatus.js new file mode 100644 index 00000000000..c4258d998be --- /dev/null +++ b/apps/user_status/src/store/userBackupStatus.js @@ -0,0 +1,117 @@ +/** + * @copyright Copyright (c) 2020 Georg Ehrke + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * @author Joas Schilling <coding@schilljs.com> + * + * @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 { + fetchBackupStatus, + revertToBackupStatus, +} from '../services/statusService.js' +import { getCurrentUser } from '@nextcloud/auth' +import { emit } from '@nextcloud/event-bus' + +const state = { + // Status (online / away / dnd / invisible / offline) + status: null, + // Whether the status is user-defined + statusIsUserDefined: null, + // A custom message set by the user + message: null, + // The icon selected by the user + icon: null, + // When to automatically clean the status + clearAt: null, + // Whether the message is predefined + // (and can automatically be translated by Nextcloud) + messageIsPredefined: null, + // The id of the message in case it's predefined + messageId: null, +} + +const mutations = { + /** + * Loads the status from initial state + * + * @param {object} state The Vuex state + * @param {object} data The destructuring object + * @param {string} data.status The status type + * @param {boolean} data.statusIsUserDefined Whether or not this status is user-defined + * @param {string} data.message The message + * @param {string} data.icon The icon + * @param {number} data.clearAt When to automatically clear the status + * @param {boolean} data.messageIsPredefined Whether or not the message is predefined + * @param {string} data.messageId The id of the predefined message + */ + loadBackupStatusFromServer(state, { status, statusIsUserDefined, message, icon, clearAt, messageIsPredefined, messageId }) { + state.status = status + state.message = message + state.icon = icon + + // Don't overwrite certain values if the refreshing comes in via short updates + // E.g. from talk participant list which only has the status, message and icon + if (typeof statusIsUserDefined !== 'undefined') { + state.statusIsUserDefined = statusIsUserDefined + } + if (typeof clearAt !== 'undefined') { + state.clearAt = clearAt + } + if (typeof messageIsPredefined !== 'undefined') { + state.messageIsPredefined = messageIsPredefined + } + if (typeof messageId !== 'undefined') { + state.messageId = messageId + } + }, +} + +const getters = {} + +const actions = { + /** + * Re-fetches the status from the server + * + * @param {object} vuex The Vuex destructuring object + * @param {Function} vuex.commit The Vuex commit function + * @return {Promise<void>} + */ + async fetchBackupFromServer({ commit }) { + const status = await fetchBackupStatus(getCurrentUser()?.uid) + commit('loadBackupStatusFromServer', status) + }, + + async revertBackupFromServer({ commit }, { messageId }) { + const status = await revertToBackupStatus(messageId) + if (status) { + commit('loadBackupStatusFromServer', {}) + commit('loadStatusFromServer', status) + emit('user_status:status.updated', { + status: status.status, + message: status.message, + icon: status.icon, + clearAt: status.clearAt, + userId: getCurrentUser()?.uid, + }) + } + }, +} + +export default { state, mutations, getters, actions } diff --git a/apps/user_status/src/store/userStatus.js b/apps/user_status/src/store/userStatus.js index 92bc4986c52..4ca5bec95c8 100644 --- a/apps/user_status/src/store/userStatus.js +++ b/apps/user_status/src/store/userStatus.js @@ -35,7 +35,7 @@ import { emit } from '@nextcloud/event-bus' const state = { // Status (online / away / dnd / invisible / offline) status: null, - // Whether or not the status is user-defined + // Whether the status is user-defined statusIsUserDefined: null, // A custom message set by the user message: null, @@ -43,7 +43,7 @@ const state = { icon: null, // When to automatically clean the status clearAt: null, - // Whether or not the message is predefined + // Whether the message is predefined // (and can automatically be translated by Nextcloud) messageIsPredefined: null, // The id of the message in case it's predefined |