core/services/follows.py

125 lines
3.9 KiB
Python
Raw Normal View History

2024-02-21 13:06:24 +00:00
import asyncio
2024-02-21 17:38:12 +00:00
2024-02-21 13:06:24 +00:00
from sqlalchemy import select, event
2024-02-21 14:37:58 +00:00
import json
2024-02-21 13:06:24 +00:00
from orm.author import Author, AuthorFollower
from orm.topic import Topic, TopicFollower
2024-02-22 23:53:19 +00:00
from resolvers.stat import get_authors_with_stat, get_topics_with_stat
2024-02-21 13:06:24 +00:00
from services.rediscache import redis
2024-02-21 17:46:29 +00:00
2024-02-22 10:20:14 +00:00
DEFAULT_FOLLOWS = {
'topics': [],
'authors': [],
'communities': [
{'slug': 'discours', 'name': 'Дискурс', 'id': 1, 'desc': ''}
],
}
2024-02-22 23:08:43 +00:00
2024-02-22 10:01:38 +00:00
async def update_author(author: Author, ttl = 25 * 60 * 60):
2024-02-21 17:46:29 +00:00
redis_key = f'user:{author.user}:author'
2024-02-22 10:01:38 +00:00
await redis.execute('SETEX', redis_key, ttl, json.dumps(author.dict()))
2024-02-21 13:06:24 +00:00
2024-02-21 16:14:58 +00:00
@event.listens_for(Author, 'after_insert')
@event.listens_for(Author, 'after_update')
2024-02-21 16:11:49 +00:00
def after_author_update(mapper, connection, author: Author):
2024-02-21 17:46:29 +00:00
asyncio.create_task(update_author(author))
2024-02-21 13:06:24 +00:00
2024-02-21 16:14:58 +00:00
@event.listens_for(TopicFollower, 'after_insert')
2024-02-21 15:07:02 +00:00
def after_topic_follower_insert(mapper, connection, target: TopicFollower):
2024-02-21 13:06:24 +00:00
asyncio.create_task(
handle_topic_follower_change(connection, target.topic, target.follower, True)
)
2024-02-21 16:14:58 +00:00
@event.listens_for(TopicFollower, 'after_delete')
2024-02-21 15:07:02 +00:00
def after_topic_follower_delete(mapper, connection, target: TopicFollower):
2024-02-21 13:06:24 +00:00
asyncio.create_task(
handle_topic_follower_change(connection, target.topic, target.follower, False)
)
2024-02-21 16:14:58 +00:00
@event.listens_for(AuthorFollower, 'after_insert')
2024-02-21 15:07:02 +00:00
def after_author_follower_insert(mapper, connection, target: AuthorFollower):
2024-02-21 13:06:24 +00:00
asyncio.create_task(
handle_author_follower_change(connection, target.author, target.follower, True)
)
2024-02-21 16:14:58 +00:00
@event.listens_for(AuthorFollower, 'after_delete')
2024-02-21 15:07:02 +00:00
def after_author_follower_delete(mapper, connection, target: AuthorFollower):
2024-02-21 13:06:24 +00:00
asyncio.create_task(
handle_author_follower_change(connection, target.author, target.follower, False)
)
2024-02-21 16:11:49 +00:00
async def update_follows_for_user(
connection, user_id, entity_type, entity: dict, is_insert
):
2024-02-21 16:14:58 +00:00
redis_key = f'user:{user_id}:follows'
2024-02-21 14:37:58 +00:00
follows_str = await redis.get(redis_key)
if follows_str:
follows = json.loads(follows_str)
else:
2024-02-22 10:20:14 +00:00
follows = DEFAULT_FOLLOWS
2024-02-21 13:06:24 +00:00
if is_insert:
2024-02-21 16:14:58 +00:00
follows[f'{entity_type}s'].append(entity)
2024-02-21 13:06:24 +00:00
else:
# Remove the entity from follows
2024-02-21 16:14:58 +00:00
follows[f'{entity_type}s'] = [
e for e in follows[f'{entity_type}s'] if e['id'] != entity['id']
2024-02-21 16:11:49 +00:00
]
2024-02-22 10:01:38 +00:00
await redis.execute('SET', redis_key, json.dumps(follows))
2024-02-21 13:06:24 +00:00
async def handle_author_follower_change(connection, author_id, follower_id, is_insert):
q = select(Author).filter(Author.id == author_id)
2024-02-22 23:53:19 +00:00
authors = get_authors_with_stat(q)
2024-02-22 23:08:43 +00:00
author = authors[0]
2024-02-21 13:06:24 +00:00
async with connection.begin() as conn:
follower = await conn.execute(
select(Author).filter(Author.id == follower_id)
).first()
if follower and author:
await update_follows_for_user(
2024-02-22 23:08:43 +00:00
conn,
2024-02-21 16:48:33 +00:00
follower.user,
'author',
{
'id': author.id,
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
'stat': author.stat,
},
is_insert,
2024-02-21 13:06:24 +00:00
)
async def handle_topic_follower_change(connection, topic_id, follower_id, is_insert):
q = select(Topic).filter(Topic.id == topic_id)
2024-02-22 23:53:19 +00:00
topics = get_topics_with_stat(q)
2024-02-22 23:08:43 +00:00
topic = topics[0]
2024-02-21 13:06:24 +00:00
async with connection.begin() as conn:
2024-02-22 23:08:43 +00:00
follower = conn.execute(select(Author).filter(Author.id == follower_id)).first()
2024-02-21 13:06:24 +00:00
if follower and topic:
await update_follows_for_user(
2024-02-22 23:08:43 +00:00
conn,
2024-02-21 16:48:33 +00:00
follower.user,
'topic',
{
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
'stat': topic.stat,
},
is_insert,
2024-02-21 13:06:24 +00:00
)