feat: create message capture policies

This commit is contained in:
h
2026-05-29 16:47:02 +02:00
parent 62aac0bf32
commit 920a0235e2
15 changed files with 700 additions and 5 deletions
@@ -0,0 +1,97 @@
"""folders and capture policy
Revision ID: b2f7c1a9d3e4
Revises: 77df960a31de
Create Date: 2026-05-29 17:30:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "b2f7c1a9d3e4"
down_revision: str | None = "77df960a31de"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
_TOGGLES = (
"messages",
"media",
"self_destruct_media",
"stt",
"reactions",
"track_edits_deletes",
"profile_history",
"stories",
"presence",
"backfill",
)
def _toggle_columns() -> list[sa.Column]:
return [
sa.Column(name, sa.Boolean(), nullable=False, server_default=sa.false())
for name in _TOGGLES
]
def upgrade() -> None:
op.create_table(
"folders",
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("folder_id", sa.Integer(), nullable=False),
sa.Column("title", sa.String(), nullable=False),
sa.Column("order_index", sa.Integer(), nullable=False),
sa.Column("is_chatlist", sa.Boolean(), nullable=False),
sa.Column("raw", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.PrimaryKeyConstraint("account_id", "folder_id"),
)
op.create_table(
"capture_policy",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=True),
sa.Column("scope_type", sa.String(), nullable=False),
sa.Column("scope_id", sa.BigInteger(), nullable=True),
*_toggle_columns(),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("account_id", "scope_type", "scope_id"),
)
op.execute(
"CREATE UNIQUE INDEX ix_capture_policy_default "
"ON capture_policy (scope_type) WHERE scope_type LIKE 'default_%'"
)
op.execute(
"INSERT INTO capture_policy "
"(scope_type, messages, media, self_destruct_media, stt, reactions, "
"track_edits_deletes, profile_history, stories, presence, backfill) VALUES "
"('default_channel', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), "
"('default_group', TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), "
"('default_dm', TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)"
)
def downgrade() -> None:
op.drop_table("capture_policy")
op.drop_table("folders")