From 33e922a9dd6511039232de461308c010304f99a9 Mon Sep 17 00:00:00 2001 From: Aevann Date: Thu, 1 Feb 2024 02:30:12 +0200 Subject: [PATCH] move modmail notifs to their already-existing section --- files/classes/user.py | 37 +++++++++---------- files/routes/chat.py | 23 ++---------- files/routes/notifications.py | 15 +++++--- files/routes/static.py | 16 +------- files/templates/notifications.html | 14 +++---- migrations/20240201-rework-modmail-notifs.sql | 2 + seed-users.sql | 10 ++--- 7 files changed, 45 insertions(+), 72 deletions(-) create mode 100644 migrations/20240201-rework-modmail-notifs.sql diff --git a/files/classes/user.py b/files/classes/user.py index 4a9329a14..e6c3594a0 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -139,6 +139,7 @@ class User(Base): currently_held_lottery_tickets = Column(Integer, default=0) total_held_lottery_tickets = Column(Integer, default=0) total_lottery_winnings = Column(Integer, default=0) + last_viewed_modmail_notifs = Column(Integer, default=0) last_viewed_post_notifs = Column(Integer, default=0) last_viewed_log_notifs = Column(Integer, default=0) last_viewed_reddit_notifs = Column(Integer, default=0) @@ -744,11 +745,6 @@ class User(Base): Notification.user_id == self.id, )) - if self.admin_level >= PERMS['VIEW_MODMAIL']: - notifs = notifs.filter( - not_(and_(Comment.sentto != None, Comment.sentto == MODMAIL_ID, User.is_muted)) - ) - if not self.admin_level >= PERMS['USER_SHADOWBAN']: notifs = notifs.filter( User.shadowbanned == None, @@ -756,13 +752,14 @@ class User(Base): Comment.deleted_utc == 0, ) - return notifs.count() + self.post_notifications_count + self.modaction_notifications_count + self.reddit_notifications_count + return notifs.count() + self.modmail_notifications_count + self.post_notifications_count + self.modaction_notifications_count + self.reddit_notifications_count @property @lazy def normal_notifications_count(self): return self.notifications_count \ - self.message_notifications_count \ + - self.modmail_notifications_count \ - self.post_notifications_count \ - self.modaction_notifications_count \ - self.reddit_notifications_count @@ -783,6 +780,17 @@ class User(Base): return notifs.count() + @property + @lazy + def modmail_notifications_count(self): + if self.admin_level < PERMS['NOTIFICATIONS_MODMAIL']: + return 0 + return g.db.query(Comment).filter( + Comment.author_id != self.id, + Comment.sentto == MODMAIL_ID, + Comment.created_utc > self.last_viewed_modmail_notifs, + ).count() + @property @lazy def post_notifications_count(self): @@ -852,6 +860,8 @@ class User(Base): return '' elif self.message_notifications_count > 0: return 'messages' + elif self.modmail_notifications_count > 0: + return 'modmail' elif self.post_notifications_count > 0: return 'posts' elif self.modaction_notifications_count > 0: @@ -866,6 +876,7 @@ class User(Base): colors = { '': '#dc3545', 'messages': '#d8910d', + 'modmail': '#f15387', 'posts': '#0000ff', 'modactions': '#1ad80d', 'reddit': '#805ad5', @@ -873,20 +884,6 @@ class User(Base): return colors[self.notifications_do] if self.notifications_do \ else colors[''] - @property - @lazy - def do_posts(self): - return self.post_notifications_count and \ - self.post_notifications_count == ( - self.notifications_count - - self.modaction_notifications_count - - self.reddit_notifications_count) - - @property - @lazy - def do_reddit(self): - return self.notifications_count == self.reddit_notifications_count - @property @lazy def moderated_holes(self): diff --git a/files/routes/chat.py b/files/routes/chat.py index 1fa119878..2ee458315 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -270,7 +270,7 @@ def messagereply(v): if v.is_permabanned and parent.sentto != MODMAIL_ID: abort(403, "You are permabanned and may not reply to messages!") elif v.is_muted and parent.sentto == MODMAIL_ID: - abort(403, "You are forbidden from replying to modmail!") + abort(403, "You are muted!") if parent.sentto == MODMAIL_ID: user_id = None elif v.id == user_id: user_id = parent.sentto @@ -332,25 +332,8 @@ def messagereply(v): top_comment = c.top_comment if top_comment.sentto == MODMAIL_ID: - admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_MODMAIL'], User.id != v.id)] - - if SITE == 'watchpeopledie.tv': - if AEVANN_ID in admin_ids: - admin_ids.remove(AEVANN_ID) - if 'delete' in top_comment.body.lower() and 'account' in top_comment.body.lower(): - admin_ids.remove(15447) - - if parent.author.id not in admin_ids + [v.id]: - admin_ids.append(parent.author.id) - - #Don't delete unread notifications, so the replies don't get collapsed and they get highlighted - ids = [top_comment.id] + [x.id for x in top_comment.replies(sort="old")] - notifications = g.db.query(Notification).filter(Notification.read == True, Notification.comment_id.in_(ids), Notification.user_id.in_(admin_ids)) - for n in notifications: - g.db.delete(n) - - for admin in admin_ids: - notif = Notification(comment_id=c.id, user_id=admin) + if parent.author.id != v.id and parent.author.admin_level < PERMS['VIEW_MODMAIL']: + notif = Notification(comment_id=c.id, user_id=parent.author.id) g.db.add(notif) elif user_id and user_id not in {v.id, MODMAIL_ID} | BOT_IDs: c.unread = True diff --git a/files/routes/notifications.py b/files/routes/notifications.py index 713371dac..05c6b6d26 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -29,6 +29,7 @@ def clear(v): n.read = True g.db.add(n) + v.last_viewed_modmail_notifs = int(time.time()) v.last_viewed_post_notifs = int(time.time()) v.last_viewed_log_notifs = int(time.time()) v.last_viewed_reddit_notifs = int(time.time()) @@ -70,6 +71,15 @@ def notifications_modmail(v): total = comments.count() listing = comments.order_by(Comment.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all() + for c in listing: + c_and_children = [c] + c.replies('old') + for c in c_and_children: + c.unread = c.created_utc > v.last_viewed_modmail_notifs + + if not session.get("GLOBAL") and not request.values.get('nr'): + v.last_viewed_modmail_notifs = int(time.time()) + g.db.add(v) + if v.client: return {"data":[x.json for x in listing]} return render_template("notifications.html", @@ -320,11 +330,6 @@ def notifications(v): or_(Comment.sentto == None, Comment.sentto != v.id), ) - if v.admin_level >= PERMS['VIEW_MODMAIL']: - comments = comments.join(Comment.author).filter( - not_(and_(Comment.sentto != None, Comment.sentto == MODMAIL_ID, User.is_muted)) - ) - if v.admin_level < PERMS['USER_SHADOWBAN']: comments = comments.filter( Comment.is_banned == False, diff --git a/files/routes/static.py b/files/routes/static.py index 678dbb661..cb390980d 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -293,7 +293,7 @@ def submit_contact(v): if not body: abort(400) if v.is_muted: - abort(403) + abort(403, "You are muted!") body = process_files(request.files, v, body) body = body.strip() @@ -314,20 +314,6 @@ def submit_contact(v): execute_under_siege(v, new_comment, new_comment.body_html, 'modmail') new_comment.top_comment_id = new_comment.id - admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_MODMAIL'])] - - if SITE == 'watchpeopledie.tv': - if AEVANN_ID in admin_ids: - admin_ids.remove(AEVANN_ID) - if 'delete' in new_comment.body.lower() and 'account' in new_comment.body.lower(): - admin_ids.remove(15447) - - for admin_id in admin_ids: - notif = Notification(comment_id=new_comment.id, user_id=admin_id) - g.db.add(notif) - - push_notif(admin_ids, f'New modmail from @{new_comment.author_name}', new_comment.body, f'{SITE_FULL}/notifications/modmail') - return {"message": "Your message has been sent to the admins!"} @cache.memoize(timeout=3600) diff --git a/files/templates/notifications.html b/files/templates/notifications.html index e38d9c3f5..fb697e5ea 100644 --- a/files/templates/notifications.html +++ b/files/templates/notifications.html @@ -19,6 +19,13 @@ Messages {% if v.message_notifications_count %}({{v.message_notifications_count}}){% endif %} + {% if v.admin_level >= PERMS['VIEW_MODMAIL'] %} + + {% endif %} {% endif %} - {% if v.admin_level >= PERMS['VIEW_MODMAIL'] %} - - {% endif %} {% if v.can_view_offsitementions %}