feat: hide content mentioning muted users (#524)

Co-authored-by: mleku <me@mleku.dev>
This commit is contained in:
Cody Tseng 2025-09-02 22:18:34 +08:00 committed by GitHub
parent d3578184fb
commit 3c657dfa8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 289 additions and 83 deletions

View file

@ -7,6 +7,9 @@ type TContentPolicyContext = {
defaultShowNsfw: boolean
setDefaultShowNsfw: (showNsfw: boolean) => void
hideContentMentioningMutedUsers?: boolean
setHideContentMentioningMutedUsers?: (hide: boolean) => void
}
const ContentPolicyContext = createContext<TContentPolicyContext | undefined>(undefined)
@ -22,6 +25,9 @@ export const useContentPolicy = () => {
export function ContentPolicyProvider({ children }: { children: React.ReactNode }) {
const [autoplay, setAutoplay] = useState<boolean>(storage.getAutoplay())
const [defaultShowNsfw, setDefaultShowNsfw] = useState<boolean>(storage.getDefaultShowNsfw())
const [hideContentMentioningMutedUsers, setHideContentMentioningMutedUsers] = useState<boolean>(
storage.getHideContentMentioningMutedUsers()
)
const updateAutoplay = (autoplay: boolean) => {
storage.setAutoplay(autoplay)
@ -33,13 +39,20 @@ export function ContentPolicyProvider({ children }: { children: React.ReactNode
setDefaultShowNsfw(defaultShowNsfw)
}
const updateHideContentMentioningMutedUsers = (hide: boolean) => {
storage.setHideContentMentioningMutedUsers(hide)
setHideContentMentioningMutedUsers(hide)
}
return (
<ContentPolicyContext.Provider
value={{
autoplay,
setAutoplay: updateAutoplay,
defaultShowNsfw,
setDefaultShowNsfw: updateDefaultShowNsfw
setDefaultShowNsfw: updateDefaultShowNsfw,
hideContentMentioningMutedUsers,
setHideContentMentioningMutedUsers: updateHideContentMentioningMutedUsers
}}
>
{children}

View file

@ -11,7 +11,7 @@ import { z } from 'zod'
import { useNostr } from './NostrProvider'
type TMuteListContext = {
mutePubkeys: string[]
mutePubkeySet: Set<string>
changing: boolean
getMutePubkeys: () => string[]
getMuteType: (pubkey: string) => 'public' | 'private' | null
@ -49,10 +49,8 @@ export function MuteListProvider({ children }: { children: React.ReactNode }) {
() => new Set(getPubkeysFromPTags(privateTags)),
[privateTags]
)
const mutePubkeys = useMemo(() => {
return Array.from(
new Set([...Array.from(privateMutePubkeySet), ...Array.from(publicMutePubkeySet)])
)
const mutePubkeySet = useMemo(() => {
return new Set([...Array.from(privateMutePubkeySet), ...Array.from(publicMutePubkeySet)])
}, [publicMutePubkeySet, privateMutePubkeySet])
const [changing, setChanging] = useState(false)
@ -94,7 +92,7 @@ export function MuteListProvider({ children }: { children: React.ReactNode }) {
}, [muteListEvent])
const getMutePubkeys = () => {
return mutePubkeys
return Array.from(mutePubkeySet)
}
const getMuteType = useCallback(
@ -253,7 +251,7 @@ export function MuteListProvider({ children }: { children: React.ReactNode }) {
return (
<MuteListContext.Provider
value={{
mutePubkeys,
mutePubkeySet,
changing,
getMutePubkeys,
getMuteType,

View file

@ -1,8 +1,10 @@
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
import { isMentioningMutedUsers } from '@/lib/event'
import client from '@/services/client.service'
import { kinds } from 'nostr-tools'
import { SubCloser } from 'nostr-tools/abstract-pool'
import { createContext, useContext, useEffect, useRef, useState } from 'react'
import { useContentPolicy } from './ContentPolicyProvider'
import { useMuteList } from './MuteListProvider'
import { useNostr } from './NostrProvider'
import { useUserTrust } from './UserTrustProvider'
@ -26,7 +28,8 @@ export const useNotification = () => {
export function NotificationProvider({ children }: { children: React.ReactNode }) {
const { pubkey, notificationsSeenAt, updateNotificationsSeenAt } = useNostr()
const { hideUntrustedNotifications, isUserTrusted } = useUserTrust()
const { mutePubkeys } = useMuteList()
const { mutePubkeySet } = useMuteList()
const { hideContentMentioningMutedUsers } = useContentPolicy()
const [newNotificationIds, setNewNotificationIds] = useState(new Set<string>())
const subCloserRef = useRef<SubCloser | null>(null)
@ -67,7 +70,8 @@ export function NotificationProvider({ children }: { children: React.ReactNode }
// Only show notification if not from self and not muted
if (
evt.pubkey !== pubkey &&
!mutePubkeys.includes(evt.pubkey) &&
!mutePubkeySet.has(evt.pubkey) &&
(!hideContentMentioningMutedUsers || !isMentioningMutedUsers(evt, mutePubkeySet)) &&
(!hideUntrustedNotifications || isUserTrusted(evt.pubkey))
) {
setNewNotificationIds((prev) => {