logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

notifications.js (5703B)


  1. import apiService from '../services/api/api.service.js'
  2. import {
  3. isStatusNotification,
  4. isValidNotification,
  5. maybeShowNotification
  6. } from '../services/notification_utils/notification_utils.js'
  7. import {
  8. closeDesktopNotification,
  9. closeAllDesktopNotifications
  10. } from '../services/desktop_notification_utils/desktop_notification_utils.js'
  11. import { useReportsStore } from 'src/stores/reports.js'
  12. const emptyNotifications = () => ({
  13. desktopNotificationSilence: true,
  14. maxId: 0,
  15. minId: Number.POSITIVE_INFINITY,
  16. data: [],
  17. idStore: {},
  18. loading: false
  19. })
  20. export const defaultState = () => ({
  21. ...emptyNotifications()
  22. })
  23. export const notifications = {
  24. state: defaultState(),
  25. mutations: {
  26. addNewNotifications (state, { notifications }) {
  27. notifications.forEach(notification => {
  28. state.data.push(notification)
  29. state.idStore[notification.id] = notification
  30. })
  31. },
  32. clearNotifications (state) {
  33. const blankState = defaultState()
  34. Object.keys(state).forEach(k => {
  35. state[k] = blankState[k]
  36. })
  37. },
  38. updateNotificationsMinMaxId (state, id) {
  39. state.maxId = id > state.maxId ? id : state.maxId
  40. state.minId = id < state.minId ? id : state.minId
  41. },
  42. setNotificationsLoading (state, { value }) {
  43. state.loading = value
  44. },
  45. setNotificationsSilence (state, { value }) {
  46. state.desktopNotificationSilence = value
  47. },
  48. markNotificationsAsSeen (state) {
  49. state.data.forEach((notification) => {
  50. notification.seen = true
  51. })
  52. },
  53. markSingleNotificationAsSeen (state, { id }) {
  54. const notification = state.idStore[id]
  55. if (notification) notification.seen = true
  56. },
  57. dismissNotification (state, { id }) {
  58. state.data = state.data.filter(n => n.id !== id)
  59. delete state.idStore[id]
  60. },
  61. updateNotification (state, { id, updater }) {
  62. const notification = state.idStore[id]
  63. notification && updater(notification)
  64. }
  65. },
  66. actions: {
  67. addNewNotifications (store, { notifications }) {
  68. const { commit, dispatch, state, rootState } = store
  69. const validNotifications = notifications.filter((notification) => {
  70. // If invalid notification, update ids but don't add it to store
  71. if (!isValidNotification(notification)) {
  72. console.error('Invalid notification:', notification)
  73. commit('updateNotificationsMinMaxId', notification.id)
  74. return false
  75. }
  76. return true
  77. })
  78. const statusNotifications = validNotifications.filter(notification => isStatusNotification(notification.type) && notification.status)
  79. // Synchronous commit to add all the statuses
  80. commit('addNewStatuses', { statuses: statusNotifications.map(notification => notification.status) })
  81. // Update references to statuses in notifications to ones in the store
  82. statusNotifications.forEach(notification => {
  83. const id = notification.status.id
  84. const referenceStatus = rootState.statuses.allStatusesObject[id]
  85. if (referenceStatus) {
  86. notification.status = referenceStatus
  87. }
  88. })
  89. validNotifications.forEach(notification => {
  90. if (notification.type === 'pleroma:report') {
  91. useReportsStore().addReport(notification.report)
  92. }
  93. if (notification.type === 'pleroma:emoji_reaction') {
  94. dispatch('fetchEmojiReactionsBy', notification.status.id)
  95. }
  96. // Only add a new notification if we don't have one for the same action
  97. // eslint-disable-next-line no-prototype-builtins
  98. if (!state.idStore.hasOwnProperty(notification.id)) {
  99. commit('updateNotificationsMinMaxId', notification.id)
  100. commit('addNewNotifications', { notifications: [notification] })
  101. maybeShowNotification(store, notification)
  102. } else if (notification.seen) {
  103. state.idStore[notification.id].seen = true
  104. }
  105. })
  106. },
  107. notificationClicked ({ state, dispatch }, { id }) {
  108. const notification = state.idStore[id]
  109. const { type, seen } = notification
  110. if (!seen) {
  111. switch (type) {
  112. case 'mention':
  113. case 'pleroma:report':
  114. case 'follow_request':
  115. break
  116. default:
  117. dispatch('markSingleNotificationAsSeen', { id })
  118. }
  119. }
  120. },
  121. setNotificationsLoading ({ commit }, { value }) {
  122. commit('setNotificationsLoading', { value })
  123. },
  124. setNotificationsSilence ({ commit }, { value }) {
  125. commit('setNotificationsSilence', { value })
  126. },
  127. markNotificationsAsSeen ({ rootState, state, commit }) {
  128. commit('markNotificationsAsSeen')
  129. apiService.markNotificationsAsSeen({
  130. id: state.maxId,
  131. credentials: rootState.users.currentUser.credentials
  132. }).then(() => {
  133. closeAllDesktopNotifications(rootState)
  134. })
  135. },
  136. markSingleNotificationAsSeen ({ rootState, commit }, { id }) {
  137. commit('markSingleNotificationAsSeen', { id })
  138. apiService.markNotificationsAsSeen({
  139. single: true,
  140. id,
  141. credentials: rootState.users.currentUser.credentials
  142. }).then(() => {
  143. closeDesktopNotification(rootState, { id })
  144. })
  145. },
  146. dismissNotificationLocal ({ commit }, { id }) {
  147. commit('dismissNotification', { id })
  148. },
  149. dismissNotification ({ rootState, commit }, { id }) {
  150. commit('dismissNotification', { id })
  151. rootState.api.backendInteractor.dismissNotification({ id })
  152. },
  153. updateNotification ({ commit }, { id, updater }) {
  154. commit('updateNotification', { id, updater })
  155. }
  156. }
  157. }
  158. export default notifications