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 @@ -184,6 +184,19 @@ class UserStatusController extends OCSController { return new DataResponse([]); } + /** + * @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 @@ + + + + + + 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" /> +
+ {{ $t('user_status', 'Your status was set automatically') }} +
+ @@ -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} + */ + 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 + }, }, } @@ -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 @@ -35,6 +35,19 @@ const fetchCurrentStatus = async () => { return response.data.ocs.data } +/** + * Fetches the current user-status + * + * @param {string} userId + * @return {Promise} + */ +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 * @@ -90,10 +103,25 @@ const clearMessage = async () => { await HttpClient.delete(url) } +/** + * Revert the automated status + * + * @param {string} messageId + * @return {Promise} + */ +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 + * + * @author Georg Ehrke + * @author Joas Schilling + * + * @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 . + * + */ + +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} + */ + 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