Skip to content
Clone
main
92 lines 2.6 KB Rust
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<super::user::Entity>,
pub notification_type: NotificationType,
pub title: String,
pub body: Option<String>,
#[sea_orm(column_name = "data")]
pub data_json: Option<JsonValue>,
pub actor_uid: Option<i64>,
#[sea_orm(belongs_to, relation_enum = "Actor", from = "actor_uid", to = "uid")]
pub actor: BelongsTo<Option<super::user::Entity>>,
pub read_at: Option<OffsetDateTime>,
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<String>,
data: &'a Option<JsonValue>,
actor_id: &'a Option<i64>,
read_at: &'a Option<OffsetDateTime>,
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<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}