following manager works

This commit is contained in:
tonyrewin 2023-02-20 20:38:20 +03:00
parent a8ad52caba
commit 80030f21b7
6 changed files with 114 additions and 95 deletions

View File

@ -51,7 +51,7 @@ async def create_message(_, info, chat: str, body: str, replyTo=None):
) )
result = FollowingResult("NEW", 'chat', new_message) result = FollowingResult("NEW", 'chat', new_message)
await FollowingManager.put('chat', result) await FollowingManager.push('chat', result)
return { return {
"message": new_message, "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)) await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message))
result = FollowingResult("UPDATED", 'chat', message) result = FollowingResult("UPDATED", 'chat', message)
await FollowingManager.put('chat', result) await FollowingManager.push('chat', result)
return { return {
"message": message, "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)) await redis.execute("LREM", f"chats/{chat_id}/unread/{user_id}", 0, str(message_id))
result = FollowingResult("DELETED", 'chat', message) result = FollowingResult("DELETED", 'chat', message)
await FollowingManager.put(result) await FollowingManager.push(result)
return {} return {}

View File

@ -21,23 +21,23 @@ async def follow(_, info, what, slug):
try: try:
if what == "AUTHOR": if what == "AUTHOR":
author_follow(auth.user_id, slug) if author_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'author', slug) result = FollowingResult("NEW", 'author', slug)
await FollowingManager.put('author', result) await FollowingManager.push('author', result)
elif what == "TOPIC": elif what == "TOPIC":
topic_follow(auth.user_id, slug) if topic_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'topic', slug) result = FollowingResult("NEW", 'topic', slug)
await FollowingManager.put('topic', result) await FollowingManager.push('topic', result)
elif what == "COMMUNITY": elif what == "COMMUNITY":
# community_follow(user, slug) if False: # TODO: use community_follow(auth.user_id, slug):
# result = FollowingResult("NEW", 'community', slug) result = FollowingResult("NEW", 'community', slug)
# await FollowingManager.put('community', result) await FollowingManager.push('community', result)
pass
elif what == "REACTIONS": elif what == "REACTIONS":
reactions_follow(auth.user_id, slug) if reactions_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'shout', slug) result = FollowingResult("NEW", 'shout', slug)
await FollowingManager.put('shout', result) await FollowingManager.push('shout', result)
except Exception as e: except Exception as e:
print(Exception(e))
return {"error": str(e)} return {"error": str(e)}
return {} return {}
@ -50,22 +50,21 @@ async def unfollow(_, info, what, slug):
try: try:
if what == "AUTHOR": if what == "AUTHOR":
author_unfollow(auth.user_id, slug) if author_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'author', slug) result = FollowingResult("DELETED", 'author', slug)
await FollowingManager.put('author', result) await FollowingManager.push('author', result)
elif what == "TOPIC": elif what == "TOPIC":
topic_unfollow(auth.user_id, slug) if topic_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'topic', slug) result = FollowingResult("DELETED", 'topic', slug)
await FollowingManager.put('topic', result) await FollowingManager.push('topic', result)
elif what == "COMMUNITY": elif what == "COMMUNITY":
# community_unfollow(user, slug) if False: # TODO: use community_unfollow(auth.user_id, slug):
# result = FollowingResult("DELETED", 'community', slug) result = FollowingResult("DELETED", 'community', slug)
# await FollowingManager.put('community', result) await FollowingManager.push('community', result)
pass
elif what == "REACTIONS": elif what == "REACTIONS":
reactions_unfollow(auth.user_id, slug) if reactions_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'shout', slug) result = FollowingResult("DELETED", 'shout', slug)
await FollowingManager.put('shout', result) await FollowingManager.push('shout', result)
except Exception as e: except Exception as e:
return {"error": str(e)} return {"error": str(e)}

View File

@ -198,11 +198,15 @@ async def rate_user(_, info, rated_userslug, value):
# for mutation.field("follow") # for mutation.field("follow")
def author_follow(user_id, slug): def author_follow(user_id, slug):
try:
with local_session() as session: with local_session() as session:
author = session.query(User).where(User.slug == slug).one() author = session.query(User).where(User.slug == slug).one()
af = AuthorFollower.create(follower=user_id, author=author.id) af = AuthorFollower.create(follower=user_id, author=author.id)
session.add(af) session.add(af)
session.commit() session.commit()
return True
except:
return False
# for mutation.field("unfollow") # for mutation.field("unfollow")
@ -217,14 +221,11 @@ def author_unfollow(user_id, slug):
) )
).first() ).first()
) )
if not flw: if flw:
return {
"error": "Follower is not exist, cant unfollow"
}
else:
session.delete(flw) session.delete(flw)
session.commit() session.commit()
return {} return True
return False
@query.field("authorsAll") @query.field("authorsAll")

View File

@ -40,6 +40,7 @@ def add_reaction_stat_columns(q):
def reactions_follow(user_id, shout_id: int, auto=False): def reactions_follow(user_id, shout_id: int, auto=False):
try:
with local_session() as session: with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one() shout = session.query(Shout).where(Shout.id == shout_id).one()
@ -58,9 +59,13 @@ def reactions_follow(user_id, shout_id: int, auto=False):
) )
session.add(following) session.add(following)
session.commit() session.commit()
return True
except:
return False
def reactions_unfollow(user_id: int, shout_id: int): def reactions_unfollow(user_id: int, shout_id: int):
try:
with local_session() as session: with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one() shout = session.query(Shout).where(Shout.id == shout_id).one()
@ -74,6 +79,10 @@ def reactions_unfollow(user_id: int, shout_id: int):
if following: if following:
session.delete(following) session.delete(following)
session.commit() session.commit()
return True
except:
pass
return False
def is_published_author(session, user_id): def is_published_author(session, user_id):

View File

@ -117,15 +117,20 @@ async def update_topic(_, _info, inp):
def topic_follow(user_id, slug): def topic_follow(user_id, slug):
try:
with local_session() as session: with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one() topic = session.query(Topic).where(Topic.slug == slug).one()
following = TopicFollower.create(topic=topic.id, follower=user_id) following = TopicFollower.create(topic=topic.id, follower=user_id)
session.add(following) session.add(following)
session.commit() session.commit()
return True
except:
return False
def topic_unfollow(user_id, slug): def topic_unfollow(user_id, slug):
try:
with local_session() as session: with local_session() as session:
sub = ( sub = (
session.query(TopicFollower).join(Topic).filter( session.query(TopicFollower).join(Topic).filter(
@ -135,11 +140,13 @@ def topic_unfollow(user_id, slug):
) )
).first() ).first()
) )
if not sub: if sub:
raise Exception("[resolvers.topics] follower not exist")
else:
session.delete(sub) session.delete(sub)
session.commit() session.commit()
return True
except:
pass
return False
@query.field("topicsRandom") @query.field("topicsRandom")

View File

@ -37,6 +37,7 @@ class FollowingManager:
@staticmethod @staticmethod
async def push(kind, payload): async def push(kind, payload):
try:
async with FollowingManager.lock: async with FollowingManager.lock:
if kind == 'chat': if kind == 'chat':
for chat in FollowingManager['chat']: for chat in FollowingManager['chat']:
@ -46,3 +47,5 @@ class FollowingManager:
for entity in FollowingManager[kind]: for entity in FollowingManager[kind]:
if payload.shout['createdBy'] == entity.uid: if payload.shout['createdBy'] == entity.uid:
entity.queue.put_nowait(payload) entity.queue.put_nowait(payload)
except Exception as e:
print(Exception(e))