aboutsummaryrefslogtreecommitdiff
path: root/src/services/notification_utils/notification_utils.js
blob: 831b27f63b2bb1758d6a4915efbf92592e8db0bd (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
170
171
172
173
174
175
import { muteWordHits } from '../status_parser/status_parser.js'
import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js'

import FaviconService from 'src/services/favicon_service/favicon_service.js'

export const ACTIONABLE_NOTIFICATION_TYPES = new Set(['mention', 'pleroma:report', 'follow_request'])

let cachedBadgeUrl = null

export const notificationsFromStore = store => store.state.notifications.data

export const visibleTypes = store => {
  // When called from within a module we need rootGetters to access wider scope
  // however when called from a component (i.e. this.$store) we already have wider scope
  const rootGetters = store.rootGetters || store.getters
  const { notificationVisibility } = rootGetters.mergedConfig

  return ([
    notificationVisibility.likes && 'like',
    notificationVisibility.mentions && 'mention',
    notificationVisibility.statuses && 'status',
    notificationVisibility.repeats && 'repeat',
    notificationVisibility.follows && 'follow',
    notificationVisibility.followRequest && 'follow_request',
    notificationVisibility.moves && 'move',
    notificationVisibility.emojiReactions && 'pleroma:emoji_reaction',
    notificationVisibility.reports && 'pleroma:report',
    notificationVisibility.polls && 'poll'
  ].filter(_ => _))
}

const statusNotifications = new Set(['like', 'mention', 'status', 'repeat', 'pleroma:emoji_reaction', 'poll'])

export const isStatusNotification = (type) => statusNotifications.has(type)

export const isValidNotification = (notification) => {
  if (isStatusNotification(notification.type) && !notification.status) {
    return false
  }
  return true
}

const sortById = (a, b) => {
  const seqA = Number(a.id)
  const seqB = Number(b.id)
  const isSeqA = !Number.isNaN(seqA)
  const isSeqB = !Number.isNaN(seqB)
  if (isSeqA && isSeqB) {
    return seqA > seqB ? -1 : 1
  } else if (isSeqA && !isSeqB) {
    return 1
  } else if (!isSeqA && isSeqB) {
    return -1
  } else {
    return a.id > b.id ? -1 : 1
  }
}

const isMutedNotification = (store, notification) => {
  if (!notification.status) return
  const rootGetters = store.rootGetters || store.getters
  return notification.status.muted || muteWordHits(notification.status, rootGetters.mergedConfig.muteWords).length > 0
}

export const maybeShowNotification = (store, notification) => {
  const rootState = store.rootState || store.state
  const rootGetters = store.rootGetters || store.getters

  if (notification.seen) return
  if (!visibleTypes(store).includes(notification.type)) return
  if (notification.type === 'mention' && isMutedNotification(store, notification)) return

  const notificationObject = prepareNotificationObject(notification, rootGetters.i18n)
  showDesktopNotification(rootState, notificationObject)
}

export const filteredNotificationsFromStore = (store, types) => {
  // map is just to clone the array since sort mutates it and it causes some issues
  const sortedNotifications = notificationsFromStore(store).map(_ => _).sort(sortById)
  // TODO implement sorting elsewhere and make it optional
  return sortedNotifications.filter(
    (notification) => (types || visibleTypes(store)).includes(notification.type)
  )
}

export const unseenNotificationsFromStore = store => {
  const rootGetters = store.rootGetters || store.getters
  const ignoreInactionableSeen = rootGetters.mergedConfig.ignoreInactionableSeen

  return filteredNotificationsFromStore(store).filter(({ seen, type }) => {
    if (!ignoreInactionableSeen) return !seen
    if (seen) return false
    return ACTIONABLE_NOTIFICATION_TYPES.has(type)
  })
}

export const prepareNotificationObject = (notification, i18n) => {
  if (cachedBadgeUrl === null) {
    const favicons = FaviconService.getOriginalFavicons()
    const favicon = favicons[favicons.length - 1]
    if (!favicon) {
      cachedBadgeUrl = 'about:blank'
    } else {
      cachedBadgeUrl = favicon.favimg.src
    }
  }

  const notifObj = {
    tag: notification.id,
    type: notification.type,
    badge: cachedBadgeUrl
  }
  const status = notification.status
  const title = notification.from_profile.name
  notifObj.title = title
  notifObj.icon = notification.from_profile.profile_image_url
  let i18nString
  switch (notification.type) {
    case 'like':
      i18nString = 'favorited_you'
      break
    case 'status':
      i18nString = 'subscribed_status'
      break
    case 'repeat':
      i18nString = 'repeated_you'
      break
    case 'follow':
      i18nString = 'followed_you'
      break
    case 'move':
      i18nString = 'migrated_to'
      break
    case 'follow_request':
      i18nString = 'follow_request'
      break
    case 'pleroma:report':
      i18nString = 'submitted_report'
      break
    case 'poll':
      i18nString = 'poll_ended'
      break
  }

  if (notification.type === 'pleroma:emoji_reaction') {
    notifObj.body = i18n.t('notifications.reacted_with', [notification.emoji])
  } else if (i18nString) {
    notifObj.body = i18n.t('notifications.' + i18nString)
  } else if (isStatusNotification(notification.type)) {
    notifObj.body = notification.status.text
  }

  // Shows first attached non-nsfw image, if any. Should add configuration for this somehow...
  if (status && status.attachments && status.attachments.length > 0 && !status.nsfw &&
    status.attachments[0].mimetype.startsWith('image/')) {
    notifObj.image = status.attachments[0].url
  }

  return notifObj
}

export const countExtraNotifications = (store) => {
  const rootGetters = store.rootGetters || store.getters
  const mergedConfig = rootGetters.mergedConfig

  if (!mergedConfig.showExtraNotifications) {
    return 0
  }

  return [
    mergedConfig.showChatsInExtraNotifications ? rootGetters.unreadChatCount : 0,
    mergedConfig.showAnnouncementsInExtraNotifications ? rootGetters.unreadAnnouncementCount : 0,
    mergedConfig.showFollowRequestsInExtraNotifications ? rootGetters.followRequestCount : 0
  ].reduce((a, c) => a + c, 0)
}