This commit is contained in:
parent
bb0a218eb7
commit
509f4409ff
|
@ -1,6 +1,8 @@
|
||||||
[0.2.18]
|
[0.2.18]
|
||||||
- schema: added Shout.seo string field
|
- schema: added Shout.seo string field
|
||||||
- resolvers: added /new-author webhook resolver
|
- resolvers: added /new-author webhook resolver
|
||||||
|
- resolvers: added reader.load_shouts_top_random
|
||||||
|
- resolvers: added reader.load_shouts_unrated
|
||||||
- services: auth connector upgraded
|
- services: auth connector upgraded
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from sqlalchemy import distinct
|
from sqlalchemy import distinct, bindparam, or_
|
||||||
from sqlalchemy.orm import aliased, joinedload
|
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, func, nulls_last, select
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
@ -305,8 +305,6 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
|
||||||
@login_required
|
@login_required
|
||||||
@query.field("load_shouts_unrated")
|
@query.field("load_shouts_unrated")
|
||||||
async def load_shouts_unrated(_, info, options):
|
async def load_shouts_unrated(_, info, options):
|
||||||
user_id = info.context.get("user_id")
|
|
||||||
|
|
||||||
q = (
|
q = (
|
||||||
select(Shout)
|
select(Shout)
|
||||||
.options(
|
.options(
|
||||||
|
@ -321,18 +319,17 @@ async def load_shouts_unrated(_, info, options):
|
||||||
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.outerjoin(Author, Author.user == bindparam("user_id"))
|
||||||
.where(
|
.where(
|
||||||
and_(
|
and_(
|
||||||
Shout.deleted_at.is_(None),
|
Shout.deleted_at.is_(None),
|
||||||
Shout.layout.is_not(None),
|
Shout.layout.is_not(None),
|
||||||
Shout.created_at >= options.get("after"),
|
Shout.created_at >= options.get("after"),
|
||||||
|
or_(Author.id.is_(None), Reaction.created_by != Author.id),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if user_id:
|
|
||||||
q = q.where(Reaction.created_by != user_id)
|
|
||||||
|
|
||||||
# 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3)
|
# 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 = q.having(func.count(distinct(Reaction.id)) <= 4)
|
||||||
|
|
||||||
|
@ -340,15 +337,15 @@ async def load_shouts_unrated(_, info, options):
|
||||||
|
|
||||||
q = q.group_by(Shout.id).order_by(func.random()).limit(options.get("limit", 50)).offset(options.get("offset", 0))
|
q = q.group_by(Shout.id).order_by(func.random()).limit(options.get("limit", 50)).offset(options.get("offset", 0))
|
||||||
|
|
||||||
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
return get_shouts_from_query(q, info.context.get("user_id"))
|
||||||
|
|
||||||
return get_shouts_from_query(q)
|
|
||||||
|
|
||||||
|
|
||||||
def get_shouts_from_query(q):
|
def get_shouts_from_query(q, user_id=None):
|
||||||
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, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(
|
||||||
|
q, {"user_id": user_id}
|
||||||
|
).unique():
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": shout.views,
|
"viewed": shout.views,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user