From 172d1ec4beb04cd88795ad2504f48217e2f829bc Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 29 Dec 2023 19:58:21 +0800 Subject: [PATCH] feat: add comment content filter --- data/filter.json | 3 ++- src/app/[locale]/providers.tsx | 4 +-- src/lib/filter-character.ts | 30 --------------------- src/lib/filter.ts | 49 ++++++++++++++++++++++++++++++++++ src/models/page.model.ts | 4 +-- 5 files changed, 55 insertions(+), 35 deletions(-) delete mode 100644 src/lib/filter-character.ts create mode 100644 src/lib/filter.ts diff --git a/data/filter.json b/data/filter.json index 936641ef..2f019a13 100644 --- a/data/filter.json +++ b/data/filter.json @@ -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"] } diff --git a/src/app/[locale]/providers.tsx b/src/app/[locale]/providers.tsx index 34f9fe74..9f0e3873 100644 --- a/src/app/[locale]/providers.tsx +++ b/src/app/[locale]/providers.tsx @@ -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 }) { {children} diff --git a/src/lib/filter-character.ts b/src/lib/filter-character.ts deleted file mode 100644 index 2f6087b4..00000000 --- a/src/lib/filter-character.ts +++ /dev/null @@ -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( - 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) => characterId, -) diff --git a/src/lib/filter.ts b/src/lib/filter.ts new file mode 100644 index 00000000..1046c721 --- /dev/null +++ b/src/lib/filter.ts @@ -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( + 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 + }, +) diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 08043882..431a60b3 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -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 }