Feature/notifications (#77)

feature - notifications

Co-authored-by: Igor Lobanov <igor.lobanov@onetwotrip.com>
This commit is contained in:
Ilya Y
2023-10-10 09:35:27 +03:00
committed by GitHub
parent 702219769a
commit 889f802429
21 changed files with 412 additions and 305 deletions

View File

@@ -1,6 +1,6 @@
import asyncio
from base.orm import local_session
from base.resolvers import mutation, subscription
from base.resolvers import mutation
from auth.authenticate import login_required
from auth.credentials import AuthCredentials
# from resolvers.community import community_follow, community_unfollow
@@ -69,79 +69,3 @@ async def unfollow(_, info, what, slug):
return {"error": str(e)}
return {}
# by author and by topic
@subscription.source("newShout")
@login_required
async def shout_generator(_, info: GraphQLResolveInfo):
print(f"[resolvers.zine] shouts generator {info}")
auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id
try:
tasks = []
with local_session() as session:
# notify new shout by followed authors
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).all()
for topic_id in following_topics:
following_topic = Following('topic', topic_id)
await FollowingManager.register('topic', following_topic)
following_topic_task = following_topic.queue.get()
tasks.append(following_topic_task)
# by followed topics
following_authors = session.query(AuthorFollower).where(
AuthorFollower.follower == user_id).all()
for author_id in following_authors:
following_author = Following('author', author_id)
await FollowingManager.register('author', following_author)
following_author_task = following_author.queue.get()
tasks.append(following_author_task)
# TODO: use communities
# by followed communities
# following_communities = session.query(CommunityFollower).where(
# CommunityFollower.follower == user_id).all()
# for community_id in following_communities:
# following_community = Following('community', author_id)
# await FollowingManager.register('community', following_community)
# following_community_task = following_community.queue.get()
# tasks.append(following_community_task)
while True:
shout = await asyncio.gather(*tasks)
yield shout
finally:
pass
@subscription.source("newReaction")
@login_required
async def reaction_generator(_, info):
print(f"[resolvers.zine] reactions generator {info}")
auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id
try:
with local_session() as session:
followings = session.query(ShoutReactionsFollower.shout).where(
ShoutReactionsFollower.follower == user_id).unique()
# notify new reaction
tasks = []
for shout_id in followings:
following_shout = Following('shout', shout_id)
await FollowingManager.register('shout', following_shout)
following_author_task = following_shout.queue.get()
tasks.append(following_author_task)
while True:
reaction = await asyncio.gather(*tasks)
yield reaction
finally:
pass