core/resolvers/comments.py

132 lines
3.5 KiB
Python
Raw Normal View History

2021-11-21 11:04:38 +00:00
from orm import Comment, CommentRating
from orm.base import local_session
2022-06-21 12:21:02 +00:00
from orm.shout import ShoutCommentsSubscription
2022-07-07 13:55:13 +00:00
from orm.user import User
2022-07-10 18:52:57 +00:00
from resolvers.base import mutation
2021-11-21 11:04:38 +00:00
from auth.authenticate import login_required
from datetime import datetime
2022-06-29 12:22:14 +00:00
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")
2022-06-21 12:21:02 +00:00
ShoutCommentsSubscription.create(
subscriber = user.slug,
2022-06-29 12:22:14 +00:00
shout = slug,
auto = auto)
2022-06-21 12:21:02 +00:00
def comments_unsubscribe(user, slug):
with local_session() as session:
sub = session.query(ShoutCommentsSubscription).\
2022-06-29 12:22:14 +00:00
filter(ShoutCommentsSubscription.subscriber == user.slug, ShoutCommentsSubscription.shout == slug).\
2022-06-21 12:21:02 +00:00
first()
if not sub:
raise Exception("subscription not exist")
2022-06-29 10:47:53 +00:00
if sub.auto:
sub.deletedAt = datetime.now()
else:
session.delete(sub)
2022-06-21 12:21:02 +00:00
session.commit()
2021-11-21 11:04:38 +00:00
@mutation.field("createComment")
@login_required
async def create_comment(_, info, body, shout, replyTo = None):
2022-06-29 12:22:14 +00:00
user = info.context["request"].user
2021-11-21 11:04:38 +00:00
comment = Comment.create(
2022-07-10 18:52:57 +00:00
createdBy = user.slug,
2021-11-21 11:04:38 +00:00
body = body,
shout = shout,
replyTo = replyTo
)
2022-06-29 12:22:14 +00:00
try:
comments_subscribe(user, shout, True)
except Exception as e:
print(f"error on comment autosubscribe: {e}")
2021-11-21 11:04:38 +00:00
return {"comment": comment}
@mutation.field("updateComment")
@login_required
async def update_comment(_, info, id, body):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
comment = session.query(Comment).filter(Comment.id == id).first()
if not comment:
return {"error": "invalid comment id"}
2022-07-07 13:55:13 +00:00
if comment.createdBy != user_id:
2021-11-21 11:04:38 +00:00
return {"error": "access denied"}
comment.body = body
comment.updatedAt = datetime.now()
session.commit()
2022-01-28 09:49:46 +00:00
return {"comment": comment}
2021-11-21 11:04:38 +00:00
@mutation.field("deleteComment")
@login_required
async def delete_comment(_, info, id):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
comment = session.query(Comment).filter(Comment.id == id).first()
if not comment:
return {"error": "invalid comment id"}
2022-07-07 13:55:13 +00:00
if comment.createdBy != user_id:
2021-11-21 11:04:38 +00:00
return {"error": "access denied"}
comment.deletedAt = datetime.now()
session.commit()
return {}
2021-11-24 06:23:48 +00:00
@mutation.field("rateComment")
@login_required
async def rate_comment(_, info, id, value):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
2022-07-07 13:55:13 +00:00
user = session.query(User).filter(User.id == user_id).first()
2022-01-28 09:49:46 +00:00
comment = session.query(Comment).filter(Comment.id == id).first()
if not comment:
return {"error": "invalid comment id"}
2021-11-24 06:23:48 +00:00
rating = session.query(CommentRating).\
2022-07-07 13:55:13 +00:00
filter(CommentRating.comment_id == id, CommentRating.createdBy == user.slug).first()
2021-11-24 06:23:48 +00:00
if rating:
rating.value = value
session.commit()
2022-01-28 09:49:46 +00:00
if not rating:
CommentRating.create(
comment_id = id,
createdBy = user_id,
value = value)
2021-11-24 06:23:48 +00:00
return {}
def get_subscribed_shout_comments(slug):
with local_session() as session:
rows = session.query(ShoutCommentsSubscription.shout).\
2022-07-07 13:55:13 +00:00
filter(ShoutCommentsSubscription.subscriber == slug,\
ShoutCommentsSubscription.deletedAt == None).\
all()
slugs = [row.shout for row in rows]
return slugs