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 authors_by_user
|
||||
from services.core import get_all_authors
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
@@ -60,6 +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()
|
||||
author = authors_by_user.get(user_id)
|
||||
if author and "id" in author:
|
||||
context["author_id"] = author["id"]
|
||||
|
@@ -1,47 +1,51 @@
|
||||
from functools import lru_cache
|
||||
from typing import Any, List
|
||||
|
||||
import requests
|
||||
from models.member import ChatMember
|
||||
from settings import API_BASE
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
authors_by_user = {}
|
||||
authors_by_id = {}
|
||||
|
||||
|
||||
@lru_cache(maxsize=128, typed=True)
|
||||
def _request_endpoint(query_name, body) -> Any:
|
||||
response = requests.post(API_BASE, headers=headers, json=body)
|
||||
print(f"[services.core] requesting {query_name}...")
|
||||
response = requests.post(API_BASE, headers={"Content-Type": "application/json"}, json=body)
|
||||
print(f"[services.core] {query_name} response: <{response.status_code}> {response.text[:65]}..")
|
||||
|
||||
if response.status_code == 200:
|
||||
r = response.json()
|
||||
if r:
|
||||
return r.get("data", {}).get(query_name, {})
|
||||
try:
|
||||
r = response.json()
|
||||
result = r.get("data", {}).get(query_name, {})
|
||||
print(f"[services.core] entries amount in result: {len(result)} ")
|
||||
return result
|
||||
except ValueError as e:
|
||||
print(f"[services.core] Error decoding JSON response: {e}")
|
||||
|
||||
return []
|
||||
|
||||
|
||||
def get_all_authors() -> List[ChatMember]:
|
||||
if len(authors_by_user.keys()) == 0:
|
||||
print("[services.core] precaching authors...")
|
||||
query_name = "get_authors_all"
|
||||
def get_all_authors():
|
||||
authors_by_user = {}
|
||||
authors_by_id = {}
|
||||
query_name = "get_authors_all"
|
||||
|
||||
# Check if authors are already cached
|
||||
if "authors" in authors_by_user:
|
||||
return authors_by_user["authors"]
|
||||
# Check if authors are already cached
|
||||
if "authors" in authors_by_user:
|
||||
return authors_by_user["authors"]
|
||||
|
||||
gql = {
|
||||
"query": "query { " + query_name + "{ id slug pic name user } }",
|
||||
"variables": None,
|
||||
}
|
||||
gql = {
|
||||
"query": "query { " + query_name + "{ id slug pic name user } }",
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
# Make a request to load authors
|
||||
authors = _request_endpoint(query_name, gql)
|
||||
# Make a request to load authors
|
||||
authors = _request_endpoint(query_name, gql)
|
||||
|
||||
for a in list(authors):
|
||||
authors_by_user.__setitem__(a["user"], a)
|
||||
authors_by_id.__setitem__(a["id"], a)
|
||||
print(f"[services.core] {len(authors)} authors precached")
|
||||
for a in list(authors):
|
||||
authors_by_user[a["user"]] = a
|
||||
authors_by_id[a["id"]] = a
|
||||
|
||||
return list(authors_by_id.values())
|
||||
return authors_by_user, authors_by_id
|
||||
|
||||
|
||||
def get_my_followed() -> List[ChatMember]:
|
||||
|
Reference in New Issue
Block a user