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

266 lines
6.7 KiB
Rust
Raw Normal View History

2021-10-12 16:11:47 +00:00
use uuid::Uuid;
use crate::database::{DatabaseClient, DatabaseError};
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
};
2021-10-12 16:11:47 +00:00
use super::types::{EventType, Notification};
async fn create_notification(
db_client: &impl DatabaseClient,
2021-10-12 16:11:47 +00:00
sender_id: &Uuid,
recipient_id: &Uuid,
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,
post_id,
2021-10-12 16:11:47 +00:00
event_type
)
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
",
&[&notification_id],
)
.await?;
Ok(())
}
pub async fn create_follow_notification(
db_client: &impl DatabaseClient,
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-15 00:27:39 +00:00
pub async fn create_reply_notification(
db_client: &impl DatabaseClient,
sender_id: &Uuid,
recipient_id: &Uuid,
post_id: &Uuid,
2021-10-15 00:27:39 +00:00
) -> Result<(), DatabaseError> {
create_notification(
2023-04-24 15:35:32 +00:00
db_client,
sender_id,
recipient_id,
Some(post_id),
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(
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
}
pub async fn create_mention_notification(
db_client: &impl DatabaseClient,
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),
EventType::Mention,
2023-04-24 15:35:32 +00:00
)
.await
}
pub async fn create_repost_notification(
db_client: &impl DatabaseClient,
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),
EventType::Repost,
2023-04-24 15:35:32 +00:00
)
.await
}
pub async fn create_subscription_notification(
db_client: &impl DatabaseClient,
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,
EventType::Subscription,
2023-04-24 15:35:32 +00:00
)
.await
}
pub async fn create_subscription_expiration_notification(
db_client: &impl DatabaseClient,
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,
EventType::SubscriptionExpiration,
2023-04-24 15:35:32 +00:00
)
.await
}
pub async fn create_move_notification(
db_client: &impl DatabaseClient,
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
}
2021-10-12 16:11:47 +00:00
pub async fn get_notifications(
db_client: &impl DatabaseClient,
2021-10-12 16:11:47 +00:00
recipient_id: &Uuid,
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> {
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},
{related_tags},
{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
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,
);
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)
}