diff --git a/files/classes/chats.py b/files/classes/chats.py index ed4a26baf..a74322cb5 100644 --- a/files/classes/chats.py +++ b/files/classes/chats.py @@ -36,6 +36,7 @@ class ChatMembership(Base): user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True) notification = Column(Boolean, default=False) + mentions = Column(Integer, default=0) created_utc = Column(Integer) user = relationship("User") diff --git a/files/classes/user.py b/files/classes/user.py index 5b27a1fa8..8e17fa29a 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -815,13 +815,14 @@ class User(Base): Comment.deleted_utc == 0, ) - return notifs.count() + self.chats_notifications_count + self.modmail_notifications_count + self.post_notifications_count + self.modaction_notifications_count + self.offsite_notifications_count + return notifs.count() + self.chat_mentions_notifications_count + self.chats_notifications_count + self.modmail_notifications_count + self.post_notifications_count + self.modaction_notifications_count + self.offsite_notifications_count @property @lazy def normal_notifications_count(self): return self.notifications_count \ - self.message_notifications_count \ + - self.chat_mentions_notifications_count \ - self.chats_notifications_count \ - self.modmail_notifications_count \ - self.post_notifications_count \ @@ -844,6 +845,11 @@ class User(Base): return notifs.count() + @property + @lazy + def chat_mentions_notifications_count(self): + return g.db.query(func.sum(ChatMembership.mentions)).filter_by(user_id=self.id).one()[0] + @property @lazy def chats_notifications_count(self): @@ -943,6 +949,8 @@ class User(Base): return '' elif self.message_notifications_count > 0: return 'messages' + elif self.chat_mentions_notifications_count > 0: + return 'chat_mentions' elif self.chats_notifications_count > 0: return 'chats' elif self.modmail_notifications_count > 0: @@ -961,6 +969,7 @@ class User(Base): colors = { '': '#dc3545', 'messages': '#d8910d', + 'chat_mentions': '#dd1ae0', 'chats': '#008080', 'modmail': '#f15387', 'posts': '#0000ff', diff --git a/files/routes/chat.py b/files/routes/chat.py index 945a9ae5d..f9fa76da2 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -146,7 +146,7 @@ def speak(data, v): g.db.delete(existing) g.db.flush() - alrdy_here = list(online[request.referrer].keys()) + alrdy_here = set(online[request.referrer].keys()) memberships = g.db.query(ChatMembership).options(load_only(ChatMembership.user_id)).filter( ChatMembership.chat_id == chat_id, ChatMembership.user_id.notin_(alrdy_here), @@ -162,6 +162,18 @@ def speak(data, v): url = f'{SITE_FULL}/chat/{chat.id}' push_notif(uids, title, body, url) + + + notify_users = NOTIFY_USERS(chat_message.text, v) - alrdy_here + memberships = g.db.query(ChatMembership).options(load_only(ChatMembership.user_id)).filter( + ChatMembership.chat_id == chat_id, + ChatMembership.user_id.in_(notify_users), + ) + for membership in memberships: + membership.mentions += 1 + g.db.add(membership) + + data = { "id": chat_message.id, "quotes": chat_message.quotes, diff --git a/files/routes/chats.py b/files/routes/chats.py index d76df6d0b..d900fc156 100644 --- a/files/routes/chats.py +++ b/files/routes/chats.py @@ -88,6 +88,7 @@ def chat(v, chat_id): else: if not session.get("GLOBAL") and membership: membership.notification = False + membership.mentions = 0 g.db.add(membership) g.db.commit() #to clear notif count diff --git a/files/routes/notifications.py b/files/routes/notifications.py index 61efd127d..02dffbb2f 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -32,6 +32,7 @@ def clear(v): chat_memberships = g.db.query(ChatMembership).filter_by(user_id=v.id, notification=True) for membership in chat_memberships: membership.notification = False + membership.mentions = 0 g.db.add(membership) v.last_viewed_modmail_notifs = int(time.time()) @@ -143,7 +144,7 @@ def notifications_messages(v): def notifications_chats(v): sq = g.db.query(ChatMessage.created_utc, ChatMessage.chat_id).distinct(ChatMessage.chat_id).order_by(ChatMessage.chat_id, ChatMessage.created_utc.desc()).subquery() - chats = g.db.query(Chat, ChatMembership.notification) \ + chats = g.db.query(Chat, ChatMembership.notification, ChatMembership.mentions) \ .join(ChatMembership, and_(Chat.id == ChatMembership.chat_id, ChatMembership.user_id == v.id)) \ .join(sq, Chat.id == sq.c.chat_id) \ .order_by(sq.c.created_utc.desc()).all() diff --git a/files/templates/notifications.html b/files/templates/notifications.html index b1a52efd3..e471beac3 100644 --- a/files/templates/notifications.html +++ b/files/templates/notifications.html @@ -21,7 +21,7 @@ {% if v.admin_level >= PERMS['VIEW_MODMAIL'] %} @@ -64,11 +64,14 @@ {% if request.path == '/notifications/chats' %} - {% for chat, notification in notifications %} + {% for chat, notification, mentions in notifications %}
{{chat.name}} + {% if mentions %} + {{mentions}} + {% endif %} {% if notification %} {% endif %} diff --git a/migrations/20240524-add-chat-mention-notifs.sql b/migrations/20240524-add-chat-mention-notifs.sql new file mode 100644 index 000000000..39801860b --- /dev/null +++ b/migrations/20240524-add-chat-mention-notifs.sql @@ -0,0 +1,2 @@ +alter table chat_memberships add column mentions int not null default 0; +alter table chat_memberships alter column mentions drop default;