remove redundancy and fix issues with private chats
parent
0da1d74c5d
commit
31622cb4aa
|
@ -28,6 +28,8 @@ class ChatMembership(Base):
|
||||||
__tablename__ = "chat_memberships"
|
__tablename__ = "chat_memberships"
|
||||||
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
|
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
|
||||||
chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True)
|
chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True)
|
||||||
|
notification = Column(Boolean, default=False)
|
||||||
|
last_notified = Column(Integer, default=0)
|
||||||
created_utc = Column(Integer)
|
created_utc = Column(Integer)
|
||||||
|
|
||||||
user = relationship("User")
|
user = relationship("User")
|
||||||
|
@ -54,20 +56,6 @@ class ChatLeave(Base):
|
||||||
return f"<{self.__class__.__name__}(user_id={self.user_id}, chat_id={self.chat_id})>"
|
return f"<{self.__class__.__name__}(user_id={self.user_id}, chat_id={self.chat_id})>"
|
||||||
|
|
||||||
|
|
||||||
class ChatNotification(Base):
|
|
||||||
__tablename__ = "chat_notifications"
|
|
||||||
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
|
|
||||||
chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True)
|
|
||||||
created_utc = Column(Integer)
|
|
||||||
|
|
||||||
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_id={self.chat_id})>"
|
|
||||||
|
|
||||||
|
|
||||||
class ChatMessage(Base):
|
class ChatMessage(Base):
|
||||||
__tablename__ = "chat_messages"
|
__tablename__ = "chat_messages"
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
|
|
|
@ -14,7 +14,7 @@ from files.classes import Base
|
||||||
from files.classes.casino_game import CasinoGame
|
from files.classes.casino_game import CasinoGame
|
||||||
from files.classes.group import *
|
from files.classes.group import *
|
||||||
from files.classes.hole import Hole
|
from files.classes.hole import Hole
|
||||||
from files.classes.private_chats import ChatNotification
|
from files.classes.private_chats import ChatMembership
|
||||||
from files.classes.currency_logs import CurrencyLog
|
from files.classes.currency_logs import CurrencyLog
|
||||||
from files.helpers.config.const import *
|
from files.helpers.config.const import *
|
||||||
from files.helpers.config.modaction_types import *
|
from files.helpers.config.modaction_types import *
|
||||||
|
@ -832,7 +832,7 @@ class User(Base):
|
||||||
@property
|
@property
|
||||||
@lazy
|
@lazy
|
||||||
def chats_notifications_count(self):
|
def chats_notifications_count(self):
|
||||||
return g.db.query(ChatNotification).filter_by(user_id=self.id).count()
|
return g.db.query(ChatMembership).filter_by(user_id=self.id, notification=True).count()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@lazy
|
@lazy
|
||||||
|
|
|
@ -175,18 +175,16 @@ def speak(data, v):
|
||||||
g.db.delete(existing)
|
g.db.delete(existing)
|
||||||
g.db.flush()
|
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()]
|
alrdy_here = list(online[request.referrer].keys())
|
||||||
dont_notify = set(dont_notify)
|
memberships = g.db.query(ChatMembership).filter(
|
||||||
to_notify = [x[0] for x in g.db.query(ChatMembership.user_id).filter(
|
|
||||||
ChatMembership.chat_id == chat_id,
|
ChatMembership.chat_id == chat_id,
|
||||||
ChatMembership.user_id.notin_(dont_notify),
|
ChatMembership.user_id.notin_(alrdy_here),
|
||||||
)]
|
ChatMembership.notification == False,
|
||||||
for uid in to_notify:
|
|
||||||
n = ChatNotification(
|
|
||||||
user_id=uid,
|
|
||||||
chat_id=chat_id,
|
|
||||||
)
|
)
|
||||||
g.db.add(n)
|
for membership in memberships:
|
||||||
|
membership.notification = True
|
||||||
|
membership.last_notified = time.time()
|
||||||
|
g.db.add(membership)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"id": chat_message.id,
|
"id": chat_message.id,
|
||||||
|
|
|
@ -29,9 +29,10 @@ def clear(v):
|
||||||
n.read = True
|
n.read = True
|
||||||
g.db.add(n)
|
g.db.add(n)
|
||||||
|
|
||||||
chat_notifs = g.db.query(ChatNotification).filter_by(user_id=v.id)
|
chat_memberships = g.db.query(ChatMembership).filter_by(user_id=v.id, notification=True)
|
||||||
for chat_notif in chat_notifs:
|
for membership in chat_memberships:
|
||||||
g.db.delete(chat_notif)
|
membership.notification = False
|
||||||
|
g.db.add(membership)
|
||||||
|
|
||||||
v.last_viewed_modmail_notifs = int(time.time())
|
v.last_viewed_modmail_notifs = int(time.time())
|
||||||
v.last_viewed_post_notifs = int(time.time())
|
v.last_viewed_post_notifs = int(time.time())
|
||||||
|
@ -140,9 +141,7 @@ def notifications_messages(v):
|
||||||
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
||||||
@auth_required
|
@auth_required
|
||||||
def notifications_chats(v):
|
def notifications_chats(v):
|
||||||
criteria1 = (Chat.id == ChatMembership.chat_id, ChatMembership.user_id == v.id)
|
chats = g.db.query(Chat, ChatMembership.notification).join(ChatMembership, and_(Chat.id == ChatMembership.chat_id, ChatMembership.user_id == v.id)).order_by(ChatMembership.last_notified.desc()).all()
|
||||||
criteria2 = (Chat.id == ChatNotification.chat_id, ChatNotification.user_id == v.id)
|
|
||||||
chats = g.db.query(Chat, func.count(ChatNotification.chat_id)).join(ChatMembership, and_(*criteria1)).outerjoin(ChatNotification, and_(*criteria2)).group_by(Chat, ChatMembership.created_utc).order_by(func.count(ChatNotification.chat_id).desc(), ChatMembership.created_utc.desc()).all()
|
|
||||||
return render_template("notifications.html", v=v, notifications=chats)
|
return render_template("notifications.html", v=v, notifications=chats)
|
||||||
|
|
||||||
@app.get("/notifications/modmail")
|
@app.get("/notifications/modmail")
|
||||||
|
|
|
@ -58,18 +58,19 @@ def private_chat(v, chat_id):
|
||||||
if not chat:
|
if not chat:
|
||||||
abort(404, "Chat not found!")
|
abort(404, "Chat not found!")
|
||||||
|
|
||||||
if v.admin_level < PERMS['VIEW_CHATS']:
|
membership = g.db.query(ChatMembership).filter_by(user_id=v.id, chat_id=chat_id).one_or_none()
|
||||||
is_member = g.db.query(ChatMembership.user_id).filter_by(user_id=v.id, chat_id=chat_id).one_or_none()
|
|
||||||
if not is_member:
|
if v.admin_level < PERMS['VIEW_CHATS'] and not membership:
|
||||||
abort(403, "You're not a member of this chat!")
|
abort(403, "You're not a member of this chat!")
|
||||||
|
|
||||||
displayed_messages = reversed(g.db.query(ChatMessage).filter_by(chat_id=chat.id).order_by(ChatMessage.id.desc()).limit(250).all())
|
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}
|
displayed_messages = {m.id: m for m in displayed_messages}
|
||||||
|
|
||||||
notif = g.db.query(ChatNotification).filter_by(user_id=v.id, chat_id=chat_id).one_or_none()
|
if not session.get("GLOBAL"):
|
||||||
if notif: g.db.delete(notif)
|
membership.notification = False
|
||||||
|
g.db.add(membership)
|
||||||
g.db.commit() #to clear notif count
|
g.db.commit() #to clear notif count
|
||||||
|
|
||||||
return render_template("private_chat.html", v=v, messages=displayed_messages, chat=chat)
|
return render_template("private_chat.html", v=v, messages=displayed_messages, chat=chat)
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,7 +120,4 @@ def leave_chat(v, chat_id):
|
||||||
)
|
)
|
||||||
g.db.add(chat_leave)
|
g.db.add(chat_leave)
|
||||||
|
|
||||||
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!"}
|
return {"message": "Chat left successfully!"}
|
||||||
|
|
|
@ -64,13 +64,13 @@
|
||||||
{% if request.path == '/notifications/chats' %}
|
{% if request.path == '/notifications/chats' %}
|
||||||
<table class="mt-4 ml-md-3" style="max-width:1000px">
|
<table class="mt-4 ml-md-3" style="max-width:1000px">
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for chat, notif_count in notifications %}
|
{% for chat, notification in notifications %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="/chat/{{chat.id}}" style="text-decoration:none!important">
|
<a href="/chat/{{chat.id}}" style="text-decoration:none!important">
|
||||||
{{chat.name}}
|
{{chat.name}}
|
||||||
{% if notif_count %}
|
{% if notification %}
|
||||||
<span class="notif-chats notif-count ml-1" style="padding-left:4.5px;background:#008080">{{notif_count}}</span>
|
<span class="notif-chats notif-count ml-1" style="padding-left:4.5px;background:#008080">1</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
alter table chat_memberships add column notification bool not null default false;
|
||||||
|
alter table chat_memberships alter column notification drop default;
|
||||||
|
|
||||||
|
alter table chat_memberships add column last_notified int not null default 0;
|
||||||
|
update chat_memberships a set last_notified=(select created_utc from chat_messages b where a.chat_id=b.chat_id order by created_utc desc limit 1);
|
||||||
|
alter table chat_memberships alter column last_notified drop default;
|
||||||
|
|
||||||
|
drop table chat_notifications;
|
Loading…
Reference in New Issue