follows-brushed

This commit is contained in:
Untone 2024-03-11 16:12:28 +03:00
parent 4bc469ab04
commit c6e045d5ee

View File

@ -6,12 +6,12 @@ from sqlalchemy import select, or_
from sqlalchemy.sql import and_ from sqlalchemy.sql import and_
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
from orm.community import Community
# from orm.community import Community # from orm.community import Community
from orm.reaction import Reaction from orm.reaction import Reaction
from orm.shout import Shout, ShoutReactionsFollower from orm.shout import Shout, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from resolvers.community import community_follow, community_unfollow
from resolvers.topic import topic_unfollow, topic_follow from resolvers.topic import topic_unfollow, topic_follow
from resolvers.stat import get_with_stat, author_follows_topics, author_follows_authors from resolvers.stat import get_with_stat, author_follows_topics, author_follows_authors
from services.auth import login_required from services.auth import login_required
@ -31,84 +31,94 @@ from services.rediscache import redis
@login_required @login_required
async def follow(_, info, what, slug): async def follow(_, info, what, slug):
follows = [] follows = []
try: error = None
user_id = info.context['user_id'] user_id = info.context.get('user_id')
follower_query = ( if not user_id:
select(Author).select_from(Author).filter(Author.user == user_id) return {"error": "unauthorized"}
) [follower] = get_with_stat(select(Author).select_from(Author).filter(Author.user == user_id))
[follower] = get_with_stat(follower_query) if not follower:
if follower: return {"error": "cant find follower"}
if what == 'AUTHOR':
if author_follow(follower.id, slug):
logger.debug(f'@{follower.slug} followed @{slug}')
[author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug))
if author:
follows = await update_follows_for_author(
follower, 'author', author, True
)
_followers = await update_followers_for_author(follower, author, True)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic_query = select(Topic).where(Topic.slug == slug)
[topic] = get_with_stat(topic_query)
if topic:
follows = await update_follows_for_author(
follower, 'topic', topic, True
)
topic_follow(follower.id, slug)
elif what == 'COMMUNITY':
community_follow(follower.id, slug)
elif what == 'REACTIONS':
reactions_follow(follower.id, slug)
return {f'{what.lower()}s': follows} if what == 'AUTHOR':
except Exception as e: if not author_follow(follower.id, slug):
logger.error(e) return {"error": "cant follow author"}
return {'error': str(e)} logger.debug(f'@{follower.slug} followed @{slug}')
[author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug))
if not author:
return {"error": "author is not found"}
follows = await update_follows_for_author(follower, 'author', author, True)
_followers = await update_followers_for_author(follower, author, True)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
if not topic_follow(follower.id, slug):
return {"error": "cant follow topic"}
topic_query = select(Topic).where(Topic.slug == slug)
[topic] = get_with_stat(topic_query)
if not topic:
return {"error": "topic is not found"}
follows = await update_follows_for_author(follower, 'topic', topic, True)
elif what == 'COMMUNITY':
follows = local_session().execute(select(Community))
elif what == 'SHOUT':
if not reactions_follow(follower.id, slug):
return {"error": "cant follow shout"}
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
if not shout:
return {"error": "cant find shout"}
follows = await update_follows_for_author(follower, 'shout', shout, True)
return {f'{what.lower()}s': follows, "error": error}
@mutation.field('unfollow') @mutation.field('unfollow')
@login_required @login_required
async def unfollow(_, info, what, slug): async def unfollow(_, info, what, slug):
follows = None follows = []
try: error = None
user_id = info.context.get('user_id') user_id = info.context.get('user_id')
if not user_id: if not user_id:
return {"error": "unauthorized"} return {"error": "unauthorized"}
follower_query = select(Author).filter(Author.user == user_id) follower_query = select(Author).filter(Author.user == user_id)
[follower] = get_with_stat(follower_query) [follower] = get_with_stat(follower_query)
if follower: if not follower:
if what == 'AUTHOR': return {"error": "follower profile is not found"}
logger.info(f'@{follower.slug} unfollowing @{slug}')
if author_unfollow(follower.id, slug): if what == 'AUTHOR':
author_query = select(Author).where(Author.slug == slug) if not author_unfollow(follower.id, slug):
[author] = get_with_stat(author_query) return {"error": "cant unfollow author"}
if author: logger.info(f'@{follower.slug} unfollowing @{slug}')
follows = await update_follows_for_author( [author] = get_with_stat(select(Author).where(Author.slug == slug))
follower, 'author', author, False if not author:
) return {"error": "cant find author"}
_followers = await update_followers_for_author( _followers = await update_followers_for_author(follower, author, False)
follower, author, False await notify_follower(follower.dict(), author.id, 'unfollow')
) follows = await update_follows_for_author(follower, 'author', author, False)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC': elif what == 'TOPIC':
logger.info(f'@{follower.slug} unfollowing §{slug}') if not topic_unfollow(follower.id, slug):
topic_query = select(Topic).where(Topic.slug == slug) return {"error": "cant unfollow topic"}
[topic] = get_with_stat(topic_query) logger.info(f'@{follower.slug} unfollowing §{slug}')
if topic: [topic] = get_with_stat(select(Topic).where(Topic.slug == slug))
follows = await update_follows_for_author( if not topic:
follower, 'topic', topic, False return {"error": "cant find topic"}
) follows = await update_follows_for_author(follower, 'topic', topic, False)
topic_unfollow(follower.id, slug)
elif what == 'COMMUNITY': elif what == 'COMMUNITY':
community_unfollow(follower.id, slug) follows = local_session().execute(select(Community))
elif what == 'REACTIONS':
reactions_unfollow(follower.id, slug) elif what == 'SHOUT':
return {'error': "", f'{what.lower()}s': follows} logger.info(f'@{follower.slug} unfollowing §{slug}')
except Exception as e: [shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
import traceback if not shout:
traceback.print_exc() return {"error": "cant find shout"}
return {'error': str(e)} if not reactions_unfollow(follower.id, slug):
return {"error": "cant unfollow shout"}
follows = await update_follows_for_author(follower, 'shout', shout, False)
return {'error': error, f'{what.lower()}s': follows}
async def get_follows_by_user_id(user_id: str): async def get_follows_by_user_id(user_id: str):