diff --git a/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs index e9732a6..1840549 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs @@ -6,6 +6,7 @@ use futures_util::{StreamExt, stream}; use matrix_sdk::Room; use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind}; use matrix_sdk::room::MessagesOptions; +use matrix_sdk::ruma::events::room::message::RoomMessageEventContent; use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomId, UInt}; use serde::{Deserialize, Serialize}; use serde_json::value::RawValue; @@ -89,3 +90,24 @@ pub async fn get_for_room( Ok(HttpResponse::Ok() .json(get_events(&room, query.limit.unwrap_or(500), query.from.as_deref()).await?)) } + +#[derive(Deserialize)] +struct SendTextMessageRequest { + content: String, +} + +pub async fn send_text_message( + client: MatrixClientExtractor, + path: web::Path, +) -> HttpResult { + let req = client.auth.decode_json_body::()?; + + let Some(room) = client.client.client.get_room(&path.id) else { + return Ok(HttpResponse::NotFound().json("Room not found!")); + }; + + room.send(RoomMessageEventContent::text_plain(req.content)) + .await?; + + Ok(HttpResponse::Accepted().finish()) +} diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 4bcf520..2f7f011 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -169,6 +169,10 @@ async fn main() -> std::io::Result<()> { "/api/matrix/room/{id}/events", web::get().to(matrix_event_controller::get_for_room), ) + .route( + "/api/matrix/room/{id}/send_text_message", + web::post().to(matrix_event_controller::send_text_message), + ) // Matrix media controller .route( "/api/matrix/media/{mxc}",