Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to revert automated status #36854

Merged
merged 8 commits into from
Apr 18, 2023
Merged
1 change: 1 addition & 0 deletions apps/user_status/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
['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
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getCapabilities() {
return [
'user_status' => [
'enabled' => true,
'restore' => true,
'supports_emoji' => $this->emojiHelper->doesPlatformSupportEmoji(),
],
];
Expand Down
13 changes: 13 additions & 0 deletions apps/user_status/lib/Controller/UserStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public function clearMessage(): DataResponse {
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
Expand Down
13 changes: 10 additions & 3 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,32 @@ public function backupCurrentStatus(string $userId): bool {
}
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion apps/user_status/src/components/PredefinedStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default {
}
&__clear-at {
opacity: .7;
color: var(--color-text-maxcontrast);
&::before {
content: '';
Expand Down
15 changes: 4 additions & 11 deletions apps/user_status/src/components/PredefinedStatusesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
-->

<template>
<div v-if="hasLoaded"
<div v-if="statusesHaveLoaded"
class="predefined-statuses-list">
<PredefinedStatus v-for="status in predefinedStatuses"
:key="status.id"
Expand All @@ -38,7 +38,7 @@

<script>
import PredefinedStatus from './PredefinedStatus.vue'
import { mapState } from 'vuex'
import { mapGetters, mapState } from 'vuex'
export default {
name: 'PredefinedStatusesList',
Expand All @@ -49,20 +49,13 @@ export default {
...mapState({
predefinedStatuses: state => state.predefinedStatuses.predefinedStatuses,
}),
/**
* Indicator whether the predefined statuses have already been loaded
*
* @return {boolean}
*/
hasLoaded() {
return this.predefinedStatuses.length > 0
},
...mapGetters(['statusesHaveLoaded']),
},
/**
* Loads all predefined statuses from the server
* when this component is mounted
*/
mounted() {
created() {
this.$store.dispatch('loadAllPredefinedStatuses')
},
methods: {
Expand Down
120 changes: 120 additions & 0 deletions apps/user_status/src/components/PreviousStatus.vue
Original file line number Diff line number Diff line change
@@ -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>
Loading