This commit is contained in:
@@ -2,7 +2,7 @@ from functools import wraps
|
||||
from aiohttp import ClientSession
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from services.core import get_all_authors, cached_authors
|
||||
from services.core import get_author
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
@@ -60,8 +60,7 @@ def login_required(f):
|
||||
user_id = await check_auth(req)
|
||||
if user_id:
|
||||
context["user_id"] = user_id
|
||||
authors_by_user, authors_by_id = get_all_authors() if not cached_authors else cached_authors
|
||||
author = authors_by_user.get(user_id)
|
||||
author = get_author(user_id)
|
||||
if author and "id" in author:
|
||||
context["author_id"] = author["id"]
|
||||
return await f(*args, **kwargs)
|
||||
|
@@ -29,15 +29,6 @@ def _request_endpoint(query_name, body) -> dict:
|
||||
|
||||
|
||||
def get_all_authors():
|
||||
global cached_authors, last_update_time
|
||||
|
||||
# Check if cached data is available and not expired
|
||||
if cached_authors is not None and (datetime.now() - last_update_time) < update_interval:
|
||||
print("[services.core] Returning cached authors data")
|
||||
return cached_authors
|
||||
|
||||
authors_by_user = {}
|
||||
authors_by_id = {}
|
||||
query_name = "get_authors_all"
|
||||
|
||||
gql = {
|
||||
@@ -45,18 +36,21 @@ def get_all_authors():
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
# Make a request to load authors
|
||||
authors = _request_endpoint(query_name, gql)
|
||||
return _request_endpoint(query_name, gql)
|
||||
|
||||
for a in list(authors):
|
||||
authors_by_user[a["user"]] = a
|
||||
authors_by_id[a["id"]] = a
|
||||
|
||||
# Cache the authors data and update the last update time
|
||||
cached_authors = authors_by_user, authors_by_id
|
||||
last_update_time = datetime.now()
|
||||
def get_author_by_user(user: str):
|
||||
operation = "GetAuthorId"
|
||||
query_name = "get_author_id"
|
||||
gql = {
|
||||
"query": f"query {operation}($user: String!) {{ {query_name}(user: $user){{ id }} }}",
|
||||
"operationName": operation,
|
||||
"variables": {"user": user},
|
||||
}
|
||||
|
||||
return authors_by_user, authors_by_id
|
||||
author = _request_endpoint(query_name, gql).popitem()
|
||||
|
||||
return author
|
||||
|
||||
|
||||
def get_my_followed() -> List[ChatMember]:
|
||||
@@ -67,6 +61,5 @@ def get_my_followed() -> List[ChatMember]:
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
return _request_endpoint(query_name, gql).get(
|
||||
"authors", []
|
||||
) # Ensure you're returning the correct part of the response
|
||||
result = _request_endpoint(query_name, gql)
|
||||
return result.get("authors", [])
|
||||
|
Reference in New Issue
Block a user