fix cooments subscription

This commit is contained in:
knst-kotov 2022-06-29 13:47:53 +03:00
parent 28e1f507bd
commit 34aeb95ef3
5 changed files with 23 additions and 58 deletions

View File

@ -18,6 +18,8 @@ class ShoutCommentsSubscription(Base):
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
shout = Column(ForeignKey('shout.slug'), primary_key = True)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
auto = Column(Boolean, nullable=False, default = False)
deletedAt: str = Column(DateTime, nullable=True)
class ShoutAuthor(Base):
__tablename__ = "shout_author"

View File

@ -6,40 +6,7 @@ from auth.authenticate import login_required
import asyncio
from datetime import datetime
class CommentResult:
def __init__(self, status, comment):
self.status = status
self.comment = comment
class ShoutCommentsSubscription:
queue = asyncio.Queue()
def __init__(self, shout_slug):
self.shout_slug = shout_slug
class ShoutCommentsStorage:
lock = asyncio.Lock()
subscriptions = []
@staticmethod
async def register_subscription(subs):
self = ShoutCommentsStorage
async with self.lock:
self.subscriptions.append(subs)
@staticmethod
async def del_subscription(subs):
self = ShoutCommentsStorage
async with self.lock:
self.subscriptions.remove(subs)
@staticmethod
async def put(comment_result):
self = ShoutCommentsStorage
async with self.lock:
for subs in self.subscriptions:
if comment_result.comment.shout == subs.shout_slug:
subs.queue.put_nowait(comment_result)
from sqlalchemy import and_
def comments_subscribe(user, slug):
ShoutCommentsSubscription.create(
@ -53,7 +20,10 @@ def comments_unsubscribe(user, slug):
first()
if not sub:
raise Exception("subscription not exist")
session.delete(sub)
if sub.auto:
sub.deletedAt = datetime.now()
else:
session.delete(sub)
session.commit()
@mutation.field("createComment")
@ -69,9 +39,6 @@ async def create_comment(_, info, body, shout, replyTo = None):
replyTo = replyTo
)
result = CommentResult("NEW", comment)
await ShoutCommentsStorage.put(result)
return {"comment": comment}
@mutation.field("updateComment")
@ -92,9 +59,6 @@ async def update_comment(_, info, id, body):
session.commit()
result = CommentResult("UPDATED", comment)
await ShoutCommentsStorage.put(result)
return {"comment": comment}
@mutation.field("deleteComment")
@ -113,9 +77,6 @@ async def delete_comment(_, info, id):
comment.deletedAt = datetime.now()
session.commit()
result = CommentResult("DELETED", comment)
await ShoutCommentsStorage.put(result)
return {}
@mutation.field("rateComment")
@ -141,7 +102,4 @@ async def rate_comment(_, info, id, value):
createdBy = user_id,
value = value)
result = CommentResult("UPDATED_RATING", comment)
await ShoutCommentsStorage.put(result)
return {}

View File

@ -4,6 +4,7 @@ from orm.comment import Comment
from orm.base import local_session
from orm.topic import Topic, TopicSubscription
from resolvers.base import mutation, query, subscription
from resolvers.community import get_subscribed_communities
from auth.authenticate import login_required
from inbox_resolvers.inbox import get_total_unread_messages_for_user

View File

@ -7,6 +7,9 @@ from orm.user import UserStorage, AuthorSubscription
from orm.topic import TopicSubscription
from resolvers.base import mutation, query
from resolvers.profile import author_subscribe, author_unsubscribe
from resolvers.topics import topic_subscribe, topic_unsubscribe
from resolvers.community import community_subscribe, community_unsubscribe
from resolvers.comments import comments_subscribe, comments_unsubscribe
from auth.authenticate import login_required
from settings import SHOUTS_REPO
@ -315,17 +318,17 @@ async def shouts_by_communities(_, info, slugs, page, size):
@mutation.field("subscribe")
@login_required
async def subscribe(_, info, subscription, slug):
async def subscribe(_, info, what, slug):
user = info.context["request"].user
try:
if subscription == "AUTHOR":
if what == "AUTHOR":
author_subscribe(user, slug)
elif subscription == "TOPIC":
elif what == "TOPIC":
topic_subscribe(user, slug)
elif subscription == "COMMUNITY":
elif what == "COMMUNITY":
community_subscribe(user, slug)
elif comments_subscription == "COMMENTS":
elif what == "COMMENTS":
comments_subscribe(user, slug)
except Exception as e:
return {"error" : e}
@ -334,17 +337,17 @@ async def subscribe(_, info, subscription, slug):
@mutation.field("unsubscribe")
@login_required
async def unsubscribe(_, info, subscription, slug):
async def unsubscribe(_, info, what, slug):
user = info.context["request"].user
try:
if subscription == "AUTHOR":
if what == "AUTHOR":
author_unsubscribe(user, slug)
elif subscription == "TOPIC":
elif what == "TOPIC":
topic_unsubscribe(user, slug)
elif subscription == "COMMUNITY":
elif what == "COMMUNITY":
community_unsubscribe(user, slug)
elif subscription == "COMMENTS":
elif what == "COMMENTS":
comments_unsubscribe(user, slug)
except Exception as e:
return {"error" : e}

View File

@ -98,6 +98,7 @@ enum SubscriptionType {
TOPIC
AUTHOR
COMMUNITY
COMMENTS
}
################################### Mutation