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 import json
from services.core import get_author from services.core import get_author, get_network
from services.redis import redis from services.redis import redis
from services.auth import login_required from services.auth import login_required
from services.schema import query from services.schema import query
@ -9,8 +9,10 @@ from .unread import get_unread_counter
# NOTE: not an API handler # 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""" """load :limit messages for :chat_id with :offset"""
if ids is None:
ids = []
messages = [] messages = []
try: try:
message_ids = [] + ids 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: if rt not in message_ids:
replies.append(rt) replies.append(rt)
if replies: 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: except Exception as e:
print(e) print(e)
return messages return messages
@ -105,8 +107,7 @@ async def load_recipients(_, _info, limit=50, offset=0):
"""load possible chat participants""" """load possible chat participants"""
onliners = (await redis.execute("SMEMBERS", "authors-online")) or [] onliners = (await redis.execute("SMEMBERS", "authors-online")) or []
members = [] members = []
with local_session() as session: all_authors = await get_network(limit, offset)
all_authors = session.query(Author).limit(limit).offset(offset)
for a in all_authors: for a in all_authors:
members.append( members.append(
{ {
@ -118,13 +119,7 @@ async def load_recipients(_, _info, limit=50, offset=0):
"online": a.id in onliners, "online": a.id in onliners,
} }
) )
# NOTE: maybe sort members here
return {"members": members, "error": None} 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 httpx import AsyncClient
from settings import API_BASE from settings import API_BASE
headers = {"Content-Type": "application/json"}
async def get_author(author_id): async def get_author(author_id):
gql = { gql = {
"query": "{ getAuthor(author_id: %s) { id slug userpic name lastSeen } }" "query": "query GetAuthor { getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id % author_id,
"operation": "GetAuthor",
"variables": None
} }
headers = {"Content-Type": "application/json"}
try: try:
async with AsyncClient() as client: async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=gql) 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") author = r.get("data", {}).get("getAuthor")
return author return author
except Exception: except Exception:
pass return None
async def get_network(author_id, limit=50, offset=0): async def get_network(author_id, limit=50, offset=0):
headers = {"Content-Type": "application/json"}
gql = { gql = {
"query": "{ authorFollowings(author_id: %s, limit: %s, offset: %s) { id slug userpic name } }" "query": "query LoadAuthors { authorFollowings(author_id: %s, limit: %s, offset: %s) { id slug userpic name } }"
% (author_id, limit, offset) % (author_id, limit, offset),
"operation": "LoadAuthors",
"variables": None
} }
followings = [] followings = []
followers = [] followers = []
try: try:
async with AsyncClient() as client: 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: if response.status_code != 200:
return False, None return False, None
r = response.json() r = response.json()
followings = r.get("data", {}).get("authorFollowers", []) followings = r.get("data", {}).get("authorFollowings", [])
more_amount = limit - len(followings) more_amount = limit - len(followings)
if more_amount > 0: if more_amount > 0:
gql = { followers = get_followers(author_id, more_amount)
"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", [])
except Exception as e: except Exception as e:
pass pass
return followings + followers 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