This commit is contained in:
parent
d4982017f6
commit
1af63dee81
1
cache/cache.py
vendored
1
cache/cache.py
vendored
|
@ -104,7 +104,6 @@ async def get_cached_topic(topic_id: int):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Get topic by slug from cache
|
# Get topic by slug from cache
|
||||||
async def get_cached_topic_by_slug(slug: str):
|
async def get_cached_topic_by_slug(slug: str):
|
||||||
topic_key = f"topic:slug:{slug}"
|
topic_key = f"topic:slug:{slug}"
|
||||||
|
|
|
@ -21,19 +21,21 @@ from services.viewed import ViewedStorage
|
||||||
|
|
||||||
|
|
||||||
def add_reaction_stat_columns(q, aliased_reaction):
|
def add_reaction_stat_columns(q, aliased_reaction):
|
||||||
reaction_subquery = (
|
# Adjust case statements to use positional arguments
|
||||||
select(
|
q = q.outerjoin(aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns(
|
||||||
aliased_reaction.shout,
|
func.count(case((aliased_reaction.body.is_not(None), 1), else_=0)).label("comments_stat"),
|
||||||
func.count(aliased_reaction.id).label("reacted_stat"),
|
# Calculate net likes as likes - dislikes
|
||||||
func.count(case([(aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1)])).label("comments_stat"),
|
func.sum(
|
||||||
func.count(case([(aliased_reaction.kind == str(ReactionKind.LIKE.value), 1)])).label("likes_stat"),
|
case(
|
||||||
func.count(case([(aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1)])).label("dislikes_stat"),
|
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
|
||||||
func.max(aliased_reaction.created_at).label("last_comment_stat"),
|
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
||||||
)
|
else_=0,
|
||||||
.where(aliased_reaction.deleted_at.is_(None))
|
)
|
||||||
.group_by(aliased_reaction.shout)
|
).label("rating_stat"),
|
||||||
|
func.max(aliased_reaction.created_at).label("last_comment_stat"),
|
||||||
)
|
)
|
||||||
return q.outerjoin(reaction_subquery, Shout.id == reaction_subquery.c.shout)
|
|
||||||
|
return q
|
||||||
|
|
||||||
|
|
||||||
def is_featured_author(session, author_id):
|
def is_featured_author(session, author_id):
|
||||||
|
@ -247,10 +249,8 @@ async def update_reaction(_, info, reaction):
|
||||||
if result:
|
if result:
|
||||||
[
|
[
|
||||||
r,
|
r,
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
commented_stat,
|
||||||
likes_stat,
|
rating_stat,
|
||||||
dislikes_stat,
|
|
||||||
last_reacted_at,
|
last_reacted_at,
|
||||||
] = result
|
] = result
|
||||||
if not r:
|
if not r:
|
||||||
|
@ -275,9 +275,8 @@ async def update_reaction(_, info, reaction):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
r.stat = {
|
r.stat = {
|
||||||
"reacted": reacted_stat,
|
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
"rating": rating_stat,
|
||||||
}
|
}
|
||||||
|
|
||||||
await notify_reaction(r.dict(), "update")
|
await notify_reaction(r.dict(), "update")
|
||||||
|
|
|
@ -100,18 +100,15 @@ async def get_shout(_, info, slug: str):
|
||||||
if results:
|
if results:
|
||||||
[
|
[
|
||||||
shout,
|
shout,
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
commented_stat,
|
||||||
likes_stat,
|
rating_stat,
|
||||||
dislikes_stat,
|
|
||||||
last_reaction_at,
|
last_reaction_at,
|
||||||
] = results
|
] = results
|
||||||
|
|
||||||
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": int(likes_stat or 0) - int(dislikes_stat or 0),
|
"rating": rating_stat,
|
||||||
"last_reacted_at": last_reaction_at,
|
"last_reacted_at": last_reaction_at,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +162,7 @@ async def load_shouts_by(_, _info, options):
|
||||||
}
|
}
|
||||||
offset: 0
|
offset: 0
|
||||||
limit: 50
|
limit: 50
|
||||||
order_by: "likes" | "followers" | "comments" | "last_reacted_at"
|
order_by: "rating" | "followers" | "comments" | "last_reacted_at"
|
||||||
order_by_desc: true
|
order_by_desc: true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -189,7 +186,7 @@ async def load_shouts_by(_, _info, options):
|
||||||
# order
|
# order
|
||||||
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
||||||
order_str = options.get("order_by")
|
order_str = options.get("order_by")
|
||||||
if order_str in ["likes", "followers", "comments", "last_reacted_at"]:
|
if order_str in ["rating", "followers", "comments", "last_reacted_at"]:
|
||||||
q = q.order_by(desc(text(f"{order_str}_stat")))
|
q = q.order_by(desc(text(f"{order_str}_stat")))
|
||||||
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
||||||
q = q.order_by(nulls_last(query_order_by))
|
q = q.order_by(nulls_last(query_order_by))
|
||||||
|
@ -203,10 +200,8 @@ async def load_shouts_by(_, _info, options):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for [
|
for [
|
||||||
shout,
|
shout,
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
commented_stat,
|
||||||
likes_stat,
|
rating_stat,
|
||||||
dislikes_stat,
|
|
||||||
last_reacted_at,
|
last_reacted_at,
|
||||||
] in session.execute(q).unique():
|
] in session.execute(q).unique():
|
||||||
main_topic = (
|
main_topic = (
|
||||||
|
@ -225,10 +220,9 @@ async def load_shouts_by(_, _info, options):
|
||||||
if main_topic:
|
if main_topic:
|
||||||
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": int(likes_stat) - int(dislikes_stat),
|
"rating": rating_stat,
|
||||||
"last_reacted_at": last_reacted_at,
|
"last_reacted_at": last_reacted_at,
|
||||||
}
|
}
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
|
@ -267,10 +261,8 @@ async def load_shouts_feed(_, info, options):
|
||||||
|
|
||||||
for [
|
for [
|
||||||
shout,
|
shout,
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
commented_stat,
|
||||||
likes_stat,
|
rating_stat,
|
||||||
dislikes_stat,
|
|
||||||
last_reacted_at,
|
last_reacted_at,
|
||||||
] in session.execute(q).unique():
|
] in session.execute(q).unique():
|
||||||
main_topic = (
|
main_topic = (
|
||||||
|
@ -290,9 +282,8 @@ async def load_shouts_feed(_, 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": likes_stat - dislikes_stat,
|
"rating": rating_stat,
|
||||||
"last_reacted_at": last_reacted_at,
|
"last_reacted_at": last_reacted_at,
|
||||||
}
|
}
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
|
@ -362,18 +353,15 @@ async def get_shouts_from_query(q, author_id=None):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for [
|
for [
|
||||||
shout,
|
shout,
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
commented_stat,
|
||||||
likes_stat,
|
rating_stat,
|
||||||
dislikes_stat,
|
|
||||||
last_reacted_at,
|
last_reacted_at,
|
||||||
] in session.execute(q, {"author_id": author_id}).unique():
|
] in session.execute(q, {"author_id": author_id}).unique():
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
|
# "viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
|
||||||
"reacted": reacted_stat,
|
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
"rating": rating_stat,
|
||||||
"last_reacted_at": last_reacted_at,
|
"last_reacted_at": last_reacted_at,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,8 +92,6 @@ type Shout {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stat {
|
type Stat {
|
||||||
viewed: Int
|
|
||||||
reacted: Int
|
|
||||||
rating: Int
|
rating: Int
|
||||||
commented: Int
|
commented: Int
|
||||||
ranking: Int
|
ranking: Int
|
||||||
|
|
Loading…
Reference in New Issue
Block a user