From 80030f21b79ae6f8d4b2ce737c45aecc933743a9 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Mon, 20 Feb 2023 20:38:20 +0300 Subject: [PATCH] following manager works --- resolvers/inbox/messages.py | 6 ++-- resolvers/zine/following.py | 51 +++++++++++++++--------------- resolvers/zine/profile.py | 23 +++++++------- resolvers/zine/reactions.py | 63 +++++++++++++++++++++---------------- resolvers/zine/topics.py | 45 +++++++++++++++----------- services/following.py | 21 +++++++------ 6 files changed, 114 insertions(+), 95 deletions(-) diff --git a/resolvers/inbox/messages.py b/resolvers/inbox/messages.py index d33af9c3..44ff1f03 100644 --- a/resolvers/inbox/messages.py +++ b/resolvers/inbox/messages.py @@ -51,7 +51,7 @@ async def create_message(_, info, chat: str, body: str, replyTo=None): ) result = FollowingResult("NEW", 'chat', new_message) - await FollowingManager.put('chat', result) + await FollowingManager.push('chat', result) return { "message": new_message, @@ -82,7 +82,7 @@ async def update_message(_, info, chat_id: str, message_id: int, body: str): await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message)) result = FollowingResult("UPDATED", 'chat', message) - await FollowingManager.put('chat', result) + await FollowingManager.push('chat', result) return { "message": message, @@ -115,7 +115,7 @@ async def delete_message(_, info, chat_id: str, message_id: int): await redis.execute("LREM", f"chats/{chat_id}/unread/{user_id}", 0, str(message_id)) result = FollowingResult("DELETED", 'chat', message) - await FollowingManager.put(result) + await FollowingManager.push(result) return {} diff --git a/resolvers/zine/following.py b/resolvers/zine/following.py index ba3f64b0..b2e039f1 100644 --- a/resolvers/zine/following.py +++ b/resolvers/zine/following.py @@ -21,23 +21,23 @@ async def follow(_, info, what, slug): try: if what == "AUTHOR": - author_follow(auth.user_id, slug) - result = FollowingResult("NEW", 'author', slug) - await FollowingManager.put('author', result) + if author_follow(auth.user_id, slug): + result = FollowingResult("NEW", 'author', slug) + await FollowingManager.push('author', result) elif what == "TOPIC": - topic_follow(auth.user_id, slug) - result = FollowingResult("NEW", 'topic', slug) - await FollowingManager.put('topic', result) + if topic_follow(auth.user_id, slug): + result = FollowingResult("NEW", 'topic', slug) + await FollowingManager.push('topic', result) elif what == "COMMUNITY": - # community_follow(user, slug) - # result = FollowingResult("NEW", 'community', slug) - # await FollowingManager.put('community', result) - pass + if False: # TODO: use community_follow(auth.user_id, slug): + result = FollowingResult("NEW", 'community', slug) + await FollowingManager.push('community', result) elif what == "REACTIONS": - reactions_follow(auth.user_id, slug) - result = FollowingResult("NEW", 'shout', slug) - await FollowingManager.put('shout', result) + if reactions_follow(auth.user_id, slug): + result = FollowingResult("NEW", 'shout', slug) + await FollowingManager.push('shout', result) except Exception as e: + print(Exception(e)) return {"error": str(e)} return {} @@ -50,22 +50,21 @@ async def unfollow(_, info, what, slug): try: if what == "AUTHOR": - author_unfollow(auth.user_id, slug) - result = FollowingResult("DELETED", 'author', slug) - await FollowingManager.put('author', result) + if author_unfollow(auth.user_id, slug): + result = FollowingResult("DELETED", 'author', slug) + await FollowingManager.push('author', result) elif what == "TOPIC": - topic_unfollow(auth.user_id, slug) - result = FollowingResult("DELETED", 'topic', slug) - await FollowingManager.put('topic', result) + if topic_unfollow(auth.user_id, slug): + result = FollowingResult("DELETED", 'topic', slug) + await FollowingManager.push('topic', result) elif what == "COMMUNITY": - # community_unfollow(user, slug) - # result = FollowingResult("DELETED", 'community', slug) - # await FollowingManager.put('community', result) - pass + if False: # TODO: use community_unfollow(auth.user_id, slug): + result = FollowingResult("DELETED", 'community', slug) + await FollowingManager.push('community', result) elif what == "REACTIONS": - reactions_unfollow(auth.user_id, slug) - result = FollowingResult("DELETED", 'shout', slug) - await FollowingManager.put('shout', result) + if reactions_unfollow(auth.user_id, slug): + result = FollowingResult("DELETED", 'shout', slug) + await FollowingManager.push('shout', result) except Exception as e: return {"error": str(e)} diff --git a/resolvers/zine/profile.py b/resolvers/zine/profile.py index a875fbf9..ddcafc33 100644 --- a/resolvers/zine/profile.py +++ b/resolvers/zine/profile.py @@ -198,11 +198,15 @@ async def rate_user(_, info, rated_userslug, value): # for mutation.field("follow") def author_follow(user_id, slug): - with local_session() as session: - author = session.query(User).where(User.slug == slug).one() - af = AuthorFollower.create(follower=user_id, author=author.id) - session.add(af) - session.commit() + try: + with local_session() as session: + author = session.query(User).where(User.slug == slug).one() + af = AuthorFollower.create(follower=user_id, author=author.id) + session.add(af) + session.commit() + return True + except: + return False # for mutation.field("unfollow") @@ -217,14 +221,11 @@ def author_unfollow(user_id, slug): ) ).first() ) - if not flw: - return { - "error": "Follower is not exist, cant unfollow" - } - else: + if flw: session.delete(flw) session.commit() - return {} + return True + return False @query.field("authorsAll") diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index 7200e8bc..05a57a0d 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -40,40 +40,49 @@ def add_reaction_stat_columns(q): def reactions_follow(user_id, shout_id: int, auto=False): - with local_session() as session: - shout = session.query(Shout).where(Shout.id == shout_id).one() + try: + with local_session() as session: + shout = session.query(Shout).where(Shout.id == shout_id).one() - following = ( - session.query(ShoutReactionsFollower).where(and_( - ShoutReactionsFollower.follower == user_id, - ShoutReactionsFollower.shout == shout.id, - )).first() - ) - - if not following: - following = ShoutReactionsFollower.create( - follower=user_id, - shout=shout.id, - auto=auto + following = ( + session.query(ShoutReactionsFollower).where(and_( + ShoutReactionsFollower.follower == user_id, + ShoutReactionsFollower.shout == shout.id, + )).first() ) - session.add(following) - session.commit() + + if not following: + following = ShoutReactionsFollower.create( + follower=user_id, + shout=shout.id, + auto=auto + ) + session.add(following) + session.commit() + return True + except: + return False def reactions_unfollow(user_id: int, shout_id: int): - with local_session() as session: - shout = session.query(Shout).where(Shout.id == shout_id).one() + try: + with local_session() as session: + shout = session.query(Shout).where(Shout.id == shout_id).one() - following = ( - session.query(ShoutReactionsFollower).where(and_( - ShoutReactionsFollower.follower == user_id, - ShoutReactionsFollower.shout == shout.id - )).first() - ) + following = ( + session.query(ShoutReactionsFollower).where(and_( + ShoutReactionsFollower.follower == user_id, + ShoutReactionsFollower.shout == shout.id + )).first() + ) - if following: - session.delete(following) - session.commit() + if following: + session.delete(following) + session.commit() + return True + except: + pass + return False def is_published_author(session, user_id): diff --git a/resolvers/zine/topics.py b/resolvers/zine/topics.py index 1eebdd60..f354a7b4 100644 --- a/resolvers/zine/topics.py +++ b/resolvers/zine/topics.py @@ -117,29 +117,36 @@ async def update_topic(_, _info, inp): def topic_follow(user_id, slug): - with local_session() as session: - topic = session.query(Topic).where(Topic.slug == slug).one() + try: + with local_session() as session: + topic = session.query(Topic).where(Topic.slug == slug).one() - following = TopicFollower.create(topic=topic.id, follower=user_id) - session.add(following) - session.commit() + following = TopicFollower.create(topic=topic.id, follower=user_id) + session.add(following) + session.commit() + return True + except: + return False def topic_unfollow(user_id, slug): - with local_session() as session: - sub = ( - session.query(TopicFollower).join(Topic).filter( - and_( - TopicFollower.follower == user_id, - Topic.slug == slug - ) - ).first() - ) - if not sub: - raise Exception("[resolvers.topics] follower not exist") - else: - session.delete(sub) - session.commit() + try: + with local_session() as session: + sub = ( + session.query(TopicFollower).join(Topic).filter( + and_( + TopicFollower.follower == user_id, + Topic.slug == slug + ) + ).first() + ) + if sub: + session.delete(sub) + session.commit() + return True + except: + pass + return False @query.field("topicsRandom") diff --git a/services/following.py b/services/following.py index f67f78a2..8410eb2d 100644 --- a/services/following.py +++ b/services/following.py @@ -37,12 +37,15 @@ class FollowingManager: @staticmethod async def push(kind, payload): - async with FollowingManager.lock: - if kind == 'chat': - for chat in FollowingManager['chat']: - if payload.message["chatId"] == chat.uid: - chat.queue.put_nowait(payload) - else: - for entity in FollowingManager[kind]: - if payload.shout['createdBy'] == entity.uid: - entity.queue.put_nowait(payload) + try: + async with FollowingManager.lock: + if kind == 'chat': + for chat in FollowingManager['chat']: + if payload.message["chatId"] == chat.uid: + chat.queue.put_nowait(payload) + else: + for entity in FollowingManager[kind]: + if payload.shout['createdBy'] == entity.uid: + entity.queue.put_nowait(payload) + except Exception as e: + print(Exception(e))