unread counter per chat

This commit is contained in:
tonyrewin 2022-11-02 12:46:25 +03:00
parent 34580f267f
commit 26174a67e4
3 changed files with 13 additions and 4 deletions

View File

@ -8,7 +8,14 @@ from base.resolvers import mutation, query, subscription
from services.inbox import MessageResult, MessagesStorage, ChatFollowing from services.inbox import MessageResult, MessagesStorage, ChatFollowing
async def get_unread_counter(user_slug): async def get_unread_counter(chat_id, user_slug):
try:
return int(await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}"))
except Exception:
return 0
async def get_total_unread_counter(user_slug):
chats = await redis.execute("GET", f"chats_by_user/{user_slug}") chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
if not chats: if not chats:
return 0 return 0
@ -16,7 +23,7 @@ async def get_unread_counter(user_slug):
chats = json.loads(chats) chats = json.loads(chats)
unread = 0 unread = 0
for chat_id in chats: for chat_id in chats:
n = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}") n = await get_unread_counter(chat_id, user_slug)
unread += n unread += n
return unread return unread
@ -92,6 +99,7 @@ async def user_chats(_, info):
chats = list(json.loads(chats)) chats = list(json.loads(chats))
for c in chats: for c in chats:
c['messages'] = await load_messages(c['id'], 50, 1) c['messages'] = await load_messages(c['id'], 50, 1)
c['unread'] = await get_unread_counter(c['id'], user.slug)
return chats return chats

View File

@ -11,7 +11,7 @@ from orm.shout import Shout
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from orm.user import User, UserRole, Role, UserRating, AuthorFollower from orm.user import User, UserRole, Role, UserRating, AuthorFollower
from .community import followed_communities from .community import followed_communities
from .inbox import get_unread_counter from .inbox import get_total_unread_counter
from .topics import get_topic_stat from .topics import get_topic_stat
from services.auth.users import UserStorage from services.auth.users import UserStorage
from services.zine.shoutscache import ShoutsCache from services.zine.shoutscache import ShoutsCache
@ -20,7 +20,7 @@ from services.stat.reacted import ReactedStorage
async def user_subscriptions(slug: str): async def user_subscriptions(slug: str):
return { return {
"unread": await get_unread_counter(slug), # unread inbox messages counter "unread": await get_total_unread_counter(slug), # unread inbox messages counter
"topics": [t.slug for t in await followed_topics(slug)], # followed topics slugs "topics": [t.slug for t in await followed_topics(slug)], # followed topics slugs
"authors": [a.slug for a in await followed_authors(slug)], # followed authors slugs "authors": [a.slug for a in await followed_authors(slug)], # followed authors slugs
"reactions": await ReactedStorage.get_shouts_by_author(slug), "reactions": await ReactedStorage.get_shouts_by_author(slug),

View File

@ -501,4 +501,5 @@ type Chat {
description: String description: String
users: [User]! users: [User]!
messages: [Message]! messages: [Message]!
unread: Int
} }