core/resolvers/follower.py

291 lines
9.8 KiB
Python
Raw Normal View History

2024-02-21 14:37:58 +00:00
import json
2024-02-22 10:20:14 +00:00
import time
2024-01-25 19:41:27 +00:00
from typing import List
2023-11-28 07:53:48 +00:00
2024-02-21 18:25:23 +00:00
from sqlalchemy import select, or_
2024-02-22 18:22:22 +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-21 08:59:47 +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
2024-02-21 17:12:47 +00:00
from resolvers.topic import topic_follow, topic_unfollow
2024-02-22 10:12:01 +00:00
from resolvers.stat import add_topic_stat_columns, get_topics_from_query, add_author_stat_columns
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
2024-02-22 10:20:14 +00:00
from services.follows import DEFAULT_FOLLOWS
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-02-21 07:27:16 +00:00
from services.rediscache import redis
2024-01-13 08:49:12 +00:00
2024-01-22 23:28:54 +00:00
2024-02-21 16:14:58 +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-02-21 16:14:58 +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-02-21 16:14:58 +00:00
if what == 'AUTHOR':
2023-11-23 23:00:28 +00:00
if author_follow(follower_id, slug):
2024-02-21 07:27:16 +00:00
author = (
session.query(Author.id).where(Author.slug == slug).one()
)
follower = (
session.query(Author).where(Author.id == follower_id).one()
)
2023-11-23 23:00:28 +00:00
await notify_follower(follower.dict(), author.id)
2024-02-21 16:14:58 +00:00
elif what == 'TOPIC':
2024-02-03 14:00:48 +00:00
topic_follow(follower_id, slug)
2024-02-21 16:14:58 +00:00
elif what == 'COMMUNITY':
2024-02-03 14:00:48 +00:00
community_follow(follower_id, slug)
2024-02-21 16:14:58 +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-02-21 16:14:58 +00:00
return {'error': str(e)}
2023-10-23 14:47:11 +00:00
return {}
2024-02-21 16:14:58 +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-02-21 16:14:58 +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-02-21 16:14:58 +00:00
if what == 'AUTHOR':
2023-11-23 23:00:28 +00:00
if author_unfollow(follower_id, slug):
2024-02-21 07:27:16 +00:00
author = (
session.query(Author.id).where(Author.slug == slug).one()
)
follower = (
session.query(Author).where(Author.id == follower_id).one()
)
2024-02-21 16:14:58 +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-02-21 16:14:58 +00:00
elif what == 'COMMUNITY':
2024-02-03 14:00:48 +00:00
community_unfollow(follower_id, slug)
2024-02-21 16:14:58 +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-02-21 16:14:58 +00:00
return {'error': str(e)}
2023-10-23 14:47:11 +00:00
return {}
2023-11-28 07:53:48 +00:00
2024-02-21 07:27:16 +00:00
def query_follows(user_id: str):
2024-02-22 10:20:14 +00:00
logger.debug(f'query follows for {user_id} from database')
2024-02-21 19:20:17 +00:00
topics = []
authors = []
2023-11-28 07:53:48 +00:00
with local_session() as session:
2024-02-22 19:56:58 +00:00
author = session.query(Author).filter(Author.user == user_id).first()
2024-02-22 10:12:01 +00:00
if isinstance(author, Author):
author_id = author.id
2024-02-22 19:56:58 +00:00
aliased_author = aliased(Author)
2024-02-22 20:07:08 +00:00
2023-12-17 12:27:26 +00:00
authors_query = (
2024-02-22 19:56:58 +00:00
session.query(aliased_author, AuthorFollower)
2024-01-18 12:30:53 +00:00
.join(AuthorFollower, AuthorFollower.follower == author_id)
2024-02-22 19:56:58 +00:00
.filter(AuthorFollower.author == aliased_author.id)
)
topics_query = (
session.query(Topic, TopicFollower)
.join(TopicFollower, TopicFollower.follower == author_id)
.filter(TopicFollower.topic == Topic.id)
2024-01-18 12:30:53 +00:00
)
2024-02-22 20:07:08 +00:00
2024-02-22 20:13:29 +00:00
authors_query = add_author_stat_columns(authors_query, author_model=aliased_author)
2024-02-22 19:56:58 +00:00
topics_query = add_topic_stat_columns(topics_query)
2024-02-21 16:48:33 +00:00
authors = [
{
2024-02-22 10:07:09 +00:00
'id': author_id,
2024-02-21 18:53:11 +00:00
'name': author.name,
'slug': author.slug,
'pic': author.pic,
'bio': author.bio,
2024-02-22 19:56:58 +00:00
'last_seen': author.last_seen or int(time.time()),
2024-02-21 16:48:33 +00:00
'stat': {
'shouts': shouts_stat,
'followers': followers_stat,
2024-02-22 19:56:58 +00:00
'followings': followings_stat, # TODO: rename to authors to
# TODO: use graphql to reserve universal type Stat { authors shouts followers views comments }
2024-02-21 16:48:33 +00:00
},
}
2024-02-21 19:12:31 +00:00
for [
2024-02-21 18:53:11 +00:00
author,
2024-02-21 18:25:23 +00:00
shouts_stat,
followers_stat,
followings_stat,
2024-02-21 19:12:31 +00:00
] in session.execute(authors_query)
2024-02-21 16:48:33 +00:00
]
2024-02-21 18:25:23 +00:00
2024-02-21 16:48:33 +00:00
topics = [
{
2024-02-21 18:53:11 +00:00
'id': topic.id,
'title': topic.title,
'slug': topic.slug,
'body': topic.body,
2024-02-21 16:48:33 +00:00
'stat': {
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
},
}
2024-02-21 19:12:31 +00:00
for [
2024-02-21 18:53:11 +00:00
topic,
2024-02-21 16:48:33 +00:00
shouts_stat,
authors_stat,
followers_stat,
2024-02-21 19:12:31 +00:00
] in session.execute(topics_query)
2024-02-21 16:48:33 +00:00
]
2024-02-21 18:25:23 +00:00
2024-02-21 07:27:16 +00:00
return {
2024-02-21 16:14:58 +00:00
'topics': topics,
'authors': authors,
2024-02-21 18:25:23 +00:00
# Include other results (e.g., shouts) if needed
2024-02-21 16:14:58 +00:00
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
2024-02-21 07:27:16 +00:00
}
async def get_follows_by_user_id(user_id: str):
2024-02-21 08:52:57 +00:00
if user_id:
2024-02-22 10:20:14 +00:00
author = await redis.execute('GET', f'user:{user_id}:author')
follows = DEFAULT_FOLLOWS
day_old = int(time.time()) - author.get('last_seen', 0) > 24*60*60
if day_old:
follows = query_follows(user_id)
else:
logger.debug(f'getting follows for {user_id} from redis')
res = await redis.execute('GET', f'user:{user_id}:follows')
if isinstance(res, str):
follows = json.loads(res)
2024-02-21 08:52:57 +00:00
return follows
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:
2024-02-21 07:27:16 +00:00
following = ShoutReactionsFollower(
follower=author_id, shout=shout.id, auto=auto
)
2024-02-02 12:03:44 +00:00
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
2024-02-21 08:52:57 +00:00
2024-02-21 16:14:58 +00:00
@query.field('get_topic_followers')
2024-02-21 08:52:57 +00:00
async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
q = select(Author)
q = add_topic_stat_columns(q)
q = (
q.join(TopicFollower, TopicFollower.follower == Author.id)
.join(Topic, Topic.id == TopicFollower.topic)
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
)
return await get_topics_from_query(q)
2024-02-21 16:14:58 +00:00
@query.field('get_shout_followers')
2024-02-21 08:52:57 +00:00
def get_shout_followers(
2024-02-21 16:14:58 +00:00
_, _info, slug: str = '', shout_id: int | None = None
2024-02-21 08:52:57 +00:00
) -> List[Author]:
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)
return followers