- add chat_notification_settings table and migration - add chat notifications API (get/update muted) - skip message notifications for muted recipients - add mute/unmute control in chat info panel
This commit is contained in:
@@ -2,7 +2,7 @@ from sqlalchemy import Select, String, func, or_, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatType
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatNotificationSetting, ChatType
|
||||
from app.messages.models import Message, MessageHidden, MessageReceipt
|
||||
|
||||
|
||||
@@ -195,3 +195,38 @@ async def get_unread_count_for_chat(db: AsyncSession, *, chat_id: int, user_id:
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return int(result.scalar_one() or 0)
|
||||
|
||||
|
||||
async def get_chat_notification_setting(
|
||||
db: AsyncSession, *, chat_id: int, user_id: int
|
||||
) -> ChatNotificationSetting | None:
|
||||
result = await db.execute(
|
||||
select(ChatNotificationSetting).where(
|
||||
ChatNotificationSetting.chat_id == chat_id,
|
||||
ChatNotificationSetting.user_id == user_id,
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def upsert_chat_notification_setting(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
chat_id: int,
|
||||
user_id: int,
|
||||
muted: bool,
|
||||
) -> ChatNotificationSetting:
|
||||
setting = await get_chat_notification_setting(db, chat_id=chat_id, user_id=user_id)
|
||||
if setting:
|
||||
setting.muted = muted
|
||||
await db.flush()
|
||||
return setting
|
||||
setting = ChatNotificationSetting(chat_id=chat_id, user_id=user_id, muted=muted)
|
||||
db.add(setting)
|
||||
await db.flush()
|
||||
return setting
|
||||
|
||||
|
||||
async def is_chat_muted_for_user(db: AsyncSession, *, chat_id: int, user_id: int) -> bool:
|
||||
setting = await get_chat_notification_setting(db, chat_id=chat_id, user_id=user_id)
|
||||
return bool(setting and setting.muted)
|
||||
|
||||
Reference in New Issue
Block a user