This commit is contained in:
parent
dfd476411f
commit
522718f3a1
|
@ -24,7 +24,7 @@ class User(Base):
|
|||
# preferred_username = Column(String, nullable=False)
|
||||
picture = Column(String)
|
||||
revoked_timestamp = Column(Integer)
|
||||
roles = Column(String, default="author, reader")
|
||||
roles = Column(String, default="author,reader")
|
||||
signup_methods = Column(String, default="magic_link_login")
|
||||
created_at = Column(Integer, default=lambda: int(time.time()))
|
||||
updated_at = Column(Integer, default=lambda: int(time.time()))
|
||||
|
|
|
@ -21,17 +21,13 @@ from services.viewed import ViewedStorage
|
|||
|
||||
|
||||
def add_reaction_stat_columns(q, aliased_reaction):
|
||||
q = q.outerjoin(aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns(
|
||||
q = q.outerjoin(
|
||||
aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns(
|
||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
||||
func.sum(case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)).label("comments_stat"),
|
||||
func.sum(case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)).label("likes_stat"),
|
||||
func.sum(case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)).label("dislikes_stat"),
|
||||
func.max(
|
||||
case(
|
||||
(aliased_reaction.kind == str(ReactionKind.COMMENT.value), aliased_reaction.created_at),
|
||||
else_=None,
|
||||
)
|
||||
).label("last_comment_stat"),
|
||||
func.max(aliased_reaction.created_at).label("last_comment_stat")
|
||||
)
|
||||
|
||||
return q
|
||||
|
@ -252,7 +248,7 @@ async def update_reaction(_, info, reaction):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reacted_at,
|
||||
] = result
|
||||
if not r:
|
||||
return {"error": "invalid reaction id"}
|
||||
|
@ -423,7 +419,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
_last_comment,
|
||||
last_reacted_at,
|
||||
] in result_rows:
|
||||
reaction.created_by = author
|
||||
reaction.shout = shout
|
||||
|
@ -431,6 +427,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
|||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
||||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"last_reacted_at": last_reacted_at
|
||||
}
|
||||
reactions.add(reaction)
|
||||
|
||||
|
@ -465,8 +462,8 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
|
|||
)
|
||||
q2 = add_reaction_stat_columns(q2, aliased(Reaction))
|
||||
|
||||
# Sort shouts by the `last_comment` field
|
||||
combined_query = union(q1, q2).order_by(desc(text("last_comment_stat"))).limit(limit).offset(offset)
|
||||
# Sort shouts by the `last_reacted_at` field
|
||||
combined_query = union(q1, q2).order_by(desc(text("last_reacted_at"))).limit(limit).offset(offset)
|
||||
|
||||
results = session.execute(combined_query).scalars()
|
||||
for [
|
||||
|
@ -475,14 +472,14 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reacted_at,
|
||||
] in results:
|
||||
shout.stat = {
|
||||
"viewed": await ViewedStorage.get_shout(shout.slug),
|
||||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
||||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"last_comment": last_comment,
|
||||
"last_reacted_at": last_reacted_at,
|
||||
}
|
||||
shouts.append(shout)
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ async def get_shout(_, info, slug: str):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reaction_at,
|
||||
] = results
|
||||
|
||||
shout.stat = {
|
||||
|
@ -111,7 +111,7 @@ async def get_shout(_, info, slug: str):
|
|||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
||||
"last_comment": last_comment,
|
||||
"last_reacted_at": last_reaction_at,
|
||||
}
|
||||
|
||||
for author_caption in (
|
||||
|
@ -164,7 +164,7 @@ async def load_shouts_by(_, _info, options):
|
|||
}
|
||||
offset: 0
|
||||
limit: 50
|
||||
order_by: "likes" | "followers" | "comments" | "last_comment"
|
||||
order_by: "likes" | "followers" | "comments" | "last_reacted_at"
|
||||
order_by_desc: true
|
||||
|
||||
}
|
||||
|
@ -188,7 +188,7 @@ async def load_shouts_by(_, _info, options):
|
|||
# order
|
||||
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
||||
order_str = options.get("order_by")
|
||||
if order_str in ["likes", "followers", "comments", "last_comment"]:
|
||||
if order_str in ["likes", "followers", "comments", "last_reacted_at"]:
|
||||
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)
|
||||
q = q.order_by(nulls_last(query_order_by))
|
||||
|
@ -206,7 +206,7 @@ async def load_shouts_by(_, _info, options):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reacted_at,
|
||||
] in session.execute(q).unique():
|
||||
main_topic = (
|
||||
session.query(Topic.slug)
|
||||
|
@ -228,7 +228,7 @@ async def load_shouts_by(_, _info, options):
|
|||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"rating": int(likes_stat) - int(dislikes_stat),
|
||||
"last_comment": last_comment,
|
||||
"last_reacted_at": last_reacted_at,
|
||||
}
|
||||
shouts.append(shout)
|
||||
|
||||
|
@ -270,7 +270,7 @@ async def load_shouts_feed(_, info, options):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reacted_at,
|
||||
] in session.execute(q).unique():
|
||||
main_topic = (
|
||||
session.query(Topic.slug)
|
||||
|
@ -292,7 +292,7 @@ async def load_shouts_feed(_, info, options):
|
|||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"rating": likes_stat - dislikes_stat,
|
||||
"last_comment": last_comment,
|
||||
"last_reacted_at": last_reacted_at,
|
||||
}
|
||||
shouts.append(shout)
|
||||
|
||||
|
@ -365,7 +365,7 @@ async def get_shouts_from_query(q, author_id=None):
|
|||
commented_stat,
|
||||
likes_stat,
|
||||
dislikes_stat,
|
||||
last_comment,
|
||||
last_reacted_at,
|
||||
] in session.execute(q, {"author_id": author_id}).unique():
|
||||
shouts.append(shout)
|
||||
shout.stat = {
|
||||
|
@ -373,7 +373,7 @@ async def get_shouts_from_query(q, author_id=None):
|
|||
"reacted": reacted_stat,
|
||||
"commented": commented_stat,
|
||||
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
|
||||
"last_comment": last_comment,
|
||||
"last_reacted_at": last_reacted_at,
|
||||
}
|
||||
|
||||
return shouts
|
||||
|
|
|
@ -97,7 +97,7 @@ type Stat {
|
|||
rating: Int
|
||||
commented: Int
|
||||
ranking: Int
|
||||
last_comment: Int
|
||||
last_reacted_at: Int
|
||||
}
|
||||
|
||||
type Community {
|
||||
|
|
|
@ -24,7 +24,7 @@ engine = create_engine(
|
|||
max_overflow=20,
|
||||
pool_timeout=30, # Время ожидания свободного соединения
|
||||
pool_recycle=1800, # Время жизни соединения
|
||||
connect_args={"sslmode": "disable"}
|
||||
connect_args={"sslmode": "disable"},
|
||||
)
|
||||
inspector = inspect(engine)
|
||||
configure_mappers()
|
||||
|
|
Loading…
Reference in New Issue
Block a user