fix cooments subscription
This commit is contained in:
parent
28e1f507bd
commit
34aeb95ef3
|
@ -18,6 +18,8 @@ class ShoutCommentsSubscription(Base):
|
||||||
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
||||||
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
shout = Column(ForeignKey('shout.slug'), primary_key = True)
|
||||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
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):
|
class ShoutAuthor(Base):
|
||||||
__tablename__ = "shout_author"
|
__tablename__ = "shout_author"
|
||||||
|
|
|
@ -6,40 +6,7 @@ from auth.authenticate import login_required
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
class CommentResult:
|
from sqlalchemy import and_
|
||||||
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)
|
|
||||||
|
|
||||||
def comments_subscribe(user, slug):
|
def comments_subscribe(user, slug):
|
||||||
ShoutCommentsSubscription.create(
|
ShoutCommentsSubscription.create(
|
||||||
|
@ -53,7 +20,10 @@ def comments_unsubscribe(user, slug):
|
||||||
first()
|
first()
|
||||||
if not sub:
|
if not sub:
|
||||||
raise Exception("subscription not exist")
|
raise Exception("subscription not exist")
|
||||||
session.delete(sub)
|
if sub.auto:
|
||||||
|
sub.deletedAt = datetime.now()
|
||||||
|
else:
|
||||||
|
session.delete(sub)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
@mutation.field("createComment")
|
@mutation.field("createComment")
|
||||||
|
@ -69,9 +39,6 @@ async def create_comment(_, info, body, shout, replyTo = None):
|
||||||
replyTo = replyTo
|
replyTo = replyTo
|
||||||
)
|
)
|
||||||
|
|
||||||
result = CommentResult("NEW", comment)
|
|
||||||
await ShoutCommentsStorage.put(result)
|
|
||||||
|
|
||||||
return {"comment": comment}
|
return {"comment": comment}
|
||||||
|
|
||||||
@mutation.field("updateComment")
|
@mutation.field("updateComment")
|
||||||
|
@ -92,9 +59,6 @@ async def update_comment(_, info, id, body):
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
result = CommentResult("UPDATED", comment)
|
|
||||||
await ShoutCommentsStorage.put(result)
|
|
||||||
|
|
||||||
return {"comment": comment}
|
return {"comment": comment}
|
||||||
|
|
||||||
@mutation.field("deleteComment")
|
@mutation.field("deleteComment")
|
||||||
|
@ -113,9 +77,6 @@ async def delete_comment(_, info, id):
|
||||||
comment.deletedAt = datetime.now()
|
comment.deletedAt = datetime.now()
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
result = CommentResult("DELETED", comment)
|
|
||||||
await ShoutCommentsStorage.put(result)
|
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@mutation.field("rateComment")
|
@mutation.field("rateComment")
|
||||||
|
@ -141,7 +102,4 @@ async def rate_comment(_, info, id, value):
|
||||||
createdBy = user_id,
|
createdBy = user_id,
|
||||||
value = value)
|
value = value)
|
||||||
|
|
||||||
result = CommentResult("UPDATED_RATING", comment)
|
|
||||||
await ShoutCommentsStorage.put(result)
|
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
|
@ -4,6 +4,7 @@ from orm.comment import Comment
|
||||||
from orm.base import local_session
|
from orm.base import local_session
|
||||||
from orm.topic import Topic, TopicSubscription
|
from orm.topic import Topic, TopicSubscription
|
||||||
from resolvers.base import mutation, query, subscription
|
from resolvers.base import mutation, query, subscription
|
||||||
|
from resolvers.community import get_subscribed_communities
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
|
|
||||||
from inbox_resolvers.inbox import get_total_unread_messages_for_user
|
from inbox_resolvers.inbox import get_total_unread_messages_for_user
|
||||||
|
|
|
@ -7,6 +7,9 @@ from orm.user import UserStorage, AuthorSubscription
|
||||||
from orm.topic import TopicSubscription
|
from orm.topic import TopicSubscription
|
||||||
|
|
||||||
from resolvers.base import mutation, query
|
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 resolvers.comments import comments_subscribe, comments_unsubscribe
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from settings import SHOUTS_REPO
|
from settings import SHOUTS_REPO
|
||||||
|
@ -315,17 +318,17 @@ async def shouts_by_communities(_, info, slugs, page, size):
|
||||||
|
|
||||||
@mutation.field("subscribe")
|
@mutation.field("subscribe")
|
||||||
@login_required
|
@login_required
|
||||||
async def subscribe(_, info, subscription, slug):
|
async def subscribe(_, info, what, slug):
|
||||||
user = info.context["request"].user
|
user = info.context["request"].user
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if subscription == "AUTHOR":
|
if what == "AUTHOR":
|
||||||
author_subscribe(user, slug)
|
author_subscribe(user, slug)
|
||||||
elif subscription == "TOPIC":
|
elif what == "TOPIC":
|
||||||
topic_subscribe(user, slug)
|
topic_subscribe(user, slug)
|
||||||
elif subscription == "COMMUNITY":
|
elif what == "COMMUNITY":
|
||||||
community_subscribe(user, slug)
|
community_subscribe(user, slug)
|
||||||
elif comments_subscription == "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" : e}
|
||||||
|
@ -334,17 +337,17 @@ async def subscribe(_, info, subscription, slug):
|
||||||
|
|
||||||
@mutation.field("unsubscribe")
|
@mutation.field("unsubscribe")
|
||||||
@login_required
|
@login_required
|
||||||
async def unsubscribe(_, info, subscription, slug):
|
async def unsubscribe(_, info, what, slug):
|
||||||
user = info.context["request"].user
|
user = info.context["request"].user
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if subscription == "AUTHOR":
|
if what == "AUTHOR":
|
||||||
author_unsubscribe(user, slug)
|
author_unsubscribe(user, slug)
|
||||||
elif subscription == "TOPIC":
|
elif what == "TOPIC":
|
||||||
topic_unsubscribe(user, slug)
|
topic_unsubscribe(user, slug)
|
||||||
elif subscription == "COMMUNITY":
|
elif what == "COMMUNITY":
|
||||||
community_unsubscribe(user, slug)
|
community_unsubscribe(user, slug)
|
||||||
elif subscription == "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" : e}
|
||||||
|
|
|
@ -98,6 +98,7 @@ enum SubscriptionType {
|
||||||
TOPIC
|
TOPIC
|
||||||
AUTHOR
|
AUTHOR
|
||||||
COMMUNITY
|
COMMUNITY
|
||||||
|
COMMENTS
|
||||||
}
|
}
|
||||||
|
|
||||||
################################### Mutation
|
################################### Mutation
|
||||||
|
|
Loading…
Reference in New Issue
Block a user