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):
with local_session() as session: try:
author = session.query(User).where(User.slug == slug).one() with local_session() as session:
af = AuthorFollower.create(follower=user_id, author=author.id) author = session.query(User).where(User.slug == slug).one()
session.add(af) af = AuthorFollower.create(follower=user_id, author=author.id)
session.commit() session.add(af)
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,40 +40,49 @@ 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):
with local_session() as session: try:
shout = session.query(Shout).where(Shout.id == shout_id).one() with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one()
following = ( following = (
session.query(ShoutReactionsFollower).where(and_( session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.follower == user_id, ShoutReactionsFollower.follower == user_id,
ShoutReactionsFollower.shout == shout.id, ShoutReactionsFollower.shout == shout.id,
)).first() )).first()
)
if not following:
following = ShoutReactionsFollower.create(
follower=user_id,
shout=shout.id,
auto=auto
) )
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): def reactions_unfollow(user_id: int, shout_id: int):
with local_session() as session: try:
shout = session.query(Shout).where(Shout.id == shout_id).one() with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one()
following = ( following = (
session.query(ShoutReactionsFollower).where(and_( session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.follower == user_id, ShoutReactionsFollower.follower == user_id,
ShoutReactionsFollower.shout == shout.id ShoutReactionsFollower.shout == shout.id
)).first() )).first()
) )
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,29 +117,36 @@ async def update_topic(_, _info, inp):
def topic_follow(user_id, slug): def topic_follow(user_id, slug):
with local_session() as session: try:
topic = session.query(Topic).where(Topic.slug == slug).one() with local_session() as session:
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):
with local_session() as session: try:
sub = ( with local_session() as session:
session.query(TopicFollower).join(Topic).filter( sub = (
and_( session.query(TopicFollower).join(Topic).filter(
TopicFollower.follower == user_id, and_(
Topic.slug == slug TopicFollower.follower == user_id,
) Topic.slug == slug
).first() )
) ).first()
if not sub: )
raise Exception("[resolvers.topics] follower not exist") if sub:
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,12 +37,15 @@ class FollowingManager:
@staticmethod @staticmethod
async def push(kind, payload): async def push(kind, payload):
async with FollowingManager.lock: try:
if kind == 'chat': async with FollowingManager.lock:
for chat in FollowingManager['chat']: if kind == 'chat':
if payload.message["chatId"] == chat.uid: for chat in FollowingManager['chat']:
chat.queue.put_nowait(payload) if payload.message["chatId"] == chat.uid:
else: chat.queue.put_nowait(payload)
for entity in FollowingManager[kind]: else:
if payload.shout['createdBy'] == entity.uid: for entity in FollowingManager[kind]:
entity.queue.put_nowait(payload) if payload.shout['createdBy'] == entity.uid:
entity.queue.put_nowait(payload)
except Exception as e:
print(Exception(e))