This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import time
|
||||
|
||||
from sqlalchemy import desc, or_, select, text
|
||||
from sqlalchemy import desc, select, text
|
||||
|
||||
from orm.author import Author
|
||||
from orm.shout import ShoutAuthor, ShoutTopic
|
||||
@@ -54,34 +54,18 @@ def get_authors_all(_, _info):
|
||||
|
||||
@query.field("get_author")
|
||||
async def get_author(_, _info, slug="", author_id=0):
|
||||
author_query = ""
|
||||
author = None
|
||||
author_dict = None
|
||||
try:
|
||||
# lookup for cached author
|
||||
author_query = select(Author)
|
||||
if slug:
|
||||
author_query = author_query.filter(Author.slug == slug)
|
||||
elif author_id:
|
||||
author_query = author_query.filter(Author.id == author_id)
|
||||
else:
|
||||
raise ValueError("Author not found")
|
||||
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
|
||||
author_dict = await get_cached_author(int(author_id), get_with_stat)
|
||||
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)
|
||||
|
||||
# update stat from db
|
||||
if not author_dict or not author_dict.get("stat"):
|
||||
result = get_with_stat(author_query)
|
||||
if not result:
|
||||
raise ValueError("Author not found")
|
||||
[author] = result
|
||||
# use found author
|
||||
# update stat from db
|
||||
author_query = select(Author).filter(Author.id == author_id)
|
||||
[author] = get_with_stat(author_query)
|
||||
if isinstance(author, Author):
|
||||
logger.debug(f"update @{author.slug} with id {author.id}")
|
||||
author_dict = author.dict()
|
||||
@@ -131,10 +115,7 @@ async def load_authors_by(_, _info, by, limit, offset):
|
||||
authors_query = authors_query.filter(Author.name.ilike(f"%{by['name']}%"))
|
||||
elif by.get("topic"):
|
||||
authors_query = (
|
||||
authors_query.join(ShoutAuthor)
|
||||
.join(ShoutTopic)
|
||||
.join(Topic)
|
||||
.where(Topic.slug == str(by["topic"]))
|
||||
authors_query.join(ShoutAuthor).join(ShoutTopic).join(Topic).where(Topic.slug == str(by["topic"]))
|
||||
)
|
||||
|
||||
if by.get("last_seen"): # in unix time
|
||||
@@ -143,16 +124,19 @@ async def load_authors_by(_, _info, by, limit, offset):
|
||||
elif by.get("created_at"): # in unix time
|
||||
before = int(time.time()) - by["created_at"]
|
||||
authors_query = authors_query.filter(Author.created_at > before)
|
||||
authors_query = authors_query.limit(limit).offset(offset)
|
||||
authors_nostat = local_session().execute(authors_query)
|
||||
|
||||
authors = []
|
||||
if authors_nostat:
|
||||
for [a] in authors_nostat:
|
||||
author_dict = None
|
||||
if isinstance(a, Author):
|
||||
author_dict = await get_cached_author(a.id, get_with_stat)
|
||||
if not author_dict or not isinstance(author_dict.get("shouts"), int):
|
||||
break
|
||||
|
||||
authors_query = authors_query.limit(limit).offset(offset)
|
||||
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):
|
||||
author_dict = await get_cached_author(a.id, get_with_stat)
|
||||
if not author_dict or not isinstance(author_dict.get("shouts"), int):
|
||||
break
|
||||
|
||||
# order
|
||||
order = by.get("order")
|
||||
@@ -163,74 +147,70 @@ async def load_authors_by(_, _info, by, limit, offset):
|
||||
return authors or []
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@query.field("get_author_follows")
|
||||
async def get_author_follows(_, _info, slug="", user=None, author_id=0):
|
||||
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:
|
||||
return {"error": "One of slug, user, or author_id must be provided"}
|
||||
result = local_session().execute(author_query)
|
||||
if result:
|
||||
# logger.debug(result)
|
||||
[author] = result
|
||||
# logger.debug(author)
|
||||
if author and isinstance(author, Author):
|
||||
# logger.debug(author.dict())
|
||||
author_id = author.id if not author_id else author_id
|
||||
topics = []
|
||||
authors = []
|
||||
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)
|
||||
return {
|
||||
"topics": topics,
|
||||
"authors": authors,
|
||||
"communities": [
|
||||
{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}
|
||||
],
|
||||
}
|
||||
author_id = get_author_id_from(slug, user, author_id)
|
||||
|
||||
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)
|
||||
return {
|
||||
"topics": topics,
|
||||
"authors": authors,
|
||||
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}],
|
||||
}
|
||||
except Exception:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return {"error": "Author not found"}
|
||||
return {"error": "Author not found"}
|
||||
|
||||
|
||||
@query.field("get_author_follows_topics")
|
||||
async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None):
|
||||
with local_session() as session:
|
||||
if user or slug:
|
||||
author_id_result = (
|
||||
session.query(Author.id)
|
||||
.filter(or_(Author.user == user, Author.slug == slug))
|
||||
.first()
|
||||
)
|
||||
author_id = author_id_result[0] if author_id_result else None
|
||||
if not author_id:
|
||||
raise ValueError("Author not found")
|
||||
return get_cached_author_follows_topics(author_id)
|
||||
try:
|
||||
author_id = get_author_id_from(slug, user, author_id)
|
||||
topics = await get_cached_author_follows_topics(author_id)
|
||||
return topics
|
||||
except Exception:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
@query.field("get_author_follows_authors")
|
||||
async def get_author_follows_authors(_, _info, slug="", user=None, author_id=None):
|
||||
with local_session() as session:
|
||||
if user or slug:
|
||||
author_id_result = (
|
||||
session.query(Author.id)
|
||||
.filter(or_(Author.user == user, Author.slug == slug))
|
||||
.first()
|
||||
)
|
||||
author_id = author_id_result[0] if author_id_result else None
|
||||
if not author_id:
|
||||
raise ValueError("Author not found")
|
||||
try:
|
||||
author_id = get_author_id_from(slug, user, author_id)
|
||||
return await get_cached_author_follows_authors(author_id)
|
||||
except Exception:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
def create_author(user_id: str, slug: str, name: str = ""):
|
||||
@@ -253,8 +233,7 @@ def create_author(user_id: str, slug: str, name: str = ""):
|
||||
@query.field("get_author_followers")
|
||||
async def get_author_followers(_, _info, slug: str):
|
||||
logger.debug(f"getting followers for @{slug}")
|
||||
author_query = select(Author.id).filter(Author.slug == slug)
|
||||
author_id = local_session().execute(author_query).scalar()
|
||||
author_id = get_author_id_from(slug=slug, user="", author_id=0)
|
||||
followers = []
|
||||
if author_id:
|
||||
followers = await get_cached_author_followers(author_id)
|
||||
|
Reference in New Issue
Block a user