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,40 +161,46 @@ 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_id = author.id author_query = author_query.filter(Author.slug == slug)
if author_id: elif author_id:
rkey = f'author:{author_id}:follows-authors' author_query = author_query.filter(Author.id == author_id)
logger.debug(f'getting {author_id} follows authors') else:
cached = await redis.execute('GET', rkey) raise ValueError('One of slug, user, or author_id must be provided')
if not cached: [author] = local_session().execute(author_query)
authors = author_follows_authors(author_id) if isinstance(author, Author):
prepared = [author.dict() for author in authors] author_id = author.id
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) rkey = f'author:{author_id}:follows-authors'
else: logger.debug(f'getting {author_id} follows authors')
authors = json.loads(cached) cached = await redis.execute('GET', rkey)
if not cached:
authors = author_follows_authors(author_id)
prepared = [author.dict() for author in authors]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
elif isinstance(cached, str):
authors = json.loads(cached)
rkey = f'author:{author_id}:follows-topics' rkey = f'author:{author_id}:follows-topics'
cached = await redis.execute('GET', rkey) cached = await redis.execute('GET', rkey)
if cached: if cached:
topics = json.loads(cached) topics = json.loads(cached)
if not cached: if not cached:
topics = author_follows_topics(author_id) topics = author_follows_topics(author_id)
prepared = [topic.dict() for topic in topics] prepared = [topic.dict() for topic in topics]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
return { return {
'topics': topics, 'topics': topics,
'authors': authors, 'authors': authors,
'communities': [ 'communities': [
{'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''} {'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''}
], ],
} }
else: else:
raise ValueError('Author not found') raise ValueError('Author not found')
@query.field('get_author_follows_topics') @query.field('get_author_follows_topics')