diff options
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/db')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e7f8a9b0c1d2_push_subscriptions.py | 35 | ||||
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/db/models.py | 28 |
2 files changed, 63 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e7f8a9b0c1d2_push_subscriptions.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e7f8a9b0c1d2_push_subscriptions.py new file mode 100644 index 0000000..4311448 --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e7f8a9b0c1d2_push_subscriptions.py @@ -0,0 +1,35 @@ +"""phones told about notifications: a Web Push endpoint, or a poll secret + +Revision ID: e7f8a9b0c1d2 +Revises: d4e5f6a7b8ca +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +from alembic import op + +revision: str = "e7f8a9b0c1d2" +down_revision: str | Sequence[str] | None = "d4e5f6a7b8ca" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.create_table( + "push_subscriptions", + sa.Column("id", sa.String(36), primary_key=True), + sa.Column("user_id", sa.String(36), sa.ForeignKey("users.id"), nullable=False), + sa.Column("endpoint", sa.String(1024), nullable=True), + sa.Column("p256dh", sa.String(128), nullable=True), + sa.Column("auth", sa.String(32), nullable=True), + sa.Column("poll_hash", sa.String(64), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True)), + ) + op.create_index("ix_push_subscriptions_user_endpoint", "push_subscriptions", + ["user_id", "endpoint"], unique=True) + + +def downgrade() -> None: + op.drop_index("ix_push_subscriptions_user_endpoint", table_name="push_subscriptions") + op.drop_table("push_subscriptions") diff --git a/packages/meshbay-hub/src/meshbay_hub/db/models.py b/packages/meshbay-hub/src/meshbay_hub/db/models.py index dbc0f10..7291485 100644 --- a/packages/meshbay-hub/src/meshbay_hub/db/models.py +++ b/packages/meshbay-hub/src/meshbay_hub/db/models.py @@ -398,6 +398,34 @@ class ContentReview(Base): decided_by: Mapped[str | None] = mapped_column(String(64)) +class PushSubscription(Base): + """A phone told about this account's notifications (§11.3): pushed to its + Web Push endpoint when it has one, fetched with its poll secret when not. + + The endpoint is a capability — whoever holds the URL can wake the phone — + and the keys are what the hub encrypts to, so the push server relays bytes + it cannot read. One account holds a handful at most + (`api/push.MAX_SUBSCRIPTIONS`): the rows are shared, and every notification + costs one outbound request per row. + """ + + __tablename__ = "push_subscriptions" + + id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_uuid) + user_id: Mapped[str] = mapped_column(ForeignKey("users.id"), nullable=False) + # All three empty for a phone with no push distributor, which fetches instead. + endpoint: Mapped[str | None] = mapped_column(String(1024), nullable=True) + p256dh: Mapped[str | None] = mapped_column(String(128), nullable=True) + auth: Mapped[str | None] = mapped_column(String(32), nullable=True) + # sha256 of the secret `POST /v1/push/poll` is answered for; never the secret. + poll_hash: Mapped[str | None] = mapped_column(String(64), nullable=True) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now) + + __table_args__ = ( + Index("ix_push_subscriptions_user_endpoint", "user_id", "endpoint", unique=True), + ) + + class UserPreference(Base): __tablename__ = "user_preferences" |