2021-10-12 16:11:47 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-01-17 23:14:18 +00:00
|
|
|
use crate::database::{DatabaseClient, DatabaseError};
|
2023-03-30 20:27:17 +00:00
|
|
|
use crate::posts::{
|
|
|
|
helpers::{add_related_posts, add_user_actions},
|
2023-04-24 15:35:32 +00:00
|
|
|
queries::{RELATED_ATTACHMENTS, RELATED_EMOJIS, RELATED_LINKS, RELATED_MENTIONS, RELATED_TAGS},
|
2021-12-07 23:28:58 +00:00
|
|
|
};
|
2023-03-30 20:27:17 +00:00
|
|
|
|
2021-10-12 16:11:47 +00:00
|
|
|
use super::types::{EventType, Notification};
|
|
|
|
|
2021-10-28 23:26:31 +00:00
|
|
|
async fn create_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-10-12 16:11:47 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
2021-10-28 23:26:31 +00:00
|
|
|
post_id: Option<&Uuid>,
|
2021-10-12 16:11:47 +00:00
|
|
|
event_type: EventType,
|
|
|
|
) -> Result<(), DatabaseError> {
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client
|
|
|
|
.execute(
|
|
|
|
"
|
2021-10-12 16:11:47 +00:00
|
|
|
INSERT INTO notification (
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
2021-10-28 23:26:31 +00:00
|
|
|
post_id,
|
2021-10-12 16:11:47 +00:00
|
|
|
event_type
|
|
|
|
)
|
2021-10-28 23:26:31 +00:00
|
|
|
VALUES ($1, $2, $3, $4)
|
2021-10-12 16:11:47 +00:00
|
|
|
",
|
2023-04-24 15:35:32 +00:00
|
|
|
&[&sender_id, &recipient_id, &post_id, &event_type],
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-12 16:11:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:19:04 +00:00
|
|
|
pub async fn delete_notification(
|
|
|
|
db_client: &impl DatabaseClient,
|
|
|
|
notification_id: i32,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
db_client
|
|
|
|
.execute(
|
|
|
|
"
|
|
|
|
DELETE FROM notification
|
|
|
|
WHERE id = $1
|
|
|
|
",
|
|
|
|
&[¬ification_id],
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:26:31 +00:00
|
|
|
pub async fn create_follow_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-10-28 23:26:31 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
2023-04-24 15:35:32 +00:00
|
|
|
create_notification(db_client, sender_id, recipient_id, None, EventType::Follow).await
|
2021-10-28 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:27:39 +00:00
|
|
|
pub async fn create_reply_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-10-28 23:26:31 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
post_id: &Uuid,
|
2021-10-15 00:27:39 +00:00
|
|
|
) -> Result<(), DatabaseError> {
|
2021-10-28 23:26:31 +00:00
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
Some(post_id),
|
2021-10-28 23:26:31 +00:00
|
|
|
EventType::Reply,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2021-10-15 00:27:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 12:30:28 +00:00
|
|
|
pub async fn create_reaction_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-10-29 12:30:28 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
post_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
Some(post_id),
|
2021-10-29 12:30:28 +00:00
|
|
|
EventType::Reaction,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2021-10-29 12:30:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:03:07 +00:00
|
|
|
pub async fn create_mention_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-11-30 22:03:07 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
post_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
Some(post_id),
|
2021-11-30 22:03:07 +00:00
|
|
|
EventType::Mention,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2021-11-30 22:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 22:18:20 +00:00
|
|
|
pub async fn create_repost_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-11-30 22:18:20 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
post_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
Some(post_id),
|
2021-11-30 22:18:20 +00:00
|
|
|
EventType::Repost,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2021-11-30 22:18:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 00:44:52 +00:00
|
|
|
pub async fn create_subscription_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2022-05-28 00:44:52 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
None,
|
2022-05-28 00:44:52 +00:00
|
|
|
EventType::Subscription,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2022-05-28 00:44:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 17:25:48 +00:00
|
|
|
pub async fn create_subscription_expiration_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2022-07-14 17:25:48 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
|
|
|
create_notification(
|
2023-04-24 15:35:32 +00:00
|
|
|
db_client,
|
|
|
|
sender_id,
|
|
|
|
recipient_id,
|
|
|
|
None,
|
2022-07-14 17:25:48 +00:00
|
|
|
EventType::SubscriptionExpiration,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await
|
2022-07-14 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 19:19:57 +00:00
|
|
|
pub async fn create_move_notification(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2022-10-22 19:19:57 +00:00
|
|
|
sender_id: &Uuid,
|
|
|
|
recipient_id: &Uuid,
|
|
|
|
) -> Result<(), DatabaseError> {
|
2023-04-24 15:35:32 +00:00
|
|
|
create_notification(db_client, sender_id, recipient_id, None, EventType::Move).await
|
2022-10-22 19:19:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 16:11:47 +00:00
|
|
|
pub async fn get_notifications(
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &impl DatabaseClient,
|
2021-10-12 16:11:47 +00:00
|
|
|
recipient_id: &Uuid,
|
2022-01-14 20:43:04 +00:00
|
|
|
max_id: Option<i32>,
|
2022-09-29 21:36:55 +00:00
|
|
|
limit: u16,
|
2021-10-12 16:11:47 +00:00
|
|
|
) -> Result<Vec<Notification>, DatabaseError> {
|
2021-11-11 18:35:27 +00:00
|
|
|
let statement = format!(
|
2021-10-12 16:11:47 +00:00
|
|
|
"
|
2021-10-15 00:27:39 +00:00
|
|
|
SELECT
|
2023-04-25 11:19:04 +00:00
|
|
|
notification, sender, post, post_author, recipient,
|
2021-11-11 19:10:28 +00:00
|
|
|
{related_attachments},
|
2021-12-07 23:28:58 +00:00
|
|
|
{related_mentions},
|
2022-08-21 21:15:30 +00:00
|
|
|
{related_tags},
|
2023-01-07 19:28:26 +00:00
|
|
|
{related_links},
|
|
|
|
{related_emojis}
|
2021-10-12 16:11:47 +00:00
|
|
|
FROM notification
|
|
|
|
JOIN actor_profile AS sender
|
|
|
|
ON notification.sender_id = sender.id
|
2021-10-15 00:27:39 +00:00
|
|
|
LEFT JOIN post
|
|
|
|
ON notification.post_id = post.id
|
|
|
|
LEFT JOIN actor_profile AS post_author
|
|
|
|
ON post.author_id = post_author.id
|
2023-04-25 11:19:04 +00:00
|
|
|
LEFT JOIN actor_profile AS recipient
|
|
|
|
ON notification.recipient_id = recipient.id
|
2022-01-14 20:43:04 +00:00
|
|
|
WHERE
|
|
|
|
recipient_id = $1
|
|
|
|
AND ($2::integer IS NULL OR notification.id < $2)
|
|
|
|
ORDER BY notification.id DESC
|
|
|
|
LIMIT $3
|
2021-10-12 16:11:47 +00:00
|
|
|
",
|
2023-04-24 15:35:32 +00:00
|
|
|
related_attachments = RELATED_ATTACHMENTS,
|
|
|
|
related_mentions = RELATED_MENTIONS,
|
|
|
|
related_tags = RELATED_TAGS,
|
|
|
|
related_links = RELATED_LINKS,
|
|
|
|
related_emojis = RELATED_EMOJIS,
|
2021-11-11 18:35:27 +00:00
|
|
|
);
|
2023-04-24 15:35:32 +00:00
|
|
|
let rows = db_client
|
|
|
|
.query(&statement, &[&recipient_id, &max_id, &i64::from(limit)])
|
|
|
|
.await?;
|
|
|
|
let mut notifications: Vec<Notification> = rows
|
|
|
|
.iter()
|
2021-11-13 17:37:31 +00:00
|
|
|
.map(Notification::try_from)
|
2021-10-12 16:11:47 +00:00
|
|
|
.collect::<Result<_, _>>()?;
|
2022-10-06 23:44:00 +00:00
|
|
|
add_related_posts(
|
|
|
|
db_client,
|
2023-04-24 15:35:32 +00:00
|
|
|
notifications
|
|
|
|
.iter_mut()
|
2022-10-06 23:44:00 +00:00
|
|
|
.filter_map(|item| item.post.as_mut())
|
|
|
|
.collect(),
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2022-10-06 23:44:00 +00:00
|
|
|
add_user_actions(
|
|
|
|
db_client,
|
|
|
|
recipient_id,
|
2023-04-24 15:35:32 +00:00
|
|
|
notifications
|
|
|
|
.iter_mut()
|
2022-10-06 23:44:00 +00:00
|
|
|
.filter_map(|item| item.post.as_mut())
|
|
|
|
.collect(),
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-10-12 16:11:47 +00:00
|
|
|
Ok(notifications)
|
|
|
|
}
|
2023-04-25 11:19:04 +00:00
|
|
|
|
|
|
|
pub async fn get_mention_notifications(
|
|
|
|
db_client: &impl DatabaseClient,
|
|
|
|
limit: u16,
|
|
|
|
) -> Result<Vec<Notification>, DatabaseError> {
|
|
|
|
let statement = format!(
|
|
|
|
"
|
|
|
|
SELECT
|
|
|
|
notification, sender, post, post_author, recipient,
|
|
|
|
{related_attachments},
|
|
|
|
{related_mentions},
|
|
|
|
{related_tags},
|
|
|
|
{related_links},
|
|
|
|
{related_emojis}
|
|
|
|
FROM notification
|
|
|
|
JOIN actor_profile AS sender
|
|
|
|
ON notification.sender_id = sender.id
|
|
|
|
LEFT JOIN post
|
|
|
|
ON notification.post_id = post.id
|
|
|
|
LEFT JOIN actor_profile AS post_author
|
|
|
|
ON post.author_id = post_author.id
|
|
|
|
LEFT JOIN actor_profile AS recipient
|
|
|
|
ON notification.recipient_id = recipient.id
|
|
|
|
WHERE
|
|
|
|
event_type = $1
|
|
|
|
ORDER BY notification.id DESC
|
|
|
|
LIMIT $2
|
|
|
|
",
|
|
|
|
related_attachments = RELATED_ATTACHMENTS,
|
|
|
|
related_mentions = RELATED_MENTIONS,
|
|
|
|
related_tags = RELATED_TAGS,
|
|
|
|
related_links = RELATED_LINKS,
|
|
|
|
related_emojis = RELATED_EMOJIS,
|
|
|
|
);
|
|
|
|
let rows = db_client
|
2023-04-26 10:55:42 +00:00
|
|
|
.query(&statement, &[&EventType::Mention, &i64::from(limit)])
|
2023-04-25 11:19:04 +00:00
|
|
|
.await?;
|
2023-04-25 13:49:35 +00:00
|
|
|
let notifications: Vec<Notification> = rows
|
2023-04-25 11:19:04 +00:00
|
|
|
.iter()
|
|
|
|
.map(Notification::try_from)
|
|
|
|
.collect::<Result<_, _>>()?;
|
|
|
|
Ok(notifications)
|
|
|
|
}
|