core/resolvers/zine/following.py

148 lines
5.7 KiB
Python
Raw Normal View History

2023-02-06 13:09:26 +00:00
import asyncio
from base.orm import local_session
2023-02-16 10:08:55 +00:00
from base.resolvers import mutation, subscription
2022-11-21 08:13:57 +00:00
from auth.authenticate import login_required
2022-12-01 14:45:19 +00:00
from auth.credentials import AuthCredentials
2022-11-21 08:13:57 +00:00
# from resolvers.community import community_follow, community_unfollow
2023-02-06 13:09:26 +00:00
from orm.user import AuthorFollower
from orm.topic import TopicFollower
2023-02-16 10:08:55 +00:00
from orm.shout import ShoutReactionsFollower
2022-11-21 08:13:57 +00:00
from resolvers.zine.profile import author_follow, author_unfollow
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
from resolvers.zine.topics import topic_follow, topic_unfollow
2023-02-06 13:09:26 +00:00
from services.following import Following, FollowingManager, FollowingResult
2023-01-31 16:51:48 +00:00
from graphql.type import GraphQLResolveInfo
2022-11-21 08:13:57 +00:00
@mutation.field("follow")
@login_required
async def follow(_, info, what, slug):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-11-21 08:13:57 +00:00
try:
if what == "AUTHOR":
2023-02-20 17:38:20 +00:00
if author_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'author', slug)
await FollowingManager.push('author', result)
2022-11-21 08:13:57 +00:00
elif what == "TOPIC":
2023-02-20 17:38:20 +00:00
if topic_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'topic', slug)
await FollowingManager.push('topic', result)
2022-11-21 08:13:57 +00:00
elif what == "COMMUNITY":
2023-02-20 17:38:20 +00:00
if False: # TODO: use community_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'community', slug)
await FollowingManager.push('community', result)
2022-11-21 08:13:57 +00:00
elif what == "REACTIONS":
2023-02-20 17:38:20 +00:00
if reactions_follow(auth.user_id, slug):
result = FollowingResult("NEW", 'shout', slug)
await FollowingManager.push('shout', result)
2022-11-21 08:13:57 +00:00
except Exception as e:
2023-02-20 17:38:20 +00:00
print(Exception(e))
2022-11-21 08:13:57 +00:00
return {"error": str(e)}
return {}
@mutation.field("unfollow")
@login_required
async def unfollow(_, info, what, slug):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-11-21 08:13:57 +00:00
try:
if what == "AUTHOR":
2023-02-20 17:38:20 +00:00
if author_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'author', slug)
await FollowingManager.push('author', result)
2022-11-21 08:13:57 +00:00
elif what == "TOPIC":
2023-02-20 17:38:20 +00:00
if topic_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'topic', slug)
await FollowingManager.push('topic', result)
2022-11-21 08:13:57 +00:00
elif what == "COMMUNITY":
2023-02-20 17:38:20 +00:00
if False: # TODO: use community_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'community', slug)
await FollowingManager.push('community', result)
2022-11-21 08:13:57 +00:00
elif what == "REACTIONS":
2023-02-20 17:38:20 +00:00
if reactions_unfollow(auth.user_id, slug):
result = FollowingResult("DELETED", 'shout', slug)
await FollowingManager.push('shout', result)
2022-11-21 08:13:57 +00:00
except Exception as e:
return {"error": str(e)}
return {}
2023-01-31 16:51:48 +00:00
2023-02-06 13:09:26 +00:00
# by author and by topic
2023-01-31 16:51:48 +00:00
@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 = []
2023-02-06 13:09:26 +00:00
with local_session() as session:
2023-02-16 10:08:55 +00:00
# notify new shout by followed authors
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).all()
2023-02-06 13:09:26 +00:00
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)
2023-02-16 10:08:55 +00:00
# by followed topics
following_authors = session.query(AuthorFollower).where(
AuthorFollower.follower == user_id).all()
2023-02-06 13:09:26 +00:00
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)
2023-01-31 16:51:48 +00:00
2023-02-16 10:08:55 +00:00
# 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)
2023-01-31 16:51:48 +00:00
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:
2023-02-06 13:09:26 +00:00
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
2023-01-31 16:51:48 +00:00
finally:
pass