calculate totalUnreadMessages for user
This commit is contained in:
parent
5b4cc95e87
commit
c4bda0b1c3
|
@ -42,6 +42,19 @@ class MessageResult:
|
|||
self.status = status
|
||||
self.message = message
|
||||
|
||||
async def get_total_unread_messages_for_user(user_slug):
|
||||
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
||||
if not chats:
|
||||
return 0
|
||||
|
||||
chats = json.loads(chats)
|
||||
total = 0
|
||||
for chat_id in chats:
|
||||
n = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
|
||||
total += n
|
||||
|
||||
return total
|
||||
|
||||
async def add_user_to_chat(user_slug, chat_id, chat = None):
|
||||
chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
|
||||
if not chats:
|
||||
|
|
|
@ -6,6 +6,8 @@ from orm.topic import Topic, TopicSubscription
|
|||
from resolvers.base import mutation, query, subscription
|
||||
from auth.authenticate import login_required
|
||||
|
||||
from inbox_resolvers.inbox import get_total_unread_messages_for_user
|
||||
|
||||
from sqlalchemy import func, and_, desc
|
||||
from sqlalchemy.orm import selectinload
|
||||
import asyncio
|
||||
|
@ -14,7 +16,11 @@ import asyncio
|
|||
@login_required
|
||||
async def get_current_user(_, info):
|
||||
user = info.context["request"].user
|
||||
return { "user": user }
|
||||
total_unread_messages = await get_total_unread_messages_for_user(user.slug)
|
||||
return {
|
||||
"user": user,
|
||||
"totalUnreadMessages": total_unread_messages
|
||||
}
|
||||
|
||||
@query.field("getUsersBySlugs")
|
||||
async def get_users_by_slugs(_, info, slugs):
|
||||
|
|
|
@ -15,6 +15,7 @@ type AuthResult {
|
|||
type UserResult {
|
||||
error: String
|
||||
user: User
|
||||
totalUnreadMessages: Int
|
||||
}
|
||||
|
||||
input ShoutInput {
|
||||
|
|
Loading…
Reference in New Issue
Block a user