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)
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 {}

View File

@ -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)}

View File

@ -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")

View File

@ -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):

View File

@ -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")

View File

@ -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))