From e1da323c2f80133ead081291cbd534993d7e4978 Mon Sep 17 00:00:00 2001 From: daniyal Date: Thu, 29 Aug 2024 22:13:36 +0500 Subject: [PATCH] fix: add fully unmoderated option for admin --- src/controllers/metadata.ts | 67 ++++++++++++++++++++++---------- src/pages/mods.tsx | 76 +++++++++++++++++++++++++++---------- 2 files changed, 103 insertions(+), 40 deletions(-) diff --git a/src/controllers/metadata.ts b/src/controllers/metadata.ts index 55938b2..30730bc 100644 --- a/src/controllers/metadata.ts +++ b/src/controllers/metadata.ts @@ -131,24 +131,21 @@ export class MetadataController { return ndkRelayList[userRelaysType] } - public getMuteLists = async (pubkey?: string): Promise => { - // Create sets to collect all unique muted authors and replaceable event Identifiers - const mutedAuthors = new Set() - const mutedEvents = new Set() + public getMuteLists = async ( + pubkey?: string + ): Promise<{ + admin: MuteLists + user: MuteLists + }> => { + const adminMutedAuthors = new Set() + const adminMutedPosts = new Set() - // construct an array of npubs with dedicated reporting npub and provided pubkey - const npubs = [this.reportingNpub] - - if (pubkey) npubs.push(pubkey) - - // Create an array of promises to fetch mute lists for each npub - const promises = npubs.map(async (npub) => { - const hexKey = npubToHex(npub) - if (!hexKey) return + const adminHexKey = npubToHex(this.reportingNpub) + if (adminHexKey) { const muteListEvent = await this.ndk.fetchEvent({ kinds: [kinds.Mutelist], - authors: [hexKey] + authors: [adminHexKey] }) if (muteListEvent) { @@ -156,19 +153,49 @@ export class MetadataController { list.items.forEach((item) => { if (item[0] === 'p') { - mutedAuthors.add(item[1]) + adminMutedAuthors.add(item[1]) } else if (item[0] === 'a') { - mutedEvents.add(item[1]) + adminMutedPosts.add(item[1]) } }) } - }) + } - await Promise.allSettled(promises) + const userMutedAuthors = new Set() + const userMutedPosts = new Set() + + if (pubkey) { + const userHexKey = npubToHex(pubkey) + + if (userHexKey) { + const muteListEvent = await this.ndk.fetchEvent({ + kinds: [kinds.Mutelist], + authors: [userHexKey] + }) + + if (muteListEvent) { + const list = NDKList.from(muteListEvent) + + list.items.forEach((item) => { + if (item[0] === 'p') { + userMutedAuthors.add(item[1]) + } else if (item[0] === 'a') { + userMutedPosts.add(item[1]) + } + }) + } + } + } return { - authors: Array.from(mutedAuthors), - replaceableEvents: Array.from(mutedEvents) + admin: { + authors: Array.from(adminMutedAuthors), + replaceableEvents: Array.from(adminMutedPosts) + }, + user: { + authors: Array.from(userMutedAuthors), + replaceableEvents: Array.from(userMutedPosts) + } } } diff --git a/src/pages/mods.tsx b/src/pages/mods.tsx index e5bc6e0..e61be15 100644 --- a/src/pages/mods.tsx +++ b/src/pages/mods.tsx @@ -36,7 +36,8 @@ enum NSFWFilter { enum ModeratedFilter { Moderated = 'Moderated', - Unmoderated = 'Unmoderated' + Unmoderated = 'Unmoderated', + Unmoderated_Fully = 'Unmoderated Fully' } interface FilterOptions { @@ -56,9 +57,18 @@ export const ModsPage = () => { source: window.location.host, moderated: ModeratedFilter.Moderated }) - const [muteLists, setMuteLists] = useState({ - authors: [], - replaceableEvents: [] + const [muteLists, setMuteLists] = useState<{ + admin: MuteLists + user: MuteLists + }>({ + admin: { + authors: [], + replaceableEvents: [] + }, + user: { + authors: [], + replaceableEvents: [] + } }) const [nsfwList, setNSFWList] = useState([]) @@ -139,11 +149,24 @@ export const ModsPage = () => { let filtered = nsfwFilter(mods) + const isAdmin = userState.user?.npub === import.meta.env.VITE_REPORTING_NPUB + const isUnmoderatedFully = + filterOptions.moderated === ModeratedFilter.Unmoderated_Fully + + // Only apply filtering if the user is not an admin or the admin has not selected "Unmoderated Fully" + if (!(isAdmin && isUnmoderatedFully)) { + filtered = filtered.filter( + (mod) => + !muteLists.admin.authors.includes(mod.author) && + !muteLists.admin.replaceableEvents.includes(mod.aTag) + ) + } + if (filterOptions.moderated === ModeratedFilter.Moderated) { filtered = filtered.filter( (mod) => - !muteLists.authors.includes(mod.author) && - !muteLists.replaceableEvents.includes(mod.aTag) + !muteLists.user.authors.includes(mod.author) && + !muteLists.user.replaceableEvents.includes(mod.aTag) ) } @@ -155,6 +178,7 @@ export const ModsPage = () => { return filtered }, [ + userState.user?.npub, filterOptions.sort, filterOptions.moderated, filterOptions.nsfw, @@ -250,6 +274,8 @@ type FiltersProps = { const Filters = React.memo( ({ filterOptions, setFilterOptions }: FiltersProps) => { + const userState = useAppSelector((state) => state.user) + return (
@@ -293,20 +319,30 @@ const Filters = React.memo( {filterOptions.moderated}
- {Object.values(ModeratedFilter).map((item, index) => ( -
- setFilterOptions((prev) => ({ - ...prev, - moderated: item - })) - } - > - {item} -
- ))} + {Object.values(ModeratedFilter).map((item, index) => { + if (item === ModeratedFilter.Unmoderated_Fully) { + const isAdmin = + userState.user?.npub === + import.meta.env.VITE_REPORTING_NPUB + + if (!isAdmin) return null + } + + return ( +
+ setFilterOptions((prev) => ({ + ...prev, + moderated: item + })) + } + > + {item} +
+ ) + })}