logo

pleroma-fe

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

notification_utils.js (5898B)


  1. import { muteWordHits } from '../status_parser/status_parser.js'
  2. import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js'
  3. import FaviconService from 'src/services/favicon_service/favicon_service.js'
  4. export const ACTIONABLE_NOTIFICATION_TYPES = new Set(['mention', 'pleroma:report', 'follow_request'])
  5. let cachedBadgeUrl = null
  6. export const notificationsFromStore = store => store.state.notifications.data
  7. export const visibleTypes = store => {
  8. // When called from within a module we need rootGetters to access wider scope
  9. // however when called from a component (i.e. this.$store) we already have wider scope
  10. const rootGetters = store.rootGetters || store.getters
  11. const { notificationVisibility } = rootGetters.mergedConfig
  12. return ([
  13. notificationVisibility.likes && 'like',
  14. notificationVisibility.mentions && 'mention',
  15. notificationVisibility.repeats && 'repeat',
  16. notificationVisibility.follows && 'follow',
  17. notificationVisibility.followRequest && 'follow_request',
  18. notificationVisibility.moves && 'move',
  19. notificationVisibility.emojiReactions && 'pleroma:emoji_reaction',
  20. notificationVisibility.reports && 'pleroma:report',
  21. notificationVisibility.polls && 'poll'
  22. ].filter(_ => _))
  23. }
  24. const statusNotifications = new Set(['like', 'mention', 'repeat', 'pleroma:emoji_reaction', 'poll'])
  25. export const isStatusNotification = (type) => statusNotifications.has(type)
  26. export const isValidNotification = (notification) => {
  27. if (isStatusNotification(notification.type) && !notification.status) {
  28. return false
  29. }
  30. return true
  31. }
  32. const sortById = (a, b) => {
  33. const seqA = Number(a.id)
  34. const seqB = Number(b.id)
  35. const isSeqA = !Number.isNaN(seqA)
  36. const isSeqB = !Number.isNaN(seqB)
  37. if (isSeqA && isSeqB) {
  38. return seqA > seqB ? -1 : 1
  39. } else if (isSeqA && !isSeqB) {
  40. return 1
  41. } else if (!isSeqA && isSeqB) {
  42. return -1
  43. } else {
  44. return a.id > b.id ? -1 : 1
  45. }
  46. }
  47. const isMutedNotification = (store, notification) => {
  48. if (!notification.status) return
  49. const rootGetters = store.rootGetters || store.getters
  50. return notification.status.muted || muteWordHits(notification.status, rootGetters.mergedConfig.muteWords).length > 0
  51. }
  52. export const maybeShowNotification = (store, notification) => {
  53. const rootState = store.rootState || store.state
  54. const rootGetters = store.rootGetters || store.getters
  55. if (notification.seen) return
  56. if (!visibleTypes(store).includes(notification.type)) return
  57. if (notification.type === 'mention' && isMutedNotification(store, notification)) return
  58. const notificationObject = prepareNotificationObject(notification, rootGetters.i18n)
  59. showDesktopNotification(rootState, notificationObject)
  60. }
  61. export const filteredNotificationsFromStore = (store, types) => {
  62. // map is just to clone the array since sort mutates it and it causes some issues
  63. const sortedNotifications = notificationsFromStore(store).map(_ => _).sort(sortById)
  64. // TODO implement sorting elsewhere and make it optional
  65. return sortedNotifications.filter(
  66. (notification) => (types || visibleTypes(store)).includes(notification.type)
  67. )
  68. }
  69. export const unseenNotificationsFromStore = store => {
  70. const rootGetters = store.rootGetters || store.getters
  71. const ignoreInactionableSeen = rootGetters.mergedConfig.ignoreInactionableSeen
  72. return filteredNotificationsFromStore(store).filter(({ seen, type }) => {
  73. if (!ignoreInactionableSeen) return !seen
  74. if (seen) return false
  75. return ACTIONABLE_NOTIFICATION_TYPES.has(type)
  76. })
  77. }
  78. export const prepareNotificationObject = (notification, i18n) => {
  79. if (cachedBadgeUrl === null) {
  80. const favicons = FaviconService.getOriginalFavicons()
  81. const favicon = favicons[favicons.length - 1]
  82. if (!favicon) {
  83. cachedBadgeUrl = 'about:blank'
  84. } else {
  85. cachedBadgeUrl = favicon.favimg.src
  86. }
  87. }
  88. const notifObj = {
  89. tag: notification.id,
  90. type: notification.type,
  91. badge: cachedBadgeUrl
  92. }
  93. const status = notification.status
  94. const title = notification.from_profile.name
  95. notifObj.title = title
  96. notifObj.icon = notification.from_profile.profile_image_url
  97. let i18nString
  98. switch (notification.type) {
  99. case 'like':
  100. i18nString = 'favorited_you'
  101. break
  102. case 'repeat':
  103. i18nString = 'repeated_you'
  104. break
  105. case 'follow':
  106. i18nString = 'followed_you'
  107. break
  108. case 'move':
  109. i18nString = 'migrated_to'
  110. break
  111. case 'follow_request':
  112. i18nString = 'follow_request'
  113. break
  114. case 'pleroma:report':
  115. i18nString = 'submitted_report'
  116. break
  117. case 'poll':
  118. i18nString = 'poll_ended'
  119. break
  120. }
  121. if (notification.type === 'pleroma:emoji_reaction') {
  122. notifObj.body = i18n.t('notifications.reacted_with', [notification.emoji])
  123. } else if (i18nString) {
  124. notifObj.body = i18n.t('notifications.' + i18nString)
  125. } else if (isStatusNotification(notification.type)) {
  126. notifObj.body = notification.status.text
  127. }
  128. // Shows first attached non-nsfw image, if any. Should add configuration for this somehow...
  129. if (status && status.attachments && status.attachments.length > 0 && !status.nsfw &&
  130. status.attachments[0].mimetype.startsWith('image/')) {
  131. notifObj.image = status.attachments[0].url
  132. }
  133. return notifObj
  134. }
  135. export const countExtraNotifications = (store) => {
  136. const rootGetters = store.rootGetters || store.getters
  137. const mergedConfig = rootGetters.mergedConfig
  138. if (!mergedConfig.showExtraNotifications) {
  139. return 0
  140. }
  141. return [
  142. mergedConfig.showChatsInExtraNotifications ? rootGetters.unreadChatCount : 0,
  143. mergedConfig.showAnnouncementsInExtraNotifications ? rootGetters.unreadAnnouncementCount : 0,
  144. mergedConfig.showFollowRequestsInExtraNotifications ? rootGetters.followRequestCount : 0
  145. ].reduce((a, c) => a + c, 0)
  146. }