- WhatsApp send: @c.us wird vor dem Anhängen entfernt (verhindert 4915..@c.us@c.us) - DELETE /api/v1/conversations/{id}: löscht Konversation + alle Nachrichten - TUI: Taste D öffnet Bestätigungsdialog zum Löschen des aktuellen Chats
93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from db.models import Contact, Conversation, Message
|
|
|
|
|
|
def get_all(db: Session, channel: str | None = None, archived: bool = False) -> list[Conversation]:
|
|
q = db.query(Conversation).filter(Conversation.is_archived == archived)
|
|
if channel:
|
|
q = q.filter(Conversation.channel == channel)
|
|
return q.order_by(Conversation.last_message_at.desc().nullslast()).all()
|
|
|
|
|
|
def get_by_id(db: Session, conv_id: str) -> Conversation | None:
|
|
return db.query(Conversation).filter(Conversation.id == conv_id).first()
|
|
|
|
|
|
def get_by_channel_id(db: Session, channel: str, channel_conversation_id: str) -> Conversation | None:
|
|
return (
|
|
db.query(Conversation)
|
|
.filter(
|
|
Conversation.channel == channel,
|
|
Conversation.channel_conversation_id == channel_conversation_id,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
|
|
def get_or_create(
|
|
db: Session,
|
|
channel: str,
|
|
channel_conversation_id: str,
|
|
contact: Contact,
|
|
title: str | None = None,
|
|
) -> Conversation:
|
|
conv = get_by_channel_id(db, channel, channel_conversation_id)
|
|
if not conv:
|
|
conv = Conversation(
|
|
channel=channel,
|
|
channel_conversation_id=channel_conversation_id,
|
|
title=title or contact.name,
|
|
)
|
|
conv.participants.append(contact)
|
|
db.add(conv)
|
|
db.commit()
|
|
db.refresh(conv)
|
|
return conv
|
|
|
|
|
|
def get_messages(
|
|
db: Session, conv_id: str, limit: int = 50, offset: int = 0
|
|
) -> list[Message]:
|
|
return (
|
|
db.query(Message)
|
|
.filter(Message.conversation_id == conv_id)
|
|
.order_by(Message.created_at.asc())
|
|
.offset(offset)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
|
|
def unread_count(db: Session, conv_id: str) -> int:
|
|
return (
|
|
db.query(Message)
|
|
.filter(
|
|
Message.conversation_id == conv_id,
|
|
Message.direction == "inbound",
|
|
Message.read_at.is_(None),
|
|
)
|
|
.count()
|
|
)
|
|
|
|
|
|
def delete(db: Session, conv: Conversation) -> None:
|
|
"""Löscht eine Konversation inkl. aller Nachrichten."""
|
|
db.query(Message).filter(Message.conversation_id == conv.id).delete()
|
|
db.delete(conv)
|
|
db.commit()
|
|
|
|
|
|
def mark_all_read(db: Session, conv_id: str) -> None:
|
|
now = datetime.utcnow()
|
|
db.query(Message).filter(
|
|
Message.conversation_id == conv_id,
|
|
Message.direction == "inbound",
|
|
Message.read_at.is_(None),
|
|
).update({"read_at": now, "status": "read"})
|
|
db.commit()
|