|
| 1 | +/** |
| 2 | + * @copyright Copyright (c) 2020 Georg Ehrke |
| 3 | + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> |
| 4 | + * |
| 5 | + * @author Georg Ehrke <oc.list@georgehrke.com> |
| 6 | + * @author Joas Schilling <coding@schilljs.com> |
| 7 | + * |
| 8 | + * @license AGPL-3.0-or-later |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +import { |
| 26 | + fetchBackupStatus, |
| 27 | + revertToBackupStatus, |
| 28 | +} from '../services/statusService' |
| 29 | +import { getCurrentUser } from '@nextcloud/auth' |
| 30 | +import { emit } from '@nextcloud/event-bus' |
| 31 | + |
| 32 | +const state = { |
| 33 | + // Status (online / away / dnd / invisible / offline) |
| 34 | + status: null, |
| 35 | + // Whether the status is user-defined |
| 36 | + statusIsUserDefined: null, |
| 37 | + // A custom message set by the user |
| 38 | + message: null, |
| 39 | + // The icon selected by the user |
| 40 | + icon: null, |
| 41 | + // When to automatically clean the status |
| 42 | + clearAt: null, |
| 43 | + // Whether the message is predefined |
| 44 | + // (and can automatically be translated by Nextcloud) |
| 45 | + messageIsPredefined: null, |
| 46 | + // The id of the message in case it's predefined |
| 47 | + messageId: null, |
| 48 | +} |
| 49 | + |
| 50 | +const mutations = { |
| 51 | + /** |
| 52 | + * Loads the status from initial state |
| 53 | + * |
| 54 | + * @param {object} state The Vuex state |
| 55 | + * @param {object} data The destructuring object |
| 56 | + * @param {string} data.status The status type |
| 57 | + * @param {boolean} data.statusIsUserDefined Whether or not this status is user-defined |
| 58 | + * @param {string} data.message The message |
| 59 | + * @param {string} data.icon The icon |
| 60 | + * @param {number} data.clearAt When to automatically clear the status |
| 61 | + * @param {boolean} data.messageIsPredefined Whether or not the message is predefined |
| 62 | + * @param {string} data.messageId The id of the predefined message |
| 63 | + */ |
| 64 | + loadBackupStatusFromServer(state, { status, statusIsUserDefined, message, icon, clearAt, messageIsPredefined, messageId }) { |
| 65 | + state.status = status |
| 66 | + state.message = message |
| 67 | + state.icon = icon |
| 68 | + |
| 69 | + // Don't overwrite certain values if the refreshing comes in via short updates |
| 70 | + // E.g. from talk participant list which only has the status, message and icon |
| 71 | + if (typeof statusIsUserDefined !== 'undefined') { |
| 72 | + state.statusIsUserDefined = statusIsUserDefined |
| 73 | + } |
| 74 | + if (typeof clearAt !== 'undefined') { |
| 75 | + state.clearAt = clearAt |
| 76 | + } |
| 77 | + if (typeof messageIsPredefined !== 'undefined') { |
| 78 | + state.messageIsPredefined = messageIsPredefined |
| 79 | + } |
| 80 | + if (typeof messageId !== 'undefined') { |
| 81 | + state.messageId = messageId |
| 82 | + } |
| 83 | + }, |
| 84 | +} |
| 85 | + |
| 86 | +const getters = {} |
| 87 | + |
| 88 | +const actions = { |
| 89 | + /** |
| 90 | + * Re-fetches the status from the server |
| 91 | + * |
| 92 | + * @param {object} vuex The Vuex destructuring object |
| 93 | + * @param {Function} vuex.commit The Vuex commit function |
| 94 | + * @return {Promise<void>} |
| 95 | + */ |
| 96 | + async fetchBackupFromServer({ commit }) { |
| 97 | + const status = await fetchBackupStatus(getCurrentUser()?.uid) |
| 98 | + commit('loadBackupStatusFromServer', status) |
| 99 | + }, |
| 100 | + |
| 101 | + async revertBackupFromServer({ commit }, { messageId }) { |
| 102 | + const status = await revertToBackupStatus(messageId) |
| 103 | + if (status) { |
| 104 | + commit('loadBackupStatusFromServer', {}) |
| 105 | + commit('loadStatusFromServer', status) |
| 106 | + emit('user_status:status.updated', { |
| 107 | + status: status.status, |
| 108 | + message: status.message, |
| 109 | + icon: status.icon, |
| 110 | + clearAt: status.clearAt, |
| 111 | + userId: getCurrentUser()?.uid, |
| 112 | + }) |
| 113 | + } |
| 114 | + }, |
| 115 | +} |
| 116 | + |
| 117 | +export default { state, mutations, getters, actions } |
0 commit comments