Files
core/resolvers/author.py

241 lines
8.0 KiB
Python
Raw Normal View History

2023-11-03 13:10:22 +03:00
import time
2023-12-17 23:30:20 +03:00
2024-05-30 12:49:46 +03:00
from sqlalchemy import desc, select, text
2023-10-23 17:47:11 +03:00
2024-05-21 01:40:57 +03:00
from orm.author import Author
2024-02-24 19:23:53 +03:00
from orm.shout import ShoutAuthor, ShoutTopic
2023-10-23 17:47:11 +03:00
from orm.topic import Topic
2024-05-21 01:40:57 +03:00
from resolvers.stat import get_with_stat
2023-12-17 23:30:20 +03:00
from services.auth import login_required
2024-05-21 01:40:57 +03:00
from services.cache import (
cache_author,
get_cached_author,
get_cached_author_by_user_id,
get_cached_author_followers,
get_cached_author_follows_authors,
get_cached_author_follows_topics,
)
2023-12-17 23:30:20 +03:00
from services.db import local_session
2024-04-08 10:38:58 +03:00
from services.logger import root_logger as logger
2023-12-17 23:30:20 +03:00
from services.schema import mutation, query
2024-01-13 11:49:12 +03:00
2023-10-23 17:47:11 +03:00
2024-04-17 18:32:23 +03:00
@mutation.field("update_author")
2023-10-23 17:47:11 +03:00
@login_required
2024-02-26 12:14:08 +03:00
async def update_author(_, info, profile):
2024-04-17 18:32:23 +03:00
user_id = info.context.get("user_id")
2024-03-18 15:01:10 +03:00
if not user_id:
2024-04-17 18:32:23 +03:00
return {"error": "unauthorized", "author": None}
2024-03-18 15:01:10 +03: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 14:15:05 +03: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 18:32:23 +03:00
return {"error": None, "author": author}
2024-03-18 15:01:10 +03:00
except Exception as exc:
2024-03-18 15:01:43 +03:00
import traceback
logger.error(traceback.format_exc())
2024-04-17 18:32:23 +03:00
return {"error": exc, "author": None}
2023-10-23 17:47:11 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_authors_all")
2024-02-23 02:08:43 +03:00
def get_authors_all(_, _info):
2023-12-19 11:09:50 +03:00
with local_session() as session:
2024-02-03 17:31:00 +03:00
authors = session.query(Author).all()
2024-02-24 13:22:35 +03:00
return authors
2023-10-23 17:47:11 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_author")
async def get_author(_, _info, slug="", author_id=0):
2024-03-28 20:36:35 +03:00
author = None
2024-03-14 09:59:38 +03:00
author_dict = None
2024-02-24 13:22:35 +03:00
try:
2024-05-30 12:49:46 +03: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-26 02:17:45 +03:00
2024-04-17 18:32:23 +03:00
if not author_dict or not author_dict.get("stat"):
2024-05-26 02:17:45 +03:00
# update stat from db
2024-05-30 12:49:46 +03:00
author_query = select(Author).filter(Author.id == author_id)
[author] = get_with_stat(author_query)
2024-04-09 11:17:32 +03:00
if isinstance(author, Author):
2024-04-17 18:32:23 +03:00
logger.debug(f"update @{author.slug} with id {author.id}")
2024-04-09 11:17:32 +03:00
author_dict = author.dict()
await cache_author(author_dict)
2024-03-29 14:44:44 +03:00
except ValueError:
pass
2024-04-09 22:02:26 +03:00
except Exception as exc:
2024-02-26 02:07:46 +03:00
import traceback
2024-04-17 18:32:23 +03:00
logger.error(f"{exc}:\n{traceback.format_exc()}")
2024-03-28 20:36:35 +03:00
return author_dict
2023-12-28 01:37:54 +03:00
2024-02-25 11:27:08 +03:00
async def get_author_by_user_id(user_id: str):
2024-04-17 18:32:23 +03:00
logger.info(f"getting author id for {user_id}")
2024-02-24 13:22:35 +03:00
author = None
try:
2024-05-21 02:01:18 +03:00
author = await get_cached_author_by_user_id(user_id, get_with_stat)
2024-05-21 01:40:57 +03:00
if author:
return author
2024-02-24 13:22:35 +03:00
2024-04-09 11:17:32 +03:00
author_query = select(Author).filter(Author.user == user_id)
result = get_with_stat(author_query)
2024-03-04 20:34:11 +03:00
if result:
[author] = result
2024-04-09 11:17:32 +03:00
await cache_author(author.dict())
2024-02-24 13:22:35 +03:00
except Exception as exc:
2024-03-04 20:25:47 +03:00
import traceback
2024-03-06 12:25:55 +03:00
2024-03-04 20:25:47 +03:00
traceback.print_exc()
2024-02-24 13:22:35 +03:00
logger.error(exc)
return author
2024-02-21 10:27:16 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_author_id")
2023-12-28 01:37:54 +03:00
async def get_author_id(_, _info, user: str):
2024-02-25 11:27:08 +03:00
return await get_author_by_user_id(user)
2023-10-23 17:47:11 +03:00
2024-04-17 18:32:23 +03:00
@query.field("load_authors_by")
2024-04-30 12:35:51 +03:00
async def load_authors_by(_, _info, by, limit, offset):
2024-04-24 10:42:09 +03:00
logger.debug(f"loading authors by {by}")
2024-05-06 20:00:26 +03:00
authors_query = select(Author)
2024-04-24 10:42:09 +03:00
if by.get("slug"):
2024-05-06 20:00:26 +03:00
authors_query = authors_query.filter(Author.slug.ilike(f"%{by['slug']}%"))
2024-04-24 10:42:09 +03:00
elif by.get("name"):
2024-05-06 20:00:26 +03:00
authors_query = authors_query.filter(Author.name.ilike(f"%{by['name']}%"))
2024-04-24 10:42:09 +03:00
elif by.get("topic"):
2024-05-06 20:00:26 +03:00
authors_query = (
2024-05-30 07:12:00 +03:00
authors_query.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == str(by["topic"]))
2024-04-24 10:42:09 +03:00
)
2024-03-12 16:18:07 +03:00
2024-04-24 10:42:09 +03:00
if by.get("last_seen"): # in unix time
before = int(time.time()) - by["last_seen"]
2024-05-06 20:00:26 +03:00
authors_query = authors_query.filter(Author.last_seen > before)
2024-04-24 10:42:09 +03:00
elif by.get("created_at"): # in unix time
before = int(time.time()) - by["created_at"]
2024-05-06 20:00:26 +03:00
authors_query = authors_query.filter(Author.created_at > before)
2024-05-30 12:49:46 +03:00
2024-04-30 12:33:41 +03:00
authors = []
2024-05-30 12:49:46 +03:00
2024-05-06 20:00:26 +03:00
authors_query = authors_query.limit(limit).offset(offset)
2024-05-26 02:17:45 +03:00
with local_session() as session:
authors_nostat = session.execute(authors_query)
if authors_nostat:
for [a] in authors_nostat:
author_dict = None
if isinstance(a, Author):
2024-05-30 12:49:46 +03:00
author_dict = await get_cached_author(a.id, get_with_stat)
2024-05-30 07:12:00 +03:00
if not author_dict or not isinstance(author_dict.get("shouts"), int):
2024-05-26 02:17:45 +03:00
break
2024-05-06 20:00:26 +03:00
# order
order = by.get("order")
if order in ["shouts", "followers"]:
authors_query = authors_query.order_by(desc(text(f"{order}_stat")))
# group by
2024-05-06 20:04:50 +03:00
authors = get_with_stat(authors_query)
return authors or []
2024-03-12 16:18:07 +03:00
2024-05-30 07:12:00 +03: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 18:32:23 +03:00
@query.field("get_author_follows")
async def get_author_follows(_, _info, slug="", user=None, author_id=0):
2024-03-28 14:09:11 +03:00
try:
2024-05-30 07:12:00 +03:00
author_id = get_author_id_from(slug, user, author_id)
2024-05-30 12:49:46 +03:00
if bool(author_id):
logger.debug(f"getting {author_id} follows authors")
authors = await get_cached_author_follows_authors(author_id)
topics = await get_cached_author_follows_topics(author_id)
2024-05-30 07:12:00 +03:00
return {
"topics": topics,
"authors": authors,
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}],
}
2024-03-28 14:09:11 +03:00
except Exception:
import traceback
traceback.print_exc()
2024-05-30 07:12:00 +03:00
return {"error": "Author not found"}
2024-02-23 21:10:11 +03:00
2024-02-24 13:22:35 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_author_follows_topics")
async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None):
2024-05-30 07:12:00 +03:00
try:
author_id = get_author_id_from(slug, user, author_id)
2024-05-30 12:49:46 +03:00
topics = await get_cached_author_follows_topics(author_id)
2024-03-28 14:05:06 +03:00
return topics
2024-05-30 07:12:00 +03:00
except Exception:
import traceback
traceback.print_exc()
2024-02-23 21:10:11 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_author_follows_authors")
async def get_author_follows_authors(_, _info, slug="", user=None, author_id=None):
2024-05-30 07:12:00 +03:00
try:
author_id = get_author_id_from(slug, user, author_id)
2024-05-21 01:40:57 +03:00
return await get_cached_author_follows_authors(author_id)
2024-05-30 07:12:00 +03:00
except Exception:
import traceback
traceback.print_exc()
2023-10-23 17:47:11 +03:00
2024-04-17 18:32:23 +03:00
def create_author(user_id: str, slug: str, name: str = ""):
2023-11-28 22:07:53 +03:00
with local_session() as session:
2024-03-03 16:59:15 +03: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 18:32:23 +03:00
logger.info(f"author created by webhook {new_author.dict()}")
2024-03-03 16:59:15 +03:00
except Exception as exc:
logger.debug(exc)
2024-02-21 12:34:12 +03:00
2024-04-17 18:32:23 +03:00
@query.field("get_author_followers")
2024-02-27 15:40:53 +03:00
async def get_author_followers(_, _info, slug: str):
2024-04-17 18:32:23 +03:00
logger.debug(f"getting followers for @{slug}")
2024-05-30 12:49:46 +03:00
author_id = get_author_id_from(slug=slug, user="", author_id=0)
2024-05-21 04:25:40 +03:00
followers = []
if author_id:
2024-05-21 04:24:58 +03:00
followers = await get_cached_author_followers(author_id)
2024-05-21 01:40:57 +03:00
return followers