From dc3f7fe6a55b52290b323a2920353963fee2ea37 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Wed, 29 Jun 2022 15:22:14 +0300 Subject: [PATCH] autosubscribe when comment created --- resolvers/comments.py | 35 +++++++++++++++++++++++++++-------- resolvers/zine.py | 4 ++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/resolvers/comments.py b/resolvers/comments.py index 0de99dcf..dc426a01 100644 --- a/resolvers/comments.py +++ b/resolvers/comments.py @@ -6,15 +6,30 @@ from auth.authenticate import login_required import asyncio from datetime import datetime -def comments_subscribe(user, slug): +def comments_subscribe(user, slug, auto = False): + with local_session() as session: + sub = session.query(ShoutCommentsSubscription).\ + filter(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug).\ + first() + if auto and sub: + return + elif not auto and sub: + if not sub.deletedAt is None: + sub.deletedAt = None + sub.auto = False + session.commit() + return + raise Exception("subscription already exist") + ShoutCommentsSubscription.create( subscriber = user.slug, - shout = slug) + shout = slug, + auto = auto) def comments_unsubscribe(user, slug): with local_session() as session: sub = session.query(ShoutCommentsSubscription).\ - filter(and_(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug)).\ + filter(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug).\ first() if not sub: raise Exception("subscription not exist") @@ -27,16 +42,20 @@ def comments_unsubscribe(user, slug): @mutation.field("createComment") @login_required async def create_comment(_, info, body, shout, replyTo = None): - auth = info.context["request"].auth - user_id = auth.user_id + user = info.context["request"].user comment = Comment.create( - author = user_id, + author = user.id, body = body, shout = shout, replyTo = replyTo ) + try: + comments_subscribe(user, shout, True) + except Exception as e: + print(f"error on comment autosubscribe: {e}") + return {"comment": comment} @mutation.field("updateComment") @@ -89,7 +108,7 @@ async def rate_comment(_, info, id, value): return {"error": "invalid comment id"} rating = session.query(CommentRating).\ - filter(CommentRating.comment_id == id and CommentRating.createdBy == user_id).first() + filter(CommentRating.comment_id == id, CommentRating.createdBy == user_id).first() if rating: rating.value = value session.commit() @@ -105,7 +124,7 @@ async def rate_comment(_, info, id, value): def get_subscribed_shout_comments(slug): with local_session() as session: rows = session.query(ShoutCommentsSubscription.shout).\ - filter(ShoutCommentsSubscription.subscriber == slug and not ShoutCommentsSubscription.deletedAt is None).\ + filter(ShoutCommentsSubscription.subscriber == slug, ShoutCommentsSubscription.deletedAt == None).\ all() slugs = [row.shout for row in rows] return slugs diff --git a/resolvers/zine.py b/resolvers/zine.py index 838036f0..17180453 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -331,7 +331,7 @@ async def subscribe(_, info, what, slug): elif what == "COMMENTS": comments_subscribe(user, slug) except Exception as e: - return {"error" : e} + return {"error" : str(e)} return {} @@ -350,7 +350,7 @@ async def unsubscribe(_, info, what, slug): elif what == "COMMENTS": comments_unsubscribe(user, slug) except Exception as e: - return {"error" : e} + return {"error" : str(e)} return {}