2023-11-03 10:10:22 +00:00
|
|
|
import time
|
2023-11-22 16:38:39 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
|
2024-02-20 18:57:39 +00:00
|
|
|
from sqlalchemy import event
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2023-10-23 14:51:13 +00:00
|
|
|
from services.db import Base
|
2024-02-20 18:57:39 +00:00
|
|
|
from orm.author import get_object, update_follows, update_app_data
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
class TopicFollower(Base):
|
2024-01-25 19:41:27 +00:00
|
|
|
__tablename__ = 'topic_followers'
|
2021-08-20 09:27:19 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
id = None # type: ignore
|
2024-02-20 08:53:55 +00:00
|
|
|
follower = Column(ForeignKey('author.id'), primary_key=True)
|
|
|
|
topic = Column(ForeignKey('topic.id'), primary_key=True)
|
2023-11-03 10:10:22 +00:00
|
|
|
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2022-09-18 14:29:21 +00:00
|
|
|
auto = Column(Boolean, nullable=False, default=False)
|
2021-08-20 08:08:32 +00:00
|
|
|
|
2021-12-12 09:44:54 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
class Topic(Base):
|
2024-01-25 19:41:27 +00:00
|
|
|
__tablename__ = 'topic'
|
2021-12-12 13:00:38 +00:00
|
|
|
|
2024-02-19 14:22:38 +00:00
|
|
|
slug = Column(String, unique=True)
|
2024-01-25 19:41:27 +00:00
|
|
|
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')
|
2024-02-20 18:57:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|