core/resolvers/author.py

242 lines
8.0 KiB
Python
Raw Normal View History

2023-11-03 10:10:22 +00:00
import time
2023-12-17 20:30:20 +00:00
2024-05-30 09:49:46 +00:00
from sqlalchemy import desc, select, text
2023-10-23 14:47:11 +00:00
2024-05-20 22:40:57 +00:00
from orm.author import Author
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-05-20 22:40:57 +00:00
from resolvers.stat import get_with_stat
2023-12-17 20:30:20 +00:00
from services.auth import login_required
2024-05-20 22:40:57 +00:00
from services.cache import (
cache_author,
get_cached_author,
2024-06-06 08:13:54 +00:00
get_cached_author_by_user_id,
2024-05-20 22:40:57 +00:00
get_cached_author_followers,
2024-05-30 16:42:38 +00:00
get_cached_follower_authors,
get_cached_follower_topics,
2024-05-20 22:40:57 +00:00
)
2023-12-17 20:30:20 +00:00
from services.db import local_session
2024-04-08 07:38:58 +00:00
from services.logger import root_logger as logger
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-05-18 11:15:05 +00:00
author_query = select(Author).where(Author.user == user_id)
[author] = get_with_stat(author_query)
if author:
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):
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-05-30 09:49:46 +00:00
author_id = get_author_id_from(slug=slug, user="", author_id=author_id)
if not author_id:
raise ValueError("cant find")
author_dict = await get_cached_author(int(author_id), get_with_stat)
2024-05-25 23:17:45 +00:00
2024-04-17 15:32:23 +00:00
if not author_dict or not author_dict.get("stat"):
2024-05-25 23:17:45 +00:00
# update stat from db
2024-05-30 09:49:46 +00:00
author_query = select(Author).filter(Author.id == author_id)
[author] = get_with_stat(author_query)
2024-04-09 08:17:32 +00:00
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-06-05 20:15:19 +00:00
@query.field("get_author_id")
async def get_author_id(_, _info, user: str):
user_id = user.strip()
2024-04-17 15:32:23 +00:00
logger.info(f"getting author id for {user_id}")
2024-02-24 10:22:35 +00:00
author = None
try:
2024-05-20 23:01:18 +00:00
author = await get_cached_author_by_user_id(user_id, get_with_stat)
2024-05-20 22:40:57 +00:00
if author:
return author
2024-02-24 10:22:35 +00:00
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-06-05 18:04:48 +00:00
if author:
await cache_author(author.dict())
2024-06-05 20:15:19 +00:00
return author
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)
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}")
2024-05-06 17:00:26 +00:00
authors_query = select(Author)
2024-06-06 11:16:16 +00:00
2024-04-24 07:42:09 +00:00
if by.get("slug"):
2024-05-06 17:00:26 +00:00
authors_query = authors_query.filter(Author.slug.ilike(f"%{by['slug']}%"))
2024-04-24 07:42:09 +00:00
elif by.get("name"):
2024-05-06 17:00:26 +00:00
authors_query = authors_query.filter(Author.name.ilike(f"%{by['name']}%"))
2024-04-24 07:42:09 +00:00
elif by.get("topic"):
2024-05-06 17:00:26 +00:00
authors_query = (
2024-06-11 11:46:10 +00:00
authors_query.join(ShoutAuthor) # Первое соединение ShoutAuthor
2024-06-06 09:37:55 +00:00
.join(ShoutTopic, ShoutAuthor.shout == ShoutTopic.shout)
2024-06-06 09:33:58 +00:00
.join(Topic, ShoutTopic.topic == Topic.id)
2024-06-06 09:23:47 +00:00
.filter(Topic.slug == str(by["topic"]))
2024-04-24 07:42:09 +00:00
)
2024-03-12 13:18:07 +00:00
2024-06-06 11:16:16 +00:00
if by.get("last_seen"): # в unix time
2024-04-24 07:42:09 +00:00
before = int(time.time()) - by["last_seen"]
2024-05-06 17:00:26 +00:00
authors_query = authors_query.filter(Author.last_seen > before)
2024-06-06 11:16:16 +00:00
elif by.get("created_at"): # в unix time
2024-04-24 07:42:09 +00:00
before = int(time.time()) - by["created_at"]
2024-05-06 17:00:26 +00:00
authors_query = authors_query.filter(Author.created_at > before)
2024-05-30 09:49:46 +00:00
2024-05-06 17:00:26 +00:00
authors_query = authors_query.limit(limit).offset(offset)
2024-06-06 11:16:16 +00:00
2024-05-25 23:17:45 +00:00
with local_session() as session:
2024-06-06 11:16:16 +00:00
authors_nostat = session.execute(authors_query).all()
authors = []
for a in authors_nostat:
if isinstance(a, Author):
author_dict = await get_cached_author(a.id, get_with_stat)
if author_dict and isinstance(author_dict.get("shouts"), int):
authors.append(author_dict)
2024-05-06 17:00:26 +00:00
# order
order = by.get("order")
if order in ["shouts", "followers"]:
authors_query = authors_query.order_by(desc(text(f"{order}_stat")))
2024-06-06 11:16:16 +00:00
2024-05-06 17:00:26 +00:00
# group by
2024-05-06 17:04:50 +00:00
authors = get_with_stat(authors_query)
return authors or []
2024-03-12 13:18:07 +00:00
2024-05-30 04:12:00 +00:00
def get_author_id_from(slug="", user=None, author_id=None):
if not slug and not user and not author_id:
raise ValueError("One of slug, user, or author_id must be provided")
author_query = select(Author.id)
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)
with local_session() as session:
author_id_result = session.execute(author_query).first()
author_id = author_id_result[0] if author_id_result else None
if not author_id:
raise ValueError("Author not found")
return author_id
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:
2024-05-30 04:12:00 +00:00
author_id = get_author_id_from(slug, user, author_id)
2024-05-30 09:49:46 +00:00
if bool(author_id):
logger.debug(f"getting {author_id} follows authors")
2024-05-30 16:42:38 +00:00
authors = await get_cached_follower_authors(author_id)
topics = await get_cached_follower_topics(author_id)
2024-05-30 04:12:00 +00:00
return {
"topics": topics,
"authors": authors,
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}],
}
2024-03-28 11:09:11 +00:00
except Exception:
import traceback
traceback.print_exc()
2024-05-30 04:12:00 +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-05-30 04:12:00 +00:00
try:
2024-05-30 16:42:38 +00:00
follower_id = get_author_id_from(slug, user, author_id)
topics = await get_cached_follower_topics(follower_id)
2024-03-28 11:05:06 +00:00
return topics
2024-05-30 04:12:00 +00:00
except Exception:
import traceback
traceback.print_exc()
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-05-30 04:12:00 +00:00
try:
2024-05-30 16:42:38 +00:00
follower_id = get_author_id_from(slug, user, author_id)
return await get_cached_follower_authors(follower_id)
2024-05-30 04:12:00 +00:00
except Exception:
import traceback
traceback.print_exc()
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-05-30 09:49:46 +00:00
author_id = get_author_id_from(slug=slug, user="", author_id=0)
2024-05-21 01:25:40 +00:00
followers = []
if author_id:
2024-05-21 01:24:58 +00:00
followers = await get_cached_author_followers(author_id)
2024-05-20 22:40:57 +00:00
return followers