Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! Database repository adapter for notifications.
#[cfg(test)]
mod test;
use crate::domain::models::device::DeviceType;
use crate::domain::models::request::{
NotificationCategory, NotificationListFilters, NotificationStatus,
};
use crate::domain::models::{
DeviceEndpoint, DisabledNotificationType, NotificationIdAndCollapseKey,
SendNotificationRequestBuilder, TaggedContent, UserNotificationRow,
};
use crate::domain::ports::NotificationRepository;
use crate::outbound::device_registration::DeviceRegistrationDbOps;
use chrono::{DateTime, Utc};
use macro_user_id::cowlike::CowLike;
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::{Entity, EntityType};
use models_pagination::{CreatedAt, Query};
use notification_state::NotificationState;
use rootcause::Report;
use serde::Serialize;
use serde::de::DeserializeOwned;
use sqlx::{PgPool, Postgres, QueryBuilder};
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use std::sync::Arc;
use uuid::Uuid;
type UserNotificationListRow = (
String,
Uuid,
String,
String,
bool,
NotificationState,
DateTime<Utc>,
Option<DateTime<Utc>>,
DateTime<Utc>,
Option<DateTime<Utc>>,
serde_json::Value,
String,
Option<String>,
);
struct UpdatedUserNotificationRow {
owner_id: String,
notification_id: Uuid,
event_item_id: String,
event_item_type: String,
sent: bool,
state: NotificationState,
created_at: DateTime<Utc>,
viewed_at: Option<DateTime<Utc>>,
updated_at: DateTime<Utc>,
deleted_at: Option<DateTime<Utc>>,
notification_metadata: serde_json::Value,
notification_event_type: String,
sender_id: Option<String>,
}
impl UpdatedUserNotificationRow {
fn into_domain(self) -> Result<UserNotificationRow<serde_json::Value>, Report> {
let entity = EntityType::from_str(&self.event_item_type)
.map_err(|error| rootcause::report!(error))?
.with_entity_string(self.event_item_id);
let sender_id = self
.sender_id
.as_deref()
.map(|sender| MacroUserIdStr::parse_from_str(sender).map(CowLike::into_owned))
.transpose()
.map_err(|error| rootcause::report!(error))?;
let owner_id = MacroUserIdStr::parse_from_str(&self.owner_id)
.map(CowLike::into_owned)
.map_err(|error| rootcause::report!(error))?;
Ok(UserNotificationRow {
owner_id,
notification_id: self.notification_id,
notification_event_type: self.notification_event_type,
entity,
sent: self.sent,
state: self.state,
created_at: self.created_at,
viewed_at: self.viewed_at,
updated_at: self.updated_at,
deleted_at: self.deleted_at,
notification_metadata: self.notification_metadata,
sender_id,
})
}
}
struct UserNotificationsQueryArgs<'a> {
user_id: &'a str,
event_item_ids: Option<&'a [String]>,
limit: i64,
cursor_id: Option<Uuid>,
cursor_timestamp: Option<DateTime<Utc>>,
filters: &'a NotificationListFilters,
}
fn build_user_notifications_query<'a>(
args: UserNotificationsQueryArgs<'a>,
) -> QueryBuilder<'a, Postgres> {
let UserNotificationsQueryArgs {
user_id,
event_item_ids,
limit,
cursor_id,
cursor_timestamp,
filters,
} = args;
let mut builder = QueryBuilder::new(
r#"
SELECT
un.user_id as owner_id,
un.notification_id,
n.event_item_id,
n.event_item_type,
un.sent,
un.state,
un.created_at::timestamptz as created_at,
un.seen_at::timestamptz as viewed_at,
un.created_at::timestamptz as updated_at,
un.deleted_at::timestamptz as deleted_at,
n.metadata as notification_metadata,
n.notification_event_type as notification_event_type,
n.sender_id as sender_id
FROM user_notification un
JOIN notification n ON n.id = un.notification_id
WHERE un.user_id = "#,
);
builder.push_bind(user_id);
push_event_item_ids_filter(&mut builder, event_item_ids);
push_notification_status_filters(&mut builder, filters);
push_include_types_filter(&mut builder, &filters.include_types);
push_entities_filter(&mut builder, &filters.entities);
push_cursor_filter(&mut builder, cursor_timestamp, cursor_id);
builder.push(" ORDER BY un.created_at DESC, un.notification_id DESC LIMIT ");
builder.push_bind(limit);
builder
}
fn push_event_item_ids_filter<'a>(
builder: &mut QueryBuilder<'a, Postgres>,
event_item_ids: Option<&'a [String]>,
) {
if let Some(event_item_ids) = event_item_ids {
builder.push(" AND n.event_item_id = ANY(");
builder.push_bind(event_item_ids);
builder.push(")");
}
}
fn push_notification_status_filters(
builder: &mut QueryBuilder<'_, Postgres>,
filters: &NotificationListFilters,
) {
builder.push(" AND un.deleted_at IS NULL");
if !filters.states.is_empty() {
builder.push(" AND un.state = ANY(");
builder.push_bind(filters.states.clone());
builder.push(")");
}
}
/// SQL fragment matching every GitHub notification event type. Keep in sync with
/// the `Notification::TYPE_NAME`s of the GitHub metadata types in `model_notifications`
/// (this crate sits below `model_notifications`, so it cannot reference them directly).
const GITHUB_EVENT_TYPES_SQL: &str = concat!(
"n.notification_event_type IN (",
"'github_pr_status_changed', ",
"'github_review_requested', ",
"'github_pr_comment', ",
"'github_pr_mention', ",
"'github_pr_review', ",
"'github_pr_check_run'",
")",
);
fn push_include_types_filter(
builder: &mut QueryBuilder<'_, Postgres>,
include_types: &[NotificationCategory],
) {
if !include_types.is_empty() {
builder.push(" AND (");
let mut needs_or = false;
for clause in [
include_types.contains(&NotificationCategory::Email)
.then_some("n.event_item_type = 'email_thread'"),
include_types.contains(&NotificationCategory::Message).then_some(
r#"(
n.notification_event_type IN ('channel_mention', 'channel_message_reply', 'channel_message_send')
OR n.metadata ? 'messageId'
OR n.metadata ? 'message_id'
)"#,
),
include_types.contains(&NotificationCategory::Channel)
.then_some("n.event_item_type = 'channel'"),
include_types.contains(&NotificationCategory::Document).then_some(
"n.event_item_type = 'document' AND COALESCE(n.metadata->>'subType', n.metadata->>'sub_type', '') <> 'task'",
),
include_types.contains(&NotificationCategory::Task).then_some(
"n.event_item_type = 'document' AND COALESCE(n.metadata->>'subType', n.metadata->>'sub_type', '') = 'task'",
),
include_types.contains(&NotificationCategory::Project)
.then_some("n.event_item_type = 'project'"),
include_types.contains(&NotificationCategory::Chat)
.then_some("n.event_item_type = 'chat'"),
include_types.contains(&NotificationCategory::Call)
.then_some("n.event_item_type = 'call'"),
include_types.contains(&NotificationCategory::Github)
.then_some(GITHUB_EVENT_TYPES_SQL),
include_types.contains(&NotificationCategory::Reminder)
.then_some("n.event_item_type = 'reminder'"),
include_types.contains(&NotificationCategory::Calendar)
.then_some("n.event_item_type = 'calendar_event'"),
include_types.contains(&NotificationCategory::Agent)
.then_some("n.event_item_type = 'agent_session'"),
]
.into_iter()
.flatten()
{
if needs_or {
builder.push(" OR ");
}
builder.push("(");
builder.push(clause);
builder.push(")");
needs_or = true;
}
builder.push(")");
}
}
fn push_entities_filter<'a>(
builder: &mut QueryBuilder<'a, Postgres>,
entities: &'a [Entity<'static>],
) {
if entities.is_empty() {
return;
}
builder.push(" AND (");
for (index, entity) in entities.iter().enumerate() {
if index > 0 {
builder.push(" OR ");
}
builder.push("((n.event_item_type = ");
builder.push_bind(entity.entity_type.as_ref());
builder.push(" AND n.event_item_id = ");
builder.push_bind(entity.entity_id.as_ref());
builder.push(") OR (n.secondary_event_item_type = ");
builder.push_bind(entity.entity_type.as_ref());
builder.push(" AND n.secondary_event_item_id = ");
builder.push_bind(entity.entity_id.as_ref());
builder.push(")");
if entity.entity_type == EntityType::ChannelMessage {
builder
.push(" OR COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = ");
builder.push_bind(entity.entity_id.as_ref());
}
builder.push(")");
}
builder.push(")");
}
fn push_cursor_filter(
builder: &mut QueryBuilder<'_, Postgres>,
cursor_timestamp: Option<DateTime<Utc>>,
cursor_id: Option<Uuid>,
) {
if let (Some(cursor_timestamp), Some(cursor_id)) = (cursor_timestamp, cursor_id) {
builder.push(" AND (un.created_at, un.notification_id) < (");
builder.push_bind(cursor_timestamp);
builder.push(", ");
builder.push_bind(cursor_id);
builder.push(")");
}
}
/// Local representation of the `notification_device_type_option` Postgres enum
/// for compile-time checked sqlx queries. The domain `DeviceType` stays
/// sqlx-free; adapters convert at the boundary.
#[derive(Debug, sqlx::Type)]
#[sqlx(
type_name = "notification_device_type_option",
rename_all = "lowercase"
)]
pub(crate) enum DbDeviceType {
Ios,
Android,
#[sqlx(rename = "iosvoip")]
IosVoip,
}
impl From<&DeviceType> for DbDeviceType {
fn from(value: &DeviceType) -> Self {
match value {
DeviceType::Ios => Self::Ios,
DeviceType::Android => Self::Android,
DeviceType::IosVoip => Self::IosVoip,
}
}
}
/// Database-backed implementation of the notification repository port.
///
/// This adapter handles all database operations for notifications including
/// creating notifications, checking user preferences, and managing device endpoints.
pub struct DbNotificationRepository<D> {
db: D,
}
impl<D> DbNotificationRepository<D> {
/// Create a new database notification repository.
pub fn new(db: D) -> Self {
Self { db }
}
}
/// Trait for database operations needed by the notification repository.
///
/// This allows the adapter to work with different database client implementations.
pub trait NotificationDbOps: DeviceRegistrationDbOps + Send + Sync + 'static {
/// Get users who have muted all notifications.
fn get_muted_users<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> impl std::future::Future<Output = Result<HashSet<MacroUserIdStr<'static>>, Report>> + Send;
/// Get users who have unsubscribed from notifications for a specific item.
fn get_unsubscribed_users<'a>(
&self,
item_id: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> impl std::future::Future<Output = Result<HashSet<MacroUserIdStr<'static>>, Report>> + Send;
/// Get device endpoints for the given users.
fn get_device_endpoints<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> impl std::future::Future<
Output = Result<HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>, Report>,
> + Send;
/// Create a notification record in the database.
///
/// Returns new or unpublished recipient rows; completed or withdrawn notifications return None.
fn create_notification<'a, T: Serialize + Send + Sync>(
&self,
request: SendNotificationRequestBuilder<'a, TaggedContent<T>>,
notification_id: Uuid,
service_name: &str,
apns_collapse_key: Option<&str>,
) -> impl std::future::Future<Output = Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report>> + Send;
/// Whether a resolved workflow has withdrawn this notification before delivery.
fn notification_is_withdrawn(
&self,
notification_id: Uuid,
) -> impl std::future::Future<Output = Result<bool, Report>> + Send;
/// Record successful delivery-queue publication; retries remain pending until this succeeds.
fn complete_notification_publication(
&self,
notification_id: Uuid,
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Update the sent status for recipients who received the notification.
fn update_sent_status<'a>(
&self,
notification_id: Uuid,
user_ids: &[MacroUserIdStr<'a>],
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Atomically acknowledge notifications, preserving done state and existing view times.
fn mark_notifications_seen(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> impl std::future::Future<
Output = Result<Vec<UserNotificationRow<serde_json::Value>>, Report>,
> + Send;
/// Atomically mark done or reopen done notifications as seen, preserving view times.
fn mark_notifications_done(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
done: bool,
) -> impl std::future::Future<
Output = Result<Vec<UserNotificationRow<serde_json::Value>>, Report>,
> + Send;
/// Get matching user-owned IDs that need a status transition or initial viewing timestamp.
fn get_notification_ids_for_entities(
&self,
user_id: &MacroUserIdStr<'_>,
entities: &[Entity<'_>],
status: &NotificationStatus,
) -> impl std::future::Future<Output = Result<Vec<Uuid>, Report>> + Send;
/// Get basic notification data (collapse keys) for push clearing.
fn get_basic_notifications(
&self,
notification_ids: &[Uuid],
) -> impl std::future::Future<Output = Result<Vec<NotificationIdAndCollapseKey>, Report>> + Send;
/// Return notification IDs that still exist for the user and are eligible for digest email.
///
/// Includes only unseen notifications that exist and are not soft-deleted.
fn get_digest_eligible_notification_ids(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> impl std::future::Future<Output = Result<HashSet<Uuid>, Report>> + Send;
/// Get a user's non-deleted notifications with cursor-based pagination.
///
/// The metadata JSON column is deserialized into `T`. `filters` selects exact states.
fn get_user_notifications<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> impl std::future::Future<Output = Result<Vec<UserNotificationRow<T>>, Report>> + Send;
/// Get a user's non-deleted notifications filtered by event item IDs, with cursor-based pagination.
fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
event_item_ids: &[Uuid],
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> impl std::future::Future<Output = Result<Vec<UserNotificationRow<T>>, Report>> + Send;
/// Get viewer-owned notification edges with per-entity filtering and limits.
fn get_entity_notifications_batch(
&self,
user_id: MacroUserIdStr<'_>,
entities: Vec<Entity<'static>>,
query: crate::domain::models::entity_query::EntityNotificationQuery,
) -> impl std::future::Future<
Output = Result<
HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>,
Report,
>,
> + Send;
/// Get a single user notification by ID.
fn get_user_notification_by_id<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> impl std::future::Future<Output = Result<Option<UserNotificationRow<T>>, Report>> + Send;
/// Soft-delete a single user notification.
fn delete_user_notification(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Soft-delete multiple user notifications.
fn bulk_delete_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Hard-delete all notifications for a user.
fn delete_all_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Get users (from the given set) who have disabled the specified notification type.
fn get_users_with_type_disabled<'a>(
&self,
notification_event_type: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> impl std::future::Future<Output = Result<HashSet<MacroUserIdStr<'static>>, Report>> + Send;
/// Get all disabled notification types for a user.
fn get_disabled_notification_types(
&self,
user_id: MacroUserIdStr<'_>,
) -> impl std::future::Future<Output = Result<Vec<DisabledNotificationType>, Report>> + Send;
/// Disable a notification type for a user (insert).
fn disable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
/// Re-enable a notification type for a user (delete).
fn enable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> impl std::future::Future<Output = Result<(), Report>> + Send;
}
impl NotificationDbOps for PgPool {
async fn get_muted_users<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
let ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();
let muted_users: Vec<String> = sqlx::query_scalar!(
r#"
SELECT user_id FROM user_mute_notification
WHERE user_id = ANY($1)
"#,
&ids
)
.fetch_all(self)
.await?;
let result = muted_users
.into_iter()
.filter_map(|id| {
MacroUserIdStr::parse_from_str(&id)
.map(CowLike::into_owned)
.ok()
})
.map(|id| id.into_owned())
.collect();
Ok(result)
}
async fn get_unsubscribed_users<'a>(
&self,
item_id: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
let ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();
let unsubscribed: Vec<String> = sqlx::query_scalar!(
r#"
SELECT user_id FROM user_notification_item_unsubscribe
WHERE item_id = $1 AND user_id = ANY($2)
AND (snoozed_until IS NULL OR snoozed_until > NOW())
"#,
item_id,
&ids
)
.fetch_all(self)
.await?;
let result = unsubscribed
.into_iter()
.filter_map(|id| {
MacroUserIdStr::parse_from_str(&id)
.map(CowLike::into_owned)
.ok()
})
.map(|id| id.into_owned())
.collect();
Ok(result)
}
async fn get_device_endpoints<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>, Report> {
let ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();
let rows = sqlx::query!(
r#"
SELECT user_id, device_endpoint, device_type as "device_type: DbDeviceType", native_call_capable
FROM notification_user_device_registration
WHERE user_id = ANY($1)
"#,
&ids
)
.fetch_all(self)
.await?;
let mut result: HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>> = HashMap::new();
for row in rows {
let Ok(parsed_id) = MacroUserIdStr::parse_from_str(&row.user_id) else {
continue;
};
let device = match row.device_type {
DbDeviceType::Ios => DeviceEndpoint::Ios(row.device_endpoint),
DbDeviceType::Android if row.native_call_capable => {
DeviceEndpoint::AndroidNativeCall(row.device_endpoint)
}
DbDeviceType::Android => DeviceEndpoint::Android(row.device_endpoint),
DbDeviceType::IosVoip => DeviceEndpoint::IosVoip(row.device_endpoint),
};
result
.entry(parsed_id.into_owned())
.or_default()
.push(device);
}
Ok(result)
}
async fn create_notification<'a, T: Serialize + Send + Sync>(
&self,
request: SendNotificationRequestBuilder<'a, TaggedContent<T>>,
notification_id: Uuid,
service_name: &str,
apns_collapse_key: Option<&str>,
) -> Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report> {
let entity_type: &str = request.notification_entity.entity_type.into();
let secondary_entity_id = request
.secondary_notification_entity
.as_ref()
.map(|entity| entity.entity_id.as_ref());
let secondary_entity_type: Option<&str> = request
.secondary_notification_entity
.as_ref()
.map(|entity| entity.entity_type.into());
let metadata = serde_json::to_value(&request.notification.content)?;
let mut tx = self.begin().await?;
let sender_id = request.sender_id.as_ref().map(|id| id.to_string());
let typename = request.notification.tag.as_ref();
super::withdrawal::lock_notification(&mut tx, notification_id).await?;
let withdrawn = sqlx::query_scalar!(r#"SELECT EXISTS(SELECT 1 FROM notification_withdrawal WHERE notification_id = $1) AS "exists!""#, notification_id).fetch_one(tx.as_mut()).await?;
if withdrawn {
return Ok(None);
}
let existing = sqlx::query_scalar!(
"SELECT publication_complete FROM notification WHERE id = $1",
notification_id
)
.fetch_optional(tx.as_mut())
.await?;
if let Some(complete) = existing {
if complete {
return Ok(None);
}
let recipients = sqlx::query!(
r#"SELECT user_id, sent, state AS "state: NotificationState",
created_at::timestamptz AS "created_at!", seen_at::timestamptz AS viewed_at,
created_at::timestamptz AS "updated_at!", deleted_at::timestamptz AS deleted_at
FROM user_notification WHERE notification_id = $1"#,
notification_id
)
.fetch_all(tx.as_mut())
.await?;
let metadata = Arc::new(request.notification.content);
let rows = recipients
.into_iter()
.map(|recipient| {
Ok(UserNotificationRow {
owner_id: MacroUserIdStr::try_from(recipient.user_id)?,
notification_id,
notification_event_type: typename.to_string(),
entity: request.notification_entity.clone().into_owned(),
sent: recipient.sent,
state: recipient.state,
created_at: recipient.created_at,
viewed_at: recipient.viewed_at,
updated_at: recipient.updated_at,
deleted_at: recipient.deleted_at,
notification_metadata: metadata.clone(),
sender_id: request.sender_id.clone().map(CowLike::into_owned),
})
})
.collect::<Result<Vec<_>, Report>>()?;
tx.commit().await?;
return Ok(Some(rows));
}
if request.recipient_ids.is_empty() {
return Ok(None);
}
// Insert notification and a pending publication checkpoint together.
let result = sqlx::query!(
r#"
INSERT INTO notification (
id,
notification_event_type,
event_item_id,
event_item_type,
service_sender,
metadata,
sender_id,
apns_collapse_key,
secondary_event_item_id,
secondary_event_item_type,
publication_complete
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, false)
ON CONFLICT (id) DO NOTHING
"#,
notification_id,
typename,
request.notification_entity.entity_id.as_ref(),
entity_type,
service_name,
metadata as serde_json::Value,
sender_id,
apns_collapse_key,
secondary_entity_id,
secondary_entity_type
)
.execute(&mut *tx)
.await?;
// Return None early if notification already exists (conflict)
if result.rows_affected() == 0 {
return Ok(None);
}
// Insert user notifications
let user_ids: Vec<String> = request
.recipient_ids
.iter()
.map(|id| id.to_string())
.collect();
let created_at = sqlx::query_scalar!(
r#"
INSERT INTO user_notification (
notification_id,
user_id,
event_item_type,
event_item_id,
notification_event_type
)
SELECT $1, user_id, $3, $4, $5
FROM UNNEST($2::text[]) as user_id
RETURNING created_at::timestamptz as "created_at!"
"#,
notification_id,
&user_ids,
entity_type,
request.notification_entity.entity_id.as_ref(),
typename
)
.fetch_one(&mut *tx)
.await?;
tx.commit().await?;
let entity = request.notification_entity.clone().into_owned();
let sender_id = request.sender_id.as_ref().map(|id| id.clone().into_owned());
let n = Arc::new(request.notification.content);
let rows = request
.recipient_ids
.iter()
.map(|recipient| UserNotificationRow {
owner_id: recipient.clone().into_owned(),
notification_id,
notification_event_type: typename.to_string(),
entity: entity.clone(),
sent: false,
state: NotificationState::Unseen,
created_at,
viewed_at: None,
updated_at: created_at,
deleted_at: None,
notification_metadata: n.clone(),
sender_id: sender_id.clone(),
})
.collect();
Ok(Some(rows))
}
async fn notification_is_withdrawn(&self, notification_id: Uuid) -> Result<bool, Report> {
Ok(sqlx::query_scalar!(
r#"SELECT EXISTS(SELECT 1 FROM notification_withdrawal WHERE notification_id = $1) AS "exists!""#,
notification_id
).fetch_one(self).await?)
}
async fn complete_notification_publication(&self, notification_id: Uuid) -> Result<(), Report> {
sqlx::query!(
"UPDATE notification SET publication_complete = true WHERE id = $1",
notification_id
)
.execute(self)
.await?;
Ok(())
}
async fn update_sent_status<'a>(
&self,
notification_id: Uuid,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<(), Report> {
let ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();
sqlx::query!(
r#"
UPDATE user_notification
SET sent = true
WHERE notification_id = $1 AND user_id = ANY($2)
"#,
notification_id,
&ids
)
.execute(self)
.await?;
Ok(())
}
async fn mark_notifications_seen(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
let rows = sqlx::query_as!(
UpdatedUserNotificationRow,
r#"
WITH updated AS (
UPDATE user_notification
SET state = CASE WHEN state = 'unseen' THEN 'seen'::notification_state ELSE state END,
seen_at = COALESCE(seen_at, NOW())
WHERE user_id = $1 AND notification_id = ANY($2) AND deleted_at IS NULL
RETURNING
user_id,
notification_id,
sent,
state,
created_at,
seen_at,
deleted_at
)
SELECT
updated.user_id as owner_id,
updated.notification_id,
n.event_item_id,
n.event_item_type,
updated.sent,
updated.state as "state!: NotificationState",
updated.created_at::timestamptz as "created_at!",
updated.seen_at::timestamptz as viewed_at,
NOW()::timestamptz as "updated_at!",
updated.deleted_at::timestamptz,
n.metadata as "notification_metadata: serde_json::Value",
n.notification_event_type,
n.sender_id
FROM updated
JOIN notification n ON n.id = updated.notification_id
ORDER BY array_position($2, updated.notification_id)
"#,
user_id.as_ref(),
notification_ids,
)
.fetch_all(self)
.await?;
rows.into_iter()
.map(UpdatedUserNotificationRow::into_domain)
.collect()
}
async fn mark_notifications_done(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
done: bool,
) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
let rows = sqlx::query_as!(
UpdatedUserNotificationRow,
r#"
WITH updated AS (
UPDATE user_notification
SET state = CASE
WHEN $3 THEN 'done'::notification_state
WHEN state = 'done' THEN 'seen'::notification_state
ELSE state
END
WHERE user_id = $1 AND notification_id = ANY($2) AND deleted_at IS NULL
RETURNING
user_id,
notification_id,
sent,
state,
created_at,
seen_at,
deleted_at
)
SELECT
updated.user_id as owner_id,
updated.notification_id,
n.event_item_id,
n.event_item_type,
updated.sent,
updated.state as "state!: NotificationState",
updated.created_at::timestamptz as "created_at!",
updated.seen_at::timestamptz as viewed_at,
NOW()::timestamptz as "updated_at!",
updated.deleted_at::timestamptz,
n.metadata as "notification_metadata: serde_json::Value",
n.notification_event_type,
n.sender_id
FROM updated
JOIN notification n ON n.id = updated.notification_id
ORDER BY array_position($2, updated.notification_id)
"#,
user_id.as_ref(),
notification_ids,
done,
)
.fetch_all(self)
.await?;
rows.into_iter()
.map(UpdatedUserNotificationRow::into_domain)
.collect()
}
async fn get_notification_ids_for_entities(
&self,
user_id: &MacroUserIdStr<'_>,
entities: &[Entity<'_>],
status: &NotificationStatus,
) -> Result<Vec<Uuid>, Report> {
let (states, include_unviewed) = status.entity_update_filter();
let entity_types = entities
.iter()
.map(|entity| entity.entity_type.as_ref().to_owned())
.collect::<Vec<_>>();
let entity_ids = entities
.iter()
.map(|entity| entity.entity_id.to_string())
.collect::<Vec<_>>();
let notification_ids = sqlx::query_scalar!(
r#"
WITH requested_entities AS (
SELECT entity_type, entity_id
FROM UNNEST($2::text[], $3::text[]) AS entity(entity_type, entity_id)
)
-- The primary entity reads only the viewer's rows from
-- idx_user_notification_viewer_entity; secondary entities and
-- message ids match few notifications each.
SELECT matched.notification_id AS "notification_id!"
FROM (
SELECT un.notification_id, un.created_at
FROM requested_entities entity
JOIN user_notification un
ON un.event_item_type = entity.entity_type
AND un.event_item_id = entity.entity_id
WHERE un.user_id = $1
AND un.deleted_at IS NULL
AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
UNION
SELECT un.notification_id, un.created_at
FROM requested_entities entity
JOIN notification n
ON n.secondary_event_item_type = entity.entity_type
AND n.secondary_event_item_id = entity.entity_id
CROSS JOIN LATERAL (
SELECT un.notification_id, un.created_at
FROM user_notification un
WHERE un.user_id = $1 AND un.notification_id = n.id
AND un.deleted_at IS NULL
AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
-- Probe the viewer's row per notification: a plain join
-- can scan the viewer's whole history for each entity.
OFFSET 0
) un
UNION
SELECT un.notification_id, un.created_at
FROM requested_entities entity
JOIN notification n
ON COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = entity.entity_id
CROSS JOIN LATERAL (
SELECT un.notification_id, un.created_at
FROM user_notification un
WHERE un.user_id = $1 AND un.notification_id = n.id
AND un.deleted_at IS NULL
AND (un.state = ANY($4::notification_state[]) OR ($5 AND un.seen_at IS NULL))
OFFSET 0
) un
WHERE entity.entity_type = 'channel_message'
) matched
ORDER BY matched.created_at, matched.notification_id
"#,
user_id.as_ref(),
&entity_types,
&entity_ids,
states as _,
include_unviewed,
)
.fetch_all(self)
.await?;
Ok(notification_ids)
}
async fn get_basic_notifications(
&self,
notification_ids: &[Uuid],
) -> Result<Vec<NotificationIdAndCollapseKey>, Report> {
let rows = sqlx::query!(
r#"
SELECT id, apns_collapse_key as "apns_collapse_key!: String"
FROM notification
WHERE id = ANY($1) AND apns_collapse_key IS NOT NULL
"#,
notification_ids
)
.fetch_all(self)
.await?;
Ok(rows
.into_iter()
.map(|row| NotificationIdAndCollapseKey {
id: row.id,
apns_collapse_key: row.apns_collapse_key,
})
.collect())
}
async fn get_digest_eligible_notification_ids(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<HashSet<Uuid>, Report> {
let user_id_str = user_id.to_string();
let rows = sqlx::query!(
r#"
SELECT un.notification_id
FROM user_notification un
JOIN notification n ON n.id = un.notification_id
WHERE un.user_id = $1
AND un.notification_id = ANY($2)
AND un.deleted_at IS NULL
AND un.state = 'unseen'
"#,
user_id_str,
notification_ids
)
.fetch_all(self)
.await?;
Ok(rows.into_iter().map(|row| row.notification_id).collect())
}
async fn get_user_notifications<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> Result<Vec<UserNotificationRow<T>>, Report> {
let query_limit = limit as i64;
let (cursor_id, cursor_timestamp) = cursor.vals();
let rows = build_user_notifications_query(UserNotificationsQueryArgs {
user_id: user_id.as_ref(),
event_item_ids: None,
limit: query_limit,
cursor_id: cursor_id.copied(),
cursor_timestamp: cursor_timestamp.copied(),
filters: &filters,
})
.build_query_as::<UserNotificationListRow>()
.fetch_all(self)
.await?;
let mut notifications = Vec::with_capacity(rows.len());
for row in rows {
let (
owner_id,
notification_id,
event_item_id,
event_item_type,
sent,
state,
created_at,
viewed_at,
updated_at,
deleted_at,
notification_metadata,
notification_event_type,
sender_id,
) = row;
let entity = match EntityType::from_str(&event_item_type) {
Ok(entity_type) => entity_type.with_entity_string(event_item_id),
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let sender_id = match sender_id
.map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned))
.transpose()
{
Ok(sender_id) => sender_id,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let owner_id = match MacroUserIdStr::parse_from_str(&owner_id).map(CowLike::into_owned)
{
Ok(owner_id) => owner_id,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let notification_metadata = match serde_json::from_value::<T>(notification_metadata) {
Ok(metadata) => metadata,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
notifications.push(UserNotificationRow {
owner_id,
notification_id,
notification_event_type,
entity,
sent,
state,
created_at,
viewed_at,
updated_at,
deleted_at,
notification_metadata,
sender_id,
});
}
Ok(notifications)
}
async fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
event_item_ids: &[Uuid],
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> Result<Vec<UserNotificationRow<T>>, Report> {
let query_limit = limit as i64;
let (cursor_id, cursor_timestamp) = cursor.vals();
let event_item_ids: Vec<String> = event_item_ids.iter().map(|id| id.to_string()).collect();
let rows = build_user_notifications_query(UserNotificationsQueryArgs {
user_id: user_id.as_ref(),
event_item_ids: Some(&event_item_ids),
limit: query_limit,
cursor_id: cursor_id.copied(),
cursor_timestamp: cursor_timestamp.copied(),
filters: &filters,
})
.build_query_as::<UserNotificationListRow>()
.fetch_all(self)
.await?;
let mut notifications = Vec::with_capacity(rows.len());
for row in rows {
let (
owner_id,
notification_id,
event_item_id,
event_item_type,
sent,
state,
created_at,
viewed_at,
updated_at,
deleted_at,
notification_metadata,
notification_event_type,
sender_id,
) = row;
let entity = match EntityType::from_str(&event_item_type) {
Ok(entity_type) => entity_type.with_entity_string(event_item_id),
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let sender_id = match sender_id
.map(|s| MacroUserIdStr::parse_from_str(&s).map(CowLike::into_owned))
.transpose()
{
Ok(sender_id) => sender_id,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let owner_id = match MacroUserIdStr::parse_from_str(&owner_id).map(CowLike::into_owned)
{
Ok(owner_id) => owner_id,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
let notification_metadata = match serde_json::from_value::<T>(notification_metadata) {
Ok(metadata) => metadata,
Err(e) => {
tracing::debug!(?notification_id, error = ?e, "skipping invalid notification");
continue;
}
};
notifications.push(UserNotificationRow {
owner_id,
notification_id,
notification_event_type,
entity,
sent,
state,
created_at,
viewed_at,
updated_at,
deleted_at,
notification_metadata,
sender_id,
});
}
Ok(notifications)
}
async fn get_entity_notifications_batch(
&self,
user_id: MacroUserIdStr<'_>,
entities: Vec<Entity<'static>>,
query: crate::domain::models::entity_query::EntityNotificationQuery,
) -> Result<HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>, Report> {
super::entity_notifications::get_filtered_entity_notifications(
self, user_id, entities, query,
)
.await
}
async fn get_user_notification_by_id<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> Result<Option<UserNotificationRow<T>>, Report> {
let row = sqlx::query!(
r#"
SELECT
un.user_id as owner_id,
un.notification_id,
n.event_item_id,
n.event_item_type,
un.sent,
un.state as "state!: NotificationState",
un.created_at::timestamptz as "created_at!",
un.seen_at::timestamptz as viewed_at,
un.created_at::timestamptz as "updated_at!",
un.deleted_at::timestamptz,
n.metadata as "notification_metadata: serde_json::Value",
n.notification_event_type as notification_event_type,
n.sender_id as sender_id
FROM user_notification un
JOIN notification n ON n.id = un.notification_id
WHERE un.user_id = $1
AND un.notification_id = $2
AND un.deleted_at IS NULL
LIMIT 1
"#,
user_id.as_ref(),
notification_id,
)
.fetch_optional(self)
.await?;
let Some(row) = row else {
return Ok(None);
};
let entity = EntityType::from_str(&row.event_item_type)
.map_err(|e| rootcause::report!(e))?
.with_entity_string(row.event_item_id);
let sender_id = row
.sender_id
.as_deref()
.map(|s| MacroUserIdStr::parse_from_str(s).map(CowLike::into_owned))
.transpose()
.map_err(|e| rootcause::report!(e))?;
let owner_id = MacroUserIdStr::parse_from_str(&row.owner_id)
.map(CowLike::into_owned)
.map_err(|e| rootcause::report!(e))?;
let notification_metadata = serde_json::from_value::<T>(row.notification_metadata)
.map_err(|e| rootcause::report!(e))?;
Ok(Some(UserNotificationRow {
owner_id,
notification_id: row.notification_id,
notification_event_type: row.notification_event_type,
entity,
sent: row.sent,
state: row.state,
created_at: row.created_at,
viewed_at: row.viewed_at,
updated_at: row.updated_at,
deleted_at: row.deleted_at,
notification_metadata,
sender_id,
}))
}
async fn delete_user_notification(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> Result<(), Report> {
sqlx::query!(
r#"
UPDATE user_notification
SET deleted_at = NOW()
WHERE user_id = $1 AND notification_id = $2
"#,
user_id.as_ref(),
notification_id,
)
.execute(self)
.await?;
Ok(())
}
async fn bulk_delete_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<(), Report> {
sqlx::query!(
r#"
UPDATE user_notification
SET deleted_at = NOW()
WHERE user_id = $1 AND notification_id = ANY($2)
"#,
user_id.as_ref(),
notification_ids,
)
.execute(self)
.await?;
Ok(())
}
async fn delete_all_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
) -> Result<(), Report> {
sqlx::query!(
r#"
DELETE FROM user_notification
WHERE user_id = $1
"#,
user_id.as_ref(),
)
.execute(self)
.await?;
Ok(())
}
async fn get_users_with_type_disabled<'a>(
&self,
notification_event_type: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
let ids: Vec<String> = user_ids.iter().map(|id| id.to_string()).collect();
let disabled: Vec<String> = sqlx::query_scalar!(
r#"
SELECT user_id FROM user_notification_type_preference
WHERE notification_event_type = $1 AND user_id = ANY($2)
"#,
notification_event_type,
&ids
)
.fetch_all(self)
.await?;
let result = disabled
.into_iter()
.filter_map(|id| {
MacroUserIdStr::parse_from_str(&id)
.map(CowLike::into_owned)
.ok()
})
.map(|id| id.into_owned())
.collect();
Ok(result)
}
async fn get_disabled_notification_types(
&self,
user_id: MacroUserIdStr<'_>,
) -> Result<Vec<DisabledNotificationType>, Report> {
let rows = sqlx::query!(
r#"
SELECT user_id, notification_event_type
FROM user_notification_type_preference
WHERE user_id = $1
"#,
user_id.as_ref()
)
.fetch_all(self)
.await?;
Ok(rows
.into_iter()
.filter_map(|row| {
let user_id = MacroUserIdStr::parse_from_str(&row.user_id)
.map(CowLike::into_owned)
.ok()?
.into_owned();
Some(DisabledNotificationType {
user_id,
notification_event_type: row.notification_event_type,
})
})
.collect())
}
async fn disable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> Result<(), Report> {
sqlx::query!(
r#"
INSERT INTO user_notification_type_preference (user_id, notification_event_type)
VALUES ($1, $2)
ON CONFLICT (user_id, notification_event_type) DO NOTHING
"#,
user_id.as_ref(),
notification_event_type
)
.execute(self)
.await?;
Ok(())
}
async fn enable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> Result<(), Report> {
sqlx::query!(
r#"
DELETE FROM user_notification_type_preference
WHERE user_id = $1 AND notification_event_type = $2
"#,
user_id.as_ref(),
notification_event_type
)
.execute(self)
.await?;
Ok(())
}
}
impl<D: NotificationDbOps + Send + Sync> NotificationRepository for DbNotificationRepository<D> {
async fn get_muted_users<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
self.db.get_muted_users(user_ids).await
}
async fn get_unsubscribed_users<'a>(
&self,
item_id: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
self.db.get_unsubscribed_users(item_id, user_ids).await
}
async fn get_device_endpoints<'a>(
&self,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>, Report> {
self.db.get_device_endpoints(user_ids).await
}
async fn create_notification<'a, T: Serialize + Send + Sync>(
&self,
request: SendNotificationRequestBuilder<'a, TaggedContent<T>>,
notification_id: Uuid,
service_name: &str,
apns_collapse_key: Option<&str>,
) -> Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report> {
self.db
.create_notification(request, notification_id, service_name, apns_collapse_key)
.await
}
async fn notification_is_withdrawn(&self, notification_id: Uuid) -> Result<bool, Report> {
self.db.notification_is_withdrawn(notification_id).await
}
async fn complete_notification_publication(&self, notification_id: Uuid) -> Result<(), Report> {
self.db
.complete_notification_publication(notification_id)
.await
}
async fn update_sent_status<'a>(
&self,
notification_id: Uuid,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<(), Report> {
self.db.update_sent_status(notification_id, user_ids).await
}
async fn mark_notifications_seen(
&self,
user_id: MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
self.db
.mark_notifications_seen(&user_id, notification_ids)
.await
}
async fn mark_notifications_done(
&self,
user_id: &MacroUserIdStr<'_>,
notification_ids: &[Uuid],
done: bool,
) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
self.db
.mark_notifications_done(user_id, notification_ids, done)
.await
}
async fn get_notification_ids_for_entities(
&self,
user_id: MacroUserIdStr<'_>,
entities: &[Entity<'_>],
status: &NotificationStatus,
) -> Result<Vec<Uuid>, Report> {
self.db
.get_notification_ids_for_entities(&user_id, entities, status)
.await
}
async fn get_basic_notifications(
&self,
notification_ids: &[Uuid],
) -> Result<Vec<NotificationIdAndCollapseKey>, Report> {
self.db.get_basic_notifications(notification_ids).await
}
async fn get_digest_eligible_notification_ids(
&self,
user_id: MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<HashSet<Uuid>, Report> {
self.db
.get_digest_eligible_notification_ids(&user_id, notification_ids)
.await
}
async fn get_user_notifications<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> Result<Vec<UserNotificationRow<T>>, Report> {
self.db
.get_user_notifications(user_id, limit, cursor, filters)
.await
}
async fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
event_item_ids: &[Uuid],
limit: u32,
cursor: Query<Uuid, CreatedAt, ()>,
filters: NotificationListFilters,
) -> Result<Vec<UserNotificationRow<T>>, Report> {
self.db
.get_user_notifications_by_event_item_ids(
user_id,
event_item_ids,
limit,
cursor,
filters,
)
.await
}
async fn get_entity_notifications_batch(
&self,
user_id: MacroUserIdStr<'_>,
entities: Vec<Entity<'static>>,
query: crate::domain::models::entity_query::EntityNotificationQuery,
) -> Result<HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>, Report> {
self.db
.get_entity_notifications_batch(user_id, entities, query)
.await
}
async fn get_user_notification_by_id<T: DeserializeOwned + Send>(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> Result<Option<UserNotificationRow<T>>, Report> {
self.db
.get_user_notification_by_id(user_id, notification_id)
.await
}
async fn delete_user_notification(
&self,
user_id: MacroUserIdStr<'_>,
notification_id: Uuid,
) -> Result<(), Report> {
self.db
.delete_user_notification(user_id, notification_id)
.await
}
async fn bulk_delete_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
notification_ids: &[Uuid],
) -> Result<(), Report> {
self.db
.bulk_delete_user_notifications(user_id, notification_ids)
.await
}
async fn delete_all_user_notifications(
&self,
user_id: MacroUserIdStr<'_>,
) -> Result<(), Report> {
self.db.delete_all_user_notifications(user_id).await
}
async fn get_device_endpoint(
&self,
device_token: &str,
device_type: &DeviceType,
) -> Result<Option<String>, Report> {
self.db.get_device_endpoint(device_token, device_type).await
}
async fn upsert_device(
&self,
user_id: MacroUserIdStr<'_>,
device_token: &str,
device_endpoint: &str,
device_type: &DeviceType,
native_call_capable: bool,
) -> Result<(), Report> {
self.db
.upsert_device(
user_id,
device_token,
device_endpoint,
device_type,
native_call_capable,
)
.await
}
async fn delete_user_devices_by_token(
&self,
user_id: MacroUserIdStr<'_>,
device_token: &str,
device_type: &DeviceType,
) -> Result<Vec<String>, Report> {
self.db
.delete_user_devices_by_token(user_id, device_token, device_type)
.await
}
async fn delete_stale_devices_by_token(
&self,
device_token: &str,
device_type: &DeviceType,
active_endpoint: &str,
) -> Result<Vec<String>, Report> {
self.db
.delete_stale_devices_by_token(device_token, device_type, active_endpoint)
.await
}
async fn delete_device_by_endpoint(&self, endpoint_arn: &str) -> Result<(), Report> {
self.db.delete_by_endpoint(endpoint_arn).await
}
async fn get_users_with_type_disabled<'a>(
&self,
notification_event_type: &str,
user_ids: &[MacroUserIdStr<'a>],
) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
self.db
.get_users_with_type_disabled(notification_event_type, user_ids)
.await
}
async fn get_disabled_notification_types(
&self,
user_id: MacroUserIdStr<'_>,
) -> Result<Vec<DisabledNotificationType>, Report> {
self.db.get_disabled_notification_types(user_id).await
}
async fn disable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> Result<(), Report> {
self.db
.disable_notification_type(user_id, notification_event_type)
.await
}
async fn enable_notification_type(
&self,
user_id: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> Result<(), Report> {
self.db
.enable_notification_type(user_id, notification_event_type)
.await
}
}