autosubscribe when comment created
This commit is contained in:
parent
3f7d9c527b
commit
dc3f7fe6a5
|
@ -6,15 +6,30 @@ from auth.authenticate import login_required
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import datetime
|
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(
|
ShoutCommentsSubscription.create(
|
||||||
subscriber = user.slug,
|
subscriber = user.slug,
|
||||||
shout = slug)
|
shout = slug,
|
||||||
|
auto = auto)
|
||||||
|
|
||||||
def comments_unsubscribe(user, slug):
|
def comments_unsubscribe(user, slug):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
sub = session.query(ShoutCommentsSubscription).\
|
sub = session.query(ShoutCommentsSubscription).\
|
||||||
filter(and_(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug)).\
|
filter(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug).\
|
||||||
first()
|
first()
|
||||||
if not sub:
|
if not sub:
|
||||||
raise Exception("subscription not exist")
|
raise Exception("subscription not exist")
|
||||||
|
@ -27,16 +42,20 @@ def comments_unsubscribe(user, slug):
|
||||||
@mutation.field("createComment")
|
@mutation.field("createComment")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_comment(_, info, body, shout, replyTo = None):
|
async def create_comment(_, info, body, shout, replyTo = None):
|
||||||
auth = info.context["request"].auth
|
user = info.context["request"].user
|
||||||
user_id = auth.user_id
|
|
||||||
|
|
||||||
comment = Comment.create(
|
comment = Comment.create(
|
||||||
author = user_id,
|
author = user.id,
|
||||||
body = body,
|
body = body,
|
||||||
shout = shout,
|
shout = shout,
|
||||||
replyTo = replyTo
|
replyTo = replyTo
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
comments_subscribe(user, shout, True)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"error on comment autosubscribe: {e}")
|
||||||
|
|
||||||
return {"comment": comment}
|
return {"comment": comment}
|
||||||
|
|
||||||
@mutation.field("updateComment")
|
@mutation.field("updateComment")
|
||||||
|
@ -89,7 +108,7 @@ async def rate_comment(_, info, id, value):
|
||||||
return {"error": "invalid comment id"}
|
return {"error": "invalid comment id"}
|
||||||
|
|
||||||
rating = session.query(CommentRating).\
|
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:
|
if rating:
|
||||||
rating.value = value
|
rating.value = value
|
||||||
session.commit()
|
session.commit()
|
||||||
|
@ -105,7 +124,7 @@ async def rate_comment(_, info, id, value):
|
||||||
def get_subscribed_shout_comments(slug):
|
def get_subscribed_shout_comments(slug):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
rows = session.query(ShoutCommentsSubscription.shout).\
|
rows = session.query(ShoutCommentsSubscription.shout).\
|
||||||
filter(ShoutCommentsSubscription.subscriber == slug and not ShoutCommentsSubscription.deletedAt is None).\
|
filter(ShoutCommentsSubscription.subscriber == slug, ShoutCommentsSubscription.deletedAt == None).\
|
||||||
all()
|
all()
|
||||||
slugs = [row.shout for row in rows]
|
slugs = [row.shout for row in rows]
|
||||||
return slugs
|
return slugs
|
||||||
|
|
|
@ -331,7 +331,7 @@ async def subscribe(_, info, what, slug):
|
||||||
elif what == "COMMENTS":
|
elif what == "COMMENTS":
|
||||||
comments_subscribe(user, slug)
|
comments_subscribe(user, slug)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error" : e}
|
return {"error" : str(e)}
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@ -350,7 +350,7 @@ async def unsubscribe(_, info, what, slug):
|
||||||
elif what == "COMMENTS":
|
elif what == "COMMENTS":
|
||||||
comments_unsubscribe(user, slug)
|
comments_unsubscribe(user, slug)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error" : e}
|
return {"error" : str(e)}
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user