Skip to content

Commit 47c8df0

Browse files
committed
chore(ffi): move the room power levels to their own file and restructure the code
1 parent 9f32dfe commit 47c8df0

File tree

2 files changed

+172
-158
lines changed

2 files changed

+172
-158
lines changed

bindings/matrix-sdk-ffi/src/room.rs renamed to bindings/matrix-sdk-ffi/src/room/mod.rs

Lines changed: 5 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ use ruma::{
2626
avatar::ImageInfo as RumaAvatarImageInfo,
2727
history_visibility::HistoryVisibility as RumaHistoryVisibility,
2828
join_rules::JoinRule as RumaJoinRule, message::RoomMessageEventContentWithoutRelation,
29-
power_levels::RoomPowerLevels as RumaPowerLevels, MediaSource,
29+
MediaSource,
3030
},
31-
AnyMessageLikeEventContent, AnySyncTimelineEvent, TimelineEventType,
31+
AnyMessageLikeEventContent, AnySyncTimelineEvent,
3232
},
3333
EventId, Int, OwnedDeviceId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId,
3434
ServerName, UserId,
3535
};
3636
use tracing::{error, warn};
3737

38+
use self::power_levels::RoomPowerLevels;
3839
use crate::{
3940
chunk_iterator::ChunkIterator,
4041
client::{JoinRule, RoomVisibility},
4142
error::{ClientError, MediaInfoError, NotYetImplemented, RoomError},
42-
event::{MessageLikeEventType, StateEventType},
4343
identity_status_change::IdentityStatusChange,
4444
live_location_share::{LastLocation, LiveLocationShare},
4545
room_info::RoomInfo,
@@ -55,6 +55,8 @@ use crate::{
5555
TaskHandle,
5656
};
5757

58+
mod power_levels;
59+
5860
#[derive(Debug, Clone, uniffi::Enum)]
5961
pub enum Membership {
6062
Invited,
@@ -1228,161 +1230,6 @@ pub fn matrix_to_room_alias_permalink(
12281230
Ok(room_alias.matrix_to_uri().to_string())
12291231
}
12301232

1231-
#[derive(uniffi::Object)]
1232-
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 {
1340-
/// The level required to ban a user.
1341-
pub ban: i64,
1342-
/// The level required to invite a user.
1343-
pub invite: i64,
1344-
/// The level required to kick a user.
1345-
pub kick: i64,
1346-
/// The level required to redact an event.
1347-
pub redact: i64,
1348-
/// The default level required to send message events.
1349-
pub events_default: i64,
1350-
/// The default level required to send state events.
1351-
pub state_default: i64,
1352-
/// The default power level for every user in the room.
1353-
pub users_default: i64,
1354-
/// The level required to change the room's name.
1355-
pub room_name: i64,
1356-
/// The level required to change the room's avatar.
1357-
pub room_avatar: i64,
1358-
/// The level required to change the room's topic.
1359-
pub room_topic: i64,
1360-
}
1361-
1362-
impl From<RumaPowerLevels> for RoomPowerLevelsValues {
1363-
fn from(value: RumaPowerLevels) -> Self {
1364-
fn state_event_level_for(
1365-
power_levels: &RumaPowerLevels,
1366-
event_type: &TimelineEventType,
1367-
) -> i64 {
1368-
let default_state: i64 = power_levels.state_default.into();
1369-
power_levels.events.get(event_type).map_or(default_state, |&level| level.into())
1370-
}
1371-
Self {
1372-
ban: value.ban.into(),
1373-
invite: value.invite.into(),
1374-
kick: value.kick.into(),
1375-
redact: value.redact.into(),
1376-
events_default: value.events_default.into(),
1377-
state_default: value.state_default.into(),
1378-
users_default: value.users_default.into(),
1379-
room_name: state_event_level_for(&value, &TimelineEventType::RoomName),
1380-
room_avatar: state_event_level_for(&value, &TimelineEventType::RoomAvatar),
1381-
room_topic: state_event_level_for(&value, &TimelineEventType::RoomTopic),
1382-
}
1383-
}
1384-
}
1385-
13861233
#[matrix_sdk_ffi_macros::export(callback_interface)]
13871234
pub trait RoomInfoListener: SyncOutsideWasm + SendOutsideWasm {
13881235
fn call(&self, room_info: RoomInfo);
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
use anyhow::Result;
2+
use ruma::{
3+
events::{room::power_levels::RoomPowerLevels as RumaPowerLevels, TimelineEventType},
4+
UserId,
5+
};
6+
7+
use crate::{
8+
error::ClientError,
9+
event::{MessageLikeEventType, StateEventType},
10+
};
11+
12+
#[derive(uniffi::Object)]
13+
pub struct RoomPowerLevels {
14+
inner: RumaPowerLevels,
15+
}
16+
17+
#[matrix_sdk_ffi_macros::export]
18+
impl RoomPowerLevels {
19+
fn values(&self) -> RoomPowerLevelsValues {
20+
self.inner.clone().into()
21+
}
22+
23+
/// Returns true if the user with the given user_id is able to ban in the
24+
/// room.
25+
///
26+
/// The call may fail if there is an error in getting the power levels.
27+
pub fn can_user_ban(&self, user_id: String) -> Result<bool, ClientError> {
28+
let user_id = UserId::parse(&user_id)?;
29+
Ok(self.inner.user_can_ban(&user_id))
30+
}
31+
32+
/// Returns true if the user with the given user_id is able to redact
33+
/// their own messages in the room.
34+
///
35+
/// The call may fail if there is an error in getting the power levels.
36+
pub fn can_user_redact_own(&self, user_id: String) -> Result<bool, ClientError> {
37+
let user_id = UserId::parse(&user_id)?;
38+
Ok(self.inner.user_can_redact_own_event(&user_id))
39+
}
40+
41+
/// Returns true if the user with the given user_id is able to redact
42+
/// messages of other users in the room.
43+
///
44+
/// The call may fail if there is an error in getting the power levels.
45+
pub fn can_user_redact_other(&self, user_id: String) -> Result<bool, ClientError> {
46+
let user_id = UserId::parse(&user_id)?;
47+
Ok(self.inner.user_can_redact_event_of_other(&user_id))
48+
}
49+
50+
/// Returns true if the user with the given user_id is able to kick in the
51+
/// room.
52+
///
53+
/// The call may fail if there is an error in getting the power levels.
54+
pub fn can_user_invite(&self, user_id: String) -> Result<bool, ClientError> {
55+
let user_id = UserId::parse(&user_id)?;
56+
Ok(self.inner.user_can_invite(&user_id))
57+
}
58+
59+
/// Returns true if the user with the given user_id is able to kick in the
60+
/// room.
61+
///
62+
/// The call may fail if there is an error in getting the power levels.
63+
pub fn can_user_kick(&self, user_id: String) -> Result<bool, ClientError> {
64+
let user_id = UserId::parse(&user_id)?;
65+
Ok(self.inner.user_can_kick(&user_id))
66+
}
67+
68+
/// Returns true if the user with the given user_id is able to send a
69+
/// specific state event type in the room.
70+
///
71+
/// The call may fail if there is an error in getting the power levels.
72+
pub fn can_user_send_state(
73+
&self,
74+
user_id: String,
75+
state_event: StateEventType,
76+
) -> Result<bool, ClientError> {
77+
let user_id = UserId::parse(&user_id)?;
78+
Ok(self.inner.user_can_send_state(&user_id, state_event.into()))
79+
}
80+
81+
/// Returns true if the user with the given user_id is able to send a
82+
/// specific message type in the room.
83+
///
84+
/// The call may fail if there is an error in getting the power levels.
85+
pub fn can_user_send_message(
86+
&self,
87+
user_id: String,
88+
message: MessageLikeEventType,
89+
) -> Result<bool, ClientError> {
90+
let user_id = UserId::parse(&user_id)?;
91+
Ok(self.inner.user_can_send_message(&user_id, message.into()))
92+
}
93+
94+
/// Returns true if the user with the given user_id is able to pin or unpin
95+
/// events in the room.
96+
///
97+
/// The call may fail if there is an error in getting the power levels.
98+
pub fn can_user_pin_unpin(&self, user_id: String) -> Result<bool, ClientError> {
99+
let user_id = UserId::parse(&user_id)?;
100+
Ok(self.inner.user_can_send_state(&user_id, StateEventType::RoomPinnedEvents.into()))
101+
}
102+
103+
/// Returns true if the user with the given user_id is able to trigger a
104+
/// notification in the room.
105+
///
106+
/// The call may fail if there is an error in getting the power levels.
107+
pub fn can_user_trigger_room_notification(&self, user_id: String) -> Result<bool, ClientError> {
108+
let user_id = UserId::parse(&user_id)?;
109+
Ok(self.inner.user_can_trigger_room_notification(&user_id))
110+
}
111+
}
112+
113+
impl From<RumaPowerLevels> for RoomPowerLevels {
114+
fn from(value: RumaPowerLevels) -> Self {
115+
Self { inner: value }
116+
}
117+
}
118+
119+
/// This intermediary struct is used to expose the power levels values through
120+
/// FFI and work around it not exposing public exported object fields.
121+
#[derive(uniffi::Record)]
122+
pub struct RoomPowerLevelsValues {
123+
/// The level required to ban a user.
124+
pub ban: i64,
125+
/// The level required to invite a user.
126+
pub invite: i64,
127+
/// The level required to kick a user.
128+
pub kick: i64,
129+
/// The level required to redact an event.
130+
pub redact: i64,
131+
/// The default level required to send message events.
132+
pub events_default: i64,
133+
/// The default level required to send state events.
134+
pub state_default: i64,
135+
/// The default power level for every user in the room.
136+
pub users_default: i64,
137+
/// The level required to change the room's name.
138+
pub room_name: i64,
139+
/// The level required to change the room's avatar.
140+
pub room_avatar: i64,
141+
/// The level required to change the room's topic.
142+
pub room_topic: i64,
143+
}
144+
145+
impl From<RumaPowerLevels> for RoomPowerLevelsValues {
146+
fn from(value: RumaPowerLevels) -> Self {
147+
fn state_event_level_for(
148+
power_levels: &RumaPowerLevels,
149+
event_type: &TimelineEventType,
150+
) -> i64 {
151+
let default_state: i64 = power_levels.state_default.into();
152+
power_levels.events.get(event_type).map_or(default_state, |&level| level.into())
153+
}
154+
Self {
155+
ban: value.ban.into(),
156+
invite: value.invite.into(),
157+
kick: value.kick.into(),
158+
redact: value.redact.into(),
159+
events_default: value.events_default.into(),
160+
state_default: value.state_default.into(),
161+
users_default: value.users_default.into(),
162+
room_name: state_event_level_for(&value, &TimelineEventType::RoomName),
163+
room_avatar: state_event_level_for(&value, &TimelineEventType::RoomAvatar),
164+
room_topic: state_event_level_for(&value, &TimelineEventType::RoomTopic),
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)