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-04-08 07:38:58 +00:00
|
|
|
from sqlalchemy import or_, select
|
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-03-11 13:12:28 +00:00
|
|
|
from orm.community import Community
|
2023-11-28 09:11:45 +00:00
|
|
|
from orm.reaction import Reaction
|
2024-02-23 16:35:40 +00:00
|
|
|
from orm.shout import Shout, ShoutReactionsFollower
|
2023-11-28 07:53:48 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
2024-04-24 07:42:33 +00:00
|
|
|
from resolvers.stat import author_follows_authors, author_follows_topics, get_with_stat
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.auth import login_required
|
2024-05-05 15:46:16 +00:00
|
|
|
from services.cache import DEFAULT_FOLLOWS, cache_author, cache_topic
|
2023-10-23 14:47:11 +00:00
|
|
|
from services.db import local_session
|
2024-02-20 16:19:46 +00:00
|
|
|
from services.logger import root_logger as logger
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.notify import notify_follower
|
2024-02-21 07:27:16 +00:00
|
|
|
from services.rediscache import redis
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.schema import mutation, query
|
2024-01-13 08:49:12 +00:00
|
|
|
|
2024-01-22 23:28:54 +00:00
|
|
|
|
2024-05-03 11:12:57 +00:00
|
|
|
async def cache_by_slug(what: str, slug: str):
|
|
|
|
is_author = what == "AUTHOR"
|
|
|
|
alias = Author if is_author else Topic
|
|
|
|
q = select(alias).filter(alias.slug == slug)
|
|
|
|
[x] = get_with_stat(q)
|
|
|
|
if not x:
|
|
|
|
return
|
|
|
|
|
|
|
|
d = x.dict() # convert object to dictionary
|
2024-05-05 15:46:16 +00:00
|
|
|
if is_author:
|
|
|
|
await cache_author(d)
|
|
|
|
else:
|
|
|
|
await cache_topic(d)
|
2024-05-03 11:12:57 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +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):
|
2024-03-11 13:12:28 +00:00
|
|
|
error = None
|
2024-04-17 15:32:23 +00:00
|
|
|
user_id = info.context.get("user_id")
|
2024-04-19 15:22:07 +00:00
|
|
|
follower_dict = info.context["author"]
|
|
|
|
follower_id = follower_dict.get("id")
|
|
|
|
if not user_id or not follower_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "unauthorized"}
|
2024-04-09 08:17:32 +00:00
|
|
|
|
2024-04-18 09:34:04 +00:00
|
|
|
entity = what.lower()
|
|
|
|
follows = []
|
2024-04-19 15:22:07 +00:00
|
|
|
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
|
2024-04-18 09:34:04 +00:00
|
|
|
if isinstance(follows_str, str):
|
2024-05-05 17:16:45 +00:00
|
|
|
follows = json.loads(follows_str) or []
|
2024-04-18 09:34:04 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
if what == "AUTHOR":
|
2024-04-19 15:22:07 +00:00
|
|
|
follower_id = int(follower_id)
|
|
|
|
error = author_follow(follower_id, slug)
|
2024-03-11 13:52:16 +00:00
|
|
|
if not error:
|
2024-05-05 18:04:38 +00:00
|
|
|
[author] = get_with_stat(select(Author).filter(Author.slug == slug))
|
|
|
|
if author:
|
|
|
|
author_dict = author.dict()
|
2024-05-06 07:25:09 +00:00
|
|
|
author_id = int(author_dict.get("id", 0))
|
|
|
|
follows_ids = set(int(a.get("id")) for a in follows)
|
2024-05-05 18:04:38 +00:00
|
|
|
if author_id not in follows_ids:
|
|
|
|
await cache_author(author_dict)
|
|
|
|
await cache_author(follower_dict)
|
|
|
|
await notify_follower(follower_dict, author_id, "follow")
|
|
|
|
follows.append(author_dict)
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "TOPIC":
|
2024-04-19 15:22:07 +00:00
|
|
|
error = topic_follow(follower_id, slug)
|
2024-05-03 11:12:57 +00:00
|
|
|
_topic_dict = await cache_by_slug(what, slug)
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "COMMUNITY":
|
2024-04-09 08:17:32 +00:00
|
|
|
# FIXME: when more communities
|
2024-03-11 13:12:28 +00:00
|
|
|
follows = local_session().execute(select(Community))
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "SHOUT":
|
2024-04-19 15:22:07 +00:00
|
|
|
error = reactions_follow(follower_id, slug)
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-09 08:17:32 +00:00
|
|
|
if error:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": error}
|
2024-04-09 08:17:32 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
return {f"{entity}s": follows}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +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-03-11 13:12:28 +00:00
|
|
|
follows = []
|
|
|
|
error = None
|
2024-04-17 15:32:23 +00:00
|
|
|
user_id = info.context.get("user_id")
|
2024-04-19 15:22:07 +00:00
|
|
|
follower_dict = info.context["author"]
|
|
|
|
follower_id = follower_dict.get("id")
|
2024-03-11 13:12:28 +00:00
|
|
|
if not user_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "unauthorized"}
|
2024-04-18 09:34:04 +00:00
|
|
|
|
2024-04-19 15:22:07 +00:00
|
|
|
if not follower_id:
|
2024-04-18 09:34:04 +00:00
|
|
|
return {"error": "cant find follower account"}
|
|
|
|
|
2024-05-03 11:12:57 +00:00
|
|
|
entity = what.lower()
|
|
|
|
follows = []
|
|
|
|
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
|
|
|
|
if isinstance(follows_str, str):
|
2024-05-05 17:16:45 +00:00
|
|
|
follows = json.loads(follows_str) or []
|
2024-05-03 11:12:57 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
if what == "AUTHOR":
|
2024-04-19 15:22:07 +00:00
|
|
|
error = author_unfollow(follower_id, slug)
|
2024-04-09 08:17:32 +00:00
|
|
|
# NOTE: after triggers should update cached stats
|
2024-03-11 13:52:16 +00:00
|
|
|
if not error:
|
2024-04-19 15:22:07 +00:00
|
|
|
logger.info(f"@{follower_dict.get('slug')} unfollowed @{slug}")
|
2024-05-05 18:04:38 +00:00
|
|
|
[author] = get_with_stat(select(Author).filter(Author.slug == slug))
|
|
|
|
if author:
|
|
|
|
author_dict = author.dict()
|
|
|
|
author_id = author.id
|
|
|
|
await cache_author(author_dict)
|
|
|
|
for idx, item in enumerate(follows):
|
|
|
|
if item["id"] == author_id:
|
|
|
|
await cache_author(follower_dict)
|
|
|
|
await notify_follower(follower_dict, author_id, "unfollow")
|
|
|
|
follows.pop(idx)
|
|
|
|
break
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "TOPIC":
|
2024-04-19 15:22:07 +00:00
|
|
|
error = topic_unfollow(follower_id, slug)
|
2024-05-03 11:12:57 +00:00
|
|
|
_topic_dict = await cache_by_slug(what, slug)
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "COMMUNITY":
|
2024-03-11 13:12:28 +00:00
|
|
|
follows = local_session().execute(select(Community))
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
elif what == "SHOUT":
|
2024-04-19 15:22:07 +00:00
|
|
|
error = reactions_unfollow(follower_id, slug)
|
2024-03-11 13:12:28 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": error, f"{entity}s": follows}
|
2023-11-28 07:53:48 +00:00
|
|
|
|
|
|
|
|
2024-02-21 07:27:16 +00:00
|
|
|
async def get_follows_by_user_id(user_id: str):
|
2024-03-11 08:56:14 +00:00
|
|
|
if not user_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "unauthorized"}
|
|
|
|
author = await redis.execute("GET", f"user:{user_id}")
|
2024-03-11 08:56:14 +00:00
|
|
|
if isinstance(author, str):
|
|
|
|
author = json.loads(author)
|
|
|
|
if not author:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if not author:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "cant find author"}
|
2024-03-11 08:56:14 +00:00
|
|
|
author = author.dict()
|
2024-04-17 15:32:23 +00:00
|
|
|
last_seen = author.get("last_seen", 0) if isinstance(author, dict) else 0
|
2024-03-11 08:56:14 +00:00
|
|
|
follows = DEFAULT_FOLLOWS
|
|
|
|
day_old = int(time.time()) - last_seen > 24 * 60 * 60
|
|
|
|
if day_old:
|
2024-04-17 15:32:23 +00:00
|
|
|
author_id = json.loads(str(author)).get("id")
|
2024-03-11 08:56:14 +00:00
|
|
|
if author_id:
|
|
|
|
topics = author_follows_topics(author_id)
|
|
|
|
authors = author_follows_authors(author_id)
|
|
|
|
follows = {
|
2024-04-17 15:32:23 +00:00
|
|
|
"topics": topics,
|
|
|
|
"authors": authors,
|
|
|
|
"communities": [
|
|
|
|
{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}
|
2024-03-11 08:56:14 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
else:
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"getting follows for {user_id} from redis")
|
|
|
|
res = await redis.execute("GET", f"user:{user_id}:follows")
|
2024-03-11 08:56:14 +00:00
|
|
|
if isinstance(res, str):
|
|
|
|
follows = json.loads(res)
|
|
|
|
return follows
|
2024-02-02 12:03:44 +00:00
|
|
|
|
|
|
|
|
2024-03-12 07:35:33 +00:00
|
|
|
def topic_follow(follower_id, slug):
|
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
topic = session.query(Topic).where(Topic.slug == slug).one()
|
|
|
|
_following = TopicFollower(topic=topic.id, follower=follower_id)
|
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant follow"
|
2024-03-12 07:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def topic_unfollow(follower_id, slug):
|
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
sub = (
|
|
|
|
session.query(TopicFollower)
|
|
|
|
.join(Topic)
|
|
|
|
.filter(and_(TopicFollower.follower == follower_id, Topic.slug == slug))
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if sub:
|
|
|
|
session.delete(sub)
|
|
|
|
session.commit()
|
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant unfollow"
|
2024-03-12 07:35:33 +00:00
|
|
|
|
|
|
|
|
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()
|
2024-03-11 13:52:16 +00:00
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant follow"
|
2024-02-02 12:03:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2024-03-11 13:52:16 +00:00
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant unfollow"
|
2024-02-02 12:03:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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()
|
2024-03-11 13:52:16 +00:00
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant follow"
|
2024-02-02 12:03:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# for mutation.field("unfollow")
|
|
|
|
def author_unfollow(follower_id, slug):
|
2024-03-11 13:52:16 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
flw = (
|
|
|
|
session.query(AuthorFollower)
|
|
|
|
.join(Author, Author.id == AuthorFollower.author)
|
2024-03-28 12:56:32 +00:00
|
|
|
.filter(
|
|
|
|
and_(AuthorFollower.follower == follower_id, Author.slug == slug)
|
|
|
|
)
|
2024-03-11 13:52:16 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if flw:
|
|
|
|
session.delete(flw)
|
|
|
|
session.commit()
|
|
|
|
return None
|
2024-05-05 17:44:57 +00:00
|
|
|
except Exception as error:
|
2024-03-13 12:34:17 +00:00
|
|
|
logger.warn(error)
|
2024-05-05 17:44:57 +00:00
|
|
|
return "cant unfollow"
|
2024-02-21 08:52:57 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_topic_followers")
|
2024-03-12 14:26:52 +00:00
|
|
|
async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
|
2024-02-21 08:52:57 +00:00
|
|
|
q = select(Author)
|
|
|
|
q = (
|
2024-03-12 11:59:36 +00:00
|
|
|
q.join(TopicFollower, TopicFollower.follower == Author.id)
|
|
|
|
.join(Topic, Topic.id == TopicFollower.topic)
|
2024-02-21 08:52:57 +00:00
|
|
|
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
|
|
|
|
)
|
2024-04-09 08:17:32 +00:00
|
|
|
return get_with_stat(q)
|
2024-02-21 08:52:57 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_shout_followers")
|
2024-02-21 08:52:57 +00:00
|
|
|
def get_shout_followers(
|
2024-04-17 15:32:23 +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
|