feat: add real media albums across backend, web, and android
- add backend media-group endpoint with atomic message and attachment creation - switch web composer to send multi-media albums through the new endpoint - switch Android batch media sending to upload first and create one album message with caption
This commit is contained in:
@@ -7,6 +7,8 @@ from app.messages.schemas import (
|
||||
MessageCreateRequest,
|
||||
MessageForwardBulkRequest,
|
||||
MessageForwardRequest,
|
||||
MessageMediaGroupCreateRequest,
|
||||
MessageMediaGroupRead,
|
||||
MessageReactionRead,
|
||||
MessageReactionToggleRequest,
|
||||
MessageRead,
|
||||
@@ -16,6 +18,7 @@ from app.messages.schemas import (
|
||||
from app.messages.repository import get_message_by_id
|
||||
from app.messages.service import (
|
||||
create_chat_message,
|
||||
create_chat_media_group,
|
||||
delete_message,
|
||||
delete_message_for_all,
|
||||
forward_message,
|
||||
@@ -49,6 +52,21 @@ async def create_message(
|
||||
return message
|
||||
|
||||
|
||||
@router.post("/media-group", response_model=MessageMediaGroupRead, status_code=status.HTTP_201_CREATED)
|
||||
async def create_media_group(
|
||||
payload: MessageMediaGroupCreateRequest,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> MessageMediaGroupRead:
|
||||
result = await create_chat_media_group(db, sender_id=current_user.id, payload=payload)
|
||||
await realtime_gateway.publish_message_created(
|
||||
message=result.message,
|
||||
sender_id=current_user.id,
|
||||
client_message_id=payload.client_message_id,
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/search", response_model=list[MessageRead])
|
||||
async def search_messages_endpoint(
|
||||
query: str,
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from app.media.schemas import AttachmentRead
|
||||
from app.messages.models import MessageType
|
||||
|
||||
|
||||
@@ -30,6 +31,26 @@ class MessageCreateRequest(BaseModel):
|
||||
reply_to_message_id: int | None = None
|
||||
|
||||
|
||||
class MessageMediaGroupAttachmentCreateRequest(BaseModel):
|
||||
file_url: str = Field(min_length=1, max_length=1024)
|
||||
file_type: str = Field(min_length=1, max_length=64)
|
||||
file_size: int = Field(gt=0)
|
||||
waveform_points: list[int] | None = Field(default=None, min_length=8, max_length=256)
|
||||
|
||||
|
||||
class MessageMediaGroupCreateRequest(BaseModel):
|
||||
chat_id: int
|
||||
text: str | None = Field(default=None, max_length=4096)
|
||||
client_message_id: str | None = Field(default=None, min_length=8, max_length=64)
|
||||
reply_to_message_id: int | None = None
|
||||
attachments: list[MessageMediaGroupAttachmentCreateRequest] = Field(min_length=1, max_length=10)
|
||||
|
||||
|
||||
class MessageMediaGroupRead(BaseModel):
|
||||
message: MessageRead
|
||||
attachments: list[AttachmentRead]
|
||||
|
||||
|
||||
class MessageUpdateRequest(BaseModel):
|
||||
text: str = Field(min_length=1, max_length=4096)
|
||||
|
||||
|
||||
@@ -9,13 +9,18 @@ from app.chats import repository as chats_repository
|
||||
from app.chats.models import ChatMemberRole, ChatType
|
||||
from app.chats.service import ensure_chat_membership
|
||||
from app.media import repository as media_repository
|
||||
from app.media.schemas import AttachmentRead
|
||||
from app.media.service import _allowed_file_url_prefixes, _decode_waveform, _normalize_waveform, _validate_media
|
||||
from app.messages import repository
|
||||
from app.messages.models import Message
|
||||
from app.messages.models import Message, MessageType
|
||||
from app.messages.spam_guard import enforce_message_spam_policy
|
||||
from app.messages.schemas import (
|
||||
MessageCreateRequest,
|
||||
MessageForwardBulkRequest,
|
||||
MessageForwardRequest,
|
||||
MessageMediaGroupCreateRequest,
|
||||
MessageMediaGroupRead,
|
||||
MessageRead,
|
||||
MessageReactionRead,
|
||||
MessageReactionToggleRequest,
|
||||
MessageStatusUpdateRequest,
|
||||
@@ -26,6 +31,32 @@ from app.users.repository import has_block_relation_between_users
|
||||
from app.users.service import can_user_receive_private_messages, get_user_by_id
|
||||
|
||||
|
||||
def _infer_media_group_message_type(payload: MessageMediaGroupCreateRequest) -> MessageType:
|
||||
normalized_types = [attachment.file_type.split(";", maxsplit=1)[0].strip().lower() for attachment in payload.attachments]
|
||||
if normalized_types and all(item.startswith("image/") for item in normalized_types):
|
||||
return MessageType.IMAGE
|
||||
if normalized_types and all(item.startswith("audio/") for item in normalized_types):
|
||||
return MessageType.AUDIO
|
||||
if normalized_types and all(item.startswith("video/") for item in normalized_types):
|
||||
return MessageType.VIDEO
|
||||
if normalized_types and all(item.startswith("image/") or item.startswith("video/") for item in normalized_types):
|
||||
return MessageType.VIDEO
|
||||
return MessageType.FILE
|
||||
|
||||
|
||||
def _serialize_attachment(attachment) -> AttachmentRead:
|
||||
return AttachmentRead.model_validate(
|
||||
{
|
||||
"id": attachment.id,
|
||||
"message_id": attachment.message_id,
|
||||
"file_url": attachment.file_url,
|
||||
"file_type": attachment.file_type,
|
||||
"file_size": attachment.file_size,
|
||||
"waveform_points": _decode_waveform(attachment.waveform_data),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def create_chat_message(db: AsyncSession, *, sender_id: int, payload: MessageCreateRequest) -> Message:
|
||||
await ensure_chat_membership(db, chat_id=payload.chat_id, user_id=sender_id)
|
||||
chat = await chats_repository.get_chat_by_id(db, payload.chat_id)
|
||||
@@ -101,6 +132,114 @@ async def create_chat_message(db: AsyncSession, *, sender_id: int, payload: Mess
|
||||
return message
|
||||
|
||||
|
||||
async def create_chat_media_group(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
sender_id: int,
|
||||
payload: MessageMediaGroupCreateRequest,
|
||||
) -> MessageMediaGroupRead:
|
||||
await ensure_chat_membership(db, chat_id=payload.chat_id, user_id=sender_id)
|
||||
chat = await chats_repository.get_chat_by_id(db, payload.chat_id)
|
||||
if not chat:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Chat not found")
|
||||
membership = await chats_repository.get_chat_member(db, chat_id=payload.chat_id, user_id=sender_id)
|
||||
if not membership:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You are not a member of this chat")
|
||||
if chat.type == ChatType.CHANNEL and membership.role == ChatMemberRole.MEMBER:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only admins can post in channels")
|
||||
if chat.type == ChatType.PRIVATE:
|
||||
counterpart_id = await chats_repository.get_private_counterpart_user_id(db, chat_id=payload.chat_id, user_id=sender_id)
|
||||
if counterpart_id and await has_block_relation_between_users(db, user_a_id=sender_id, user_b_id=counterpart_id):
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Cannot send message due to block settings")
|
||||
if counterpart_id:
|
||||
counterpart = await get_user_by_id(db, counterpart_id)
|
||||
if counterpart and not await can_user_receive_private_messages(db, target_user=counterpart, actor_user_id=sender_id):
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="User does not accept private messages")
|
||||
if payload.reply_to_message_id is not None:
|
||||
reply_to = await repository.get_message_by_id(db, payload.reply_to_message_id)
|
||||
if not reply_to or reply_to.chat_id != payload.chat_id:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid reply target")
|
||||
if payload.client_message_id:
|
||||
existing = await repository.get_message_by_client_message_id(
|
||||
db,
|
||||
chat_id=payload.chat_id,
|
||||
sender_id=sender_id,
|
||||
client_message_id=payload.client_message_id,
|
||||
)
|
||||
if existing:
|
||||
existing_attachments = await media_repository.list_attachments_by_message_ids(db, message_ids=[existing.id])
|
||||
return MessageMediaGroupRead(
|
||||
message=MessageRead.model_validate(existing),
|
||||
attachments=[_serialize_attachment(attachment) for attachment in existing_attachments],
|
||||
)
|
||||
|
||||
message_type = _infer_media_group_message_type(payload)
|
||||
await enforce_message_spam_policy(user_id=sender_id, chat_id=payload.chat_id, text=payload.text)
|
||||
|
||||
for attachment in payload.attachments:
|
||||
_validate_media(attachment.file_type, attachment.file_size)
|
||||
if not attachment.file_url.startswith(_allowed_file_url_prefixes()):
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid file URL")
|
||||
|
||||
try:
|
||||
message = await repository.create_message(
|
||||
db,
|
||||
chat_id=payload.chat_id,
|
||||
sender_id=sender_id,
|
||||
reply_to_message_id=payload.reply_to_message_id,
|
||||
forwarded_from_message_id=None,
|
||||
message_type=message_type,
|
||||
text=payload.text,
|
||||
)
|
||||
created_attachments = []
|
||||
for attachment in payload.attachments:
|
||||
normalized_waveform = _normalize_waveform(attachment.waveform_points)
|
||||
created_attachments.append(
|
||||
await media_repository.create_attachment(
|
||||
db,
|
||||
message_id=message.id,
|
||||
file_url=attachment.file_url,
|
||||
file_type=attachment.file_type,
|
||||
file_size=attachment.file_size,
|
||||
waveform_data=json.dumps(normalized_waveform, ensure_ascii=True) if normalized_waveform else None,
|
||||
)
|
||||
)
|
||||
if payload.client_message_id:
|
||||
await repository.create_message_idempotency_key(
|
||||
db,
|
||||
chat_id=payload.chat_id,
|
||||
sender_id=sender_id,
|
||||
client_message_id=payload.client_message_id,
|
||||
message_id=message.id,
|
||||
)
|
||||
await db.commit()
|
||||
await db.refresh(message)
|
||||
except IntegrityError:
|
||||
await db.rollback()
|
||||
if payload.client_message_id:
|
||||
existing = await repository.get_message_by_client_message_id(
|
||||
db,
|
||||
chat_id=payload.chat_id,
|
||||
sender_id=sender_id,
|
||||
client_message_id=payload.client_message_id,
|
||||
)
|
||||
if existing:
|
||||
existing_attachments = await media_repository.list_attachments_by_message_ids(db, message_ids=[existing.id])
|
||||
return MessageMediaGroupRead(
|
||||
message=MessageRead.model_validate(existing),
|
||||
attachments=[_serialize_attachment(attachment) for attachment in existing_attachments],
|
||||
)
|
||||
raise
|
||||
try:
|
||||
await dispatch_message_notifications(db, message)
|
||||
except Exception:
|
||||
pass
|
||||
return MessageMediaGroupRead(
|
||||
message=MessageRead.model_validate(message),
|
||||
attachments=[_serialize_attachment(attachment) for attachment in created_attachments],
|
||||
)
|
||||
|
||||
|
||||
async def get_messages(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user