diff --git a/files/classes/private_chats.py b/files/classes/private_chats.py index b05743f9a..08da03353 100644 --- a/files/classes/private_chats.py +++ b/files/classes/private_chats.py @@ -39,10 +39,6 @@ class ChatMembership(Base): def __repr__(self): return f"<{self.__class__.__name__}(user_id={self.user_id}, chat_id={self.chat_id})>" - @lazy - def unread_count(v): - return g.db.query(ChatNotification).filter_by(user_id=v.id, read=False, chat_id=self.chat_id).count() - class ChatLeave(Base): __tablename__ = "chat_leaves" @@ -61,18 +57,15 @@ class ChatLeave(Base): class ChatNotification(Base): __tablename__ = "chat_notifications" user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) - chat_message_id = Column(Integer, ForeignKey("chat_messages.id"), primary_key=True) - chat_id = Column(Integer, ForeignKey("chats.id")) + chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True) created_utc = Column(Integer) - chat_message = relationship("ChatMessage") - def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) super().__init__(*args, **kwargs) def __repr__(self): - return f"<{self.__class__.__name__}(user_id={self.user_id}, chat_message_id={self.message_id})>" + return f"<{self.__class__.__name__}(user_id={self.user_id}, chat_id={self.chat_id})>" class ChatMessage(Base): diff --git a/files/classes/user.py b/files/classes/user.py index c541c16f8..c455e6f75 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -836,7 +836,7 @@ class User(Base): @lazy def chat_notifications_count(self, chat_id): - return g.db.query(ChatNotification).filter_by(user_id=self.id, chat_id=chat_id).count() + return len(g.db.query(ChatNotification.user_id).filter_by(user_id=self.id, chat_id=chat_id).one_or_none()) @property @lazy diff --git a/files/routes/chat.py b/files/routes/chat.py index 7296201d6..f5045c637 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -156,14 +156,15 @@ def speak(data, v): g.db.delete(existing) g.db.flush() + dont_notify = list(online[request.referrer].keys()) + [x[0] for x in g.db.query(ChatNotification.user_id).filter_by(chat_id=chat_id).all()] + dont_notify = set(dont_notify) to_notify = [x[0] for x in g.db.query(ChatMembership.user_id).filter( ChatMembership.chat_id == chat_id, - ChatMembership.user_id.notin_(online[request.referrer].keys()), + ChatMembership.user_id.notin_(dont_notify), )] for uid in to_notify: n = ChatNotification( user_id=uid, - chat_message_id=chat_message.id, chat_id=chat_id, ) g.db.add(n) diff --git a/files/routes/private_chats.py b/files/routes/private_chats.py index 0b56ad37c..28212bf63 100644 --- a/files/routes/private_chats.py +++ b/files/routes/private_chats.py @@ -66,12 +66,8 @@ def private_chat(v, chat_id): displayed_messages = reversed(g.db.query(ChatMessage).filter_by(chat_id=chat.id).order_by(ChatMessage.id.desc()).limit(250).all()) displayed_messages = {m.id: m for m in displayed_messages} - notifs = g.db.query(ChatNotification).filter( - ChatNotification.user_id == v.id, - ChatNotification.chat_id == chat.id, - ).all() - for n in notifs: - g.db.delete(n) + notif = g.db.query(ChatNotification).filter_by(user_id=v.id, chat_id=chat_id).one_or_none() + if notif: g.db.delete(notif) g.db.commit() #to clear notif count return render_template("private_chat.html", v=v, messages=displayed_messages, chat=chat) @@ -123,8 +119,7 @@ def leave_chat(v, chat_id): ) g.db.add(chat_leave) - chat_notifs = g.db.query(ChatNotification).filter_by(user_id=v.id, chat_id=chat_id) - for chat_notif in chat_notifs: - g.db.delete(chat_notif) + notif = g.db.query(ChatNotification).filter_by(user_id=v.id, chat_id=chat_id).one_or_none() + if notif: g.db.delete(notif) return {"message": "Chat left successfully!"} diff --git a/migrations/20240311-one-notif-only.sql b/migrations/20240311-one-notif-only.sql new file mode 100644 index 000000000..1780a2852 --- /dev/null +++ b/migrations/20240311-one-notif-only.sql @@ -0,0 +1,13 @@ +delete from chat_notifications; + +alter table chat_notifications drop column chat_message_id; + +delete from chat_notifications; + +alter table chat_notifications drop constraint chat_notifications_pkey; + +delete from chat_notifications; + +alter table chat_notifications add constraint chat_notifications_pkey primary key (user_id, chat_id); + +delete from chat_notifications;