core/orm/author.py

76 lines
3.0 KiB
Python
Raw Normal View History

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-20 18:57:39 +00:00
from sqlalchemy import event
2023-11-22 16:38:39 +00:00
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-01-25 19:41:27 +00:00
__tablename__ = 'author_rating'
2023-10-23 14:47:11 +00:00
id = None # type: ignore
2024-02-20 08:47:37 +00:00
rater = Column(ForeignKey('author.id'), primary_key=True)
2024-02-20 08:53:55 +00:00
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-01-25 19:41:27 +00:00
__tablename__ = 'author_follower'
2023-10-23 14:47:11 +00:00
id = None # type: ignore
2024-02-20 08:47:37 +00:00
follower = Column(ForeignKey('author.id'), primary_key=True)
2024-02-20 08:53:55 +00:00
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-01-25 19:41:27 +00:00
__tablename__ = 'author'
2023-10-23 14:47:11 +00:00
2024-02-19 14:22:38 +00:00
user = Column(String,
) # unbounded link with authorizer's User type
2023-11-03 10:10:22 +00:00
2024-01-25 19:41:27 +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-01-25 19:41:27 +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-01-25 19:41:27 +00:00
deleted_at = Column(Integer, nullable=True, comment='Deleted at')
2024-02-20 18:57:39 +00:00
def get_object(connection, table_name, object_id):
return connection.execute(f"SELECT * FROM {table_name} WHERE id = :object_id", {"object_id": object_id}).fetchone()
def update_app_data(connection, user_id, app_data):
connection.execute("UPDATE authorizer_users SET app_data = :app_data WHERE id = :user_id", {"app_data": app_data, "user_id": user_id})
def update_follows(user, entity_type, entity):
app_data = user.app_data or {}
app_data['follows'] = user.app_data or {"topics": [], "authors": [], "shouts": [], "communities": []}
app_data['follows'][f'{entity_type}s'].append(vars(entity))
return app_data
@event.listens_for(Author, 'after_insert')
@event.listens_for(Author, 'after_update')
def after_author_update(mapper, connection, target):
user_id = target.user
user = get_object(connection, 'authorizer_users', user_id)
if user:
app_data = update_follows(user, 'author', target)
update_app_data(connection, user_id, app_data)
@event.listens_for(AuthorFollower, 'after_insert')
@event.listens_for(AuthorFollower, 'after_delete')
def after_author_follower_change(mapper, connection, target):
author_id = target.author
follower_id = target.follower
user = get_object(connection, 'authorizer_users', follower_id)
if user:
app_data = update_follows(user, 'author', get_object(connection, 'author', author_id))
update_app_data(connection, follower_id, app_data)