2023-11-03 10:10:22 +00:00
|
|
|
import time
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
|
2024-02-21 09:22:55 +00:00
|
|
|
from sqlalchemy import event, select
|
|
|
|
|
|
|
|
from services.rediscache import redis
|
|
|
|
from orm.topic import Topic, TopicFollower
|
2023-10-25 16:55:30 +00:00
|
|
|
from services.db import Base
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorRating(Base):
|
2024-02-21 07:27:16 +00:00
|
|
|
__tablename__ = "author_rating"
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
id = None # type: ignore
|
2024-02-21 07:27:16 +00:00
|
|
|
rater = Column(ForeignKey("author.id"), primary_key=True)
|
|
|
|
author = Column(ForeignKey("author.id"), primary_key=True)
|
2023-11-29 20:22:39 +00:00
|
|
|
plus = Column(Boolean)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorFollower(Base):
|
2024-02-21 07:27:16 +00:00
|
|
|
__tablename__ = "author_follower"
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
id = None # type: ignore
|
2024-02-21 07:27:16 +00:00
|
|
|
follower = Column(ForeignKey("author.id"), primary_key=True)
|
|
|
|
author = Column(ForeignKey("author.id"), primary_key=True)
|
2023-11-03 10:10:22 +00:00
|
|
|
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2023-10-23 14:47:11 +00:00
|
|
|
auto = Column(Boolean, nullable=False, default=False)
|
|
|
|
|
|
|
|
|
|
|
|
class Author(Base):
|
2024-02-21 07:27:16 +00:00
|
|
|
__tablename__ = "author"
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
user = Column(String) # unbounded link with authorizer's User type
|
2023-11-03 10:10:22 +00:00
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
name = Column(String, nullable=True, comment="Display name")
|
2024-02-19 14:22:38 +00:00
|
|
|
slug = Column(String, unique=True, comment="Author's slug")
|
2024-02-21 07:27:16 +00:00
|
|
|
bio = Column(String, nullable=True, comment="Bio") # status description
|
|
|
|
about = Column(String, nullable=True, comment="About") # long and formatted
|
|
|
|
pic = Column(String, nullable=True, comment="Picture")
|
|
|
|
links = Column(JSON, nullable=True, comment="Links")
|
2023-11-03 10:10:22 +00:00
|
|
|
created_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
|
|
|
last_seen = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
|
|
|
updated_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
2024-02-21 07:27:16 +00:00
|
|
|
deleted_at = Column(Integer, nullable=True, comment="Deleted at")
|
2024-02-20 18:57:39 +00:00
|
|
|
|
|
|
|
|
2024-02-21 09:22:55 +00:00
|
|
|
@event.listens_for(Author, "after_insert")
|
|
|
|
@event.listens_for(Author, "after_update")
|
|
|
|
async def after_author_update(mapper, connection, target):
|
|
|
|
redis_key = f"user:{target.user}:author"
|
|
|
|
await redis.execute("HSET", redis_key, vars(target))
|
|
|
|
|
|
|
|
|
|
|
|
async def update_follows_for_user(connection, user_id, entity_type, entity, is_insert):
|
|
|
|
redis_key = f"user:{user_id}:follows"
|
|
|
|
follows = await redis.execute("HGET", redis_key)
|
|
|
|
if not follows:
|
|
|
|
follows = {
|
|
|
|
"topics": [],
|
|
|
|
"authors": [],
|
|
|
|
"communities": [
|
|
|
|
{"slug": "discours", "name": "Дискурс", "id": 1, "desc": ""}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
if is_insert:
|
|
|
|
follows[f"{entity_type}s"].append(entity)
|
|
|
|
else:
|
|
|
|
# Remove the entity from follows
|
|
|
|
follows[f"{entity_type}s"] = [
|
|
|
|
e for e in follows[f"{entity_type}s"] if e["id"] != entity.id
|
|
|
|
]
|
|
|
|
|
|
|
|
await redis.execute("HSET", redis_key, vars(follows))
|
|
|
|
|
|
|
|
|
|
|
|
async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
|
|
|
|
author = connection.execute(select(Author).filter(Author.id == author_id)).first()
|
|
|
|
follower = connection.execute(
|
|
|
|
select(Author).filter(Author.id == follower_id)
|
|
|
|
).first()
|
|
|
|
if follower and author:
|
|
|
|
await update_follows_for_user(
|
|
|
|
connection, follower.user, "author", author, is_insert
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def handle_topic_follower_change(connection, topic_id, follower_id, is_insert):
|
|
|
|
topic = connection.execute(select(Topic).filter(Topic.id == topic_id)).first()
|
|
|
|
follower = connection.execute(
|
|
|
|
select(Author).filter(Author.id == follower_id)
|
|
|
|
).first()
|
|
|
|
if follower and topic:
|
|
|
|
await update_follows_for_user(
|
|
|
|
connection, follower.user, "topic", topic, is_insert
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@event.listens_for(TopicFollower, "after_insert")
|
|
|
|
async def after_topic_follower_insert(mapper, connection, target):
|
|
|
|
await handle_topic_follower_change(connection, target.topic, target.follower, True)
|
|
|
|
|
|
|
|
|
|
|
|
@event.listens_for(TopicFollower, "after_delete")
|
|
|
|
async def after_topic_follower_delete(mapper, connection, target):
|
|
|
|
await handle_topic_follower_change(connection, target.topic, target.follower, False)
|
|
|
|
|
|
|
|
|
|
|
|
@event.listens_for(AuthorFollower, "after_insert")
|
|
|
|
async def after_author_follower_insert(mapper, connection, target):
|
|
|
|
await handle_author_follower_change(
|
|
|
|
connection, target.author, target.follower, True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@event.listens_for(AuthorFollower, "after_delete")
|
|
|
|
async def after_author_follower_delete(mapper, connection, target):
|
|
|
|
await handle_author_follower_change(
|
|
|
|
connection, target.author, target.follower, False
|
|
|
|
)
|