autosubscribe when comment created

This commit is contained in:
knst-kotov 2022-06-29 15:22:14 +03:00
parent 3f7d9c527b
commit dc3f7fe6a5
2 changed files with 29 additions and 10 deletions

View File

@ -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

View File

@ -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 {}