2021-11-21 11:04:38 +00:00
|
|
|
from orm import Comment, CommentRating
|
|
|
|
from orm.base import local_session
|
|
|
|
from resolvers.base import mutation, query, subscription
|
|
|
|
from auth.authenticate import login_required
|
|
|
|
import asyncio
|
|
|
|
from datetime import datetime
|
|
|
|
|
2022-01-28 09:49:46 +00:00
|
|
|
class CommentResult:
|
|
|
|
def __init__(self, status, comment):
|
|
|
|
self.status = status
|
|
|
|
self.comment = comment
|
|
|
|
|
2022-06-21 12:21:02 +00:00
|
|
|
class ShoutCommentsSubscription:
|
2022-01-28 09:49:46 +00:00
|
|
|
queue = asyncio.Queue()
|
|
|
|
|
|
|
|
def __init__(self, shout_slug):
|
|
|
|
self.shout_slug = shout_slug
|
|
|
|
|
2022-06-21 12:21:02 +00:00
|
|
|
class ShoutCommentsStorage:
|
2022-01-28 09:49:46 +00:00
|
|
|
lock = asyncio.Lock()
|
|
|
|
subscriptions = []
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def register_subscription(subs):
|
2022-06-21 12:21:02 +00:00
|
|
|
self = ShoutCommentsStorage
|
2022-01-28 09:49:46 +00:00
|
|
|
async with self.lock:
|
|
|
|
self.subscriptions.append(subs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def del_subscription(subs):
|
2022-06-21 12:21:02 +00:00
|
|
|
self = ShoutCommentsStorage
|
2022-01-28 09:49:46 +00:00
|
|
|
async with self.lock:
|
|
|
|
self.subscriptions.remove(subs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def put(comment_result):
|
2022-06-21 12:21:02 +00:00
|
|
|
self = ShoutCommentsStorage
|
2022-01-28 09:49:46 +00:00
|
|
|
async with self.lock:
|
|
|
|
for subs in self.subscriptions:
|
|
|
|
if comment_result.comment.shout == subs.shout_slug:
|
|
|
|
subs.queue.put_nowait(comment_result)
|
|
|
|
|
2021-11-21 11:04:38 +00:00
|
|
|
@mutation.field("createComment")
|
|
|
|
@login_required
|
|
|
|
async def create_comment(_, info, body, shout, replyTo = None):
|
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
|
|
|
comment = Comment.create(
|
|
|
|
author = user_id,
|
|
|
|
body = body,
|
|
|
|
shout = shout,
|
|
|
|
replyTo = replyTo
|
|
|
|
)
|
|
|
|
|
2022-01-28 09:49:46 +00:00
|
|
|
result = CommentResult("NEW", comment)
|
2022-06-21 12:21:02 +00:00
|
|
|
await ShoutCommentsStorage.put(result)
|
2022-01-28 09:49:46 +00:00
|
|
|
|
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"}
|
|
|
|
if comment.author != user_id:
|
|
|
|
return {"error": "access denied"}
|
|
|
|
|
|
|
|
comment.body = body
|
|
|
|
comment.updatedAt = datetime.now()
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
2022-01-28 09:49:46 +00:00
|
|
|
result = CommentResult("UPDATED", comment)
|
2022-06-21 12:21:02 +00:00
|
|
|
await ShoutCommentsStorage.put(result)
|
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"}
|
|
|
|
if comment.author != user_id:
|
|
|
|
return {"error": "access denied"}
|
|
|
|
|
|
|
|
comment.deletedAt = datetime.now()
|
|
|
|
session.commit()
|
|
|
|
|
2022-01-28 09:49:46 +00:00
|
|
|
result = CommentResult("DELETED", comment)
|
2022-06-21 12:21:02 +00:00
|
|
|
await ShoutCommentsStorage.put(result)
|
2022-01-28 09:49:46 +00:00
|
|
|
|
2021-11-21 11:04:38 +00:00
|
|
|
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-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).\
|
|
|
|
filter(CommentRating.comment_id == id and CommentRating.createdBy == user_id).first()
|
|
|
|
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)
|
|
|
|
|
|
|
|
result = CommentResult("UPDATED_RATING", comment)
|
2022-06-21 12:21:02 +00:00
|
|
|
await ShoutCommentsStorage.put(result)
|
2022-01-28 09:49:46 +00:00
|
|
|
|
2021-11-24 06:23:48 +00:00
|
|
|
return {}
|