core/orm/topic.py
Untone 4f26812340
All checks were successful
Deploy to core / deploy (push) Successful in 1m16s
appdata-triggers
2024-02-20 21:57:39 +03:00

39 lines
1.4 KiB
Python

import time
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy import event
from services.db import Base
from orm.author import get_object, update_follows, update_app_data
class TopicFollower(Base):
__tablename__ = 'topic_followers'
id = None # type: ignore
follower = Column(ForeignKey('author.id'), primary_key=True)
topic = Column(ForeignKey('topic.id'), primary_key=True)
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
auto = Column(Boolean, nullable=False, default=False)
class Topic(Base):
__tablename__ = 'topic'
slug = Column(String, unique=True)
title = Column(String, nullable=False, comment='Title')
body = Column(String, nullable=True, comment='Body')
pic = Column(String, nullable=True, comment='Picture')
community = Column(ForeignKey('community.id'), default=1)
oid = Column(String, nullable=True, comment='Old ID')
@event.listens_for(TopicFollower, 'after_insert')
@event.listens_for(TopicFollower, 'after_delete')
def after_topic_follower_change(mapper, connection, target):
topic_id = target.topic
follower_id = target.follower
user = get_object(connection, 'authorizer_users', follower_id)
if user:
app_data = update_follows(user, 'topic', get_object(connection, 'topic', topic_id))
update_app_data(connection, follower_id, app_data)