gql-format-fix

This commit is contained in:
Untone 2023-10-11 22:12:55 +03:00
parent 444b504c95
commit 05967c1ce0
2 changed files with 56 additions and 43 deletions

View File

@ -1,6 +1,6 @@
import json
from services.core import get_author
from services.core import get_author, get_network
from services.redis import redis
from services.auth import login_required
from services.schema import query
@ -9,8 +9,10 @@ from .unread import get_unread_counter
# NOTE: not an API handler
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None):
"""load :limit messages for :chat_id with :offset"""
if ids is None:
ids = []
messages = []
try:
message_ids = [] + ids
@ -32,7 +34,7 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
if rt not in message_ids:
replies.append(rt)
if replies:
messages += await load_messages(redis, chat_id, limit=0, ids=replies)
messages += await load_messages(chat_id, offset, limit, replies)
except Exception as e:
print(e)
return messages
@ -105,8 +107,7 @@ async def load_recipients(_, _info, limit=50, offset=0):
"""load possible chat participants"""
onliners = (await redis.execute("SMEMBERS", "authors-online")) or []
members = []
with local_session() as session:
all_authors = session.query(Author).limit(limit).offset(offset)
all_authors = await get_network(limit, offset)
for a in all_authors:
members.append(
{
@ -118,13 +119,7 @@ async def load_recipients(_, _info, limit=50, offset=0):
"online": a.id in onliners,
}
)
# NOTE: maybe sort members here
return {"members": members, "error": None}
load_resolvers = {
"Query": {
"loadRecipients": load_recipients,
"loadMessagesBy": load_messages_by,
"loadChats": load_chats,
}
}

View File

@ -1,14 +1,19 @@
import json
from httpx import AsyncClient
from settings import API_BASE
headers = {"Content-Type": "application/json"}
async def get_author(author_id):
gql = {
"query": "{ getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id
"query": "query GetAuthor { getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id,
"operation": "GetAuthor",
"variables": None
}
headers = {"Content-Type": "application/json"}
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=gql)
@ -18,37 +23,50 @@ async def get_author(author_id):
author = r.get("data", {}).get("getAuthor")
return author
except Exception:
pass
return None
async def get_network(author_id, limit=50, offset=0):
headers = {"Content-Type": "application/json"}
gql = {
"query": "{ authorFollowings(author_id: %s, limit: %s, offset: %s) { id slug userpic name } }"
% (author_id, limit, offset)
"query": "query LoadAuthors { authorFollowings(author_id: %s, limit: %s, offset: %s) { id slug userpic name } }"
% (author_id, limit, offset),
"operation": "LoadAuthors",
"variables": None
}
followings = []
followers = []
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=gql)
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
if response.status_code != 200:
return False, None
r = response.json()
followings = r.get("data", {}).get("authorFollowers", [])
followings = r.get("data", {}).get("authorFollowings", [])
more_amount = limit - len(followings)
if more_amount > 0:
gql = {
"query": "{ authorFollowers(author_id: %s, limit: %s) { id slug userpic name } }"
% (author_id, more_amount)
}
response = await client.post(API_BASE, headers=headers, data=gql)
if response.status_code != 200:
return False, None
r = response.json()
followers = r.get("data", {}).get("authorFollowers", [])
followers = get_followers(author_id, more_amount)
except Exception as e:
pass
return followings + followers
async def get_followers(author_id, amount):
gql = {
"query": "query LoadAuthors { authorFollowers(author_id: %s, limit: %s) { id slug userpic name } }"
% (author_id, amount),
"operation": "LoadAuthors",
"variables": None
}
followers = []
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
if response.status_code != 200:
return False, None
r = response.json()
followers = r.get("data", {}).get("authorFollowers", [])
except Exception:
followers = []
return followers