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 (6027B)


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