feat: add notifications table and model

This commit is contained in:
əlemi 2024-06-26 03:54:15 +02:00
parent 4e9f6ea419
commit 5a4671f20d
Signed by: alemi
GPG key ID: A4895B84D311642C
6 changed files with 145 additions and 2 deletions

View file

@ -33,6 +33,8 @@ pub enum Relation {
Actors, Actors,
#[sea_orm(has_many = "super::addressing::Entity")] #[sea_orm(has_many = "super::addressing::Entity")]
Addressing, Addressing,
#[sea_orm(has_many = "super::notification::Entity")]
Notifications,
#[sea_orm( #[sea_orm(
belongs_to = "super::object::Entity", belongs_to = "super::object::Entity",
from = "Column::Object", from = "Column::Object",
@ -55,6 +57,12 @@ impl Related<super::addressing::Entity> for Entity {
} }
} }
impl Related<super::notification::Entity> for Entity {
fn to() -> RelationDef {
Relation::Notifications.def()
}
}
impl Related<super::object::Entity> for Entity { impl Related<super::object::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::Objects.def() Relation::Objects.def()

View file

@ -56,6 +56,8 @@ pub enum Relation {
Likes, Likes,
#[sea_orm(has_many = "super::mention::Entity")] #[sea_orm(has_many = "super::mention::Entity")]
Mentions, Mentions,
#[sea_orm(has_many = "super::notification::Entity")]
Notifications,
#[sea_orm(has_many = "super::object::Entity")] #[sea_orm(has_many = "super::object::Entity")]
Objects, Objects,
#[sea_orm(has_many = "super::relation::Entity")] #[sea_orm(has_many = "super::relation::Entity")]
@ -118,6 +120,12 @@ impl Related<super::mention::Entity> for Entity {
} }
} }
impl Related<super::notification::Entity> for Entity {
fn to() -> RelationDef {
Relation::Notifications.def()
}
}
impl Related<super::object::Entity> for Entity { impl Related<super::object::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::Objects.def() Relation::Objects.def()

View file

@ -7,10 +7,11 @@ pub mod credential;
pub mod session; pub mod session;
pub mod instance; pub mod instance;
pub mod addressing;
pub mod job; pub mod job;
pub mod addressing;
pub mod notification;
pub mod relation; pub mod relation;
pub mod announce; pub mod announce;
pub mod like; pub mod like;
pub mod dislike; pub mod dislike;

View file

@ -0,0 +1,46 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "notifications")]
pub struct Model {
#[sea_orm(primary_key)]
pub internal: i64,
pub activity: i64,
pub actor: i64,
pub seen: bool,
pub published: ChronoDateTimeUtc,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::activity::Entity",
from = "Column::Activity",
to = "super::activity::Column::Internal",
on_update = "Cascade",
on_delete = "Cascade"
)]
Activities,
#[sea_orm(
belongs_to = "super::actor::Entity",
from = "Column::Actor",
to = "super::actor::Column::Internal",
on_update = "Cascade",
on_delete = "Cascade"
)]
Actors,
}
impl Related<super::actor::Entity> for Entity {
fn to() -> RelationDef {
Relation::Actors.def()
}
}
impl Related<super::activity::Entity> for Entity {
fn to() -> RelationDef {
Relation::Activities.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -11,6 +11,7 @@ mod m20240606_000001_add_audience_to_objects;
mod m20240607_000001_activity_ref_is_optional; mod m20240607_000001_activity_ref_is_optional;
mod m20240609_000001_add_instance_field_to_relations; mod m20240609_000001_add_instance_field_to_relations;
mod m20240623_000001_add_dislikes_table; mod m20240623_000001_add_dislikes_table;
mod m20240626_000001_add_notifications_table;
pub struct Migrator; pub struct Migrator;
@ -29,6 +30,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240607_000001_activity_ref_is_optional::Migration), Box::new(m20240607_000001_activity_ref_is_optional::Migration),
Box::new(m20240609_000001_add_instance_field_to_relations::Migration), Box::new(m20240609_000001_add_instance_field_to_relations::Migration),
Box::new(m20240623_000001_add_dislikes_table::Migration), Box::new(m20240623_000001_add_dislikes_table::Migration),
Box::new(m20240626_000001_add_notifications_table::Migration),
] ]
} }
} }

View file

@ -0,0 +1,78 @@
use sea_orm_migration::prelude::*;
use crate::m20240524_000001_create_actor_activity_object_tables::{Activities, Actors};
#[derive(DeriveIden)]
#[allow(clippy::enum_variant_names)]
pub enum Notifications {
Table,
Internal,
Activity,
Actor,
Seen,
Published,
}
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Notifications::Table)
.comment("notifications table, connecting activities to users")
.col(
ColumnDef::new(Notifications::Internal)
.big_integer()
.not_null()
.primary_key()
.auto_increment()
)
.col(ColumnDef::new(Notifications::Actor).big_integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fkey-notifications-actor")
.from(Notifications::Table, Notifications::Actor)
.to(Actors::Table, Actors::Internal)
.on_update(ForeignKeyAction::Cascade)
.on_delete(ForeignKeyAction::Cascade)
)
.col(ColumnDef::new(Notifications::Activity).big_integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fkey-notifications-activity")
.from(Notifications::Table, Notifications::Activity)
.to(Activities::Table, Activities::Internal)
.on_update(ForeignKeyAction::Cascade)
.on_delete(ForeignKeyAction::Cascade)
)
.col(ColumnDef::new(Notifications::Seen).boolean().not_null().default(false))
.col(ColumnDef::new(Notifications::Published).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
.to_owned()
)
.await?;
manager
.create_index(
Index::create()
.name("index-notifications-actor-published")
.table(Notifications::Table)
.col(Notifications::Actor)
.col((Notifications::Published, IndexOrder::Desc))
.to_owned()
).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Notifications::Table).to_owned())
.await?;
Ok(())
}
}