2021-10-12 16:11:47 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use postgres_types::FromSql;
|
|
|
|
use tokio_postgres::Row;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-11-19 17:32:51 +00:00
|
|
|
use crate::database::int_enum::{int_enum_from_sql, int_enum_to_sql};
|
2021-10-12 16:11:47 +00:00
|
|
|
use crate::errors::{ConversionError, DatabaseError};
|
2021-10-15 00:27:39 +00:00
|
|
|
use crate::models::attachments::types::DbMediaAttachment;
|
|
|
|
use crate::models::posts::types::{DbPost, Post};
|
2021-10-12 16:11:47 +00:00
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
|
|
|
|
2021-11-19 17:32:51 +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,
|
2021-11-30 22:03:07 +00:00
|
|
|
Mention,
|
2021-11-30 22:18:20 +00:00
|
|
|
Repost,
|
2022-05-28 00:44:52 +00:00
|
|
|
Subscription,
|
2022-07-14 17:25:48 +00:00
|
|
|
SubscriptionStart,
|
|
|
|
SubscriptionExpiration,
|
2021-10-12 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 17:32:51 +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,
|
2021-11-30 22:03:07 +00:00
|
|
|
EventType::Mention => 5,
|
2021-11-30 22:18:20 +00:00
|
|
|
EventType::Repost => 6,
|
2022-05-28 00:44:52 +00:00
|
|
|
EventType::Subscription => 7,
|
2022-07-23 20:21:23 +00:00
|
|
|
EventType::SubscriptionStart => unimplemented!("not supported"),
|
2022-07-14 17:25:48 +00:00
|
|
|
EventType::SubscriptionExpiration => 9,
|
2021-10-12 16:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<i16> for EventType {
|
|
|
|
type Error = ConversionError;
|
|
|
|
|
|
|
|
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,
|
2021-11-30 22:03:07 +00:00
|
|
|
5 => Self::Mention,
|
2021-11-30 22:18:20 +00:00
|
|
|
6 => Self::Repost,
|
2022-05-28 00:44:52 +00:00
|
|
|
7 => Self::Subscription,
|
2022-07-14 17:25:48 +00:00
|
|
|
8 => Self::SubscriptionStart,
|
|
|
|
9 => Self::SubscriptionExpiration,
|
2021-10-12 16:11:47 +00:00
|
|
|
_ => return Err(ConversionError),
|
|
|
|
};
|
|
|
|
Ok(event_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 17:32:51 +00:00
|
|
|
int_enum_from_sql!(EventType);
|
|
|
|
int_enum_to_sql!(EventType);
|
|
|
|
|
2022-05-28 00:44:52 +00:00
|
|
|
#[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,
|
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")?;
|
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")?;
|
2022-08-21 21:15:30 +00:00
|
|
|
let db_links: Vec<Uuid> = row.try_get("links")?;
|
2021-12-07 23:28:58 +00:00
|
|
|
let post = Post::new(
|
|
|
|
db_post,
|
|
|
|
db_post_author,
|
|
|
|
db_attachments,
|
|
|
|
db_mentions,
|
|
|
|
db_tags,
|
2022-08-21 21:15:30 +00:00
|
|
|
db_links,
|
2021-12-07 23:28:58 +00:00
|
|
|
)?;
|
2021-11-21 15:19:50 +00:00
|
|
|
Some(post)
|
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,
|
2021-10-15 00:27:39 +00:00
|
|
|
post: maybe_post,
|
2021-11-19 17:32:51 +00:00
|
|
|
event_type: db_notification.event_type,
|
2021-10-12 16:11:47 +00:00
|
|
|
created_at: db_notification.created_at,
|
|
|
|
};
|
|
|
|
Ok(notification)
|
|
|
|
}
|
|
|
|
}
|