feat(lua/matrix): add more fields and methods
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
use super::room::Room;
|
||||
use matrix_sdk::{Client as MatrixClient, ruma::UserId};
|
||||
use matrix_sdk::{
|
||||
Client as MatrixClient,
|
||||
ruma::{RoomId, UserId},
|
||||
};
|
||||
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -7,6 +10,30 @@ pub struct Client(pub Arc<MatrixClient>);
|
||||
|
||||
impl UserData for Client {
|
||||
fn add_fields<F: UserDataFields<Self>>(f: &mut F) {
|
||||
f.add_field_method_get("invited_rooms", |_, this| {
|
||||
Ok(this
|
||||
.0
|
||||
.invited_rooms()
|
||||
.into_iter()
|
||||
.map(Room)
|
||||
.collect::<Vec<_>>())
|
||||
});
|
||||
f.add_field_method_get("joined_rooms", |_, this| {
|
||||
Ok(this
|
||||
.0
|
||||
.joined_rooms()
|
||||
.into_iter()
|
||||
.map(Room)
|
||||
.collect::<Vec<_>>())
|
||||
});
|
||||
f.add_field_method_get("left_rooms", |_, this| {
|
||||
Ok(this
|
||||
.0
|
||||
.left_rooms()
|
||||
.into_iter()
|
||||
.map(Room)
|
||||
.collect::<Vec<_>>())
|
||||
});
|
||||
f.add_field_method_get("rooms", |_, this| {
|
||||
Ok(this.0.rooms().into_iter().map(Room).collect::<Vec<_>>())
|
||||
});
|
||||
@@ -23,5 +50,12 @@ impl UserData for Client {
|
||||
.map_err(Error::external)
|
||||
.map(Room)
|
||||
});
|
||||
m.add_async_method("join_room_by_id", async |_, this, room_id: String| {
|
||||
this.0
|
||||
.join_room_by_id(&RoomId::parse(room_id).map_err(Error::external)?)
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(Room)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user