get_author_follows-fix

This commit is contained in:
Untone 2024-03-28 13:33:41 +03:00
parent 235b908766
commit 65fd4df5ef

View File

@ -71,7 +71,7 @@ async def get_author(_, _info, slug='', author_id=None):
else: else:
logger.warn('author was not cached!') logger.warn('author was not cached!')
author_query = select(Author).filter(Author.id == author_id) author_query = select(Author).filter(Author.id == author_id)
author = get_with_stat(author_query) [author] = get_with_stat(author_query)
author_dict = author.dict() author_dict = author.dict()
if author_dict: if author_dict:
await set_author_cache(author_dict) await set_author_cache(author_dict)
@ -161,13 +161,19 @@ def load_authors_by(_, _info, by, limit, offset):
@query.field('get_author_follows') @query.field('get_author_follows')
async def get_author_follows(_, _info, slug='', user=None, author_id=None): async def get_author_follows(_, _info, slug='', user=None, author_id=0):
with (local_session() as session): author_query = select(Author)
if user or slug: if user:
author = session.query(Author).filter(or_(Author.user == user, Author.slug == slug)).first() author_query = author_query.filter(Author.user == user)
if author: elif slug:
author_query = author_query.filter(Author.slug == slug)
elif author_id:
author_query = author_query.filter(Author.id == author_id)
else:
raise ValueError('One of slug, user, or author_id must be provided')
[author] = local_session().execute(author_query)
if isinstance(author, Author):
author_id = author.id author_id = author.id
if author_id:
rkey = f'author:{author_id}:follows-authors' rkey = f'author:{author_id}:follows-authors'
logger.debug(f'getting {author_id} follows authors') logger.debug(f'getting {author_id} follows authors')
cached = await redis.execute('GET', rkey) cached = await redis.execute('GET', rkey)
@ -175,7 +181,7 @@ async def get_author_follows(_, _info, slug='', user=None, author_id=None):
authors = author_follows_authors(author_id) authors = author_follows_authors(author_id)
prepared = [author.dict() for author in authors] prepared = [author.dict() for author in authors]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
else: elif isinstance(cached, str):
authors = json.loads(cached) authors = json.loads(cached)
rkey = f'author:{author_id}:follows-topics' rkey = f'author:{author_id}:follows-topics'