feat(lua/matrix): add more fields and methods
This commit is contained in:
parent
638dc75cb7
commit
0f2a5a0dc5
@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
use super::member::Member;
|
||||
use matrix_sdk::{
|
||||
RoomMemberships, room::Room as MatrixRoom, ruma::events::room::message::RoomMessageEventContent,
|
||||
RoomMemberships,
|
||||
room::Room as MatrixRoom,
|
||||
ruma::{EventId, UserId, events::room::message::RoomMessageEventContent},
|
||||
};
|
||||
use mlua::{Error, UserData, UserDataFields, UserDataMethods};
|
||||
|
||||
@ -17,16 +19,18 @@ impl UserData for Room {
|
||||
}
|
||||
|
||||
fn add_methods<M: UserDataMethods<Self>>(m: &mut M) {
|
||||
m.add_async_method("send", async |_, this, body: String| {
|
||||
m.add_async_method(
|
||||
"ban_user",
|
||||
async |_, this, (user_id, reason): (String, Option<String>)| {
|
||||
this.0
|
||||
.send(RoomMessageEventContent::text_plain(body))
|
||||
.ban_user(
|
||||
&UserId::parse(user_id).map_err(Error::external)?,
|
||||
reason.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(|response| response.event_id.to_string())
|
||||
});
|
||||
m.add_async_method("leave", async |_, this, (): ()| {
|
||||
this.0.leave().await.map_err(Error::external)
|
||||
});
|
||||
},
|
||||
);
|
||||
m.add_async_method("get_members", async |_, this, (): ()| {
|
||||
this.0
|
||||
.members(RoomMemberships::all())
|
||||
@ -39,5 +43,41 @@ impl UserData for Room {
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
});
|
||||
m.add_async_method(
|
||||
"kick_user",
|
||||
async |_, this, (user_id, reason): (String, Option<String>)| {
|
||||
this.0
|
||||
.kick_user(
|
||||
&UserId::parse(user_id).map_err(Error::external)?,
|
||||
reason.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
},
|
||||
);
|
||||
m.add_async_method("leave", async |_, this, (): ()| {
|
||||
this.0.leave().await.map_err(Error::external)
|
||||
});
|
||||
m.add_async_method(
|
||||
"redact",
|
||||
async |_, this, (event_id, reason): (String, Option<String>)| {
|
||||
this.0
|
||||
.redact(
|
||||
&EventId::parse(event_id).map_err(Error::external)?,
|
||||
reason.as_deref(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(|response| response.event_id.to_string())
|
||||
},
|
||||
);
|
||||
m.add_async_method("send", async |_, this, body: String| {
|
||||
this.0
|
||||
.send(RoomMessageEventContent::text_plain(body))
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(|response| response.event_id.to_string())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user