refactored-cache-following
All checks were successful
Deploy on push / deploy (push) Successful in 36s
All checks were successful
Deploy on push / deploy (push) Successful in 36s
This commit is contained in:
@@ -8,16 +8,12 @@ from sqlalchemy.sql import and_
|
||||
|
||||
from orm.author import Author, AuthorFollower
|
||||
from orm.community import Community
|
||||
# from orm.community import Community
|
||||
from orm.reaction import Reaction
|
||||
from orm.shout import Shout, ShoutReactionsFollower
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from resolvers.stat import (author_follows_authors, author_follows_topics,
|
||||
get_authors_with_stat_cached,
|
||||
get_topics_with_stat_cached)
|
||||
from resolvers.stat import author_follows_authors, author_follows_topics, get_with_stat
|
||||
from services.auth import login_required
|
||||
from services.cache import (DEFAULT_FOLLOWS, update_followers_for_author,
|
||||
update_follows_for_author)
|
||||
from services.cache import DEFAULT_FOLLOWS
|
||||
from services.db import local_session
|
||||
from services.logger import root_logger as logger
|
||||
from services.notify import notify_follower
|
||||
@@ -33,53 +29,37 @@ async def follow(_, info, what, slug):
|
||||
user_id = info.context.get('user_id')
|
||||
if not user_id:
|
||||
return {'error': 'unauthorized'}
|
||||
[follower] = await get_authors_with_stat_cached(
|
||||
select(Author).select_from(Author).filter(Author.user == user_id)
|
||||
)
|
||||
|
||||
follower_query = select(Author).select_from(Author).filter(Author.user == user_id)
|
||||
[follower] = local_session().execute(follower_query)
|
||||
if not follower:
|
||||
return {'error': 'cant find follower'}
|
||||
|
||||
if what == 'AUTHOR':
|
||||
error = author_follow(follower.id, slug)
|
||||
if not error:
|
||||
logger.debug(f'@{follower.slug} followed @{slug}')
|
||||
[author] = await get_authors_with_stat_cached(
|
||||
select(Author).select_from(Author).where(Author.slug == slug)
|
||||
)
|
||||
if not author:
|
||||
return {'error': 'author is not found'}
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'author', author.dict(), True
|
||||
)
|
||||
_followers = await update_followers_for_author(follower, author, True)
|
||||
await notify_follower(follower.dict(), author.id, 'unfollow')
|
||||
author_query = select(Author).where(Author.slug == slug)
|
||||
[author] = local_session().execute(author_query)
|
||||
await notify_follower(follower.dict(), author.id, 'follow')
|
||||
|
||||
elif what == 'TOPIC':
|
||||
error = topic_follow(follower.id, slug)
|
||||
if not error:
|
||||
[topic] = await get_topics_with_stat_cached(
|
||||
select(Topic).where(Topic.slug == slug)
|
||||
)
|
||||
if not topic:
|
||||
return {'error': 'topic is not found'}
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'topic', topic.dict(), True
|
||||
)
|
||||
|
||||
elif what == 'COMMUNITY':
|
||||
# FIXME: when more communities
|
||||
follows = local_session().execute(select(Community))
|
||||
|
||||
elif what == 'SHOUT':
|
||||
error = reactions_follow(follower.id, slug)
|
||||
if not error:
|
||||
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
|
||||
if not shout:
|
||||
return {'error': 'cant find shout'}
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'shout', shout.dict(), True
|
||||
)
|
||||
|
||||
return {f'{what.lower()}s': follows, 'error': error}
|
||||
if error:
|
||||
return {'error': error}
|
||||
|
||||
entity = what.lower()
|
||||
follows_str = await redis.execute('GET', f'author:{follower.id}:follows-{entity}s')
|
||||
if follows_str:
|
||||
follows = json.loads(follows_str)
|
||||
return { f'{entity}s': follows }
|
||||
|
||||
|
||||
@mutation.field('unfollow')
|
||||
@@ -91,54 +71,33 @@ async def unfollow(_, info, what, slug):
|
||||
if not user_id:
|
||||
return {'error': 'unauthorized'}
|
||||
follower_query = select(Author).filter(Author.user == user_id)
|
||||
[follower] = await get_authors_with_stat_cached(follower_query)
|
||||
[follower] = local_session().execute(follower_query)
|
||||
if not follower:
|
||||
return {'error': 'follower profile is not found'}
|
||||
|
||||
if what == 'AUTHOR':
|
||||
error = author_unfollow(follower.id, slug)
|
||||
# NOTE: after triggers should update cached stats
|
||||
if not error:
|
||||
logger.info(f'@{follower.slug} unfollowing @{slug}')
|
||||
[author] = await get_authors_with_stat_cached(
|
||||
select(Author).where(Author.slug == slug)
|
||||
)
|
||||
if not author:
|
||||
return {'error': 'cant find author'}
|
||||
_followers = await update_followers_for_author(follower, author, False)
|
||||
logger.info(f'@{follower.slug} unfollowed @{slug}')
|
||||
author_query = select(Author).where(Author.slug == slug)
|
||||
[author] = local_session().execute(author_query)
|
||||
await notify_follower(follower.dict(), author.id, 'unfollow')
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'author', author.dict(), False
|
||||
)
|
||||
|
||||
elif what == 'TOPIC':
|
||||
error = topic_unfollow(follower.id, slug)
|
||||
if not error:
|
||||
logger.info(f'@{follower.slug} unfollowing §{slug}')
|
||||
[topic] = await get_topics_with_stat_cached(
|
||||
select(Topic).where(Topic.slug == slug)
|
||||
)
|
||||
if not topic:
|
||||
return {'error': 'cant find topic'}
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'topic', topic.dict(), False
|
||||
)
|
||||
|
||||
elif what == 'COMMUNITY':
|
||||
follows = local_session().execute(select(Community))
|
||||
|
||||
elif what == 'SHOUT':
|
||||
error = reactions_unfollow(follower.id, slug)
|
||||
if not error:
|
||||
logger.info(f'@{follower.slug} unfollowing §{slug}')
|
||||
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
|
||||
if not shout:
|
||||
return {'error': 'cant find shout'}
|
||||
if not error:
|
||||
follows = await update_follows_for_author(
|
||||
follower, 'shout', shout.dict(), False
|
||||
)
|
||||
|
||||
return {'error': error, f'{what.lower()}s': follows}
|
||||
entity = what.lower()
|
||||
follows_str = await redis.execute('GET', f'author:{follower.id}:follows-{entity}s')
|
||||
if follows_str:
|
||||
follows = json.loads(follows_str)
|
||||
return {'error': error, f'{entity}s': follows}
|
||||
|
||||
|
||||
async def get_follows_by_user_id(user_id: str):
|
||||
@@ -321,7 +280,7 @@ async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author
|
||||
.join(Topic, Topic.id == TopicFollower.topic)
|
||||
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
|
||||
)
|
||||
return await get_authors_with_stat_cached(q)
|
||||
return get_with_stat(q)
|
||||
|
||||
|
||||
@query.field('get_shout_followers')
|
||||
|
Reference in New Issue
Block a user