2024-03-10 14:27:21 +00:00
|
|
|
import time
|
2024-04-09 06:59:05 +00:00
|
|
|
from flask import g
|
2024-03-10 14:27:21 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Column, ForeignKey
|
2024-08-07 22:52:53 +00:00
|
|
|
from sqlalchemy.orm import deferred, relationship
|
2024-03-10 14:27:21 +00:00
|
|
|
from sqlalchemy.sql.sqltypes import *
|
|
|
|
|
|
|
|
from files.classes import Base
|
|
|
|
from files.helpers.lazy import lazy
|
2024-04-07 08:46:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2024-08-17 14:53:37 +00:00
|
|
|
from files.helpers.sorting_and_time import make_age_string
|
2024-03-10 14:27:21 +00:00
|
|
|
|
|
|
|
class Chat(Base):
|
|
|
|
__tablename__ = "chats"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
created_utc = Column(Integer)
|
2024-08-07 22:52:53 +00:00
|
|
|
css = deferred(Column(String))
|
2024-03-10 14:27:21 +00:00
|
|
|
|
2024-08-09 13:35:36 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def mod_ids(self):
|
|
|
|
return [x[0] for x in g.db.query(ChatMembership.user_id).filter_by(chat_id=self.id, is_mod=True).order_by(ChatMembership.created_utc, ChatMembership.user_id).all()]
|
|
|
|
|
2024-04-07 08:41:09 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def owner_id(self):
|
2024-08-09 16:22:17 +00:00
|
|
|
return self.mod_ids[0] if self.mod_ids else AUTOJANNY_ID
|
2024-04-07 08:41:09 +00:00
|
|
|
|
2024-07-03 15:39:24 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def membership_count(self):
|
|
|
|
return g.db.query(ChatMembership).filter_by(chat_id=self.id).count()
|
|
|
|
|
2024-03-10 14:27:21 +00:00
|
|
|
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__}(id={self.id})>"
|
|
|
|
|
|
|
|
|
|
|
|
class ChatMembership(Base):
|
|
|
|
__tablename__ = "chat_memberships"
|
|
|
|
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
|
|
|
|
chat_id = Column(Integer, ForeignKey("chats.id"), primary_key=True)
|
2024-03-26 22:32:13 +00:00
|
|
|
notification = Column(Boolean, default=False)
|
2024-08-02 08:38:47 +00:00
|
|
|
muted = Column(Boolean, default=False)
|
2024-05-23 23:47:40 +00:00
|
|
|
mentions = Column(Integer, default=0)
|
2024-03-10 14:27:21 +00:00
|
|
|
created_utc = Column(Integer)
|
2024-08-09 13:35:36 +00:00
|
|
|
is_mod = Column(Boolean, default=False)
|
2024-03-10 14:27:21 +00:00
|
|
|
|
|
|
|
user = relationship("User")
|
|
|
|
|
|
|
|
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):
|
|
|
|
__tablename__ = "chat_messages"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey("users.id"))
|
|
|
|
chat_id = Column(Integer, ForeignKey("chats.id"))
|
|
|
|
quotes = Column(Integer, ForeignKey("chat_messages.id"))
|
|
|
|
text = Column(String)
|
|
|
|
text_censored = Column(String)
|
|
|
|
text_html = Column(String)
|
|
|
|
text_html_censored = Column(String)
|
|
|
|
created_utc = Column(Integer)
|
|
|
|
|
|
|
|
user = relationship("User")
|
2024-04-07 06:29:11 +00:00
|
|
|
quoted_message = relationship("ChatMessage", remote_side=[id])
|
2024-03-10 14:27:21 +00:00
|
|
|
|
|
|
|
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__}(id={self.id})>"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def username(self):
|
|
|
|
return self.user.username
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def hat(self):
|
|
|
|
return self.user.hat_active(None)[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def namecolor(self):
|
|
|
|
return self.user.name_color
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def patron(self):
|
|
|
|
return self.user.patron
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def pride_username(self):
|
|
|
|
return self.user.pride_username(None)
|
2024-05-17 14:30:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def permalink(self):
|
|
|
|
return f"{SITE_FULL}/chat/{self.chat_id}#{self.id}"
|
2024-08-17 14:53:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def age_string(self):
|
|
|
|
return make_age_string(self.created_utc)
|