use sea_orm::JsonValue; use sea_orm::entity::prelude::*; use time::OffsetDateTime; use uuid::Uuid; #[derive( Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum, serde::Serialize, serde::Deserialize, utoipa::ToSchema, )] #[serde(rename_all = "snake_case")] #[sea_orm(rs_type = "String", db_type = "String(StringLen::N(16))")] pub enum NotificationType { #[sea_orm(string_value = "mention")] Mention, #[sea_orm(string_value = "reply")] Reply, #[sea_orm(string_value = "follow")] Follow, #[sea_orm(string_value = "invite")] Invite, #[sea_orm(string_value = "assignment")] Assignment, #[sea_orm(string_value = "system")] System, } #[sea_orm::model] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[sea_orm(table_name = "user_notifications")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: Uuid, pub user_id: i64, #[sea_orm(belongs_to, from = "user_id", to = "uid")] pub user: BelongsTo, pub notification_type: NotificationType, pub title: String, pub body: Option, #[sea_orm(column_name = "data")] pub data_json: Option, pub actor_uid: Option, #[sea_orm(belongs_to, relation_enum = "Actor", from = "actor_uid", to = "uid")] pub actor: BelongsTo>, pub read_at: Option, pub created_at: OffsetDateTime, } impl ActiveModelBehavior for ActiveModel {} impl Model { /// Canonical payload for the WS `notification` user event; mirrors the /// `NotificationDto` returned by `GET /me/notifications`. pub fn to_event_value(&self) -> JsonValue { #[derive(serde::Serialize)] struct EventPayload<'a> { id: Uuid, notification_type: &'a NotificationType, title: &'a str, body: &'a Option, data: &'a Option, actor_id: &'a Option, read_at: &'a Option, created_at: &'a OffsetDateTime, } serde_json::to_value(EventPayload { id: self.id, notification_type: &self.notification_type, title: &self.title, body: &self.body, data: &self.data_json, actor_id: &self.actor_uid, read_at: &self.read_at, created_at: &self.created_at, }) .unwrap_or(serde_json::Value::Null) } } impl Related for Entity { fn to() -> RelationDef { Relation::User.def() } }