aboutsummaryrefslogtreecommitdiff
path: root/src/modules/notifications.js
blob: c1f5a63e730ac26ac1eca811e263af219459ff63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import apiService from '../services/api/api.service.js'

import {
  isStatusNotification,
  isValidNotification,
  maybeShowNotification
} from '../services/notification_utils/notification_utils.js'

import {
  closeDesktopNotification,
  closeAllDesktopNotifications
} from '../services/desktop_notification_utils/desktop_notification_utils.js'

const emptyNotifications = () => ({
  desktopNotificationSilence: true,
  maxId: 0,
  minId: Number.POSITIVE_INFINITY,
  data: [],
  idStore: {},
  loading: false
})

export const defaultState = () => ({
  ...emptyNotifications()
})

export const notifications = {
  state: defaultState(),
  mutations: {
    addNewNotifications (state, { notifications }) {
      notifications.forEach(notification => {
        state.data.push(notification)
        state.idStore[notification.id] = notification
      })
    },
    clearNotifications (state) {
      state = emptyNotifications()
    },
    updateNotificationsMinMaxId (state, id) {
      state.maxId = id > state.maxId ? id : state.maxId
      state.minId = id < state.minId ? id : state.minId
    },
    setNotificationsLoading (state, { value }) {
      state.loading = value
    },
    setNotificationsSilence (state, { value }) {
      state.desktopNotificationSilence = value
    },
    markNotificationsAsSeen (state) {
      state.data.forEach((notification) => {
        notification.seen = true
      })
    },
    markSingleNotificationAsSeen (state, { id }) {
      const notification = state.idStore[id]
      if (notification) notification.seen = true
    },
    dismissNotification (state, { id }) {
      state.data = state.data.filter(n => n.id !== id)
      delete state.idStore[id]
    },
    updateNotification (state, { id, updater }) {
      const notification = state.idStore[id]
      notification && updater(notification)
    }
  },
  actions: {
    addNewNotifications (store, { notifications, older }) {
      const { commit, dispatch, state, rootState } = store
      const validNotifications = notifications.filter((notification) => {
        // If invalid notification, update ids but don't add it to store
        if (!isValidNotification(notification)) {
          console.error('Invalid notification:', notification)
          commit('updateNotificationsMinMaxId', notification.id)
          return false
        }
        return true
      })

      const statusNotifications = validNotifications.filter(notification => isStatusNotification(notification.type) && notification.status)

      // Synchronous commit to add all the statuses
      commit('addNewStatuses', { statuses: statusNotifications.map(notification => notification.status) })

      // Update references to statuses in notifications to ones in the store
      statusNotifications.forEach(notification => {
        const id = notification.status.id
        const referenceStatus = rootState.statuses.allStatusesObject[id]

        if (referenceStatus) {
          notification.status = referenceStatus
        }
      })

      validNotifications.forEach(notification => {
        if (notification.type === 'pleroma:report') {
          dispatch('addReport', notification.report)
        }

        if (notification.type === 'pleroma:emoji_reaction') {
          dispatch('fetchEmojiReactionsBy', notification.status.id)
        }

        // Only add a new notification if we don't have one for the same action
        // eslint-disable-next-line no-prototype-builtins
        if (!state.idStore.hasOwnProperty(notification.id)) {
          commit('updateNotificationsMinMaxId', notification.id)
          commit('addNewNotifications', { notifications: [notification] })

          maybeShowNotification(store, notification)
        } else if (notification.seen) {
          state.idStore[notification.id].seen = true
        }
      })
    },
    notificationClicked ({ state, dispatch }, { id }) {
      const notification = state.idStore[id]
      const { type, seen } = notification

      if (!seen) {
        switch (type) {
          case 'mention':
          case 'pleroma:report':
          case 'follow_request':
            break
          default:
            dispatch('markSingleNotificationAsSeen', { id })
        }
      }
    },
    setNotificationsLoading ({ rootState, commit }, { value }) {
      commit('setNotificationsLoading', { value })
    },
    setNotificationsSilence ({ rootState, commit }, { value }) {
      commit('setNotificationsSilence', { value })
    },
    markNotificationsAsSeen ({ rootState, state, commit }) {
      commit('markNotificationsAsSeen')
      apiService.markNotificationsAsSeen({
        id: state.maxId,
        credentials: rootState.users.currentUser.credentials
      }).then(() => {
        closeAllDesktopNotifications(rootState)
      })
    },
    markSingleNotificationAsSeen ({ rootState, commit }, { id }) {
      commit('markSingleNotificationAsSeen', { id })
      apiService.markNotificationsAsSeen({
        single: true,
        id,
        credentials: rootState.users.currentUser.credentials
      }).then(() => {
        closeDesktopNotification(rootState, { id })
      })
    },
    dismissNotificationLocal ({ rootState, commit }, { id }) {
      commit('dismissNotification', { id })
    },
    dismissNotification ({ rootState, commit }, { id }) {
      commit('dismissNotification', { id })
      rootState.api.backendInteractor.dismissNotification({ id })
    },
    updateNotification ({ rootState, commit }, { id, updater }) {
      commit('updateNotification', { id, updater })
    }
  }
}

export default notifications