logo

pleroma-fe

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

notifications.js (5604B)


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