rating-fix

This commit is contained in:
Untone 2024-01-23 02:08:59 +03:00
parent 438baeb1a2
commit 0a38ae8e7e

View File

@ -14,35 +14,14 @@ from services.search import SearchService
from services.viewed import ViewedStorage from services.viewed import ViewedStorage
from resolvers.topic import get_random_topic from resolvers.topic import get_random_topic
def add_stat_columns(q): def add_stat_columns(q):
aliased_reaction = aliased(Reaction) aliased_reaction = aliased(Reaction)
q = q.outerjoin(aliased_reaction).add_columns( q = q.outerjoin(aliased_reaction).add_columns(
# func.sum(aliased_reaction.id).label("reacted_stat"), func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("comments_stat"),
func.sum( func.sum(case((aliased_reaction.kind == ReactionKind.AGREE.value, 1), else_=0)).label("likes_stat"),
case( func.sum(case((aliased_reaction.kind == ReactionKind.DISAGREE.value, 1), else_=0)).label("dislikes_stat"),
(aliased_reaction.kind == ReactionKind.COMMENT.value, 1), func.max(case((aliased_reaction.kind != ReactionKind.COMMENT.value, None),else_=aliased_reaction.created_at)).label("last_comment"),
(aliased_reaction.kind == ReactionKind.PROOF.value, 1),
(aliased_reaction.kind == ReactionKind.DISPROOF.value, 1),
else_=0
)
).label("commented_stat"),
func.sum(
case(
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
(aliased_reaction.kind == ReactionKind.DISAGREE.value, -1),
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0,
)
).label("rating_stat"),
func.max(
case(
(aliased_reaction.kind != ReactionKind.COMMENT.value, None),
else_=aliased_reaction.created_at,
)
).label("last_comment"),
) )
return q return q
@ -93,13 +72,13 @@ async def get_shout(_, _info, slug=None, shout_id=None):
try: try:
results = session.execute(q).first() results = session.execute(q).first()
if results: if results:
[shout, commented_stat, rating_stat, _last_comment] = results [shout, commented_stat, likes_stat, dislikes_stat, _last_comment] = results
shout.stat = { shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug), "viewed": await ViewedStorage.get_shout(shout.slug),
# "reacted": reacted_stat, # "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": rating_stat, "rating": int(likes_stat or 0) - int(dislikes_stat or 0),
} }
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug): for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
@ -174,7 +153,7 @@ async def load_shouts_by(_, _info, options):
shouts = [] shouts = []
with local_session() as session: with local_session() as session:
for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique(): for [shout, commented_stat, likes_stat, dislikes_stat, _last_comment] in session.execute(q).unique():
main_topic = ( main_topic = (
session.query(Topic.slug) session.query(Topic.slug)
.join( .join(
@ -190,9 +169,8 @@ async def load_shouts_by(_, _info, options):
shout.main_topic = main_topic[0] shout.main_topic = main_topic[0]
shout.stat = { shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug), "viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": rating_stat, "rating": int(likes_stat) - int(dislikes_stat),
} }
shouts.append(shout) shouts.append(shout)