diff --git a/orm/shout.py b/orm/shout.py index de6f1d6b..6b108bf3 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -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" diff --git a/resolvers/comments.py b/resolvers/comments.py index 29a4163c..3e58c7d3 100644 --- a/resolvers/comments.py +++ b/resolvers/comments.py @@ -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 {} diff --git a/resolvers/profile.py b/resolvers/profile.py index c22c3b1f..7e603495 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -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 diff --git a/resolvers/zine.py b/resolvers/zine.py index 357e1910..838036f0 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -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} @@ -374,4 +377,4 @@ async def rate_shout(_, info, slug, value): await ShoutRatingStorage.update_rating(rating) - return {"error" : ""} \ No newline at end of file + return {"error" : ""} diff --git a/schema.graphql b/schema.graphql index a5b77615..d1459826 100644 --- a/schema.graphql +++ b/schema.graphql @@ -98,6 +98,7 @@ enum SubscriptionType { TOPIC AUTHOR COMMUNITY + COMMENTS } ################################### Mutation