Files
Messenger/app/media/repository.py
benya 30169a3a27
All checks were successful
CI / test (push) Successful in 23s
feat: add waveform voice messages end-to-end
2026-03-08 12:40:49 +03:00

63 lines
1.6 KiB
Python

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.media.models import Attachment
from app.messages.models import Message
async def create_attachment(
db: AsyncSession,
*,
message_id: int,
file_url: str,
file_type: str,
file_size: int,
waveform_data: str | None = None,
) -> Attachment:
attachment = Attachment(
message_id=message_id,
file_url=file_url,
file_type=file_type,
file_size=file_size,
waveform_data=waveform_data,
)
db.add(attachment)
await db.flush()
return attachment
async def get_attachment_by_id(db: AsyncSession, attachment_id: int) -> Attachment | None:
return await db.get(Attachment, attachment_id)
async def list_chat_attachments(
db: AsyncSession,
*,
chat_id: int,
limit: int = 100,
before_id: int | None = None,
) -> list[tuple[Attachment, Message]]:
stmt = (
select(Attachment, Message)
.join(Message, Message.id == Attachment.message_id)
.where(Message.chat_id == chat_id)
.order_by(Attachment.id.desc())
.limit(limit)
)
if before_id is not None:
stmt = stmt.where(Attachment.id < before_id)
result = await db.execute(stmt)
return [(row[0], row[1]) for row in result.all()]
async def list_attachments_by_message_ids(
db: AsyncSession,
*,
message_ids: list[int],
) -> list[Attachment]:
if not message_ids:
return []
stmt = select(Attachment).where(Attachment.message_id.in_(message_ids))
result = await db.execute(stmt)
return list(result.scalars().all())