Skip to main content

xmtp_mls/groups/
notifications.rs

1//! Per-conversation notification rules.
2
3use super::MlsGroup;
4use crate::context::XmtpSharedContext;
5use crate::core::notifications::{
6    NotificationOverride, enabled_from_record, notification_settings_changed,
7};
8use xmtp_db::consent_record::conversation_consent;
9use xmtp_db::{StorageError, TransactionOutcome::Continue, XmtpMlsStorageProvider, prelude::*};
10
11impl<Context: XmtpSharedContext> MlsGroup<Context> {
12    /// Set an override after which the task recomputes the desired set.
13    pub fn set_notifications(&self, value: NotificationOverride) -> Result<(), StorageError> {
14        crate::state_tx::state_write(self.context.mls_storage(), |tx| {
15            tx.storage().db().set_notification_override(
16                &self.group_id,
17                match value {
18                    NotificationOverride::Enabled => Some(1),
19                    NotificationOverride::Disabled => Some(0),
20                    NotificationOverride::Default => None,
21                },
22            )?;
23            Ok::<_, StorageError>(Continue(()))
24        })?;
25        notification_settings_changed(&self.context);
26        Ok(())
27    }
28
29    /// Return the effective local rule for this conversation.
30    pub fn notifications_enabled(&self) -> Result<bool, StorageError> {
31        let db = self.context.db();
32        let record = db.notification_record()?;
33        if record.push_state != 1 {
34            return Ok(false);
35        }
36        let group = db
37            .find_group(&self.group_id)?
38            .ok_or(xmtp_db::NotFound::GroupById(self.group_id))?;
39        let consent = conversation_consent(&db, self.context.inbox_id(), &self.group_id)?;
40        enabled_from_record(&record, &group, consent)
41    }
42}