core/resolvers/follower.py

199 lines
6.8 KiB
Python
Raw Normal View History

2024-01-25 19:41:27 +00:00
from typing import List
2023-11-28 07:53:48 +00:00
2024-02-03 17:13:51 +00:00
from sqlalchemy.orm import aliased
2024-02-02 12:03:44 +00:00
from sqlalchemy.sql import and_
2024-01-31 14:48:36 +00:00
2023-12-17 20:30:20 +00:00
from orm.author import Author, AuthorFollower
2024-02-03 17:08:22 +00:00
from orm.community import Community
2023-11-28 09:11:45 +00:00
from orm.reaction import Reaction
2024-02-02 12:03:44 +00:00
from orm.shout import Shout, ShoutReactionsFollower
2023-11-28 07:53:48 +00:00
from orm.topic import Topic, TopicFollower
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.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-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
2024-02-20 16:19:46 +00:00
from services.logger import root_logger as logger
2024-01-13 08:49:12 +00:00
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):
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':
2024-02-03 14:00:48 +00:00
topic_follow(follower_id, slug)
2024-01-25 19:41:27 +00:00
elif what == 'COMMUNITY':
2024-02-03 14:00:48 +00:00
community_follow(follower_id, slug)
2024-01-25 19:41:27 +00:00
elif what == 'REACTIONS':
2024-02-03 14:00:48 +00:00
reactions_follow(follower_id, slug)
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):
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':
2024-02-03 14:00:48 +00:00
topic_unfollow(follower_id, slug)
2024-01-25 19:41:27 +00:00
elif what == 'COMMUNITY':
2024-02-03 14:00:48 +00:00
community_unfollow(follower_id, slug)
2024-01-25 19:41:27 +00:00
elif what == 'REACTIONS':
2024-02-03 14:00:48 +00:00
reactions_unfollow(follower_id, slug)
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-02-03 17:08:22 +00:00
topics = set()
authors = set()
2024-01-13 08:01:59 +00:00
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-02-03 17:08:22 +00:00
aliased_author = aliased(Author)
2023-12-17 12:27:26 +00:00
authors_query = (
2024-02-03 17:08:22 +00:00
session.query(aliased_author, AuthorFollower)
2024-01-18 12:30:53 +00:00
.join(AuthorFollower, AuthorFollower.follower == author_id)
2024-02-03 17:08:22 +00:00
.filter(AuthorFollower.author == aliased_author.id)
2024-01-18 12:30:53 +00:00
)
2024-01-31 14:11:53 +00:00
topics_query = (
2024-02-03 17:08:22 +00:00
session.query(Topic, TopicFollower)
2024-01-31 14:11:53 +00:00
.join(TopicFollower, TopicFollower.follower == author_id)
2024-02-03 17:08:22 +00:00
.filter(TopicFollower.topic == Topic.id)
2024-01-31 14:11:53 +00:00
)
2023-12-17 12:15:08 +00:00
2024-02-03 17:08:22 +00:00
authors = set(session.execute(authors_query).scalars())
topics = set(session.execute(topics_query).scalars())
communities = session.query(Community).all()
2024-02-03 14:00:48 +00:00
2024-02-03 17:08:22 +00:00
return {'topics': list(topics), 'authors': list(authors), 'communities': communities}
2023-11-28 07:53:48 +00:00
2023-11-28 09:11:45 +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
2024-02-02 12:03:44 +00:00
def reactions_follow(author_id, shout_id, auto=False):
try:
with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one()
following = (
session.query(ShoutReactionsFollower)
.where(
and_(
ShoutReactionsFollower.follower == author_id,
ShoutReactionsFollower.shout == shout.id,
)
)
.first()
)
if not following:
following = ShoutReactionsFollower(follower=author_id, shout=shout.id, auto=auto)
session.add(following)
session.commit()
return True
except Exception:
return False
def reactions_unfollow(author_id, shout_id: int):
try:
with local_session() as session:
shout = session.query(Shout).where(Shout.id == shout_id).one()
following = (
session.query(ShoutReactionsFollower)
.where(
and_(
ShoutReactionsFollower.follower == author_id,
ShoutReactionsFollower.shout == shout.id,
)
)
.first()
)
if following:
session.delete(following)
session.commit()
return True
except Exception as ex:
logger.debug(ex)
return False
# for mutation.field("follow")
def author_follow(follower_id, slug):
try:
with local_session() as session:
author = session.query(Author).where(Author.slug == slug).one()
af = AuthorFollower(follower=follower_id, author=author.id)
session.add(af)
session.commit()
return True
except Exception:
return False
# for mutation.field("unfollow")
def author_unfollow(follower_id, slug):
with local_session() as session:
flw = (
session.query(AuthorFollower)
.join(Author, Author.id == AuthorFollower.author)
.filter(and_(AuthorFollower.follower == follower_id, Author.slug == slug))
.first()
)
if flw:
session.delete(flw)
session.commit()
return True
return False