From 712239157fe609ca823c615f0f5ce4c57e2937c9 Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 1 Mar 2023 22:28:34 +0200 Subject: [PATCH] two-way-blocks for ping groups --- files/classes/user.py | 7 +++++++ files/helpers/alerts.py | 9 +++++---- files/helpers/get.py | 13 +++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 47f665a83..f0fbcbd94 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -478,6 +478,13 @@ class User(Base): or_(and_(UserBlock.user_id == self.id, UserBlock.target_id == other.id), and_( UserBlock.user_id == other.id, UserBlock.target_id == self.id))).first() + @property + @lazy + def all_twoway_blocks(self): + return [x[0] for x in g.db.query(UserBlock.target_id).filter_by(user_id=self.id).all() + \ + g.db.query(UserBlock.user_id).filter_by(target_id=self.id).all()] + + def validate_2fa(self, token): x = pyotp.TOTP(self.mfa_secret) diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index 5cb50806b..761f15d9b 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -129,7 +129,7 @@ def NOTIFY_USERS(text, v): notify_users = set() for word, id in NOTIFIED_USERS.items(): - if word in text and id not in notify_users: + if word in text: notify_users.add(id) if FEATURES['PING_GROUPS']: @@ -151,14 +151,15 @@ def NOTIFY_USERS(text, v): names = set(m.group(2) for m in mention_regex.finditer(text)) - for user in get_users(names, graceful=True): - if v.id != user.id and not v.any_block_exists(user): - notify_users.add(user.id) + user_ids = get_users(names, ids_only=True, graceful=True) + notify_users.update(user_ids) if SITE_NAME == "WPD" and 'daisy' in text: admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_SPECIFIC_WPD_COMMENTS']).all()] notify_users.update(admin_ids) + notify_users = set([id for id in notify_users if id not in v.all_twoway_blocks]) + return notify_users - bots - {v.id, 0} diff --git a/files/helpers/get.py b/files/helpers/get.py index 2dc3210fa..e8d05f240 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -60,13 +60,19 @@ def get_user(username:Optional[str], v:Optional[User]=None, graceful=False, incl user = add_block_props(user, v) return user -def get_users(usernames:Iterable[str], graceful=False) -> List[User]: +def get_users(usernames:Iterable[str], ids_only=False, graceful=False) -> List[User]: if not usernames: return [] usernames = [sanitize_username(n) for n in usernames] if not any(usernames): if graceful and len(usernames) == 0: return [] abort(404) - users = g.db.query(User).filter( + + if ids_only: + users = g.db.query(User.id) + else: + users = g.db.query(User) + + users = users.filter( or_( User.username.ilike(any_(usernames)), User.original_username.ilike(any_(usernames)) @@ -76,6 +82,9 @@ def get_users(usernames:Iterable[str], graceful=False) -> List[User]: if len(users) != len(usernames) and not graceful: abort(404) + if ids_only: + users = [x[0] for x in users] + return users def get_account(id:Union[str, int], v:Optional[User]=None, graceful=False, include_blocks=False) -> Optional[User]: