You've already forked AstralRinth
forked from xxxOFFxxx/AstralRinth
[DO NOT MERGE] Email notification system (#4338)
* Migration * Fixup db models * Redis * Stuff * Switch PKs to BIGSERIALs, insert to notifications_deliveries when inserting notifications * Queue, templates * Query cache * Fixes, fixtures * Perf, cache template data & HTML bodies * Notification type configuration, ResetPassword notification type * Reset password * Query cache * Clippy + fmt * Traces, fix typo, fix user email in ResetPassword * send_email * Models, db * Remove dead code, adjust notification settings in migration * Clippy fmt * Delete dead code, fixes * Fmt * Update apps/labrinth/src/queue/email.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Remove old fixtures * Unify email retry delay * Fix type * External notifications * Remove `notifications_types_preference_restrictions`, as user notification preferences is out of scope for this PR * Query cache, fmt, clippy * Fix join in get_many_user_exposed_on_site * Remove migration comment * Query cache * Update html body urls * Remove comment * Add paymentfailed.service variable to PaymentFailed notification variant * Fix compile error * Fix deleting notifications * Update apps/labrinth/src/database/models/user_item.rs Co-authored-by: Josiah Glosson <soujournme@gmail.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Update apps/labrinth/src/database/models/user_item.rs Co-authored-by: Josiah Glosson <soujournme@gmail.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Update Cargo.toml Co-authored-by: Josiah Glosson <soujournme@gmail.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Update apps/labrinth/migrations/20250902133943_notification-extension.sql Co-authored-by: Josiah Glosson <soujournme@gmail.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Address review comments * Fix compliation * Update apps/labrinth/src/database/models/users_notifications_preferences_item.rs Co-authored-by: Josiah Glosson <soujournme@gmail.com> Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> * Use strfmt to format emails * Configurable Reply-To * Configurable Reply-To * Refactor for email background task * Send some emails inline * Fix account creation email check * Revert "Use strfmt to format emails" This reverts commit e0d6614afe51fa6349918377e953ba294c34ae0b. * Reintroduce fill_template * Set password reset email inline * Process more emails per index * clippy fmt * Query cache --------- Signed-off-by: François-Xavier Talbot <108630700+fetchfern@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Josiah Glosson <soujournme@gmail.com>
This commit is contained in:
committed by
GitHub
parent
1491642209
commit
902d749293
@@ -0,0 +1,111 @@
|
||||
use super::ids::*;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::models::v3::notifications::{NotificationChannel, NotificationType};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UserNotificationPreference {
|
||||
pub id: i64,
|
||||
pub user_id: Option<DBUserId>,
|
||||
pub channel: NotificationChannel,
|
||||
pub notification_type: NotificationType,
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
struct UserNotificationPreferenceQueryResult {
|
||||
id: i64,
|
||||
user_id: Option<i64>,
|
||||
channel: String,
|
||||
notification_type: String,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl From<UserNotificationPreferenceQueryResult>
|
||||
for UserNotificationPreference
|
||||
{
|
||||
fn from(r: UserNotificationPreferenceQueryResult) -> Self {
|
||||
UserNotificationPreference {
|
||||
id: r.id,
|
||||
user_id: r.user_id.map(DBUserId),
|
||||
channel: NotificationChannel::from_str_or_default(&r.channel),
|
||||
notification_type: NotificationType::from_str_or_default(
|
||||
&r.notification_type,
|
||||
),
|
||||
enabled: r.enabled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserNotificationPreference {
|
||||
pub async fn get_user_or_default(
|
||||
user_id: DBUserId,
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<Vec<UserNotificationPreference>, DatabaseError> {
|
||||
Self::get_many_users_or_default(&[user_id], exec).await
|
||||
}
|
||||
|
||||
pub async fn get_many_users_or_default(
|
||||
user_ids: &[DBUserId],
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<Vec<UserNotificationPreference>, DatabaseError> {
|
||||
let results = sqlx::query!(
|
||||
r#"
|
||||
SELECT
|
||||
COALESCE(unp.id, dnp.id) "id!",
|
||||
unp.user_id,
|
||||
dnp.channel "channel!",
|
||||
dnp.notification_type "notification_type!",
|
||||
COALESCE(unp.enabled, dnp.enabled, false) "enabled!"
|
||||
FROM users_notifications_preferences dnp
|
||||
LEFT JOIN users_notifications_preferences unp
|
||||
ON unp.channel = dnp.channel
|
||||
AND unp.notification_type = dnp.notification_type
|
||||
AND unp.user_id = ANY($1::bigint[])
|
||||
"#,
|
||||
&user_ids.iter().map(|x| x.0).collect::<Vec<_>>(),
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?;
|
||||
|
||||
let preferences = results
|
||||
.into_iter()
|
||||
.map(|r| UserNotificationPreference {
|
||||
id: r.id,
|
||||
user_id: r.user_id.map(DBUserId),
|
||||
channel: NotificationChannel::from_str_or_default(&r.channel),
|
||||
notification_type: NotificationType::from_str_or_default(
|
||||
&r.notification_type,
|
||||
),
|
||||
enabled: r.enabled,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(preferences)
|
||||
}
|
||||
|
||||
/// Inserts the row into the table and updates its ID.
|
||||
pub async fn insert(
|
||||
&mut self,
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let id = sqlx::query_scalar!(
|
||||
"
|
||||
INSERT INTO users_notifications_preferences (
|
||||
user_id, channel, notification_type, enabled
|
||||
)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id
|
||||
",
|
||||
self.user_id.map(|x| x.0),
|
||||
self.channel.as_str(),
|
||||
self.notification_type.as_str(),
|
||||
self.enabled,
|
||||
)
|
||||
.fetch_one(exec)
|
||||
.await?;
|
||||
|
||||
self.id = id;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user