get-my-followings-fix
Some checks failed
Deploy to core / deploy (push) Failing after 1m34s

This commit is contained in:
Untone 2024-02-03 20:08:22 +03:00
parent e4846f8abb
commit e7a1697f11
3 changed files with 14 additions and 17 deletions

View File

@ -35,7 +35,6 @@ async def start_up():
# start viewed service # start viewed service
await ViewedStorage.init() await ViewedStorage.init()
# start search service # start search service
search_service.info() search_service.info()

View File

@ -1,10 +1,11 @@
import logging import logging
from typing import List from typing import List
from sqlalchemy.orm import selectinload from sqlalchemy import aliased
from sqlalchemy.sql import and_ from sqlalchemy.sql import and_
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
from orm.community import Community
from orm.reaction import Reaction from orm.reaction import Reaction
from orm.shout import Shout, ShoutReactionsFollower from orm.shout import Shout, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
@ -79,34 +80,31 @@ async def unfollow(_, info, what, slug):
@login_required @login_required
async def get_my_followed(_, info): async def get_my_followed(_, info):
user_id = info.context['user_id'] user_id = info.context['user_id']
topics = set()
authors = set()
communities = [] communities = []
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
if isinstance(author, Author): if isinstance(author, Author):
author_id = author.id author_id = author.id
aliased_author = aliased(Author)
# Using joinedload to eagerly load AuthorFollower and Author data
authors_query = ( authors_query = (
session.query(Author) session.query(aliased_author, AuthorFollower)
.join(AuthorFollower, AuthorFollower.follower == author_id) .join(AuthorFollower, AuthorFollower.follower == author_id)
.options(selectinload(Author.followers)) .filter(AuthorFollower.author == aliased_author.id)
.all()
) )
# Using joinedload to eagerly load TopicFollower and Topic data
topics_query = ( topics_query = (
session.query(Topic) session.query(Topic, TopicFollower)
.join(TopicFollower, TopicFollower.follower == author_id) .join(TopicFollower, TopicFollower.follower == author_id)
.options(selectinload(Topic.followers)) .filter(TopicFollower.topic == Topic.id)
.all()
) )
# No need for a separate query for communities as it's fetched directly authors = set(session.execute(authors_query).scalars())
communities = author.communities topics = set(session.execute(topics_query).scalars())
communities = session.query(Community).all()
return {'topics': topics_query, 'authors': authors_query, 'communities': communities} return {'topics': list(topics), 'authors': list(authors), 'communities': communities}
@query.field('get_shout_followers') @query.field('get_shout_followers')

View File

@ -132,7 +132,7 @@ async def update_topic(_, _info, inp):
session.commit() session.commit()
return {'topic': topic} return {'topic': topic}
return {'error': 'cannot update' } return {'error': 'cannot update'}
@mutation.field('delete_topic') @mutation.field('delete_topic')