From 614525a97af211c6e6e785cfebf8e67f8a0b6595 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 19 Apr 2024 19:20:09 +1200 Subject: [PATCH] notification subscription - migration --- app/constants.py | 8 ++++ app/models.py | 9 ++++ .../7aee4cb7db24_notification_subscription.py | 47 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 migrations/versions/7aee4cb7db24_notification_subscription.py diff --git a/app/constants.py b/app/constants.py index a6e64f05..d80740c7 100644 --- a/app/constants.py +++ b/app/constants.py @@ -23,3 +23,11 @@ REPORT_STATE_ESCALATED = 1 REPORT_STATE_APPEALED = 2 REPORT_STATE_RESOLVED = 3 REPORT_STATE_DISCARDED = -1 + +# different types of content notification that people can have. e.g. when a new post is made by a user or in a community. +# see NotificationSubscription in models.py +NOTIF_USER = 0 +NOTIF_COMMUNITY = 1 +NOTIF_TOPIC = 2 +NOTIF_POST = 3 +NOTIF_REPLY = 4 diff --git a/app/models.py b/app/models.py index 05326496..229ee164 100644 --- a/app/models.py +++ b/app/models.py @@ -1283,6 +1283,15 @@ class Report(db.Model): return self.source_instance_id == 1 +class NotificationSubscription(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(256)) # to avoid needing to look up the thing subscribed to via entity_id + type = db.Column(db.Integer, default=0, index=True) # see constants.py for possible values: NOTIF_* + entity_id = db.Column(db.Integer, index=True) # ID of the user, post, community, etc being subscribed to + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) # To whom this subscription belongs + created_at = db.Column(db.DateTime, default=utcnow) # Perhaps very old subscriptions can be automatically deleted + + class IpBan(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(50), index=True) diff --git a/migrations/versions/7aee4cb7db24_notification_subscription.py b/migrations/versions/7aee4cb7db24_notification_subscription.py new file mode 100644 index 00000000..7f168dd1 --- /dev/null +++ b/migrations/versions/7aee4cb7db24_notification_subscription.py @@ -0,0 +1,47 @@ +"""notification subscription + +Revision ID: 7aee4cb7db24 +Revises: 944c5ae8f4ba +Create Date: 2024-04-19 19:12:59.022726 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '7aee4cb7db24' +down_revision = '944c5ae8f4ba' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('notification_subscription', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=256), nullable=True), + sa.Column('type', sa.Integer(), nullable=True), + sa.Column('entity_id', sa.Integer(), nullable=True), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('notification_subscription', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_notification_subscription_entity_id'), ['entity_id'], unique=False) + batch_op.create_index(batch_op.f('ix_notification_subscription_type'), ['type'], unique=False) + batch_op.create_index(batch_op.f('ix_notification_subscription_user_id'), ['user_id'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('notification_subscription', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_notification_subscription_user_id')) + batch_op.drop_index(batch_op.f('ix_notification_subscription_type')) + batch_op.drop_index(batch_op.f('ix_notification_subscription_entity_id')) + + op.drop_table('notification_subscription') + # ### end Alembic commands ###