feat: add comment content filter

This commit is contained in:
DIYgod 2023-12-29 19:58:21 +08:00
parent 3070b17729
commit 172d1ec4be
No known key found for this signature in database
5 changed files with 55 additions and 35 deletions

View File

@ -3,5 +3,6 @@
151, 53994, 54570, 54481, 54537, 54302, 53944, 55768, 55855, 56592, 54986,
56875, 56873, 56666, 55996, 57407, 56249, 57858, 56973, 58600
],
"comment": [54986]
"comment": [54986],
"comment_content": ["snowcherryblossom"]
}

View File

@ -27,7 +27,7 @@ import { useMobileLayout } from "~/hooks/useMobileLayout"
import { useNProgress } from "~/hooks/useNProgress"
import { DARK_MODE_STORAGE_KEY } from "~/lib/constants"
import { APP_NAME, WALLET_CONNECT_V2_PROJECT_ID } from "~/lib/env"
import { filterNotificationCharacter } from "~/lib/filter-character"
import { filterNotification } from "~/lib/filter"
import { toGateway } from "~/lib/ipfs-parser"
import { urlComposer } from "~/lib/url-composer"
@ -100,7 +100,7 @@ export default function Providers({ children }: { children: React.ReactNode }) {
<ModalStackProvider>{children}</ModalStackProvider>
<NotificationModal
colorScheme={colorScheme}
filter={filterNotificationCharacter}
filter={filterNotification}
/>
</ConnectKitProvider>
</QueryClientProvider>

View File

@ -1,30 +0,0 @@
import { CharacterEntity } from "crossbell"
import { ParsedNotification } from "@crossbell/indexer"
import filter from "../../data/filter.json"
type CharacterId = CharacterEntity["characterId"]
export function filterCharacter<T>(
blacklist: CharacterId[],
getCharacterId: (item: T) => CharacterId | null | undefined,
) {
const cache = new Map(blacklist.map((characterId) => [characterId, true]))
return function filter(item: T): boolean {
const characterId = getCharacterId(item)
return characterId ? !cache.has(characterId) : false
}
}
export const filterNotificationCharacter = filterCharacter(
filter.comment,
({ fromCharacter }: ParsedNotification) => fromCharacter?.characterId,
)
export const filterCommentCharacter = filterCharacter(
filter.comment,
({ characterId }: Pick<CharacterEntity, "characterId">) => characterId,
)

49
src/lib/filter.ts Normal file
View File

@ -0,0 +1,49 @@
import { CharacterEntity, NoteEntity } from "crossbell"
import { ParsedNotification } from "@crossbell/indexer"
import filter from "../../data/filter.json"
type CharacterId = CharacterEntity["characterId"]
export function filterGenerator<T>(
blacklist: CharacterId[],
getCharacterId: (item: T) => CharacterId | null | undefined,
blackKeywords: string[],
getContent: (item: T) => string | null | undefined,
) {
const cache = new Map(blacklist.map((characterId) => [characterId, true]))
return function filter(item: T): boolean {
const characterId = getCharacterId(item)
const content = getContent(item)
const characterIdCheck = characterId && !cache.has(characterId)
const contentCheck =
!content || !blackKeywords.some((keyword) => content.includes(keyword))
return !!(characterIdCheck && contentCheck)
}
}
export const filterNotification = filterGenerator(
filter.comment,
({ fromCharacter }: ParsedNotification) => fromCharacter?.characterId,
filter.comment_content,
(notification: ParsedNotification) => {
if (notification.type === "comment-note") {
return notification.commentNote.metadata?.content?.content
} else {
return null
}
},
)
export const filterComment = filterGenerator(
filter.comment,
({ characterId }: NoteEntity) => characterId,
filter.comment_content,
({ metadata }: NoteEntity) => {
return metadata?.content?.content
},
)

View File

@ -16,7 +16,7 @@ import { gql } from "@urql/core"
import { RESERVED_TAGS } from "~/lib/constants"
import { editor2Crossbell } from "~/lib/editor-converter"
import { expandCrossbellNote } from "~/lib/expand-unit"
import { filterCommentCharacter } from "~/lib/filter-character"
import { filterComment } from "~/lib/filter"
import { getNoteSlug } from "~/lib/helpers"
import { checkSlugReservedWords } from "~/lib/slug-reserved-words"
import { getKeys, getStorage } from "~/lib/storage"
@ -755,7 +755,7 @@ export async function getComments({
list: [],
}
res.list = res.list.filter(filterCommentCharacter)
res.list = res.list.filter(filterComment)
return res
}