Private
Public Access
Add URL previews for chat messages (#43)
Slack/Discord-style link unfurling: the first http(s) URL in a message's content gets a small preview card (title/description/image/site name) fetched from the page's Open Graph tags. Backend: - Message.preview_url (extracted at create/edit time, cheap regex, no I/O) points at a link_previews cache row keyed by URL -- the same URL posted in different messages/rooms fetches once, and a failed fetch is cached too so a dead URL isn't retried on every reference. - The actual fetch runs in a background asyncio.create_task from broadcast_new_message/broadcast_message_update, on its own DB session, so a slow third-party site never delays message delivery. A separate "link_preview" WS envelope carries the result once it resolves. - SSRF protection reuses app/services/ssrf.py's validate_target_url (renamed from UnsafeWebhookUrlError to UnsafeUrlError now that it's shared with webhooks), but re-validates before every hop of a redirect chain rather than once up front -- redirects are followed manually so each intermediate URL is checked before it's ever connected to. - Parsed with stdlib html.parser -- no new dependency. Frontend: a LinkPreviewCard rendered under message content when present, patched into state live via the new WS envelope and included in message history for reloads. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""link previews for URL unfurling in chat messages
|
||||
|
||||
Revision ID: d00f93766fa5
|
||||
Revises: ffdd409227bb
|
||||
Create Date: 2026-08-17 17:31:13.598917
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'd00f93766fa5'
|
||||
down_revision: Union[str, Sequence[str], None] = 'ffdd409227bb'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
op.create_table(
|
||||
"link_previews",
|
||||
sa.Column("id", sa.Uuid(), nullable=False),
|
||||
sa.Column("url", sa.String(length=2048), nullable=False),
|
||||
sa.Column("title", sa.String(length=500), nullable=True),
|
||||
sa.Column("description", sa.String(length=1000), nullable=True),
|
||||
sa.Column("image_url", sa.String(length=2048), nullable=True),
|
||||
sa.Column("site_name", sa.String(length=200), nullable=True),
|
||||
sa.Column("fetch_failed", sa.Boolean(), nullable=False),
|
||||
sa.Column(
|
||||
"fetched_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("clock_timestamp()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(
|
||||
op.f("ix_link_previews_url"), "link_previews", ["url"], unique=True
|
||||
)
|
||||
# Nullable, no default -- just adds a column to the catalog, no table
|
||||
# rewrite, no lock beyond the instant one ALTER TABLE ADD COLUMN always
|
||||
# takes for a nullable column with no default.
|
||||
op.add_column("messages", sa.Column("preview_url", sa.String(length=2048), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
op.drop_column("messages", "preview_url")
|
||||
op.drop_index(op.f("ix_link_previews_url"), table_name="link_previews")
|
||||
op.drop_table("link_previews")
|
||||
Reference in New Issue
Block a user