feat: add matrix support
This commit is contained in:
27
src/lua/matrix/client.rs
Normal file
27
src/lua/matrix/client.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use super::room::Room;
|
||||
use matrix_sdk::{Client as MatrixClient, ruma::UserId};
|
||||
use mlua::{Error, UserData};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Client(pub Arc<MatrixClient>);
|
||||
|
||||
impl UserData for Client {
|
||||
fn add_fields<F: mlua::UserDataFields<Self>>(f: &mut F) {
|
||||
f.add_field_method_get("rooms", |_, this| {
|
||||
Ok(this.0.rooms().into_iter().map(Room).collect::<Vec<_>>())
|
||||
});
|
||||
f.add_field_method_get("user_id", |_, this| {
|
||||
Ok(this.0.user_id().map(std::string::ToString::to_string))
|
||||
});
|
||||
}
|
||||
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(m: &mut M) {
|
||||
m.add_async_method("create_dm", async |_, this, user_id: String| {
|
||||
this.0
|
||||
.create_dm(&UserId::parse(user_id).map_err(Error::external)?)
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(Room)
|
||||
});
|
||||
}
|
||||
}
|
12
src/lua/matrix/member.rs
Normal file
12
src/lua/matrix/member.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use matrix_sdk::room::RoomMember;
|
||||
use mlua::UserData;
|
||||
|
||||
pub struct Member(pub RoomMember);
|
||||
|
||||
impl UserData for Member {
|
||||
fn add_fields<F: mlua::UserDataFields<Self>>(f: &mut F) {
|
||||
f.add_field_method_get("id", |_, this| Ok(this.0.user_id().to_string()));
|
||||
f.add_field_method_get("name", |_, this| Ok(this.0.name().to_owned()));
|
||||
f.add_field_method_get("power_level", |_, this| Ok(this.0.power_level()));
|
||||
}
|
||||
}
|
3
src/lua/matrix/mod.rs
Normal file
3
src/lua/matrix/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod client;
|
||||
pub mod member;
|
||||
pub mod room;
|
43
src/lua/matrix/room.rs
Normal file
43
src/lua/matrix/room.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use super::member::Member;
|
||||
use matrix_sdk::{
|
||||
RoomMemberships, room::Room as MatrixRoom, ruma::events::room::message::RoomMessageEventContent,
|
||||
};
|
||||
use mlua::{Error, UserData};
|
||||
|
||||
pub struct Room(pub MatrixRoom);
|
||||
|
||||
impl UserData for Room {
|
||||
fn add_fields<F: mlua::UserDataFields<Self>>(f: &mut F) {
|
||||
f.add_field_method_get("id", |_, this| Ok(this.0.room_id().to_string()));
|
||||
f.add_field_method_get("name", |_, this| Ok(this.0.name()));
|
||||
f.add_field_method_get("topic", |_, this| Ok(this.0.topic()));
|
||||
f.add_field_method_get("type", |_, this| {
|
||||
Ok(this.0.room_type().map(|room_type| room_type.to_string()))
|
||||
});
|
||||
}
|
||||
|
||||
fn add_methods<M: mlua::UserDataMethods<Self>>(m: &mut M) {
|
||||
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())
|
||||
});
|
||||
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())
|
||||
.await
|
||||
.map_err(Error::external)
|
||||
.map(|members| {
|
||||
members
|
||||
.into_iter()
|
||||
.map(|member| Member(member.clone()))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@ pub mod container;
|
||||
pub mod direction;
|
||||
pub mod events;
|
||||
pub mod logging;
|
||||
pub mod matrix;
|
||||
pub mod nochatreports;
|
||||
pub mod player;
|
||||
pub mod system;
|
||||
|
Reference in New Issue
Block a user