You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

userStatus.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @copyright Copyright (c) 2020 Georg Ehrke
  3. *
  4. * @author Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import {
  23. fetchCurrentStatus,
  24. setStatus,
  25. setPredefinedMessage,
  26. setCustomMessage,
  27. clearMessage,
  28. } from '../services/statusService'
  29. import { loadState } from '@nextcloud/initial-state'
  30. import { getTimestampForClearAt } from '../services/clearAtService'
  31. const state = {
  32. // Status (online / away / dnd / invisible / offline)
  33. status: null,
  34. // Whether or not the status is user-defined
  35. statusIsUserDefined: null,
  36. // A custom message set by the user
  37. message: null,
  38. // The icon selected by the user
  39. icon: null,
  40. // When to automatically clean the status
  41. clearAt: null,
  42. // Whether or not the message is predefined
  43. // (and can automatically be translated by Nextcloud)
  44. messageIsPredefined: null,
  45. // The id of the message in case it's predefined
  46. messageId: null,
  47. }
  48. const mutations = {
  49. /**
  50. * Sets a new status
  51. *
  52. * @param {Object} state The Vuex state
  53. * @param {Object} data The destructuring object
  54. * @param {String} data.statusType The new status type
  55. */
  56. setStatus(state, { statusType }) {
  57. state.status = statusType
  58. state.statusIsUserDefined = true
  59. },
  60. /**
  61. * Sets a message using a predefined message
  62. *
  63. * @param {Object} state The Vuex state
  64. * @param {Object} data The destructuring object
  65. * @param {String} data.messageId The messageId
  66. * @param {Number|null} data.clearAt When to automatically clear the status
  67. * @param {String} data.message The message
  68. * @param {String} data.icon The icon
  69. */
  70. setPredefinedMessage(state, { messageId, clearAt, message, icon }) {
  71. state.messageId = messageId
  72. state.messageIsPredefined = true
  73. state.message = message
  74. state.icon = icon
  75. state.clearAt = clearAt
  76. },
  77. /**
  78. * Sets a custom message
  79. *
  80. * @param {Object} state The Vuex state
  81. * @param {Object} data The destructuring object
  82. * @param {String} data.message The message
  83. * @param {String} data.icon The icon
  84. * @param {Number} data.clearAt When to automatically clear the status
  85. */
  86. setCustomMessage(state, { message, icon, clearAt }) {
  87. state.messageId = null
  88. state.messageIsPredefined = false
  89. state.message = message
  90. state.icon = icon
  91. state.clearAt = clearAt
  92. },
  93. /**
  94. * Clears the status
  95. *
  96. * @param {Object} state The Vuex state
  97. */
  98. clearMessage(state) {
  99. state.messageId = null
  100. state.messageIsPredefined = false
  101. state.message = null
  102. state.icon = null
  103. state.clearAt = null
  104. },
  105. /**
  106. * Loads the status from initial state
  107. *
  108. * @param {Object} state The Vuex state
  109. * @param {Object} data The destructuring object
  110. * @param {String} data.status The status type
  111. * @param {Boolean} data.statusIsUserDefined Whether or not this status is user-defined
  112. * @param {String} data.message The message
  113. * @param {String} data.icon The icon
  114. * @param {Number} data.clearAt When to automatically clear the status
  115. * @param {Boolean} data.messageIsPredefined Whether or not the message is predefined
  116. * @param {string} data.messageId The id of the predefined message
  117. */
  118. loadStatusFromServer(state, { status, statusIsUserDefined, message, icon, clearAt, messageIsPredefined, messageId }) {
  119. state.status = status
  120. state.statusIsUserDefined = statusIsUserDefined
  121. state.message = message
  122. state.icon = icon
  123. state.clearAt = clearAt
  124. state.messageIsPredefined = messageIsPredefined
  125. state.messageId = messageId
  126. },
  127. }
  128. const getters = {}
  129. const actions = {
  130. /**
  131. * Sets a new status
  132. *
  133. * @param {Object} vuex The Vuex destructuring object
  134. * @param {Function} vuex.commit The Vuex commit function
  135. * @param {Object} data The data destructuring object
  136. * @param {String} data.statusType The new status type
  137. * @returns {Promise<void>}
  138. */
  139. async setStatus({ commit }, { statusType }) {
  140. await setStatus(statusType)
  141. commit('setStatus', { statusType })
  142. },
  143. /**
  144. * Sets a message using a predefined message
  145. *
  146. * @param {Object} vuex The Vuex destructuring object
  147. * @param {Function} vuex.commit The Vuex commit function
  148. * @param {Object} vuex.rootState The Vuex root state
  149. * @param {Object} data The data destructuring object
  150. * @param {String} data.messageId The messageId
  151. * @param {Object|null} data.clearAt When to automatically clear the status
  152. * @returns {Promise<void>}
  153. */
  154. async setPredefinedMessage({ commit, rootState }, { messageId, clearAt }) {
  155. const resolvedClearAt = getTimestampForClearAt(clearAt)
  156. await setPredefinedMessage(messageId, resolvedClearAt)
  157. const status = rootState.predefinedStatuses.predefinedStatuses.find((status) => status.id === messageId)
  158. const { message, icon } = status
  159. commit('setPredefinedMessage', { messageId, clearAt: resolvedClearAt, message, icon })
  160. },
  161. /**
  162. * Sets a custom message
  163. *
  164. * @param {Object} vuex The Vuex destructuring object
  165. * @param {Function} vuex.commit The Vuex commit function
  166. * @param {Object} data The data destructuring object
  167. * @param {String} data.message The message
  168. * @param {String} data.icon The icon
  169. * @param {Object|null} data.clearAt When to automatically clear the status
  170. * @returns {Promise<void>}
  171. */
  172. async setCustomMessage({ commit }, { message, icon, clearAt }) {
  173. const resolvedClearAt = getTimestampForClearAt(clearAt)
  174. await setCustomMessage(message, icon, resolvedClearAt)
  175. commit('setCustomMessage', { message, icon, clearAt: resolvedClearAt })
  176. },
  177. /**
  178. * Clears the status
  179. *
  180. * @param {Object} vuex The Vuex destructuring object
  181. * @param {Function} vuex.commit The Vuex commit function
  182. * @returns {Promise<void>}
  183. */
  184. async clearMessage({ commit }) {
  185. await clearMessage()
  186. commit('clearMessage')
  187. },
  188. /**
  189. * Re-fetches the status from the server
  190. *
  191. * @param {Object} vuex The Vuex destructuring object
  192. * @param {Function} vuex.commit The Vuex commit function
  193. * @returns {Promise<void>}
  194. */
  195. async reFetchStatusFromServer({ commit }) {
  196. const status = await fetchCurrentStatus()
  197. commit('loadStatusFromServer', status)
  198. },
  199. /**
  200. * Loads the server from the initial state
  201. *
  202. * @param {Object} vuex The Vuex destructuring object
  203. * @param {Function} vuex.commit The Vuex commit function
  204. */
  205. loadStatusFromInitialState({ commit }) {
  206. const status = loadState('user_status', 'status')
  207. commit('loadStatusFromServer', status)
  208. },
  209. }
  210. export default { state, mutations, getters, actions }