Skip to content

Commit

Permalink
fixup! Make it possible to revert the automatic status
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Oct 25, 2021
1 parent 0eab804 commit 2a062f6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 18 deletions.
1 change: 1 addition & 0 deletions apps/user_status/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
['name' => 'UserStatus#clearMessage', 'url' => '/api/v1/user_status/message', 'verb' => 'DELETE'],
// Routes for getting your backup status (if it exists)
['name' => 'UserStatus#getBackupStatus', 'url' => '/api/v1/user_status_backup', 'verb' => 'GET'],
['name' => 'UserStatus#revert', 'url' => '/api/v1/user_status/revert', 'verb' => 'POST'],
// Routes for listing default routes
['name' => 'PredefinedStatus#findAll', 'url' => '/api/v1/predefined_statuses/', 'verb' => 'GET']
],
Expand Down
10 changes: 10 additions & 0 deletions apps/user_status/lib/Controller/UserStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ public function clearMessage(): DataResponse {
return new DataResponse([]);
}

/**
* @NoAdminRequired
* @param array $status The current status.
* @return DataResponse
*/
public function revert(array $status): DataResponse {
$this->service->revertUserStatus($this->userId, null, $status['status']);
return new DataResponse([]);
}

/**
* @param UserStatus $status
* @return array
Expand Down
5 changes: 3 additions & 2 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function backupCurrentStatus(string $userId): bool {
return true;
}

public function revertUserStatus(string $userId, string $messageId, string $status): void {
public function revertUserStatus(string $userId, ?string $messageId, string $status): void {
try {
/** @var UserStatus $userStatus */
$backupUserStatus = $this->mapper->findByUserId($userId, true);
Expand All @@ -467,7 +467,8 @@ public function revertUserStatus(string $userId, string $messageId, string $stat
}
try {
$userStatus = $this->mapper->findByUserId($userId);
if ($userStatus->getMessageId() !== $messageId || $userStatus->getStatus() !== $status) {
if (($messageId !== null && $userStatus->getMessageId() !== $messageId)
|| $userStatus->getStatus() !== $status) {
// Another status is set automatically, do nothing
return;
}
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/src/UserStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { getCurrentUser } from '@nextcloud/auth'
import { loadState } from '@nextcloud/initial-state'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import debounce from 'debounce'
import OnlineStatusMixin from './mixins/OnlineStatusMixin'
import { sendHeartbeat } from './services/heartbeatService'
Expand Down
28 changes: 18 additions & 10 deletions apps/user_status/src/components/ResetStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
</template>

<script>
import { mapState } from 'vuex'
import { mapState, mapActions } from 'vuex'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { getCurrentUser } from '@nextcloud/auth'
export default {
name: 'ResetStatus',
computed: {
...mapState({
backupStatus: state => state.backupStatus,
status: state => state.status,
userStatus: state => state.userStatus,
}),
/**
* Indicator whether the backup status has already been loaded
Expand Down Expand Up @@ -77,7 +78,7 @@ export default {
return this.$t('user_status', 'Offline')
}
return ''
}
},
},
/**
* Loads all predefined statuses from the server
Expand All @@ -92,15 +93,22 @@ export default {
},
methods: {
/**
* Emits an event when the user selects a status
* Revert the current status.
*/
revertCurrentStatus(status) {
this.$emit('selectStatus', status)
revertCurrentStatus() {
this.$store.dispatch('revertStatus', {
status: this.userStatus,
backupStatus: this.backupStatus,
})
},
handleUserStatusUpdated(state) {
if (OC.getCurrentUser().uid === state.userId) {
/**
* Handle change of status, and check if we still need to display the
* option to revert the current status.
* @param {Object} status The current status
*/
handleUserStatusUpdated(status) {
if (getCurrentUser().uid === status.userId) {
// Update backup information
// TODO optimize
this.$store.dispatch('loadBackupStatus')
}
},
Expand All @@ -111,7 +119,7 @@ export default {
<style lang="scss" scoped>
a {
font-weight: bold;
&::focus, &::hover {
&:focus, &:hover {
text-decoration: underline;
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/user_status/src/components/SetStatusModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
@select="changeStatus" />
</div>

<ResetStatus />

<!-- Status message -->
<div class="set-status-modal__header">
<h3>{{ $t('user_status', 'Status message') }}</h3>
Expand All @@ -59,7 +61,6 @@
<ClearAtSelect
:clear-at="clearAt"
@selectClearAt="setClearAt" />
<ResetStatus />
<div class="status-buttons">
<button class="status-buttons__select" :disabled="isSavingStatus" @click="clearStatus">
{{ $t('user_status', 'Clear status message') }}
Expand Down Expand Up @@ -244,7 +245,7 @@ export default {
min-height: 200px;
padding: 8px 20px 20px 20px;
// Enable scrollbar for too long content, same way as in Dashboard customize
max-height: 70vh;
max-height: 75vh;
overflow: auto;
&__header {
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/src/main-user-status-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const avatarDiv = document.getElementById('avatardiv-menu')
const propsData = {
preloadedUserStatus: {
message: avatarDiv.dataset.userstatus_message,
messageId: avatarDiv.dataset.userstatus_message_id,
icon: avatarDiv.dataset.userstatus_icon,
status: avatarDiv.dataset.userstatus,
},
Expand Down
10 changes: 6 additions & 4 deletions apps/user_status/src/services/statusService.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ const fetchCurrentBackupStatus = async() => {
/**
* Revert the current user status to the one who is in the backup.
*
* @param {String} statusType The status (online / away / dnd / invisible)
* @param {Object} status The current status
* @returns {Promise<void>}
*/
const setStatus = async(statusType) => {
const revertStatus = async(status) => {
console.debug(status)
const url = generateOcsUrl('apps/user_status/api/v1/user_status/revert')
await HttpClient.put(url, {
statusType,
await HttpClient.post(url, {
status,
})
}

Expand Down Expand Up @@ -122,4 +123,5 @@ export {
setCustomMessage,
setPredefinedMessage,
clearMessage,
revertStatus,
}
1 change: 1 addition & 0 deletions apps/user_status/src/store/backupUserStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const actions = {
async loadBackupStatus({ commit }) {
const status = await fetchCurrentBackupStatus()
if ('hasBackup' in status && status.hasBackup === false) {
commit('loadBackupStatusFromServer', {})
return
}
commit('loadBackupStatusFromServer', status)
Expand Down
19 changes: 19 additions & 0 deletions apps/user_status/src/store/userStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
setPredefinedMessage,
setCustomMessage,
clearMessage,
revertStatus,
} from '../services/statusService'
import { loadState } from '@nextcloud/initial-state'
import { getCurrentUser } from '@nextcloud/auth'
Expand Down Expand Up @@ -165,6 +166,24 @@ const actions = {
})
},

/**
* Sets a new status
*
* @param {Object} vuex The Vuex destructuring object
* @param {Function} vuex.commit The Vuex commit function
* @param {Object} vuex.state The Vuex state object
* @param {Object} vuex.dispatch The Vuex dispatch object
* @param {Object} data The data destructuring object
* @param {Object} data.status The current status
* @param {Object} data.backupStatus The backup status
* @returns {Promise<void>}
*/
async revertStatus({ commit, state, dispatch }, { status, backupStatus }) {
await revertStatus(status)
commit('loadStatusFromServer', backupStatus)
dispatch('loadBackupStatus')
},

/**
* Update status from 'user_status:status.updated' update.
* This doesn't trigger another 'user_status:status.updated'
Expand Down

0 comments on commit 2a062f6

Please sign in to comment.