Skip to content

Commit 9f32dfe

Browse files
committed
change(ffi): expose the full RoomPowerLevels object and move corresponding methods to it.
This patch expands on the already existent `RoomPowerLevels` record (which it renames to `RoomPowerLevelsValues` to work around uniffi not exposing public fields) and nests them inside a new exported object that also provides methods for retrieving the actions that an user can take, methods moved from the room object. This reduces the amount of async calls the clients need to make, simplifies the API and groups the code better together.
1 parent 040fd6c commit 9f32dfe

File tree

2 files changed

+121
-150
lines changed
  • bindings/matrix-sdk-ffi/src
  • crates/matrix-sdk/src/room

2 files changed

+121
-150
lines changed

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 113 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -570,21 +570,6 @@ impl Room {
570570
Ok(())
571571
}
572572

573-
pub async fn can_user_redact_own(&self, user_id: String) -> Result<bool, ClientError> {
574-
let user_id = UserId::parse(&user_id)?;
575-
Ok(self.inner.can_user_redact_own(&user_id).await?)
576-
}
577-
578-
pub async fn can_user_redact_other(&self, user_id: String) -> Result<bool, ClientError> {
579-
let user_id = UserId::parse(&user_id)?;
580-
Ok(self.inner.can_user_redact_other(&user_id).await?)
581-
}
582-
583-
pub async fn can_user_ban(&self, user_id: String) -> Result<bool, ClientError> {
584-
let user_id = UserId::parse(&user_id)?;
585-
Ok(self.inner.can_user_ban(&user_id).await?)
586-
}
587-
588573
pub async fn ban_user(
589574
&self,
590575
user_id: String,
@@ -603,16 +588,6 @@ impl Room {
603588
Ok(self.inner.unban_user(&user_id, reason.as_deref()).await?)
604589
}
605590

606-
pub async fn can_user_invite(&self, user_id: String) -> Result<bool, ClientError> {
607-
let user_id = UserId::parse(&user_id)?;
608-
Ok(self.inner.can_user_invite(&user_id).await?)
609-
}
610-
611-
pub async fn can_user_kick(&self, user_id: String) -> Result<bool, ClientError> {
612-
let user_id = UserId::parse(&user_id)?;
613-
Ok(self.inner.can_user_kick(&user_id).await?)
614-
}
615-
616591
pub async fn kick_user(
617592
&self,
618593
user_id: String,
@@ -622,37 +597,6 @@ impl Room {
622597
Ok(self.inner.kick_user(&user_id, reason.as_deref()).await?)
623598
}
624599

625-
pub async fn can_user_send_state(
626-
&self,
627-
user_id: String,
628-
state_event: StateEventType,
629-
) -> Result<bool, ClientError> {
630-
let user_id = UserId::parse(&user_id)?;
631-
Ok(self.inner.can_user_send_state(&user_id, state_event.into()).await?)
632-
}
633-
634-
pub async fn can_user_send_message(
635-
&self,
636-
user_id: String,
637-
message: MessageLikeEventType,
638-
) -> Result<bool, ClientError> {
639-
let user_id = UserId::parse(&user_id)?;
640-
Ok(self.inner.can_user_send_message(&user_id, message.into()).await?)
641-
}
642-
643-
pub async fn can_user_pin_unpin(&self, user_id: String) -> Result<bool, ClientError> {
644-
let user_id = UserId::parse(&user_id)?;
645-
Ok(self.inner.can_user_pin_unpin(&user_id).await?)
646-
}
647-
648-
pub async fn can_user_trigger_room_notification(
649-
&self,
650-
user_id: String,
651-
) -> Result<bool, ClientError> {
652-
let user_id = UserId::parse(&user_id)?;
653-
Ok(self.inner.can_user_trigger_room_notification(&user_id).await?)
654-
}
655-
656600
pub fn own_user_id(&self) -> String {
657601
self.inner.own_user_id().to_string()
658602
}
@@ -717,9 +661,9 @@ impl Room {
717661
Ok(())
718662
}
719663

720-
pub async fn get_power_levels(&self) -> Result<RoomPowerLevels, ClientError> {
664+
pub async fn get_power_levels(&self) -> Result<Arc<RoomPowerLevels>, ClientError> {
721665
let power_levels = self.inner.power_levels().await.map_err(matrix_sdk::Error::from)?;
722-
Ok(RoomPowerLevels::from(power_levels))
666+
Ok(Arc::new(RoomPowerLevels::from(power_levels)))
723667
}
724668

725669
pub async fn apply_power_level_changes(
@@ -755,8 +699,8 @@ impl Room {
755699
Ok(self.inner.get_suggested_user_role(&user_id).await?)
756700
}
757701

758-
pub async fn reset_power_levels(&self) -> Result<RoomPowerLevels, ClientError> {
759-
Ok(RoomPowerLevels::from(self.inner.reset_power_levels().await?))
702+
pub async fn reset_power_levels(&self) -> Result<Arc<RoomPowerLevels>, ClientError> {
703+
Ok(Arc::new(RoomPowerLevels::from(self.inner.reset_power_levels().await?)))
760704
}
761705

762706
pub async fn matrix_to_permalink(&self) -> Result<String, ClientError> {
@@ -1284,8 +1228,115 @@ pub fn matrix_to_room_alias_permalink(
12841228
Ok(room_alias.matrix_to_uri().to_string())
12851229
}
12861230

1287-
#[derive(uniffi::Record)]
1231+
#[derive(uniffi::Object)]
12881232
pub struct RoomPowerLevels {
1233+
inner: RumaPowerLevels,
1234+
}
1235+
1236+
#[matrix_sdk_ffi_macros::export]
1237+
impl RoomPowerLevels {
1238+
fn values(&self) -> RoomPowerLevelsValues {
1239+
self.inner.clone().into()
1240+
}
1241+
1242+
/// Returns true if the user with the given user_id is able to ban in the
1243+
/// room.
1244+
///
1245+
/// The call may fail if there is an error in getting the power levels.
1246+
pub fn can_user_ban(&self, user_id: String) -> Result<bool, ClientError> {
1247+
let user_id = UserId::parse(&user_id)?;
1248+
Ok(self.inner.user_can_ban(&user_id))
1249+
}
1250+
1251+
/// Returns true if the user with the given user_id is able to redact
1252+
/// their own messages in the room.
1253+
///
1254+
/// The call may fail if there is an error in getting the power levels.
1255+
pub fn can_user_redact_own(&self, user_id: String) -> Result<bool, ClientError> {
1256+
let user_id = UserId::parse(&user_id)?;
1257+
Ok(self.inner.user_can_redact_own_event(&user_id))
1258+
}
1259+
1260+
/// Returns true if the user with the given user_id is able to redact
1261+
/// messages of other users in the room.
1262+
///
1263+
/// The call may fail if there is an error in getting the power levels.
1264+
pub fn can_user_redact_other(&self, user_id: String) -> Result<bool, ClientError> {
1265+
let user_id = UserId::parse(&user_id)?;
1266+
Ok(self.inner.user_can_redact_event_of_other(&user_id))
1267+
}
1268+
1269+
/// Returns true if the user with the given user_id is able to kick in the
1270+
/// room.
1271+
///
1272+
/// The call may fail if there is an error in getting the power levels.
1273+
pub fn can_user_invite(&self, user_id: String) -> Result<bool, ClientError> {
1274+
let user_id = UserId::parse(&user_id)?;
1275+
Ok(self.inner.user_can_invite(&user_id))
1276+
}
1277+
1278+
/// Returns true if the user with the given user_id is able to kick in the
1279+
/// room.
1280+
///
1281+
/// The call may fail if there is an error in getting the power levels.
1282+
pub fn can_user_kick(&self, user_id: String) -> Result<bool, ClientError> {
1283+
let user_id = UserId::parse(&user_id)?;
1284+
Ok(self.inner.user_can_kick(&user_id))
1285+
}
1286+
1287+
/// Returns true if the user with the given user_id is able to send a
1288+
/// specific state event type in the room.
1289+
///
1290+
/// The call may fail if there is an error in getting the power levels.
1291+
pub fn can_user_send_state(
1292+
&self,
1293+
user_id: String,
1294+
state_event: StateEventType,
1295+
) -> Result<bool, ClientError> {
1296+
let user_id = UserId::parse(&user_id)?;
1297+
Ok(self.inner.user_can_send_state(&user_id, state_event.into()))
1298+
}
1299+
1300+
/// Returns true if the user with the given user_id is able to send a
1301+
/// specific message type in the room.
1302+
///
1303+
/// The call may fail if there is an error in getting the power levels.
1304+
pub fn can_user_send_message(
1305+
&self,
1306+
user_id: String,
1307+
message: MessageLikeEventType,
1308+
) -> Result<bool, ClientError> {
1309+
let user_id = UserId::parse(&user_id)?;
1310+
Ok(self.inner.user_can_send_message(&user_id, message.into()))
1311+
}
1312+
1313+
/// Returns true if the user with the given user_id is able to pin or unpin
1314+
/// events in the room.
1315+
///
1316+
/// The call may fail if there is an error in getting the power levels.
1317+
pub fn can_user_pin_unpin(&self, user_id: String) -> Result<bool, ClientError> {
1318+
let user_id = UserId::parse(&user_id)?;
1319+
Ok(self.inner.user_can_send_state(&user_id, StateEventType::RoomPinnedEvents.into()))
1320+
}
1321+
1322+
/// Returns true if the user with the given user_id is able to trigger a
1323+
/// notification in the room.
1324+
///
1325+
/// The call may fail if there is an error in getting the power levels.
1326+
pub fn can_user_trigger_room_notification(&self, user_id: String) -> Result<bool, ClientError> {
1327+
let user_id = UserId::parse(&user_id)?;
1328+
Ok(self.inner.user_can_trigger_room_notification(&user_id))
1329+
}
1330+
}
1331+
1332+
impl From<RumaPowerLevels> for RoomPowerLevels {
1333+
fn from(value: RumaPowerLevels) -> Self {
1334+
Self { inner: value }
1335+
}
1336+
}
1337+
1338+
#[derive(uniffi::Record)]
1339+
pub struct RoomPowerLevelsValues {
12891340
/// The level required to ban a user.
12901341
pub ban: i64,
12911342
/// The level required to invite a user.
@@ -1308,7 +1359,7 @@ pub struct RoomPowerLevels {
13081359
pub room_topic: i64,
13091360
}
13101361

1311-
impl From<RumaPowerLevels> for RoomPowerLevels {
1362+
impl From<RumaPowerLevels> for RoomPowerLevelsValues {
13121363
fn from(value: RumaPowerLevels) -> Self {
13131364
fn state_event_level_for(
13141365
power_levels: &RumaPowerLevels,

crates/matrix-sdk/src/room/mod.rs

Lines changed: 8 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ use ruma::{
113113
tag::{TagInfo, TagName},
114114
typing::SyncTypingEvent,
115115
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent, AnyTimelineEvent, EmptyStateKey,
116-
Mentions, MessageLikeEventContent, MessageLikeEventType, OriginalSyncStateEvent,
117-
RedactContent, RedactedStateEventContent, RoomAccountDataEvent,
118-
RoomAccountDataEventContent, RoomAccountDataEventType, StateEventContent, StateEventType,
119-
StaticEventContent, StaticStateEventContent, SyncStateEvent,
116+
Mentions, MessageLikeEventContent, OriginalSyncStateEvent, RedactContent,
117+
RedactedStateEventContent, RoomAccountDataEvent, RoomAccountDataEventContent,
118+
RoomAccountDataEventType, StateEventContent, StateEventType, StaticEventContent,
119+
StaticStateEventContent, SyncStateEvent,
120120
},
121121
push::{Action, PushConditionRoomCtx, Ruleset},
122122
serde::Raw,
@@ -2759,89 +2759,6 @@ impl Room {
27592759
self.client.send(request).await
27602760
}
27612761

2762-
/// Returns true if the user with the given user_id is able to redact
2763-
/// their own messages in the room.
2764-
///
2765-
/// The call may fail if there is an error in getting the power levels.
2766-
pub async fn can_user_redact_own(&self, user_id: &UserId) -> Result<bool> {
2767-
Ok(self.power_levels().await?.user_can_redact_own_event(user_id))
2768-
}
2769-
2770-
/// Returns true if the user with the given user_id is able to redact
2771-
/// messages of other users in the room.
2772-
///
2773-
/// The call may fail if there is an error in getting the power levels.
2774-
pub async fn can_user_redact_other(&self, user_id: &UserId) -> Result<bool> {
2775-
Ok(self.power_levels().await?.user_can_redact_event_of_other(user_id))
2776-
}
2777-
2778-
/// Returns true if the user with the given user_id is able to ban in the
2779-
/// room.
2780-
///
2781-
/// The call may fail if there is an error in getting the power levels.
2782-
pub async fn can_user_ban(&self, user_id: &UserId) -> Result<bool> {
2783-
Ok(self.power_levels().await?.user_can_ban(user_id))
2784-
}
2785-
2786-
/// Returns true if the user with the given user_id is able to kick in the
2787-
/// room.
2788-
///
2789-
/// The call may fail if there is an error in getting the power levels.
2790-
pub async fn can_user_invite(&self, user_id: &UserId) -> Result<bool> {
2791-
Ok(self.power_levels().await?.user_can_invite(user_id))
2792-
}
2793-
2794-
/// Returns true if the user with the given user_id is able to kick in the
2795-
/// room.
2796-
///
2797-
/// The call may fail if there is an error in getting the power levels.
2798-
pub async fn can_user_kick(&self, user_id: &UserId) -> Result<bool> {
2799-
Ok(self.power_levels().await?.user_can_kick(user_id))
2800-
}
2801-
2802-
/// Returns true if the user with the given user_id is able to send a
2803-
/// specific state event type in the room.
2804-
///
2805-
/// The call may fail if there is an error in getting the power levels.
2806-
pub async fn can_user_send_state(
2807-
&self,
2808-
user_id: &UserId,
2809-
state_event: StateEventType,
2810-
) -> Result<bool> {
2811-
Ok(self.power_levels().await?.user_can_send_state(user_id, state_event))
2812-
}
2813-
2814-
/// Returns true if the user with the given user_id is able to send a
2815-
/// specific message type in the room.
2816-
///
2817-
/// The call may fail if there is an error in getting the power levels.
2818-
pub async fn can_user_send_message(
2819-
&self,
2820-
user_id: &UserId,
2821-
message: MessageLikeEventType,
2822-
) -> Result<bool> {
2823-
Ok(self.power_levels().await?.user_can_send_message(user_id, message))
2824-
}
2825-
2826-
/// Returns true if the user with the given user_id is able to pin or unpin
2827-
/// events in the room.
2828-
///
2829-
/// The call may fail if there is an error in getting the power levels.
2830-
pub async fn can_user_pin_unpin(&self, user_id: &UserId) -> Result<bool> {
2831-
Ok(self
2832-
.power_levels()
2833-
.await?
2834-
.user_can_send_state(user_id, StateEventType::RoomPinnedEvents))
2835-
}
2836-
2837-
/// Returns true if the user with the given user_id is able to trigger a
2838-
/// notification in the room.
2839-
///
2840-
/// The call may fail if there is an error in getting the power levels.
2841-
pub async fn can_user_trigger_room_notification(&self, user_id: &UserId) -> Result<bool> {
2842-
Ok(self.power_levels().await?.user_can_trigger_room_notification(user_id))
2843-
}
2844-
28452762
/// Get a list of servers that should know this room.
28462763
///
28472764
/// Uses the synced members of the room and the suggested [routing
@@ -3330,7 +3247,10 @@ impl Room {
33303247
return Ok(false);
33313248
}
33323249

3333-
if !self.can_user_trigger_room_notification(self.own_user_id()).await? {
3250+
let can_user_trigger_room_notification =
3251+
self.power_levels().await?.user_can_trigger_room_notification(self.own_user_id());
3252+
3253+
if !can_user_trigger_room_notification {
33343254
warn!(
33353255
"User can't send notifications to everyone in the room {}. \
33363256
Not sending a new notify event.",

0 commit comments

Comments
 (0)