Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unrated shouts query update #111

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions resolvers/zine/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
from datetime import datetime, timedelta

from sqlalchemy.orm import aliased, joinedload
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
from sqlalchemy.sql.expression import (
and_,
asc,
case,
desc,
distinct,
func,
nulls_last,
select,
)

from auth.authenticate import login_required
from auth.credentials import AuthCredentials
Expand Down Expand Up @@ -247,6 +256,8 @@ async def load_random_top_shouts(_, info, params):

@query.field("loadUnratedShouts")
async def load_unrated_shouts(_, info, limit):
auth: AuthCredentials = info.context["request"].auth

q = (
select(Shout)
.options(
Expand All @@ -261,12 +272,25 @@ async def load_unrated_shouts(_, info, limit):
Reaction.kind.in_([ReactionKind.LIKE, ReactionKind.DISLIKE]),
),
)
.where(and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None), Reaction.id.is_(None)))
.where(
and_(
Shout.deletedAt.is_(None),
Shout.layout.is_not(None),
Shout.createdAt >= (datetime.now() - timedelta(days=14)).date(),
)
)
)

user_id = auth.user_id
if user_id:
q = q.where(Reaction.createdBy != user_id)

# 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3)
q = q.having(func.count(distinct(Reaction.id)) <= 4)

q = add_stat_columns(q)

q = q.group_by(Shout.id).order_by(desc(Shout.createdAt)).limit(limit)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)

# print(q.compile(compile_kwargs={"literal_binds": True}))

Expand Down
Loading