Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ffi): Allow to set the notification mode for a room #1967

Closed
wants to merge 12 commits into from
Closed
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ futures-core = "0.3.28"
futures-executor = "0.3.21"
futures-util = { version = "0.3.26", default-features = false, features = ["alloc"] }
http = "0.2.6"
ruma = { git = "https://github.com/ruma/ruma", rev = "e3282d8bfebc69d2e64815105ebe9dced130b94a", features = ["client-api-c", "compat-user-id"] }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "e3282d8bfebc69d2e64815105ebe9dced130b94a" }
ruma = { git = "https://github.com/ruma/ruma", rev = "a2b64c20bc715239142e522fc6ea6d4936ef54d1", features = ["client-api-c", "compat-user-id"] }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "a2b64c20bc715239142e522fc6ea6d4936ef54d1" }
once_cell = "1.16.0"
serde = "1.0.151"
serde_html_form = "0.2.0"
Expand Down
4 changes: 4 additions & 0 deletions bindings/matrix-sdk-ffi/src/api.udl
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,7 @@ callback interface SessionVerificationControllerDelegate {
void did_cancel();
void did_finish();
};

callback interface NotificationSettingsDelegate {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've started calling those "listeners" recently:

Suggested change
callback interface NotificationSettingsDelegate {
callback interface NotificationSettingsListener {

void notification_settings_did_change();
};
13 changes: 12 additions & 1 deletion bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use serde_json::Value;
use tokio::sync::broadcast::error::RecvError;
use tracing::{debug, error, warn};

use super::{room::Room, session_verification::SessionVerificationController, RUNTIME};
use super::{
notification_settings::NotificationSettings, room::Room,
session_verification::SessionVerificationController, RUNTIME,
};
use crate::{client, ClientError, NotificationItem};

#[derive(Clone, uniffi::Record)]
Expand Down Expand Up @@ -112,6 +115,7 @@ pub struct Client {
notification_delegate: Arc<RwLock<Option<Box<dyn NotificationDelegate>>>>,
session_verification_controller:
Arc<tokio::sync::RwLock<Option<SessionVerificationController>>>,
notification_settings: Arc<NotificationSettings>,
/// The sliding sync proxy that the client is configured to use by default.
/// If this value is `Some`, it will be automatically added to the builder
/// when calling `sliding_sync()`.
Expand All @@ -137,11 +141,14 @@ impl Client {
}
});

let notification_settings = Arc::new(NotificationSettings::new(sdk_client.clone()));

let client = Client {
inner: sdk_client,
delegate: Arc::new(RwLock::new(None)),
notification_delegate: Arc::new(RwLock::new(None)),
session_verification_controller,
notification_settings,
sliding_sync_proxy: Arc::new(RwLock::new(None)),
sliding_sync_reset_broadcast_tx: Default::default(),
};
Expand Down Expand Up @@ -596,6 +603,10 @@ impl Client {
Ok(notification)
})
}

pub fn get_notification_settings(&self) -> Arc<NotificationSettings> {
self.notification_settings.to_owned()
}
}

#[derive(uniffi::Record)]
Expand Down
54 changes: 53 additions & 1 deletion bindings/matrix-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fmt::Display;

use matrix_sdk::{self, encryption::CryptoStoreError, HttpError, IdParseError, StoreError};
use matrix_sdk::{
self, encryption::CryptoStoreError, HttpError, IdParseError,
NotificationSettingsError as SdkNotificationSettingsError, StoreError,
};

#[derive(Debug, thiserror::Error)]
pub enum ClientError {
Expand Down Expand Up @@ -68,6 +71,12 @@ impl From<mime::FromStrError> for ClientError {
}
}

impl From<NotificationSettingsError> for ClientError {
fn from(e: NotificationSettingsError) -> Self {
anyhow::Error::from(e).into()
}
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
pub enum RoomError {
Expand All @@ -91,3 +100,46 @@ pub enum TimelineError {
#[error("Media info field invalid")]
InvalidMediaInfoField,
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
pub enum NotificationSettingsError {
#[error("client error: {msg}")]
Generic { msg: String },
/// Invalid room id.
#[error("Invalid room id")]
InvalidRoomId,
/// Room not found
#[error("Room not found")]
RoomNotFound,
/// Rule not found
#[error("Rule not found")]
RuleNotFound,
/// Unable to add push rule.
#[error("Unable to add push rule")]
UnableToAddPushRule,
/// Unable to remove push rule.
#[error("Unable to remove push rule")]
UnableToRemovePushRule,
/// Unable to save the push rules
#[error("Unable to save push rules")]
UnableToSavePushRules,
}

impl From<SdkNotificationSettingsError> for NotificationSettingsError {
fn from(value: SdkNotificationSettingsError) -> Self {
match value {
SdkNotificationSettingsError::InvalidRoomId => Self::InvalidRoomId,
SdkNotificationSettingsError::RuleNotFound => Self::RuleNotFound,
SdkNotificationSettingsError::UnableToAddPushRule => Self::UnableToAddPushRule,
SdkNotificationSettingsError::UnableToRemovePushRule => Self::UnableToRemovePushRule,
SdkNotificationSettingsError::UnableToSavePushRules => Self::UnableToSavePushRules,
}
}
}

impl From<matrix_sdk::Error> for NotificationSettingsError {
fn from(e: matrix_sdk::Error) -> Self {
Self::Generic { msg: e.to_string() }
}
}
5 changes: 3 additions & 2 deletions bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod error;
pub mod event;
mod helpers;
pub mod notification;
pub mod notification_settings;
pub mod room;
pub mod room_member;
pub mod session_verification;
Expand All @@ -42,8 +43,8 @@ pub use matrix_sdk_ui::timeline::PaginationOutcome;
pub use platform::*;

pub use self::{
authentication_service::*, client::*, event::*, notification::*, room::*, room_member::*,
session_verification::*, sliding_sync::*, timeline::*, tracing::*,
authentication_service::*, client::*, event::*, notification::*, notification_settings::*,
room::*, room_member::*, session_verification::*, sliding_sync::*, timeline::*, tracing::*,
};
// Re-exports for more convenient use inside other submodules
use self::{client::Client, error::ClientError};
Expand Down
Loading