fedimovies/fedimovies-models/src/notifications/types.rs

131 lines
3.8 KiB
Rust
Raw Normal View History

2021-10-12 16:11:47 +00:00
use chrono::{DateTime, Utc};
use postgres_types::FromSql;
use tokio_postgres::Row;
use uuid::Uuid;
use crate::attachments::types::DbMediaAttachment;
2022-12-03 22:09:42 +00:00
use crate::database::{
int_enum::{int_enum_from_sql, int_enum_to_sql},
2023-04-24 15:35:32 +00:00
DatabaseError, DatabaseTypeError,
2022-12-03 22:09:42 +00:00
};
use crate::emojis::types::DbEmoji;
use crate::posts::types::{DbPost, Post};
use crate::profiles::types::DbActorProfile;
2021-10-12 16:11:47 +00:00
#[derive(Debug)]
2021-10-12 16:11:47 +00:00
pub enum EventType {
Follow,
2021-10-15 00:27:39 +00:00
FollowRequest,
Reply,
2021-10-29 12:30:28 +00:00
Reaction,
Mention,
Repost,
Subscription,
SubscriptionStart,
SubscriptionExpiration,
Move,
2021-10-12 16:11:47 +00:00
}
impl From<&EventType> for i16 {
fn from(value: &EventType) -> i16 {
2021-10-12 16:11:47 +00:00
match value {
EventType::Follow => 1,
2021-10-15 00:27:39 +00:00
EventType::FollowRequest => 2,
EventType::Reply => 3,
2021-10-29 12:30:28 +00:00
EventType::Reaction => 4,
EventType::Mention => 5,
EventType::Repost => 6,
EventType::Subscription => 7,
EventType::SubscriptionStart => unimplemented!("not supported"),
EventType::SubscriptionExpiration => 9,
EventType::Move => 10,
2021-10-12 16:11:47 +00:00
}
}
}
impl TryFrom<i16> for EventType {
2022-12-03 22:09:42 +00:00
type Error = DatabaseTypeError;
2021-10-12 16:11:47 +00:00
fn try_from(value: i16) -> Result<Self, Self::Error> {
let event_type = match value {
1 => Self::Follow,
2021-10-15 00:27:39 +00:00
2 => Self::FollowRequest,
3 => Self::Reply,
2021-10-29 12:30:28 +00:00
4 => Self::Reaction,
5 => Self::Mention,
6 => Self::Repost,
7 => Self::Subscription,
8 => Self::SubscriptionStart,
9 => Self::SubscriptionExpiration,
10 => Self::Move,
2022-12-03 22:09:42 +00:00
_ => return Err(DatabaseTypeError),
2021-10-12 16:11:47 +00:00
};
Ok(event_type)
}
}
int_enum_from_sql!(EventType);
int_enum_to_sql!(EventType);
#[allow(dead_code)]
#[derive(FromSql)]
#[postgres(name = "notification")]
struct DbNotification {
id: i32,
sender_id: Uuid,
recipient_id: Uuid,
post_id: Option<Uuid>,
event_type: EventType,
created_at: DateTime<Utc>,
}
2021-10-12 16:11:47 +00:00
pub struct Notification {
pub id: i32,
pub sender: DbActorProfile,
2023-04-25 11:19:04 +00:00
pub recipient: DbActorProfile,
2021-10-15 00:27:39 +00:00
pub post: Option<Post>,
2021-10-12 16:11:47 +00:00
pub event_type: EventType,
pub created_at: DateTime<Utc>,
}
impl TryFrom<&Row> for Notification {
type Error = DatabaseError;
fn try_from(row: &Row) -> Result<Self, Self::Error> {
let db_notification: DbNotification = row.try_get("notification")?;
let db_sender: DbActorProfile = row.try_get("sender")?;
2023-04-25 11:19:04 +00:00
let db_recipient: DbActorProfile = row.try_get("recipient")?;
2021-10-15 00:27:39 +00:00
let maybe_db_post: Option<DbPost> = row.try_get("post")?;
let maybe_post = match maybe_db_post {
Some(db_post) => {
let db_post_author: DbActorProfile = row.try_get("post_author")?;
let db_attachments: Vec<DbMediaAttachment> = row.try_get("attachments")?;
2021-11-11 19:10:28 +00:00
let db_mentions: Vec<DbActorProfile> = row.try_get("mentions")?;
2021-12-07 23:28:58 +00:00
let db_tags: Vec<String> = row.try_get("tags")?;
let db_links: Vec<Uuid> = row.try_get("links")?;
let db_emojis: Vec<DbEmoji> = row.try_get("emojis")?;
2021-12-07 23:28:58 +00:00
let post = Post::new(
db_post,
db_post_author,
db_attachments,
db_mentions,
db_tags,
db_links,
db_emojis,
2021-12-07 23:28:58 +00:00
)?;
Some(post)
2023-04-24 15:35:32 +00:00
}
2021-10-15 00:27:39 +00:00
None => None,
};
2021-10-12 16:11:47 +00:00
let notification = Self {
id: db_notification.id,
sender: db_sender,
2023-04-25 11:19:04 +00:00
recipient: db_recipient,
2021-10-15 00:27:39 +00:00
post: maybe_post,
event_type: db_notification.event_type,
2021-10-12 16:11:47 +00:00
created_at: db_notification.created_at,
};
Ok(notification)
}
}