From f486d0b9832448a34e201e7bd9e67cfcaf7a4111 Mon Sep 17 00:00:00 2001 From: Azide Date: Sat, 20 Aug 2022 22:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E5=B1=8F?= =?UTF-8?q?=E8=94=BD=E7=89=B9=E5=AE=9Atag=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonebot_bison/platform/platform.py | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/plugins/nonebot_bison/platform/platform.py b/src/plugins/nonebot_bison/platform/platform.py index d0e2cf6..417c9cd 100644 --- a/src/plugins/nonebot_bison/platform/platform.py +++ b/src/plugins/nonebot_bison/platform/platform.py @@ -111,6 +111,30 @@ class Platform(metaclass=RegistryABCMeta, base=True): def set_stored_data(self, target: Target, data: Any): self.store[target] = data + def tag_separator(self, stored_tags: list[Tag]): + subscribed_tags = [] + banned_tags = [] + for tag in stored_tags: + if tag.startswith("~"): + banned_tags.append(tag.lstrip("~")) + else: + subscribed_tags.append(tag) + return subscribed_tags, banned_tags + + def is_banned_post( + self, + post_tags: Collection[Tag], + subscribed_tags: list[Tag], + banned_tags: list[Tag], + ) -> bool: + for tag in post_tags or []: + if banned_tags and tag in banned_tags: + return True + elif subscribed_tags and tag not in subscribed_tags: + return True + + return False + async def filter_user_custom( self, raw_post_list: list[RawPost], cats: list[Category], tags: list[Tag] ) -> list[RawPost]: @@ -121,13 +145,8 @@ class Platform(metaclass=RegistryABCMeta, base=True): if cats and cat not in cats: continue if self.enable_tag and tags: - flag = False post_tags = self.get_tags(raw_post) - for tag in post_tags or []: - if tag in tags: - flag = True - break - if not flag: + if self.is_banned_post(post_tags, **self.tag_separator(tags)): continue res.append(raw_post) return res