reaction-fix

This commit is contained in:
Untone 2023-11-29 14:24:59 +03:00
parent cd955ecf8a
commit 3454766063
3 changed files with 18 additions and 12 deletions

View File

@ -146,8 +146,8 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
if publish: if publish:
if shout.visibility is ShoutVisibility.AUTHORS: if shout.visibility is ShoutVisibility.AUTHORS:
shout_dict = shout.dict() shout_dict = shout.dict()
shout_dict['visibility'] = ShoutVisibility.COMMUNITY shout_dict["visibility"] = ShoutVisibility.COMMUNITY
shout_dict['published_at'] = current_time # Set published_at as Unix timestamp shout_dict["published_at"] = current_time # Set published_at as Unix timestamp
Shout.update(shout, shout_dict) Shout.update(shout, shout_dict)
session.add(shout) session.add(shout)
await notify_shout(shout.dict(), "public") await notify_shout(shout.dict(), "public")
@ -175,7 +175,7 @@ async def delete_shout(_, info, shout_id):
# Replace datetime with Unix timestamp # Replace datetime with Unix timestamp
current_time = int(time.time()) current_time = int(time.time())
shout_dict = shout.dict() shout_dict = shout.dict()
shout_dict['deleted_at'] = current_time # Set deleted_at as Unix timestamp shout_dict["deleted_at"] = current_time # Set deleted_at as Unix timestamp
Shout.update(shout, shout_dict) Shout.update(shout, shout_dict)
session.add(shout) session.add(shout)
session.commit() session.commit()

View File

@ -17,7 +17,7 @@ def add_reaction_stat_columns(q):
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns( q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns(
func.sum(aliased_reaction.id).label("reacted_stat"), func.sum(aliased_reaction.id).label("reacted_stat"),
func.sum(case((aliased_reaction.body != "", 1), else_=0)).label("commented_stat"), func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT, 1), else_=0)).label("commented_stat"),
func.sum( func.sum(
case( case(
(aliased_reaction.kind == ReactionKind.AGREE, 1), (aliased_reaction.kind == ReactionKind.AGREE, 1),
@ -91,7 +91,7 @@ def is_published_author(session, author_id):
return ( return (
session.query(Shout) session.query(Shout)
.where(Shout.authors.contains(author_id)) .where(Shout.authors.contains(author_id))
.filter(and_(Shout.published_at !="", Shout.deleted_at.is_(None))) .filter(and_(Shout.published_at != "", Shout.deleted_at.is_(None)))
.count() .count()
> 0 > 0
) )
@ -216,7 +216,7 @@ async def create_reaction(_, info, reaction):
end = int(end) end = int(end)
new_body = old_body[:start] + replied_reaction.body + old_body[end:] new_body = old_body[:start] + replied_reaction.body + old_body[end:]
shout_dict = shout.dict() shout_dict = shout.dict()
shout_dict['body'] = new_body shout_dict["body"] = new_body
Shout.update(shout, shout_dict) Shout.update(shout, shout_dict)
session.add(r) session.add(r)
@ -339,7 +339,7 @@ def apply_reaction_filters(by, q):
# NOTE: not using ElasticSearch here # NOTE: not using ElasticSearch here
by_search = by.get("search", "") by_search = by.get("search", "")
if len(by_search) > 2: if len(by_search) > 2:
q = q.filter(Reaction.body.ilike(f'%{by_search}%')) q = q.filter(Reaction.body.ilike(f"%{by_search}%"))
if by.get("after"): if by.get("after"):
after = int(by["after"]) after = int(by["after"])
@ -440,7 +440,7 @@ async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
if author: if author:
author_id: int = author.dict()['id'] author_id: int = author.dict()["id"]
shouts = reacted_shouts_updates(author_id, limit, offset) shouts = reacted_shouts_updates(author_id, limit, offset)
return shouts return shouts
else: else:

View File

@ -51,9 +51,11 @@ def apply_filters(q, filters, author_id=None):
by_visibility = filters.get("visibility") by_visibility = filters.get("visibility")
if by_visibility: if by_visibility:
visibility = { visibility = {
'public': [ShoutVisibility.PUBLIC,], "public": [
'community': [ShoutVisibility.PUBLIC, ShoutVisibility.COMMUNITY], ShoutVisibility.PUBLIC,
'authors': [ShoutVisibility.PUBLIC, ShoutVisibility.COMMUNITY, ShoutVisibility.AUTHORS] ],
"community": [ShoutVisibility.PUBLIC, ShoutVisibility.COMMUNITY],
"authors": [ShoutVisibility.PUBLIC, ShoutVisibility.COMMUNITY, ShoutVisibility.AUTHORS],
} }
q = q.filter(Shout.visibility.in_(visibility.get(by_visibility) or [])) q = q.filter(Shout.visibility.in_(visibility.get(by_visibility) or []))
by_layouts = filters.get("layouts") by_layouts = filters.get("layouts")
@ -114,7 +116,11 @@ async def get_shout(_, _info, slug=None, shout_id=None):
if author.id == author_caption.author: if author.id == author_caption.author:
author.caption = author_caption.caption author.caption = author_caption.caption
return shout return shout
except Exception: except Exception as e:
import traceback
traceback.print_exc()
print(e)
return None return None