2024-01-13 08:49:12 +00:00
|
|
|
import logging
|
2024-01-25 19:41:27 +00:00
|
|
|
from typing import List
|
2023-11-28 07:53:48 +00:00
|
|
|
|
2023-12-17 20:30:20 +00:00
|
|
|
from orm.author import Author, AuthorFollower
|
|
|
|
from orm.community import Community
|
2023-11-28 09:11:45 +00:00
|
|
|
from orm.reaction import Reaction
|
|
|
|
from orm.shout import Shout
|
2023-11-28 07:53:48 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
2023-10-23 14:47:11 +00:00
|
|
|
from resolvers.author import author_follow, author_unfollow
|
2023-12-17 20:30:20 +00:00
|
|
|
from resolvers.community import community_follow, community_unfollow
|
2023-10-23 14:47:11 +00:00
|
|
|
from resolvers.reaction import reactions_follow, reactions_unfollow
|
|
|
|
from resolvers.topic import topic_follow, topic_unfollow
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.auth import login_required
|
2023-10-23 14:47:11 +00:00
|
|
|
from services.db import local_session
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.following import FollowingManager, FollowingResult
|
2023-10-25 18:33:53 +00:00
|
|
|
from services.notify import notify_follower
|
2023-11-28 07:53:48 +00:00
|
|
|
from services.schema import mutation, query
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-01-13 08:49:12 +00:00
|
|
|
logging.basicConfig()
|
2024-01-25 19:41:27 +00:00
|
|
|
logger = logging.getLogger('\t[resolvers.reaction]\t')
|
2024-01-13 08:49:12 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2024-01-22 23:28:54 +00:00
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
@mutation.field('follow')
|
2024-01-22 23:28:54 +00:00
|
|
|
@login_required
|
2023-10-23 14:47:11 +00:00
|
|
|
async def follow(_, info, what, slug):
|
|
|
|
try:
|
2024-01-25 19:41:27 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-11-23 23:00:28 +00:00
|
|
|
with local_session() as session:
|
|
|
|
actor = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if actor:
|
|
|
|
follower_id = actor.id
|
2024-01-25 19:41:27 +00:00
|
|
|
if what == 'AUTHOR':
|
2023-11-23 23:00:28 +00:00
|
|
|
if author_follow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('NEW', 'author', slug)
|
|
|
|
await FollowingManager.push('author', result)
|
2023-11-23 23:00:28 +00:00
|
|
|
author = session.query(Author.id).where(Author.slug == slug).one()
|
|
|
|
follower = session.query(Author).where(Author.id == follower_id).one()
|
|
|
|
await notify_follower(follower.dict(), author.id)
|
2024-01-25 19:41:27 +00:00
|
|
|
elif what == 'TOPIC':
|
2023-11-23 23:00:28 +00:00
|
|
|
if topic_follow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('NEW', 'topic', slug)
|
|
|
|
await FollowingManager.push('topic', result)
|
|
|
|
elif what == 'COMMUNITY':
|
2023-11-23 23:00:28 +00:00
|
|
|
if community_follow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('NEW', 'community', slug)
|
|
|
|
await FollowingManager.push('community', result)
|
|
|
|
elif what == 'REACTIONS':
|
2023-11-23 23:00:28 +00:00
|
|
|
if reactions_follow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('NEW', 'shout', slug)
|
|
|
|
await FollowingManager.push('shout', result)
|
2023-10-23 14:47:11 +00:00
|
|
|
except Exception as e:
|
2024-01-22 23:23:31 +00:00
|
|
|
logger.debug(info, what, slug)
|
2024-01-13 08:49:12 +00:00
|
|
|
logger.error(e)
|
2024-01-25 19:41:27 +00:00
|
|
|
return {'error': str(e)}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
@mutation.field('unfollow')
|
2024-01-22 23:28:54 +00:00
|
|
|
@login_required
|
2023-10-23 14:47:11 +00:00
|
|
|
async def unfollow(_, info, what, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
user_id = info.context['user_id']
|
2023-10-23 14:47:11 +00:00
|
|
|
try:
|
2023-11-23 23:00:28 +00:00
|
|
|
with local_session() as session:
|
|
|
|
actor = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if actor:
|
|
|
|
follower_id = actor.id
|
2024-01-25 19:41:27 +00:00
|
|
|
if what == 'AUTHOR':
|
2023-11-23 23:00:28 +00:00
|
|
|
if author_unfollow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('DELETED', 'author', slug)
|
|
|
|
await FollowingManager.push('author', result)
|
2023-11-23 23:00:28 +00:00
|
|
|
author = session.query(Author.id).where(Author.slug == slug).one()
|
|
|
|
follower = session.query(Author).where(Author.id == follower_id).one()
|
2024-01-25 19:41:27 +00:00
|
|
|
await notify_follower(follower.dict(), author.id, 'unfollow')
|
|
|
|
elif what == 'TOPIC':
|
2023-11-23 23:00:28 +00:00
|
|
|
if topic_unfollow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('DELETED', 'topic', slug)
|
|
|
|
await FollowingManager.push('topic', result)
|
|
|
|
elif what == 'COMMUNITY':
|
2023-11-23 23:00:28 +00:00
|
|
|
if community_unfollow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('DELETED', 'community', slug)
|
|
|
|
await FollowingManager.push('community', result)
|
|
|
|
elif what == 'REACTIONS':
|
2023-11-23 23:00:28 +00:00
|
|
|
if reactions_unfollow(follower_id, slug):
|
2024-01-25 19:41:27 +00:00
|
|
|
result = FollowingResult('DELETED', 'shout', slug)
|
|
|
|
await FollowingManager.push('shout', result)
|
2023-10-23 14:47:11 +00:00
|
|
|
except Exception as e:
|
2024-01-25 19:41:27 +00:00
|
|
|
return {'error': str(e)}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
return {}
|
2023-11-28 07:53:48 +00:00
|
|
|
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
@query.field('get_my_followed')
|
2023-11-28 07:53:48 +00:00
|
|
|
@login_required
|
|
|
|
async def get_my_followed(_, info):
|
2024-01-25 19:41:27 +00:00
|
|
|
user_id = info.context['user_id']
|
2024-01-13 08:01:59 +00:00
|
|
|
topics = []
|
|
|
|
authors = []
|
|
|
|
communities = []
|
2023-11-28 07:53:48 +00:00
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
2024-01-23 01:34:48 +00:00
|
|
|
if isinstance(author, Author):
|
|
|
|
author_id = author.id
|
2024-01-18 12:12:40 +00:00
|
|
|
|
2023-12-17 12:27:26 +00:00
|
|
|
authors_query = (
|
2024-01-18 12:30:53 +00:00
|
|
|
session.query(Author)
|
|
|
|
.join(AuthorFollower, AuthorFollower.follower == author_id)
|
|
|
|
.filter(AuthorFollower.author == Author.id)
|
|
|
|
)
|
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
topics_query = session.query(Topic).join(TopicFollower, TopicFollower.follower == author_id)
|
2023-12-17 12:15:08 +00:00
|
|
|
|
2024-01-13 08:01:59 +00:00
|
|
|
for [author] in session.execute(authors_query):
|
|
|
|
authors.append(author)
|
|
|
|
|
|
|
|
for [topic] in session.execute(topics_query):
|
|
|
|
topics.append(topic)
|
2023-11-28 07:53:48 +00:00
|
|
|
|
2023-12-17 12:30:28 +00:00
|
|
|
communities = session.query(Community).all()
|
2023-11-28 07:53:48 +00:00
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
return {'topics': topics, 'authors': authors, 'communities': communities}
|
2023-11-28 09:11:45 +00:00
|
|
|
|
2024-01-18 12:30:53 +00:00
|
|
|
|
2024-01-25 19:41:27 +00:00
|
|
|
@query.field('get_shout_followers')
|
|
|
|
def get_shout_followers(_, _info, slug: str = '', shout_id: int | None = None) -> List[Author]:
|
2023-11-28 09:11:45 +00:00
|
|
|
followers = []
|
|
|
|
with local_session() as session:
|
|
|
|
shout = None
|
|
|
|
if slug:
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
|
|
|
elif shout_id:
|
|
|
|
shout = session.query(Shout).filter(Shout.id == shout_id).first()
|
|
|
|
if shout:
|
|
|
|
reactions = session.query(Reaction).filter(Reaction.shout == shout.id).all()
|
|
|
|
for r in reactions:
|
|
|
|
followers.append(r.created_by)
|
|
|
|
|
2024-01-13 08:01:59 +00:00
|
|
|
return followers
|