2024-02-21 14:37:58 +00:00
|
|
|
import json
|
2023-11-03 10:10:22 +00:00
|
|
|
import time
|
2023-12-17 20:30:20 +00:00
|
|
|
|
2024-04-08 07:38:58 +00:00
|
|
|
from sqlalchemy import and_, desc, or_, select, text
|
2023-10-23 14:47:11 +00:00
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
2024-02-24 18:45:38 +00:00
|
|
|
from orm.author import Author, AuthorFollower
|
2024-02-24 16:23:53 +00:00
|
|
|
from orm.shout import ShoutAuthor, ShoutTopic
|
2023-10-23 14:47:11 +00:00
|
|
|
from orm.topic import Topic
|
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-04-09 08:17:32 +00:00
|
|
|
from services.cache import cache_author, cache_follower
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.db import local_session
|
2024-03-06 18:57:04 +00:00
|
|
|
from services.encoders import CustomJSONEncoder
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.logger import root_logger as logger
|
2024-02-21 07:27:16 +00:00
|
|
|
from services.rediscache import redis
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.schema import mutation, query
|
2024-01-13 08:49:12 +00:00
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@mutation.field("update_author")
|
2023-10-23 14:47:11 +00:00
|
|
|
@login_required
|
2024-02-26 09:14:08 +00:00
|
|
|
async def update_author(_, info, profile):
|
2024-04-17 15:32:23 +00:00
|
|
|
user_id = info.context.get("user_id")
|
2024-03-18 12:01:10 +00:00
|
|
|
if not user_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "unauthorized", "author": None}
|
2024-03-18 12:01:10 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).where(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
Author.update(author, profile)
|
|
|
|
session.add(author)
|
|
|
|
session.commit()
|
2024-04-17 17:30:05 +00:00
|
|
|
[author] = get_with_stat(select(Author).where(Author.user == user_id))
|
|
|
|
await cache_author(author.dict())
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": None, "author": author}
|
2024-03-18 12:01:10 +00:00
|
|
|
except Exception as exc:
|
2024-03-18 12:01:43 +00:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
logger.error(traceback.format_exc())
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": exc, "author": None}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_authors_all")
|
2024-02-22 23:08:43 +00:00
|
|
|
def get_authors_all(_, _info):
|
2023-12-19 08:09:50 +00:00
|
|
|
with local_session() as session:
|
2024-02-03 14:31:00 +00:00
|
|
|
authors = session.query(Author).all()
|
2024-02-24 10:22:35 +00:00
|
|
|
return authors
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author")
|
|
|
|
async def get_author(_, _info, slug="", author_id=0):
|
|
|
|
author_query = ""
|
2024-03-28 17:36:35 +00:00
|
|
|
author = None
|
2024-03-14 06:59:38 +00:00
|
|
|
author_dict = None
|
2024-02-24 10:22:35 +00:00
|
|
|
try:
|
2024-04-09 08:17:32 +00:00
|
|
|
# lookup for cached author
|
2024-04-17 15:32:23 +00:00
|
|
|
author_query = select(Author).filter(
|
|
|
|
or_(Author.slug == slug, Author.id == author_id)
|
|
|
|
)
|
2024-04-18 09:34:04 +00:00
|
|
|
lookup_result = local_session().execute(author_query).first()
|
|
|
|
if lookup_result:
|
|
|
|
[found_author] = lookup_result
|
|
|
|
logger.debug(found_author)
|
|
|
|
if found_author:
|
|
|
|
logger.debug(f"found author id: {found_author.id}")
|
|
|
|
author_id = found_author.id if found_author.id else author_id
|
|
|
|
if author_id:
|
|
|
|
cached_result = await redis.execute("GET", f"author:{author_id}")
|
|
|
|
if isinstance(cached_result, str):
|
|
|
|
author_dict = json.loads(cached_result)
|
2024-04-09 08:17:32 +00:00
|
|
|
|
|
|
|
# update stat from db
|
2024-04-17 15:32:23 +00:00
|
|
|
if not author_dict or not author_dict.get("stat"):
|
2024-04-09 08:17:32 +00:00
|
|
|
result = get_with_stat(author_query)
|
|
|
|
if not result:
|
2024-04-17 15:32:23 +00:00
|
|
|
raise ValueError("Author not found")
|
2024-04-09 08:17:32 +00:00
|
|
|
[author] = result
|
|
|
|
# use found author
|
|
|
|
if isinstance(author, Author):
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"update @{author.slug} with id {author.id}")
|
2024-04-09 08:17:32 +00:00
|
|
|
author_dict = author.dict()
|
|
|
|
await cache_author(author_dict)
|
2024-03-29 11:44:44 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2024-04-09 19:02:26 +00:00
|
|
|
except Exception as exc:
|
2024-02-25 23:07:46 +00:00
|
|
|
import traceback
|
2024-04-17 15:32:23 +00:00
|
|
|
|
|
|
|
logger.error(f"{exc}:\n{traceback.format_exc()}")
|
2024-03-28 17:36:35 +00:00
|
|
|
return author_dict
|
2023-12-27 22:37:54 +00:00
|
|
|
|
|
|
|
|
2024-02-25 08:27:08 +00:00
|
|
|
async def get_author_by_user_id(user_id: str):
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.info(f"getting author id for {user_id}")
|
|
|
|
redis_key = f"user:{user_id}"
|
2024-02-24 10:22:35 +00:00
|
|
|
author = None
|
|
|
|
try:
|
2024-04-17 15:32:23 +00:00
|
|
|
res = await redis.execute("GET", redis_key)
|
2024-02-24 10:22:35 +00:00
|
|
|
if isinstance(res, str):
|
|
|
|
author = json.loads(res)
|
2024-04-17 15:32:23 +00:00
|
|
|
author_id = author.get("id")
|
|
|
|
author_slug = author.get("slug")
|
2024-02-29 10:04:25 +00:00
|
|
|
if author_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"got author @{author_slug} #{author_id} cached")
|
2024-02-24 10:22:35 +00:00
|
|
|
return author
|
|
|
|
|
2024-04-09 08:17:32 +00:00
|
|
|
author_query = select(Author).filter(Author.user == user_id)
|
|
|
|
result = get_with_stat(author_query)
|
2024-03-04 17:34:11 +00:00
|
|
|
if result:
|
|
|
|
[author] = result
|
2024-04-09 08:17:32 +00:00
|
|
|
await cache_author(author.dict())
|
2024-02-24 10:22:35 +00:00
|
|
|
except Exception as exc:
|
2024-03-04 17:25:47 +00:00
|
|
|
import traceback
|
2024-03-06 09:25:55 +00:00
|
|
|
|
2024-03-04 17:25:47 +00:00
|
|
|
traceback.print_exc()
|
2024-02-24 10:22:35 +00:00
|
|
|
logger.error(exc)
|
|
|
|
return author
|
2024-02-21 07:27:16 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author_id")
|
2023-12-27 22:37:54 +00:00
|
|
|
async def get_author_id(_, _info, user: str):
|
2024-02-25 08:27:08 +00:00
|
|
|
return await get_author_by_user_id(user)
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("load_authors_by")
|
2024-04-30 09:35:51 +00:00
|
|
|
async def load_authors_by(_, _info, by, limit, offset):
|
2024-04-24 07:42:09 +00:00
|
|
|
logger.debug(f"loading authors by {by}")
|
|
|
|
q = select(Author)
|
|
|
|
if by.get("slug"):
|
|
|
|
q = q.filter(Author.slug.ilike(f"%{by['slug']}%"))
|
|
|
|
elif by.get("name"):
|
|
|
|
q = q.filter(Author.name.ilike(f"%{by['name']}%"))
|
|
|
|
elif by.get("topic"):
|
|
|
|
q = (
|
|
|
|
q.join(ShoutAuthor)
|
|
|
|
.join(ShoutTopic)
|
|
|
|
.join(Topic)
|
|
|
|
.where(Topic.slug == str(by["topic"]))
|
|
|
|
)
|
2024-03-12 13:18:07 +00:00
|
|
|
|
2024-04-24 07:42:09 +00:00
|
|
|
if by.get("last_seen"): # in unix time
|
|
|
|
before = int(time.time()) - by["last_seen"]
|
|
|
|
q = q.filter(Author.last_seen > before)
|
|
|
|
elif by.get("created_at"): # in unix time
|
|
|
|
before = int(time.time()) - by["created_at"]
|
|
|
|
q = q.filter(Author.created_at > before)
|
2024-03-12 13:18:07 +00:00
|
|
|
|
2024-04-24 07:42:09 +00:00
|
|
|
order = by.get("order")
|
2024-04-30 09:33:41 +00:00
|
|
|
if order in ["shouts", "followers"]:
|
2024-04-24 07:42:09 +00:00
|
|
|
q = q.order_by(desc(text(f"{order}_stat")))
|
2024-03-12 13:18:07 +00:00
|
|
|
|
2024-04-24 07:42:09 +00:00
|
|
|
q = q.limit(limit).offset(offset)
|
2024-03-12 13:18:07 +00:00
|
|
|
|
2024-05-04 21:00:58 +00:00
|
|
|
authors_nostat = local_session().execute(q)
|
2024-04-30 09:33:41 +00:00
|
|
|
authors = []
|
|
|
|
if authors_nostat:
|
|
|
|
for [a] in authors_nostat:
|
|
|
|
if isinstance(a, Author):
|
|
|
|
author_id = a.id
|
|
|
|
if author_id:
|
|
|
|
cached_result = await redis.execute("GET", f"author:{author_id}")
|
|
|
|
if isinstance(cached_result, str):
|
|
|
|
author_dict = json.loads(cached_result)
|
|
|
|
authors.append(author_dict)
|
2024-03-12 13:18:07 +00:00
|
|
|
|
2024-04-24 07:42:09 +00:00
|
|
|
return authors
|
2024-03-12 13:18:07 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author_follows")
|
|
|
|
async def get_author_follows(_, _info, slug="", user=None, author_id=0):
|
2024-03-28 11:09:11 +00:00
|
|
|
try:
|
|
|
|
author_query = select(Author)
|
|
|
|
if user:
|
|
|
|
author_query = author_query.filter(Author.user == user)
|
|
|
|
elif slug:
|
|
|
|
author_query = author_query.filter(Author.slug == slug)
|
|
|
|
elif author_id:
|
|
|
|
author_query = author_query.filter(Author.id == author_id)
|
|
|
|
else:
|
2024-04-17 15:32:23 +00:00
|
|
|
raise ValueError("One of slug, user, or author_id must be provided")
|
2024-03-28 12:48:58 +00:00
|
|
|
[result] = local_session().execute(author_query)
|
|
|
|
if len(result) > 0:
|
2024-03-28 12:56:32 +00:00
|
|
|
# logger.debug(result)
|
2024-03-28 12:48:58 +00:00
|
|
|
[author] = result
|
2024-03-28 12:56:32 +00:00
|
|
|
# logger.debug(author)
|
2024-03-28 12:48:58 +00:00
|
|
|
if author and isinstance(author, Author):
|
2024-03-28 13:01:48 +00:00
|
|
|
# logger.debug(author.dict())
|
2024-04-18 09:34:04 +00:00
|
|
|
author_id = author.id if not author_id else author_id
|
2024-04-09 15:06:29 +00:00
|
|
|
topics = []
|
2024-04-24 07:42:09 +00:00
|
|
|
authors = []
|
|
|
|
if author_id:
|
|
|
|
rkey = f"author:{author_id}:follows-authors"
|
|
|
|
logger.debug(f"getting {author_id} follows authors")
|
|
|
|
cached = await redis.execute("GET", rkey)
|
|
|
|
if not cached:
|
|
|
|
authors = author_follows_authors(author_id)
|
|
|
|
prepared = [author.dict() for author in authors]
|
|
|
|
await redis.execute(
|
|
|
|
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
|
|
|
|
)
|
|
|
|
elif isinstance(cached, str):
|
|
|
|
authors = json.loads(cached)
|
|
|
|
|
|
|
|
rkey = f"author:{author_id}:follows-topics"
|
|
|
|
cached = await redis.execute("GET", rkey)
|
|
|
|
if cached and isinstance(cached, str):
|
|
|
|
topics = json.loads(cached)
|
|
|
|
if not cached:
|
|
|
|
topics = author_follows_topics(author_id)
|
|
|
|
prepared = [topic.dict() for topic in topics]
|
|
|
|
await redis.execute(
|
|
|
|
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
|
|
|
|
)
|
2024-03-28 12:48:58 +00:00
|
|
|
return {
|
2024-04-17 15:32:23 +00:00
|
|
|
"topics": topics,
|
|
|
|
"authors": authors,
|
|
|
|
"communities": [
|
|
|
|
{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}
|
2024-03-28 12:48:58 +00:00
|
|
|
],
|
|
|
|
}
|
2024-03-28 11:09:11 +00:00
|
|
|
except Exception:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
traceback.print_exc()
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": "Author not found"}
|
2024-02-23 18:10:11 +00:00
|
|
|
|
2024-02-24 10:22:35 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author_follows_topics")
|
|
|
|
async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None):
|
2024-02-23 18:10:11 +00:00
|
|
|
with local_session() as session:
|
|
|
|
if user or slug:
|
2024-02-24 18:45:38 +00:00
|
|
|
author_id_result = (
|
|
|
|
session.query(Author.id)
|
|
|
|
.filter(or_(Author.user == user, Author.slug == slug))
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-23 18:10:11 +00:00
|
|
|
author_id = author_id_result[0] if author_id_result else None
|
2024-03-28 11:05:06 +00:00
|
|
|
if not author_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
raise ValueError("Author not found")
|
|
|
|
logger.debug(f"getting {author_id} follows topics")
|
|
|
|
rkey = f"author:{author_id}:follows-topics"
|
|
|
|
cached = await redis.execute("GET", rkey)
|
2024-03-28 11:05:06 +00:00
|
|
|
topics = []
|
|
|
|
if isinstance(cached, str):
|
|
|
|
topics = json.loads(cached)
|
|
|
|
if not cached:
|
|
|
|
topics = author_follows_topics(author_id)
|
|
|
|
prepared = [topic.dict() for topic in topics]
|
2024-03-28 12:56:32 +00:00
|
|
|
await redis.execute(
|
2024-04-17 15:32:23 +00:00
|
|
|
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
|
2024-03-28 12:56:32 +00:00
|
|
|
)
|
2024-03-28 11:05:06 +00:00
|
|
|
return topics
|
2024-02-23 18:10:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author_follows_authors")
|
|
|
|
async def get_author_follows_authors(_, _info, slug="", user=None, author_id=None):
|
2024-02-23 18:10:11 +00:00
|
|
|
with local_session() as session:
|
|
|
|
if user or slug:
|
2024-02-24 18:45:38 +00:00
|
|
|
author_id_result = (
|
|
|
|
session.query(Author.id)
|
|
|
|
.filter(or_(Author.user == user, Author.slug == slug))
|
|
|
|
.first()
|
|
|
|
)
|
2024-02-23 18:10:11 +00:00
|
|
|
author_id = author_id_result[0] if author_id_result else None
|
|
|
|
if author_id:
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"getting {author_id} follows authors")
|
|
|
|
rkey = f"author:{author_id}:follows-authors"
|
|
|
|
cached = await redis.execute("GET", rkey)
|
2024-03-28 11:05:06 +00:00
|
|
|
authors = []
|
|
|
|
if isinstance(cached, str):
|
|
|
|
authors = json.loads(cached)
|
|
|
|
if not authors:
|
|
|
|
authors = author_follows_authors(author_id)
|
2024-02-26 02:36:18 +00:00
|
|
|
prepared = [author.dict() for author in authors]
|
2024-03-28 12:56:32 +00:00
|
|
|
await redis.execute(
|
2024-04-17 15:32:23 +00:00
|
|
|
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
|
2024-03-28 12:56:32 +00:00
|
|
|
)
|
2024-02-26 01:58:27 +00:00
|
|
|
return authors
|
2024-02-21 16:14:58 +00:00
|
|
|
else:
|
2024-04-17 15:32:23 +00:00
|
|
|
raise ValueError("Author not found")
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
def create_author(user_id: str, slug: str, name: str = ""):
|
2023-11-28 19:07:53 +00:00
|
|
|
with local_session() as session:
|
2024-03-03 13:59:15 +00:00
|
|
|
try:
|
|
|
|
author = None
|
|
|
|
if user_id:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
elif slug:
|
|
|
|
author = session.query(Author).filter(Author.slug == slug).first()
|
|
|
|
if not author:
|
|
|
|
new_author = Author(user=user_id, slug=slug, name=name)
|
|
|
|
session.add(new_author)
|
|
|
|
session.commit()
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.info(f"author created by webhook {new_author.dict()}")
|
2024-03-03 13:59:15 +00:00
|
|
|
except Exception as exc:
|
|
|
|
logger.debug(exc)
|
2024-02-21 09:34:12 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@query.field("get_author_followers")
|
2024-02-27 12:40:53 +00:00
|
|
|
async def get_author_followers(_, _info, slug: str):
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"getting followers for @{slug}")
|
2024-02-25 08:27:08 +00:00
|
|
|
try:
|
2024-04-09 08:17:32 +00:00
|
|
|
author_alias = aliased(Author)
|
|
|
|
author_query = select(author_alias).filter(author_alias.slug == slug)
|
|
|
|
result = local_session().execute(author_query).first()
|
|
|
|
if result:
|
|
|
|
[author] = result
|
|
|
|
author_id = author.id
|
2024-04-17 15:32:23 +00:00
|
|
|
cached = await redis.execute("GET", f"author:{author_id}:followers")
|
2024-04-09 08:17:32 +00:00
|
|
|
if not cached:
|
2024-04-17 15:32:23 +00:00
|
|
|
author_follower_alias = aliased(AuthorFollower, name="af")
|
2024-04-09 08:17:32 +00:00
|
|
|
q = select(Author).join(
|
|
|
|
author_follower_alias,
|
|
|
|
and_(
|
|
|
|
author_follower_alias.author == author_id,
|
|
|
|
author_follower_alias.follower == Author.id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
results = get_with_stat(q)
|
|
|
|
if isinstance(results, list):
|
|
|
|
for follower in results:
|
2024-04-26 08:21:00 +00:00
|
|
|
await cache_follower(follower.dict(), author.dict())
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"@{slug} cache updated with {len(results)} followers")
|
2024-04-09 08:17:32 +00:00
|
|
|
return results
|
|
|
|
else:
|
2024-04-17 15:32:23 +00:00
|
|
|
logger.debug(f"@{slug} got followers cached")
|
2024-04-09 08:17:32 +00:00
|
|
|
if isinstance(cached, str):
|
|
|
|
return json.loads(cached)
|
2024-02-25 08:27:08 +00:00
|
|
|
except Exception as exc:
|
2024-02-27 13:41:09 +00:00
|
|
|
import traceback
|
2024-03-06 09:25:55 +00:00
|
|
|
|
2024-02-25 08:27:08 +00:00
|
|
|
logger.error(exc)
|
2024-02-27 13:41:09 +00:00
|
|
|
logger.error(traceback.format_exc())
|
2024-02-25 08:27:08 +00:00
|
|
|
return []
|