reactions-cache-update
All checks were successful
Deploy on push / deploy (push) Successful in 34s

This commit is contained in:
Untone 2024-04-09 16:43:06 +03:00
parent 1769b0925b
commit 2bf456b343
2 changed files with 31 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import time import time
from typing import List from typing import List
from resolvers.stat import update_author_stat
from sqlalchemy import and_, asc, case, desc, func, select, text from sqlalchemy import and_, asc, case, desc, func, select, text
from sqlalchemy.orm import aliased, joinedload from sqlalchemy.orm import aliased, joinedload
from sqlalchemy.sql import union from sqlalchemy.sql import union
@ -121,6 +122,10 @@ async def _create_reaction(session, shout, author, reaction):
session.commit() session.commit()
rdict = r.dict() rdict = r.dict()
# пересчет счетчика комментариев
if r.kind == ReactionKind.COMMENT.value:
await update_author_stat(author)
# collaborative editing # collaborative editing
if ( if (
rdict.get('reply_to') rdict.get('reply_to')
@ -129,6 +134,7 @@ async def _create_reaction(session, shout, author, reaction):
): ):
handle_proposing(session, r, shout) handle_proposing(session, r, shout)
# рейтинг и саморегуляция
if r.kind in RATING_REACTIONS: if r.kind in RATING_REACTIONS:
# self-regultaion mechanics # self-regultaion mechanics
if check_to_unfeature(session, author.id, r): if check_to_unfeature(session, author.id, r):
@ -144,8 +150,12 @@ async def _create_reaction(session, shout, author, reaction):
except Exception: except Exception:
pass pass
# обновление счетчика комментариев в кеше
if r.kind == ReactionKind.COMMENT.value:
await update_author_stat(author)
rdict['shout'] = shout.dict() rdict['shout'] = shout.dict()
rdict['created_by'] = author.dict() rdict['created_by'] = author.id
rdict['stat'] = {'commented': 0, 'reacted': 0, 'rating': 0} rdict['stat'] = {'commented': 0, 'reacted': 0, 'rating': 0}
# notifications call # notifications call
@ -302,7 +312,6 @@ async def delete_reaction(_, info, reaction_id: int):
try: try:
author = session.query(Author).filter(Author.user == user_id).one() author = session.query(Author).filter(Author.user == user_id).one()
r = session.query(Reaction).filter(Reaction.id == reaction_id).one() r = session.query(Reaction).filter(Reaction.id == reaction_id).one()
if r and author:
if r.created_by != author.id and 'editor' not in roles: if r.created_by != author.id and 'editor' not in roles:
return {'error': 'access denied'} return {'error': 'access denied'}
@ -310,6 +319,10 @@ async def delete_reaction(_, info, reaction_id: int):
reaction_dict = r.dict() reaction_dict = r.dict()
session.delete(r) session.delete(r)
session.commit() session.commit()
# обновление счетчика комментариев в кеше
if r.kind == ReactionKind.COMMENT.value:
await update_author_stat(author)
await notify_reaction(reaction_dict, 'delete') await notify_reaction(reaction_dict, 'delete')
return {'error': None, 'reaction': reaction_dict} return {'error': None, 'reaction': reaction_dict}

View File

@ -6,6 +6,7 @@ from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from services.db import local_session from services.db import local_session
from services.cache import cache_author
def add_topic_stat_columns(q): def add_topic_stat_columns(q):
@ -152,3 +153,10 @@ def author_follows_topics(author_id: int):
.where(TopicFollower.follower == author_id) .where(TopicFollower.follower == author_id)
) )
return get_with_stat(q) return get_with_stat(q)
async def update_author_stat(author: Author):
author_with_stat = get_with_stat(select(Author).where(Author=author.id))
if isinstance(author_with_stat, Author):
author_dict = author_with_stat.dict()
await cache_author(author_dict)