feat: 💨

This commit is contained in:
codytseng
2025-09-10 22:15:31 +08:00
parent 5f165308c1
commit 63869ef3b7
5 changed files with 72 additions and 28 deletions

View File

@@ -1,6 +1,6 @@
import Image from '@/components/Image'
import { useFetchEvent } from '@/hooks'
import { generateBech32IdFromETag, tagNameEquals } from '@/lib/tag'
import { generateBech32IdFromATag, generateBech32IdFromETag, tagNameEquals } from '@/lib/tag'
import { useNostr } from '@/providers/NostrProvider'
import { Heart } from 'lucide-react'
import { Event } from 'nostr-tools'
@@ -18,9 +18,10 @@ export function ReactionNotification({
const { t } = useTranslation()
const { pubkey } = useNostr()
const eventId = useMemo(() => {
const targetPubkey = notification.tags.findLast(tagNameEquals('p'))?.[1]
if (targetPubkey !== pubkey) return undefined
const aTag = notification.tags.findLast(tagNameEquals('a'))
if (aTag) {
return generateBech32IdFromATag(aTag)
}
const eTag = notification.tags.findLast(tagNameEquals('e'))
return eTag ? generateBech32IdFromETag(eTag) : undefined
}, [notification, pubkey])

View File

@@ -1,7 +1,9 @@
import { ExtendedKind } from '@/constants'
import { isMentioningMutedUsers } from '@/lib/event'
import { notificationFilter } from '@/lib/notification'
import { useContentPolicy } from '@/providers/ContentPolicyProvider'
import { useMuteList } from '@/providers/MuteListProvider'
import { useNostr } from '@/providers/NostrProvider'
import { useUserTrust } from '@/providers/UserTrustProvider'
import { Event, kinds } from 'nostr-tools'
import { useMemo } from 'react'
import { MentionNotification } from './MentionNotification'
@@ -17,18 +19,26 @@ export function NotificationItem({
notification: Event
isNew?: boolean
}) {
const { pubkey } = useNostr()
const { mutePubkeySet } = useMuteList()
const { hideContentMentioningMutedUsers } = useContentPolicy()
const shouldHide = useMemo(() => {
if (mutePubkeySet.has(notification.pubkey)) {
return true
}
if (hideContentMentioningMutedUsers && isMentioningMutedUsers(notification, mutePubkeySet)) {
return true
}
return false
}, [])
if (shouldHide) return null
const { hideUntrustedNotifications, isUserTrusted } = useUserTrust()
const canShow = useMemo(() => {
return notificationFilter(notification, {
pubkey,
mutePubkeySet,
hideContentMentioningMutedUsers,
hideUntrustedNotifications,
isUserTrusted
})
}, [
notification,
mutePubkeySet,
hideContentMentioningMutedUsers,
hideUntrustedNotifications,
isUserTrusted
])
if (!canShow) return null
if (notification.kind === kinds.Reaction) {
return <ReactionNotification notification={notification} isNew={isNew} />

View File

@@ -4,7 +4,6 @@ import { usePrimaryPage } from '@/PageManager'
import { useNostr } from '@/providers/NostrProvider'
import { useNotification } from '@/providers/NotificationProvider'
import { useUserPreferences } from '@/providers/UserPreferencesProvider'
import { useUserTrust } from '@/providers/UserTrustProvider'
import client from '@/services/client.service'
import noteStatsService from '@/services/note-stats.service'
import { TNotificationType } from '@/types'
@@ -33,7 +32,6 @@ const NotificationList = forwardRef((_, ref) => {
const { current, display } = usePrimaryPage()
const active = useMemo(() => current === 'notifications' && display, [current, display])
const { pubkey } = useNostr()
const { hideUntrustedNotifications, isUserTrusted } = useUserTrust()
const { getNotificationsSeenAt } = useNotification()
const { notificationListStyle } = useUserPreferences()
const [notificationType, setNotificationType] = useState<TNotificationType>('all')
@@ -180,13 +178,8 @@ const NotificationList = forwardRef((_, ref) => {
}, [pubkey, active, filterKinds, handleNewEvent])
useEffect(() => {
let visibleNotifications = notifications.slice(0, showCount)
if (hideUntrustedNotifications) {
visibleNotifications = visibleNotifications.filter((event) => isUserTrusted(event.pubkey))
}
setVisibleNotifications(visibleNotifications)
}, [notifications, showCount, hideUntrustedNotifications, isUserTrusted])
setVisibleNotifications(notifications.slice(0, showCount))
}, [notifications, showCount])
useEffect(() => {
const options = {

35
src/lib/notification.ts Normal file
View File

@@ -0,0 +1,35 @@
import { kinds, NostrEvent } from 'nostr-tools'
import { isMentioningMutedUsers } from './event'
import { tagNameEquals } from './tag'
export function notificationFilter(
event: NostrEvent,
{
pubkey,
mutePubkeySet,
hideContentMentioningMutedUsers,
hideUntrustedNotifications,
isUserTrusted
}: {
pubkey?: string | null
mutePubkeySet: Set<string>
hideContentMentioningMutedUsers?: boolean
hideUntrustedNotifications?: boolean
isUserTrusted: (pubkey: string) => boolean
}
): boolean {
if (
mutePubkeySet.has(event.pubkey) ||
(hideContentMentioningMutedUsers && isMentioningMutedUsers(event, mutePubkeySet)) ||
(hideUntrustedNotifications && !isUserTrusted(event.pubkey))
) {
return false
}
if (pubkey && event.kind === kinds.Reaction) {
const targetPubkey = event.tags.findLast(tagNameEquals('p'))?.[1]
if (targetPubkey !== pubkey) return false
}
return true
}

View File

@@ -1,5 +1,6 @@
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
import { compareEvents, isMentioningMutedUsers } from '@/lib/event'
import { compareEvents } from '@/lib/event'
import { notificationFilter } from '@/lib/notification'
import { usePrimaryPage } from '@/PageManager'
import client from '@/services/client.service'
import storage from '@/services/local-storage.service'
@@ -47,9 +48,13 @@ export function NotificationProvider({ children }: { children: React.ReactNode }
break
}
if (
mutePubkeySet.has(notification.pubkey) ||
(hideContentMentioningMutedUsers && isMentioningMutedUsers(notification, mutePubkeySet)) ||
(hideUntrustedNotifications && !isUserTrusted(notification.pubkey))
!notificationFilter(notification, {
pubkey,
mutePubkeySet,
hideContentMentioningMutedUsers,
hideUntrustedNotifications,
isUserTrusted
})
) {
continue
}