Private
Public Access
Add generic file attachments to chat messages
Messages can now carry an arbitrary file (MessageFile), parallel to the existing MessageImage feature rather than a refactor of it. Files serve with Content-Disposition: attachment to force a download and prevent an uploaded HTML/SVG from executing same-origin. No content-type allowlist, same 8MB cap as images for now (a separate size-limit redesign is tracked as its own issue). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ from app.models.event_subscription import EventSubscription
|
||||
from app.models.invite import InviteStatus
|
||||
from app.models.membership import RoomMembership, RoomRole
|
||||
from app.models.message import Message
|
||||
from app.models.message_file import MessageFile
|
||||
from app.models.message_image import MessageImage
|
||||
from app.models.message_reaction import MessageReaction
|
||||
from app.models.password_reset import PasswordReset
|
||||
@@ -22,6 +23,7 @@ __all__ = [
|
||||
"RoomMembership",
|
||||
"RoomRole",
|
||||
"Message",
|
||||
"MessageFile",
|
||||
"MessageImage",
|
||||
"MessageReaction",
|
||||
"InviteStatus",
|
||||
|
||||
@@ -11,8 +11,8 @@ class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"content IS NOT NULL OR image_id IS NOT NULL",
|
||||
name="messages_content_or_image_required",
|
||||
"content IS NOT NULL OR image_id IS NOT NULL OR file_id IS NOT NULL",
|
||||
name="messages_content_or_attachment_required",
|
||||
),
|
||||
)
|
||||
|
||||
@@ -21,9 +21,10 @@ class Message(Base):
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||
# Nullable since Phase "image uploads": a message can be an image with
|
||||
# no caption. The CheckConstraint above still requires at least one of
|
||||
# content/image_id.
|
||||
# content/image_id/file_id.
|
||||
content: Mapped[str | None] = mapped_column(Text)
|
||||
image_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("message_images.id"))
|
||||
file_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("message_files.id"))
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), index=True, nullable=False
|
||||
)
|
||||
@@ -32,3 +33,4 @@ class Message(Base):
|
||||
|
||||
user = relationship("User")
|
||||
image = relationship("MessageImage")
|
||||
file = relationship("MessageFile")
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import BigInteger, DateTime, ForeignKey, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class MessageFile(Base):
|
||||
__tablename__ = "message_files"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||
room_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("rooms.id"), index=True, nullable=False)
|
||||
uploaded_by: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||
storage_filename: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
original_filename: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
# Wider than MessageImage's content_type column -- generic MIME strings
|
||||
# (e.g. the Office Open XML types) run 60-80 chars, unlike images'
|
||||
# four known short values.
|
||||
content_type: Mapped[str] = mapped_column(String(150), nullable=False)
|
||||
size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), nullable=False
|
||||
)
|
||||
|
||||
room = relationship("Room")
|
||||
uploader = relationship("User")
|
||||
Reference in New Issue
Block a user