forked from mtusi2026/mtusi2026lab2
добавлены чаты, сообщения и проверка сохранности
This commit is contained in:
+424
-1
@@ -3,8 +3,10 @@ from uuid import UUID, uuid4
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.models import CreateUser, User
|
||||
from src.models import Chat, ChatPage, CreateChat, CreateUser, User, CreateMessage, Message, MessagePage
|
||||
from src.security import hash_password
|
||||
from src.errors import APIError
|
||||
from src.cursor import decode_cursor, encode_cursor
|
||||
|
||||
|
||||
async def create_user(
|
||||
@@ -109,4 +111,425 @@ async def get_user_by_id(
|
||||
username=row.username,
|
||||
display_name=row.display_name,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def create_chat(
|
||||
session: AsyncSession,
|
||||
data: CreateChat,
|
||||
current_user_id: UUID,
|
||||
) -> Chat:
|
||||
if current_user_id not in data.member_ids:
|
||||
raise APIError(
|
||||
status_code=400,
|
||||
code="invalid_request",
|
||||
message="Current user must be a chat member",
|
||||
)
|
||||
|
||||
users_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT id
|
||||
FROM users
|
||||
WHERE id = ANY(:member_ids)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"member_ids": data.member_ids,
|
||||
},
|
||||
)
|
||||
|
||||
existing_user_ids = {
|
||||
UUID(str(row.id))
|
||||
for row in users_result
|
||||
}
|
||||
|
||||
if existing_user_ids != set(data.member_ids):
|
||||
raise APIError(
|
||||
status_code=400,
|
||||
code="invalid_request",
|
||||
message="Unknown chat member",
|
||||
)
|
||||
|
||||
chat_id = uuid4()
|
||||
|
||||
chat_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
)
|
||||
VALUES (
|
||||
:id,
|
||||
:title
|
||||
)
|
||||
RETURNING
|
||||
id,
|
||||
title,
|
||||
created_at
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": chat_id,
|
||||
"title": data.title,
|
||||
},
|
||||
)
|
||||
|
||||
chat_row = chat_result.one()
|
||||
|
||||
for member_id in data.member_ids:
|
||||
await session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
VALUES (
|
||||
:chat_id,
|
||||
:user_id
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"user_id": member_id,
|
||||
},
|
||||
)
|
||||
|
||||
return Chat(
|
||||
id=UUID(str(chat_row.id)),
|
||||
title=chat_row.title,
|
||||
member_ids=data.member_ids,
|
||||
created_at=chat_row.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def get_chat_by_id(
|
||||
session: AsyncSession,
|
||||
chat_id: UUID,
|
||||
current_user_id: UUID,
|
||||
) -> Chat | None:
|
||||
chat_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.created_at
|
||||
FROM chats AS c
|
||||
JOIN chat_members AS cm
|
||||
ON cm.chat_id = c.id
|
||||
WHERE c.id = :chat_id
|
||||
AND cm.user_id = :current_user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"current_user_id": current_user_id,
|
||||
},
|
||||
)
|
||||
|
||||
chat_row = chat_result.one_or_none()
|
||||
|
||||
if chat_row is None:
|
||||
return None
|
||||
|
||||
members_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT user_id
|
||||
FROM chat_members
|
||||
WHERE chat_id = :chat_id
|
||||
ORDER BY user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
},
|
||||
)
|
||||
|
||||
member_ids = [
|
||||
UUID(str(row.user_id))
|
||||
for row in members_result
|
||||
]
|
||||
|
||||
return Chat(
|
||||
id=UUID(str(chat_row.id)),
|
||||
title=chat_row.title,
|
||||
member_ids=member_ids,
|
||||
created_at=chat_row.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def list_chats(
|
||||
session: AsyncSession,
|
||||
current_user_id: UUID,
|
||||
limit: int,
|
||||
) -> ChatPage:
|
||||
chats_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.created_at
|
||||
FROM chats AS c
|
||||
JOIN chat_members AS cm
|
||||
ON cm.chat_id = c.id
|
||||
WHERE cm.user_id = :current_user_id
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT :limit
|
||||
"""
|
||||
),
|
||||
{
|
||||
"current_user_id": current_user_id,
|
||||
"limit": limit,
|
||||
},
|
||||
)
|
||||
|
||||
chat_rows = chats_result.all()
|
||||
|
||||
items = []
|
||||
|
||||
for chat_row in chat_rows:
|
||||
members_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT user_id
|
||||
FROM chat_members
|
||||
WHERE chat_id = :chat_id
|
||||
ORDER BY user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_row.id,
|
||||
},
|
||||
)
|
||||
|
||||
member_ids = [
|
||||
UUID(str(row.user_id))
|
||||
for row in members_result
|
||||
]
|
||||
|
||||
items.append(
|
||||
Chat(
|
||||
id=UUID(str(chat_row.id)),
|
||||
title=chat_row.title,
|
||||
member_ids=member_ids,
|
||||
created_at=chat_row.created_at,
|
||||
)
|
||||
)
|
||||
|
||||
return ChatPage(
|
||||
items=items,
|
||||
next_cursor=None,
|
||||
)
|
||||
|
||||
|
||||
async def create_message(
|
||||
session: AsyncSession,
|
||||
chat_id: UUID,
|
||||
current_user_id: UUID,
|
||||
data: CreateMessage,
|
||||
) -> Message:
|
||||
membership_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = :chat_id
|
||||
AND user_id = :current_user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"current_user_id": current_user_id,
|
||||
},
|
||||
)
|
||||
|
||||
if membership_result.scalar_one_or_none() is None:
|
||||
raise APIError(
|
||||
status_code=404,
|
||||
code="not_found",
|
||||
message="Chat not found",
|
||||
)
|
||||
|
||||
message_id = uuid4()
|
||||
|
||||
message_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text
|
||||
)
|
||||
VALUES (
|
||||
:id,
|
||||
:chat_id,
|
||||
:sender_id,
|
||||
:text
|
||||
)
|
||||
RETURNING
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": message_id,
|
||||
"chat_id": chat_id,
|
||||
"sender_id": current_user_id,
|
||||
"text": data.text,
|
||||
},
|
||||
)
|
||||
|
||||
row = message_result.one()
|
||||
|
||||
return Message(
|
||||
id=UUID(str(row.id)),
|
||||
chat_id=UUID(str(row.chat_id)),
|
||||
sender_id=UUID(str(row.sender_id)),
|
||||
text=row.text,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def get_messages(
|
||||
session: AsyncSession,
|
||||
chat_id: UUID,
|
||||
current_user_id: UUID,
|
||||
limit: int,
|
||||
cursor: str | None,
|
||||
) -> MessagePage:
|
||||
membership_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = :chat_id
|
||||
AND user_id = :current_user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"current_user_id": current_user_id,
|
||||
},
|
||||
)
|
||||
|
||||
if membership_result.scalar_one_or_none() is None:
|
||||
raise APIError(
|
||||
status_code=404,
|
||||
code="not_found",
|
||||
message="Chat not found",
|
||||
)
|
||||
|
||||
cursor_created_at = None
|
||||
cursor_message_id = None
|
||||
|
||||
if cursor is not None:
|
||||
try:
|
||||
(
|
||||
cursor_chat_id,
|
||||
cursor_created_at,
|
||||
cursor_message_id,
|
||||
) = decode_cursor(cursor)
|
||||
except ValueError:
|
||||
raise APIError(
|
||||
status_code=400,
|
||||
code="invalid_request",
|
||||
message="Invalid cursor",
|
||||
)
|
||||
|
||||
if cursor_chat_id != chat_id:
|
||||
raise APIError(
|
||||
status_code=400,
|
||||
code="invalid_request",
|
||||
message="Cursor belongs to another chat",
|
||||
)
|
||||
|
||||
if cursor is None:
|
||||
messages_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :chat_id
|
||||
ORDER BY created_at, id
|
||||
LIMIT :fetch_limit
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"fetch_limit": limit + 1,
|
||||
},
|
||||
)
|
||||
else:
|
||||
messages_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :chat_id
|
||||
AND (created_at, id) > (
|
||||
:cursor_created_at,
|
||||
:cursor_message_id
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT :fetch_limit
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"cursor_created_at": cursor_created_at,
|
||||
"cursor_message_id": cursor_message_id,
|
||||
"fetch_limit": limit + 1,
|
||||
},
|
||||
)
|
||||
|
||||
rows = messages_result.all()
|
||||
|
||||
has_more = len(rows) > limit
|
||||
rows = rows[:limit]
|
||||
|
||||
items = [
|
||||
Message(
|
||||
id=UUID(str(row.id)),
|
||||
chat_id=UUID(str(row.chat_id)),
|
||||
sender_id=UUID(str(row.sender_id)),
|
||||
text=row.text,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
next_cursor = None
|
||||
|
||||
if has_more:
|
||||
last_message = items[-1]
|
||||
|
||||
next_cursor = encode_cursor(
|
||||
chat_id=chat_id,
|
||||
created_at=last_message.created_at,
|
||||
message_id=last_message.id,
|
||||
)
|
||||
|
||||
return MessagePage(
|
||||
items=items,
|
||||
next_cursor=next_cursor,
|
||||
)
|
||||
Reference in New Issue
Block a user