added heavy stat for getAuthor
This commit is contained in:
parent
d5bf841eac
commit
2361f404f4
|
@ -17,11 +17,10 @@ from resolvers.inbox.unread import get_total_unread_counter
|
||||||
from resolvers.zine.topics import followed_by_user
|
from resolvers.zine.topics import followed_by_user
|
||||||
|
|
||||||
|
|
||||||
def add_author_stat_columns(q):
|
def add_author_stat_columns(q, include_heavy_stat=False):
|
||||||
author_followers = aliased(AuthorFollower)
|
author_followers = aliased(AuthorFollower)
|
||||||
author_following = aliased(AuthorFollower)
|
author_following = aliased(AuthorFollower)
|
||||||
shout_author_aliased = aliased(ShoutAuthor)
|
shout_author_aliased = aliased(ShoutAuthor)
|
||||||
# user_rating_aliased = aliased(UserRating)
|
|
||||||
|
|
||||||
q = q.outerjoin(shout_author_aliased).add_columns(
|
q = q.outerjoin(shout_author_aliased).add_columns(
|
||||||
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
|
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
|
||||||
|
@ -34,17 +33,26 @@ def add_author_stat_columns(q):
|
||||||
func.count(distinct(author_following.author)).label('followings_stat')
|
func.count(distinct(author_following.author)).label('followings_stat')
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.add_columns(literal(0).label('rating_stat'))
|
if include_heavy_stat:
|
||||||
# FIXME
|
user_rating_aliased = aliased(UserRating)
|
||||||
# q = q.outerjoin(user_rating_aliased, user_rating_aliased.user == User.id).add_columns(
|
q = q.outerjoin(user_rating_aliased, user_rating_aliased.user == User.id).add_columns(
|
||||||
# # TODO: check
|
func.sum(user_rating_aliased.value).label('rating_stat')
|
||||||
# func.sum(user_rating_aliased.value).label('rating_stat')
|
)
|
||||||
# )
|
|
||||||
|
|
||||||
q = q.add_columns(literal(0).label('commented_stat'))
|
else:
|
||||||
# q = q.outerjoin(Reaction, and_(Reaction.createdBy == User.id, Reaction.body.is_not(None))).add_columns(
|
q = q.add_columns(literal(-1).label('rating_stat'))
|
||||||
# func.count(distinct(Reaction.id)).label('commented_stat')
|
|
||||||
# )
|
if include_heavy_stat:
|
||||||
|
q = q.outerjoin(
|
||||||
|
Reaction,
|
||||||
|
and_(
|
||||||
|
Reaction.createdBy == User.id,
|
||||||
|
Reaction.body.is_not(None)
|
||||||
|
)).add_columns(
|
||||||
|
func.count(distinct(Reaction.id)).label('commented_stat')
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
q = q.add_columns(literal(-1).label('commented_stat'))
|
||||||
|
|
||||||
q = q.group_by(User.id)
|
q = q.group_by(User.id)
|
||||||
|
|
||||||
|
@ -101,6 +109,7 @@ async def followed_reactions(user_id):
|
||||||
Reaction.createdAt > user.lastSeen
|
Reaction.createdAt > user.lastSeen
|
||||||
).all()
|
).all()
|
||||||
|
|
||||||
|
|
||||||
# dufok mod (^*^') :
|
# dufok mod (^*^') :
|
||||||
@query.field("userFollowedTopics")
|
@query.field("userFollowedTopics")
|
||||||
async def get_followed_topics(_, info, slug) -> List[Topic]:
|
async def get_followed_topics(_, info, slug) -> List[Topic]:
|
||||||
|
@ -117,6 +126,7 @@ async def get_followed_topics(_, info, slug) -> List[Topic]:
|
||||||
async def followed_topics(user_id):
|
async def followed_topics(user_id):
|
||||||
return followed_by_user(user_id)
|
return followed_by_user(user_id)
|
||||||
|
|
||||||
|
|
||||||
# dufok mod (^*^') :
|
# dufok mod (^*^') :
|
||||||
@query.field("userFollowedAuthors")
|
@query.field("userFollowedAuthors")
|
||||||
async def get_followed_authors(_, _info, slug) -> List[User]:
|
async def get_followed_authors(_, _info, slug) -> List[User]:
|
||||||
|
@ -130,6 +140,7 @@ async def get_followed_authors(_, _info, slug) -> List[User]:
|
||||||
|
|
||||||
return await followed_authors(user_id)
|
return await followed_authors(user_id)
|
||||||
|
|
||||||
|
|
||||||
# 2. Now, we can use the user_id to get the followed authors
|
# 2. Now, we can use the user_id to get the followed authors
|
||||||
async def followed_authors(user_id):
|
async def followed_authors(user_id):
|
||||||
q = select(User)
|
q = select(User)
|
||||||
|
@ -255,7 +266,7 @@ async def get_authors_all(_, _info):
|
||||||
@query.field("getAuthor")
|
@query.field("getAuthor")
|
||||||
async def get_author(_, _info, slug):
|
async def get_author(_, _info, slug):
|
||||||
q = select(User).where(User.slug == slug)
|
q = select(User).where(User.slug == slug)
|
||||||
q = add_author_stat_columns(q)
|
q = add_author_stat_columns(q, True)
|
||||||
|
|
||||||
authors = get_authors_from_query(q)
|
authors = get_authors_from_query(q)
|
||||||
return authors[0]
|
return authors[0]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user