Aevann 2024-03-11 01:53:23 +02:00
parent 769185ea55
commit 89f9c1d7a8
5 changed files with 23 additions and 21 deletions

View File

@ -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):

View File

@ -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

View File

@ -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)

View File

@ -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!"}

View File

@ -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;