zulip_bot/app/models/zulip.py
2025-05-16 18:00:22 +04:00

96 lines
3.1 KiB
Python

"""
SQLAlchemy models for the Zulip database tables.
"""
from sqlalchemy import Column, Integer, String, Text, Boolean, SmallInteger, DateTime, ForeignKey, BigInteger
from sqlalchemy.orm import relationship
from app.db import Base
class Recipient(Base):
"""
Model for zerver_recipient table in Zulip DB.
Recipients can be of different types (e.g., stream, user, huddle).
"""
__tablename__ = 'zerver_recipient'
__table_args__ = {'schema': 'zulip'}
id = Column(Integer, primary_key=True)
type_id = Column(Integer)
type = Column(SmallInteger) # 1 for stream, 2 for user, 3 for huddle
# Relationships
messages = relationship("Message", back_populates="recipient")
stream = relationship("Stream", back_populates="recipient", uselist=False)
class Stream(Base):
"""
Model for zerver_stream table in Zulip DB.
Represents a Zulip channel (called stream in Zulip terminology).
"""
__tablename__ = 'zerver_stream'
__table_args__ = {'schema': 'zulip'}
id = Column(BigInteger, primary_key=True)
name = Column(String)
date_created = Column(DateTime)
deactivated = Column(Boolean)
description = Column(String)
rendered_description = Column(Text)
invite_only = Column(Boolean)
recipient_id = Column(Integer, ForeignKey('zulip.zerver_recipient.id'))
realm_id = Column(Integer)
# Relationships
recipient = relationship("Recipient", back_populates="stream")
class Message(Base):
"""
Model for zerver_message table in Zulip DB.
Represents a message sent in Zulip.
"""
__tablename__ = 'zerver_message'
__table_args__ = {'schema': 'zulip'}
id = Column(Integer, primary_key=True)
sender_id = Column(Integer, ForeignKey('zulip.zerver_userprofile.id'))
recipient_id = Column(Integer, ForeignKey('zulip.zerver_recipient.id'))
subject = Column(String)
content = Column(Text)
rendered_content = Column(Text)
date_sent = Column(DateTime)
type = Column(SmallInteger) # 1 for stream message, 2 for private message
has_attachment = Column(Boolean)
has_image = Column(Boolean)
has_link = Column(Boolean)
is_channel_message = Column(Boolean)
realm_id = Column(Integer)
# Relationships
sender = relationship("UserProfile", back_populates="messages")
recipient = relationship("Recipient", back_populates="messages")
class UserProfile(Base):
"""
Model for zerver_userprofile table in Zulip DB.
Represents a Zulip user.
"""
__tablename__ = 'zerver_userprofile'
__table_args__ = {'schema': 'zulip'}
id = Column(Integer, primary_key=True)
email = Column(String)
full_name = Column(String)
is_active = Column(Boolean)
realm_id = Column(Integer)
# Relationships
messages = relationship("Message", back_populates="sender")
# Constants for the channels we're monitoring
IT_CHANNELS = {
"IT Discussions": 5, # id = 5, recipient_id = 16
"IT Knowledge": 17, # id = 17, recipient_id = 47
"IT Support": 16 # id = 16, recipient_id = 43
}
# Recipient IDs for the channels we're monitoring
IT_RECIPIENT_IDS = [16, 47, 43]