aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/db
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/db')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e5f6a7b8c9d0_add_mail_quota.py36
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/db/models.py22
2 files changed, 58 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e5f6a7b8c9d0_add_mail_quota.py b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e5f6a7b8c9d0_add_mail_quota.py
new file mode 100644
index 0000000..8f2e4d2
--- /dev/null
+++ b/packages/meshbay-hub/src/meshbay_hub/db/migrations/versions/e5f6a7b8c9d0_add_mail_quota.py
@@ -0,0 +1,36 @@
+"""add mail_quota
+
+The hub's outbound mail allowance, kept across restarts. It lived in two
+dicts in `mail.py`, so every deploy handed out a fresh budget — and the hub is
+deployed often. A bound a restart forgets is not a bound.
+
+Revision ID: e5f6a7b8c9d0
+Revises: d4e5f6a7b8c9
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+from alembic import op
+
+revision: str = "e5f6a7b8c9d0"
+down_revision: Union[str, Sequence[str], None] = "d4e5f6a7b8c9"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.create_table(
+ "mail_quota",
+ sa.Column("key", sa.String(64), primary_key=True),
+ sa.Column("window_start", sa.DateTime(timezone=True), nullable=False),
+ sa.Column("count", sa.Integer(), nullable=False, server_default="0"),
+ sa.Column("last_sent", sa.DateTime(timezone=True), nullable=True),
+ )
+
+
+def downgrade() -> None:
+ # Dropping this returns the hub to sending with no recorded history, not to
+ # sending without a bound: the limits themselves live in `hub_settings` and
+ # in the configuration file.
+ op.drop_table("mail_quota")
diff --git a/packages/meshbay-hub/src/meshbay_hub/db/models.py b/packages/meshbay-hub/src/meshbay_hub/db/models.py
index e6c7d33..b052e00 100644
--- a/packages/meshbay-hub/src/meshbay_hub/db/models.py
+++ b/packages/meshbay-hub/src/meshbay_hub/db/models.py
@@ -308,6 +308,28 @@ class UserPreference(Base):
value: Mapped[str] = mapped_column(Text, nullable=False)
+class MailQuota(Base):
+ """How much mail has gone where, kept across restarts.
+
+ This was a pair of dicts in `mail.py`, which meant a restart handed out a
+ fresh allowance — and a hub restarts whenever it is deployed. A budget a
+ restart forgets is not a budget, for the same reason the denylist is
+ persisted rather than held in memory (S3).
+
+ One row per thing being counted:
+ `hour` the instance's hourly total
+ `dest:<hash>` one recipient, hashed — this table must not become a
+ list of plaintext addresses (S2)
+ """
+
+ __tablename__ = "mail_quota"
+
+ key: Mapped[str] = mapped_column(String(64), primary_key=True)
+ window_start: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
+ count: Mapped[int] = mapped_column(Integer, default=0)
+ last_sent: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
+
+
class HubSetting(Base):
"""
Instance-wide settings an admin changes at runtime from the panel.