This commit is contained in:
parent
93c1727be3
commit
9d1a4e90c9
|
@ -52,36 +52,37 @@ async def load_messages(
|
||||||
@login_required
|
@login_required
|
||||||
async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Union[List[Dict[str, Any]], None]]:
|
async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Union[List[Dict[str, Any]], None]]:
|
||||||
"""load :limit chats of current user with :offset"""
|
"""load :limit chats of current user with :offset"""
|
||||||
author_id = info.context["author_id"]
|
author_id = info.context.get("author_id")
|
||||||
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
if author_id:
|
||||||
chats = []
|
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
|
||||||
if cids:
|
chats = []
|
||||||
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
if cids:
|
||||||
cids = list(cids)[offset : (offset + limit)]
|
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
||||||
lock = asyncio.Lock()
|
cids = list(cids)[offset : (offset + limit)]
|
||||||
if len(cids) == 0:
|
lock = asyncio.Lock()
|
||||||
print(f"[resolvers.load] no chats for user with id={author_id}")
|
if len(cids) == 0:
|
||||||
r = await create_chat(None, info, members=[1]) # member with id = 1 is discours
|
print(f"[resolvers.load] no chats for user with id={author_id}")
|
||||||
print(f"[resolvers.load] created chat: {r['chat_id']}")
|
r = await create_chat(None, info, members=[1]) # member with id = 1 is discours
|
||||||
cids.append(r["chat"]["id"])
|
print(f"[resolvers.load] created chat: {r['chat_id']}")
|
||||||
all_authors: List[ChatMember] = await get_all_authors()
|
cids.append(r["chat"]["id"])
|
||||||
authors = {a["id"]: a for a in all_authors}
|
all_authors: List[ChatMember] = await get_all_authors()
|
||||||
for cid in cids:
|
authors = {a["id"]: a for a in all_authors}
|
||||||
async with lock:
|
for cid in cids:
|
||||||
chat_str: str = await redis.execute("GET", f"chats/{cid}")
|
async with lock:
|
||||||
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
|
chat_str: str = await redis.execute("GET", f"chats/{cid}")
|
||||||
if chat_str:
|
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
|
||||||
c: ChatPayload = json.loads(chat_str)
|
if chat_str:
|
||||||
c["messages"] = await load_messages(cid, 5, 0)
|
c: ChatPayload = json.loads(chat_str)
|
||||||
c["unread"] = await get_unread_counter(cid, author_id)
|
c["messages"] = await load_messages(cid, 5, 0)
|
||||||
member_ids = c["members"].copy()
|
c["unread"] = await get_unread_counter(cid, author_id)
|
||||||
c["members"] = []
|
member_ids = c["members"].copy()
|
||||||
for member_id in member_ids:
|
c["members"] = []
|
||||||
a = authors.get(member_id)
|
for member_id in member_ids:
|
||||||
if a:
|
a = authors.get(member_id)
|
||||||
a["online"] = a.get("id") in members_online
|
if a:
|
||||||
c["members"].append(a)
|
a["online"] = a.get("id") in members_online
|
||||||
chats.append(c)
|
c["members"].append(a)
|
||||||
|
chats.append(c)
|
||||||
return {"chats": chats, "error": None}
|
return {"chats": chats, "error": None}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ async def check_auth(req) -> str | None:
|
||||||
print(f"[services.auth] {e}")
|
print(f"[services.auth] {e}")
|
||||||
|
|
||||||
if not user_id:
|
if not user_id:
|
||||||
raise HTTPException(status_code=401,detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
|
@ -57,7 +57,9 @@ def login_required(f):
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(*args, **kwargs):
|
||||||
info = args[1]
|
info = args[1]
|
||||||
context = info.context
|
context = info.context
|
||||||
|
print("[login_required] context: " + str(context))
|
||||||
req = context.get("request")
|
req = context.get("request")
|
||||||
|
print("[login_required] request: " + str(req))
|
||||||
user_id = await check_auth(req)
|
user_id = await check_auth(req)
|
||||||
if user_id:
|
if user_id:
|
||||||
context["user_id"] = user_id
|
context["user_id"] = user_id
|
||||||
|
|
|
@ -23,7 +23,13 @@ async def get_all_authors(limit: int = 50, offset: int = 0) -> List[ChatMember]:
|
||||||
query_name = "load_authors_all"
|
query_name = "load_authors_all"
|
||||||
|
|
||||||
gql = {
|
gql = {
|
||||||
"query": "query { " + query_name + "(limit: " + str(limit) + ", offset: " + str(offset) +") { id slug pic name } }",
|
"query": "query { "
|
||||||
|
+ query_name
|
||||||
|
+ "(limit: "
|
||||||
|
+ str(limit)
|
||||||
|
+ ", offset: "
|
||||||
|
+ str(offset)
|
||||||
|
+ ") { id slug pic name } }",
|
||||||
"variables": None,
|
"variables": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user