From 05967c1ce0d98532fad976083846d827f9d141c9 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 11 Oct 2023 22:12:55 +0300 Subject: [PATCH] gql-format-fix --- resolvers/load.py | 45 ++++++++++++++++++--------------------- services/core.py | 54 +++++++++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/resolvers/load.py b/resolvers/load.py index f8abccd..b4ccb00 100644 --- a/resolvers/load.py +++ b/resolvers/load.py @@ -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,26 +107,19 @@ 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) - for a in all_authors: - members.append( - { - "id": a.id, - "slug": a.slug, - "userpic": a.userpic, - "name": a.name, - "lastSeen": a.lastSeen, - "online": a.id in onliners, - } - ) + all_authors = await get_network(limit, offset) + for a in all_authors: + members.append( + { + "id": a.id, + "slug": a.slug, + "userpic": a.userpic, + "name": a.name, + "lastSeen": a.lastSeen, + "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, - } -} diff --git a/services/core.py b/services/core.py index de0ae90..1c629ed 100644 --- a/services/core.py +++ b/services/core.py @@ -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