diff --git a/src/components/notification/notification.js b/src/components/notification/notification.js index b641600ef..c4e219724 100644 --- a/src/components/notification/notification.js +++ b/src/components/notification/notification.js @@ -7,7 +7,7 @@ import Timeago from 'src/components/timeago/timeago.vue' import UserAvatar from 'src/components/user_avatar/user_avatar.vue' import UserLink from 'src/components/user_link/user_link.vue' import UserPopover from 'src/components/user_popover/user_popover.vue' -import { isStatusNotification } from '../../services/notification_utils/notification_utils.js' +import { isStatusNotification } from '../../services/notification_utils/notification_utils_sw.js' import { highlightClass, highlightStyle, diff --git a/src/services/entity_normalizer/entity_normalizer.service.js b/src/services/entity_normalizer/entity_normalizer.service.js index c893f135c..0b5370a06 100644 --- a/src/services/entity_normalizer/entity_normalizer.service.js +++ b/src/services/entity_normalizer/entity_normalizer.service.js @@ -4,7 +4,7 @@ import { unescape as lodashUnescape } from 'lodash' import punycode from 'punycode.js' import { fileType } from '../file_type/file_type.service.js' -import { isStatusNotification } from '../notification_utils/notification_utils.js' +import { isStatusNotification } from '../notification_utils/notification_utils_sw.js' /** NOTICE! ** * Do not initialize UI-generated data here. diff --git a/src/services/notification_utils/notification_utils.js b/src/services/notification_utils/notification_utils.js index 07433497a..5157504b2 100644 --- a/src/services/notification_utils/notification_utils.js +++ b/src/services/notification_utils/notification_utils.js @@ -1,18 +1,15 @@ import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js' import { muteFilterHits } from '../status_parser/status_parser.js' +import { prepareNotificationObject, isStatusNotification } from './notification_utils_sw.js' import { useNotificationsStore } from 'src/stores/notifications.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 - const visibleTypes = (notificationVisibility) => { return [ notificationVisibility.likes && 'like', @@ -28,17 +25,6 @@ const visibleTypes = (notificationVisibility) => { ].filter(Boolean) } -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 @@ -108,70 +94,6 @@ export const unseenNotifications = ( ) } -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.nsfw && status?.attachments?.[0]?.mimetype.startsWith('image/')) { - notifObj.image = status.attachments[0].url - } - - return notifObj -} - export const countExtraNotifications = ( store, mergedConfig, diff --git a/src/services/notification_utils/notification_utils_sw.js b/src/services/notification_utils/notification_utils_sw.js new file mode 100644 index 000000000..ca66df281 --- /dev/null +++ b/src/services/notification_utils/notification_utils_sw.js @@ -0,0 +1,78 @@ +import FaviconService from 'src/services/favicon_service/favicon_service.js' + +const statusNotifications = new Set([ + 'like', + 'mention', + 'status', + 'repeat', + 'pleroma:emoji_reaction', + 'poll', +]) + +let cachedBadgeUrl = null + +export const isStatusNotification = (type) => statusNotifications.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.nsfw && status?.attachments?.[0]?.mimetype.startsWith('image/')) { + notifObj.image = status.attachments[0].url + } + + return notifObj +} diff --git a/src/stores/notifications.js b/src/stores/notifications.js index 1099b0e83..c45f5b0a3 100644 --- a/src/stores/notifications.js +++ b/src/stores/notifications.js @@ -5,10 +5,12 @@ import { closeDesktopNotification, } from '../services/desktop_notification_utils/desktop_notification_utils.js' import { - isStatusNotification, isValidNotification, maybeShowNotification, } from '../services/notification_utils/notification_utils.js' +import { + isStatusNotification, +} from '../services/notification_utils/notification_utils_sw.js' import { useI18nStore } from 'src/stores/i18n.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' diff --git a/src/sw.js b/src/sw.js index 908e86567..48a4c5e60 100644 --- a/src/sw.js +++ b/src/sw.js @@ -8,7 +8,7 @@ import { createI18n } from 'vue-i18n' import { storage } from 'src/lib/storage.js' import { INSTANCE_DEFAULT_CONFIG } from 'src/modules/default_config_state.js' import { parseNotification } from 'src/services/entity_normalizer/entity_normalizer.service.js' -import { prepareNotificationObject } from 'src/services/notification_utils/notification_utils.js' +import { prepareNotificationObject } from 'src/services/notification_utils/notification_utils_sw.js' import { cacheKey, emojiCacheKey, shouldCache } from 'src/services/sw/sw.js' // Collects all messages for service workers