This commit is contained in:
parent
66f1c654cf
commit
4f26812340
|
@ -1,6 +1,7 @@
|
|||
import time
|
||||
|
||||
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy import event
|
||||
|
||||
from services.db import Base
|
||||
|
||||
|
@ -40,3 +41,35 @@ class Author(Base):
|
|||
last_seen = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
||||
updated_at = Column(Integer, nullable=False, default=lambda: int(time.time()))
|
||||
deleted_at = Column(Integer, nullable=True, comment='Deleted at')
|
||||
|
||||
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)
|
||||
|
|
13
orm/shout.py
13
orm/shout.py
|
@ -1,11 +1,13 @@
|
|||
import time
|
||||
|
||||
from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from services.db import Base
|
||||
from orm.community import Community
|
||||
from orm.author import Author
|
||||
from orm.author import get_object, update_follows, update_app_data
|
||||
from orm.reaction import Reaction
|
||||
from orm.topic import Topic
|
||||
|
||||
|
@ -81,3 +83,14 @@ class Shout(Base):
|
|||
oid = Column(String, nullable=True)
|
||||
|
||||
seo = Column(String, nullable=True) # JSON
|
||||
|
||||
|
||||
@event.listens_for(ShoutReactionsFollower, 'after_insert')
|
||||
@event.listens_for(ShoutReactionsFollower, 'after_delete')
|
||||
def after_topic_follower_change(mapper, connection, target):
|
||||
shout_id = target.shout
|
||||
follower_id = target.follower
|
||||
user = get_object(connection, 'authorizer_users', follower_id)
|
||||
if user:
|
||||
app_data = update_follows(user, 'shout', get_object(connection, 'shout', shout_id))
|
||||
update_app_data(connection, follower_id, app_data)
|
||||
|
|
14
orm/topic.py
14
orm/topic.py
|
@ -1,9 +1,10 @@
|
|||
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'
|
||||
|
@ -24,3 +25,14 @@ class Topic(Base):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user