Merge remote-tracking branch 'origin/main' into storages-to-qeuries
This commit is contained in:
commit
32d668b53c
|
@ -1,6 +1,6 @@
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.orm import joinedload, aliased, make_transient
|
from sqlalchemy.orm import joinedload, aliased
|
||||||
from sqlalchemy.sql.expression import desc, asc, select, case
|
from sqlalchemy.sql.expression import desc, asc, select, case
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import query
|
from base.resolvers import query
|
||||||
|
@ -40,6 +40,32 @@ def add_rating_stat_column(q):
|
||||||
)).label('rating_stat'))
|
)).label('rating_stat'))
|
||||||
|
|
||||||
|
|
||||||
|
# def calc_reactions(q):
|
||||||
|
# aliased_reaction = aliased(Reaction)
|
||||||
|
# return q.join(aliased_reaction).add_columns(
|
||||||
|
# sa.func.sum(case(
|
||||||
|
# (aliased_reaction.kind == ReactionKind.AGREE, 1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.PROOF, 1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.REJECT, -1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.LIKE, 1),
|
||||||
|
# (aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
||||||
|
# else_=0)
|
||||||
|
# ).label('rating'),
|
||||||
|
# sa.func.sum(
|
||||||
|
# case(
|
||||||
|
# (aliased_reaction.body.is_not(None), 1),
|
||||||
|
# else_=0
|
||||||
|
# )
|
||||||
|
# ).label('commented'),
|
||||||
|
# sa.func.sum(
|
||||||
|
# aliased_reaction.id
|
||||||
|
# ).label('reacted')
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
def apply_filters(q, filters, user=None):
|
def apply_filters(q, filters, user=None):
|
||||||
filters = {} if filters is None else filters
|
filters = {} if filters is None else filters
|
||||||
if filters.get("reacted") and user:
|
if filters.get("reacted") and user:
|
||||||
|
|
|
@ -205,16 +205,6 @@ async def delete_reaction(_, info, rid):
|
||||||
session.commit()
|
session.commit()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def map_result_item(result_item):
|
|
||||||
[user, shout, reaction] = result_item
|
|
||||||
print(reaction)
|
|
||||||
reaction.createdBy = user
|
|
||||||
reaction.shout = shout
|
|
||||||
reaction.replyTo = reaction
|
|
||||||
return reaction
|
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadReactionsBy")
|
@query.field("loadReactionsBy")
|
||||||
async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
"""
|
"""
|
||||||
|
@ -235,15 +225,12 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
|
|
||||||
CreatedByUser = aliased(User)
|
CreatedByUser = aliased(User)
|
||||||
ReactedShout = aliased(Shout)
|
ReactedShout = aliased(Shout)
|
||||||
RepliedReaction = aliased(Reaction)
|
|
||||||
q = select(
|
q = select(
|
||||||
Reaction, CreatedByUser, ReactedShout, RepliedReaction
|
Reaction, CreatedByUser, ReactedShout
|
||||||
).join(
|
).join(
|
||||||
CreatedByUser, Reaction.createdBy == CreatedByUser.slug
|
CreatedByUser, Reaction.createdBy == CreatedByUser.slug
|
||||||
).join(
|
).join(
|
||||||
ReactedShout, Reaction.shout == ReactedShout.slug
|
ReactedShout, Reaction.shout == ReactedShout.slug
|
||||||
).join(
|
|
||||||
RepliedReaction, Reaction.replyTo == RepliedReaction.id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if by.get("shout"):
|
if by.get("shout"):
|
||||||
|
@ -261,23 +248,26 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
if by.get("days"):
|
if by.get("days"):
|
||||||
after = datetime.now(tz=timezone.utc) - timedelta(days=int(by["days"]) or 30)
|
after = datetime.now(tz=timezone.utc) - timedelta(days=int(by["days"]) or 30)
|
||||||
q = q.filter(Reaction.createdAt > after)
|
q = q.filter(Reaction.createdAt > after)
|
||||||
|
|
||||||
order_way = asc if by.get("sort", "").startswith("-") else desc
|
order_way = asc if by.get("sort", "").startswith("-") else desc
|
||||||
order_field = by.get("sort") or Reaction.createdAt
|
order_field = by.get("sort") or Reaction.createdAt
|
||||||
|
|
||||||
q = q.group_by(
|
q = q.group_by(
|
||||||
Reaction.id, CreatedByUser.id, ReactedShout.id
|
Reaction.id, CreatedByUser.id, ReactedShout.id
|
||||||
).order_by(
|
).order_by(
|
||||||
order_way(order_field)
|
order_way(order_field)
|
||||||
)
|
)
|
||||||
|
|
||||||
q = calc_reactions(q)
|
q = calc_reactions(q)
|
||||||
|
|
||||||
q = q.where(Reaction.deletedAt.is_(None))
|
q = q.where(Reaction.deletedAt.is_(None))
|
||||||
q = q.limit(limit).offset(offset)
|
q = q.limit(limit).offset(offset)
|
||||||
reactions = []
|
reactions = []
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for [
|
for [reaction, user, shout, rating, commented, reacted] in session.execute(q):
|
||||||
[reaction, rating, commented, reacted], shout, reply
|
reaction.createdBy = user
|
||||||
] in list(map(map_result_item, session.execute(q))):
|
|
||||||
reaction.shout = shout
|
reaction.shout = shout
|
||||||
reaction.replyTo = reply
|
|
||||||
reaction.stat = {
|
reaction.stat = {
|
||||||
"rating": rating,
|
"rating": rating,
|
||||||
"commented": commented,
|
"commented": commented,
|
||||||
|
|
|
@ -152,7 +152,7 @@ type Mutation {
|
||||||
updateChat(chat: ChatInput!): Result!
|
updateChat(chat: ChatInput!): Result!
|
||||||
deleteChat(chatId: String!): Result!
|
deleteChat(chatId: String!): Result!
|
||||||
|
|
||||||
createMessage(chat: String!, body: String!, replyTo: String): Result!
|
createMessage(chat: String!, body: String!, replyTo: Int): Result!
|
||||||
updateMessage(chatId: String!, id: Int!, body: String!): Result!
|
updateMessage(chatId: String!, id: Int!, body: String!): Result!
|
||||||
deleteMessage(chatId: String!, id: Int!): Result!
|
deleteMessage(chatId: String!, id: Int!): Result!
|
||||||
markAsRead(chatId: String!, ids: [Int]!): Result!
|
markAsRead(chatId: String!, ids: [Int]!): Result!
|
||||||
|
@ -410,7 +410,7 @@ type Reaction {
|
||||||
range: String # full / 0:2340
|
range: String # full / 0:2340
|
||||||
kind: ReactionKind!
|
kind: ReactionKind!
|
||||||
body: String
|
body: String
|
||||||
replyTo: Reaction
|
replyTo: Int
|
||||||
stat: Stat
|
stat: Stat
|
||||||
old_id: String
|
old_id: String
|
||||||
old_thread: String
|
old_thread: String
|
||||||
|
|
Loading…
Reference in New Issue
Block a user