This commit is contained in:
parent
6207f7d3ed
commit
9968fb27f4
|
@ -1,7 +1,7 @@
|
||||||
import time
|
import time
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from sqlalchemy import and_, case, distinct, func, literal, select
|
from sqlalchemy import and_, case, distinct, func, literal, select, cast, Integer
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.author import Author, AuthorFollower, AuthorRating
|
from orm.author import Author, AuthorFollower, AuthorRating
|
||||||
|
@ -121,21 +121,7 @@ async def get_author_id(_, _info, user: str):
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def count_author_rating(session, author_id) -> int:
|
def count_author_comments_rating(session, author_id) -> int:
|
||||||
shouts_likes = (
|
|
||||||
session.query(Reaction, Shout)
|
|
||||||
.join(Shout, Shout.id == Reaction.shout)
|
|
||||||
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.LIKE.value))
|
|
||||||
.count()
|
|
||||||
or 0
|
|
||||||
)
|
|
||||||
shouts_dislikes = (
|
|
||||||
session.query(Reaction, Shout)
|
|
||||||
.join(Shout, Shout.id == Reaction.shout)
|
|
||||||
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.DISLIKE.value))
|
|
||||||
.count()
|
|
||||||
or 0
|
|
||||||
)
|
|
||||||
replied_alias = aliased(Reaction)
|
replied_alias = aliased(Reaction)
|
||||||
replies_likes = (
|
replies_likes = (
|
||||||
session.query(replied_alias)
|
session.query(replied_alias)
|
||||||
|
@ -152,7 +138,25 @@ def count_author_rating(session, author_id) -> int:
|
||||||
.count()
|
.count()
|
||||||
) or 0
|
) or 0
|
||||||
|
|
||||||
return shouts_likes - shouts_dislikes + replies_likes - replies_dislikes
|
return replies_likes - replies_dislikes
|
||||||
|
|
||||||
|
|
||||||
|
def count_author_shouts_rating(session, author_id) -> int:
|
||||||
|
shouts_likes = (
|
||||||
|
session.query(Reaction, Shout)
|
||||||
|
.join(Shout, Shout.id == Reaction.shout)
|
||||||
|
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.LIKE.value))
|
||||||
|
.count()
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
shouts_dislikes = (
|
||||||
|
session.query(Reaction, Shout)
|
||||||
|
.join(Shout, Shout.id == Reaction.shout)
|
||||||
|
.filter(and_(Shout.authors.any(id=author_id), Reaction.kind == ReactionKind.DISLIKE.value))
|
||||||
|
.count()
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
return shouts_likes - shouts_dislikes
|
||||||
|
|
||||||
|
|
||||||
@query.field("get_author")
|
@query.field("get_author")
|
||||||
|
@ -164,9 +168,10 @@ async def get_author(_, _info, slug="", author_id=None):
|
||||||
elif author_id:
|
elif author_id:
|
||||||
q = select(Author).where(Author.id == author_id)
|
q = select(Author).where(Author.id == author_id)
|
||||||
q = add_author_stat_columns(q)
|
q = add_author_stat_columns(q)
|
||||||
authors = get_authors_from_query(q)
|
|
||||||
if authors:
|
[author] = get_authors_from_query(q)
|
||||||
author = authors[0]
|
|
||||||
|
if author:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
comments_count = (
|
comments_count = (
|
||||||
session.query(Reaction)
|
session.query(Reaction)
|
||||||
|
@ -179,13 +184,21 @@ async def get_author(_, _info, slug="", author_id=None):
|
||||||
)
|
)
|
||||||
.count()
|
.count()
|
||||||
)
|
)
|
||||||
rating_sum = (
|
ratings_sum = (
|
||||||
session.query(func.sum(AuthorRating.value))
|
session.query(
|
||||||
|
func.sum(
|
||||||
|
case((AuthorRating.plus == True, cast(1, Integer)),
|
||||||
|
else_=cast(-1, Integer))).label("rating")
|
||||||
|
)
|
||||||
.filter(AuthorRating.author == author.id)
|
.filter(AuthorRating.author == author.id)
|
||||||
.scalar()
|
.scalar()
|
||||||
)
|
)
|
||||||
author.stat["rating"] = rating_sum
|
|
||||||
|
author.stat["rating"] = ratings_sum or 0
|
||||||
|
author.stat["rating_shouts"] = count_author_shouts_rating(session, author.id)
|
||||||
|
author.stat["rating_comments"] = count_author_comments_rating(session, author.id)
|
||||||
author.stat["commented"] = comments_count
|
author.stat["commented"] = comments_count
|
||||||
|
return count_author_comments_rating
|
||||||
else:
|
else:
|
||||||
return {"error": "cant find author"}
|
return {"error": "cant find author"}
|
||||||
|
|
||||||
|
@ -261,13 +274,13 @@ async def rate_author(_, info, rated_slug, value):
|
||||||
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
||||||
rater = session.query(Author).filter(Author.slug == user_id).first()
|
rater = session.query(Author).filter(Author.slug == user_id).first()
|
||||||
if rater and rated_author:
|
if rater and rated_author:
|
||||||
rating = (
|
rating: AuthorRating = (
|
||||||
session.query(AuthorRating)
|
session.query(AuthorRating)
|
||||||
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.author == rated_author.id))
|
.filter(and_(AuthorRating.rater == rater.id, AuthorRating.author == rated_author.id))
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
if value > 0:
|
if rating:
|
||||||
rating.plus = True
|
rating.plus = value > 0
|
||||||
session.add(rating)
|
session.add(rating)
|
||||||
session.commit()
|
session.commit()
|
||||||
return {}
|
return {}
|
||||||
|
|
|
@ -57,6 +57,8 @@ type AuthorStat {
|
||||||
followings: Int
|
followings: Int
|
||||||
followers: Int
|
followers: Int
|
||||||
rating: Int
|
rating: Int
|
||||||
|
rating_shouts: Int
|
||||||
|
rating_comments: Int
|
||||||
commented: Int
|
commented: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user