core/resolvers/follower.py

259 lines
8.6 KiB
Python
Raw Normal View History

2024-02-21 14:37:58 +00:00
import json
2024-01-25 19:41:27 +00:00
from typing import List
2023-11-28 07:53:48 +00:00
2024-02-21 15:51:37 +00:00
from sqlalchemy import select, or_, column
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-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 08:59:47 +00:00
from resolvers.topic import (
topic_follow,
topic_unfollow,
add_topic_stat_columns,
get_topics_from_query,
)
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-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 07:27:16 +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 07:27:16 +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 07:27:16 +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 07:27:16 +00:00
elif what == "TOPIC":
2024-02-03 14:00:48 +00:00
topic_follow(follower_id, slug)
2024-02-21 07:27:16 +00:00
elif what == "COMMUNITY":
2024-02-03 14:00:48 +00:00
community_follow(follower_id, slug)
2024-02-21 07:27:16 +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 07:27:16 +00:00
return {"error": str(e)}
2023-10-23 14:47:11 +00:00
return {}
2024-02-21 07:27:16 +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 07:27:16 +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 07:27:16 +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()
)
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 07:27:16 +00:00
elif what == "COMMUNITY":
2024-02-03 14:00:48 +00:00
community_unfollow(follower_id, slug)
2024-02-21 07:27:16 +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 07:27:16 +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):
2023-11-28 07:53:48 +00:00
with local_session() as session:
2024-02-21 15:51:37 +00:00
aliased_author = aliased(Author)
author = session.query(aliased_author).filter(aliased_author.user == user_id).first()
2024-01-23 01:34:48 +00:00
if isinstance(author, Author):
author_id = author.id
2023-12-17 12:27:26 +00:00
authors_query = (
2024-02-21 15:51:37 +00:00
select(column('name'), column('id'), column('slug'), column('pic'), ).select_from(Author)
2024-01-18 12:30:53 +00:00
.join(AuthorFollower, AuthorFollower.follower == author_id)
2024-02-21 15:51:37 +00:00
.filter(AuthorFollower.author == Author.id)
2024-02-21 15:13:43 +00:00
.all()
2024-01-18 12:30:53 +00:00
)
2024-01-31 14:11:53 +00:00
topics_query = (
2024-02-21 15:51:37 +00:00
select(column('title'), column('id'), column('slug'), column('pic'), ).select_from(Author)
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-02-21 15:13:43 +00:00
.all()
2024-01-31 14:11:53 +00:00
)
2023-12-17 12:15:08 +00:00
2024-02-21 15:13:43 +00:00
# Convert query results to lists of dictionaries
2024-02-21 15:38:15 +00:00
authors = [author.to_dict() for author in authors_query]
topics = [topic.to_dict() for topic in topics_query]
2024-02-21 08:59:47 +00:00
# shouts_query = (
2024-02-21 15:13:43 +00:00
# session.query(Shout)
2024-02-21 08:59:47 +00:00
# .join(ShoutReactionsFollower, ShoutReactionsFollower.follower == author_id)
# .filter(ShoutReactionsFollower.shout == Shout.id)
2024-02-21 15:13:43 +00:00
# .options(load_only(Shout.id)) # Exclude unnecessary columns
# .all()
2024-02-21 08:59:47 +00:00
# )
2024-02-21 15:38:15 +00:00
# shouts = [shout.to_dict() for shout in shouts_query]
2024-02-21 08:59:47 +00:00
# communities = session.query(Community).all()
2024-02-03 14:00:48 +00:00
2024-02-21 07:27:16 +00:00
return {
2024-02-21 15:13:43 +00:00
"topics": topics,
"authors": authors,
# "shouts": shouts,
2024-02-21 10:59:17 +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:
redis_key = f"user:{user_id}:follows"
2024-02-21 14:55:54 +00:00
res = await redis.execute("GET", redis_key)
2024-02-21 14:37:58 +00:00
if isinstance(res, str):
follows = json.loads(res)
return follows
2024-02-21 07:27:16 +00:00
2024-02-21 08:52:57 +00:00
logger.debug(f"getting follows for {user_id}")
follows = query_follows(user_id)
2024-02-21 14:55:54 +00:00
await redis.execute("SET", redis_key, json.dumps(follows))
2024-02-21 07:27:16 +00:00
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
@query.field("get_topic_followers")
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)
@query.field("get_shout_followers")
def get_shout_followers(
2024-02-21 08:59:47 +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