diff --git a/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs index a8228de..e9732a6 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_event_controller.rs @@ -1,9 +1,13 @@ +use crate::controllers::HttpResult; +use crate::controllers::matrix::matrix_room_controller::RoomIdInPath; +use crate::extractors::matrix_client_extractor::MatrixClientExtractor; +use actix_web::{HttpResponse, web}; 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::{MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomId, UInt}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::value::RawValue; #[derive(Serialize)] @@ -42,8 +46,13 @@ pub struct APIEventsList { } /// Get messages for a given room -pub(super) async fn get_events(room: &Room, limit: u32) -> anyhow::Result { +pub(super) async fn get_events( + room: &Room, + limit: u32, + from: Option<&str>, +) -> anyhow::Result { let mut msg_opts = MessagesOptions::backward(); + msg_opts.from = from.map(str::to_string); msg_opts.limit = UInt::from(limit); let messages = room.messages(msg_opts).await?; @@ -58,3 +67,25 @@ pub(super) async fn get_events(room: &Room, limit: u32) -> anyhow::Result, _>>()?, }) } + +#[derive(Deserialize)] +pub struct GetRoomEventsQuery { + #[serde(default)] + limit: Option, + #[serde(default)] + from: Option, +} + +/// Get the events for a room +pub async fn get_for_room( + client: MatrixClientExtractor, + path: web::Path, + query: web::Query, +) -> HttpResult { + let Some(room) = client.client.client.get_room(&path.id) else { + return Ok(HttpResponse::NotFound().json("Room not found!")); + }; + + Ok(HttpResponse::Ok() + .json(get_events(&room, query.limit.unwrap_or(500), query.from.as_deref()).await?)) +} diff --git a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs index 3970c7d..e2384b5 100644 --- a/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs +++ b/matrixgw_backend/src/controllers/matrix/matrix_room_controller.rs @@ -52,7 +52,7 @@ impl APIRoomInfo { is_space: r.is_space(), parents: parent_spaces, number_unread_messages: r.num_unread_messages(), - latest_event: get_events(r, 1).await?.messages.into_iter().next(), + latest_event: get_events(r, 1, None).await?.messages.into_iter().next(), }) } } @@ -83,7 +83,7 @@ pub async fn get_joined_spaces(client: MatrixClientExtractor) -> HttpResult { #[derive(serde::Deserialize)] pub struct RoomIdInPath { - id: OwnedRoomId, + pub(crate) id: OwnedRoomId, } /// Get the list of joined rooms of the user diff --git a/matrixgw_backend/src/main.rs b/matrixgw_backend/src/main.rs index 6aa4e83..4bcf520 100644 --- a/matrixgw_backend/src/main.rs +++ b/matrixgw_backend/src/main.rs @@ -10,7 +10,8 @@ use matrixgw_backend::app_config::AppConfig; use matrixgw_backend::broadcast_messages::BroadcastMessage; use matrixgw_backend::constants; use matrixgw_backend::controllers::matrix::{ - matrix_media_controller, matrix_profile_controller, matrix_room_controller, + matrix_event_controller, matrix_media_controller, matrix_profile_controller, + matrix_room_controller, }; use matrixgw_backend::controllers::{ auth_controller, matrix_link_controller, matrix_sync_thread_controller, server_controller, @@ -163,6 +164,11 @@ async fn main() -> std::io::Result<()> { "/api/matrix/profile/get_multiple", web::post().to(matrix_profile_controller::get_multiple), ) + // Matrix events controller + .route( + "/api/matrix/room/{id}/events", + web::get().to(matrix_event_controller::get_for_room), + ) // Matrix media controller .route( "/api/matrix/media/{mxc}",