core/resolvers/zine/following.py

67 lines
2.7 KiB
Python
Raw Normal View History

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
2023-10-30 21:00:55 +00:00
from base.resolvers import mutation
2022-11-21 08:13:57 +00:00
# from resolvers.community import community_follow, community_unfollow
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-10-30 21:00:55 +00:00
from services.following import FollowingManager, FollowingResult
2022-11-21 08:13:57 +00:00
@mutation.field("follow")
@login_required
2023-10-30 21:00:55 +00:00
async def follow(_, info, what, slug): # noqa: C901
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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
2023-10-30 21:00:55 +00:00
async def unfollow(_, info, what, slug): # noqa: C901
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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):
2023-10-30 21:00:55 +00:00
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 {}