fixes-api

This commit is contained in:
2022-11-21 11:13:57 +03:00
parent 4c4f18147e
commit 5ad8328c03
17 changed files with 187 additions and 170 deletions

View File

@@ -4,8 +4,7 @@ from datetime import datetime
from auth.authenticate import login_required
from base.redis import redis
from base.resolvers import mutation, query
from services.auth.users import UserStorage
from base.resolvers import mutation
async def add_user_to_chat(user_slug: str, chat_id: str, chat=None):
@@ -122,10 +121,3 @@ async def delete_chat(_, info, chat_id: str):
return {
"error": "chat not exist"
}
@query.field("chatUsersAll")
@login_required
async def get_chat_users_all(_, info):
chat_users = await UserStorage.get_all_chat_users()
return chat_users

View File

@@ -3,27 +3,11 @@ from datetime import datetime, timedelta
from auth.authenticate import login_required
from base.redis import redis
from base.orm import local_session
from base.resolvers import query
async def get_unread_counter(chat_id: str, user_slug: str):
try:
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
if unread:
return unread
except Exception:
return 0
async def get_total_unread_counter(user_slug: str):
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
unread = 0
if chats:
chats = json.loads(chats)
for chat_id in chats:
n = await get_unread_counter(chat_id, user_slug)
unread += n
return unread
from orm.user import User
from resolvers.zine.profile import followed_authors
from .unread import get_unread_counter
async def load_messages(chatId: str, limit: int, offset: int):
@@ -100,3 +84,20 @@ async def load_messages_by(_, info, by, limit: int = 50, offset: int = 0):
"messages": messages,
"error": None
}
@query.field("loadRecipients")
async def load_recipients(_, info, limit=50, offset=0):
chat_users = []
user = info.context["request"].user
try:
chat_users += await followed_authors(user.slug)
limit = limit - len(chat_users)
except Exception:
pass
with local_session() as session:
chat_users += session.query(User).where(User.emailConfirmed).limit(limit).offset(offset)
return {
"members": chat_users,
"error": None
}

View File

@@ -7,9 +7,9 @@ from base.orm import local_session
from orm.user import AuthorFollower
@query.field("searchUsers")
@query.field("searchRecipients")
@login_required
async def search_users(_, info, query: str, limit: int = 50, offset: int = 0):
async def search_recipients(_, info, query: str, limit: int = 50, offset: int = 0):
result = []
# TODO: maybe redis scan?
user = info.context["request"].user
@@ -38,6 +38,6 @@ async def search_users(_, info, query: str, limit: int = 50, offset: int = 0):
result += session.query(AuthorFollower.follower).where(AuthorFollower.author.startswith(query))\
.offset(offset + len(result)).limit(offset + len(result) + limit)
return {
"slugs": list(result),
"members": list(result),
"error": None
}

22
resolvers/inbox/unread.py Normal file
View File

@@ -0,0 +1,22 @@
from base.redis import redis
import json
async def get_unread_counter(chat_id: str, user_slug: str):
try:
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
if unread:
return unread
except Exception:
return 0
async def get_total_unread_counter(user_slug: str):
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
unread = 0
if chats:
chats = json.loads(chats)
for chat_id in chats:
n = await get_unread_counter(chat_id, user_slug)
unread += n
return unread