Can get room messages

This commit is contained in:
2025-11-24 12:36:59 +01:00
parent 7562a7fc61
commit bf119a34fb
3 changed files with 42 additions and 5 deletions

View File

@@ -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<APIEventsList> {
pub(super) async fn get_events(
room: &Room,
limit: u32,
from: Option<&str>,
) -> anyhow::Result<APIEventsList> {
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<APIEve
.collect::<Result<Vec<_>, _>>()?,
})
}
#[derive(Deserialize)]
pub struct GetRoomEventsQuery {
#[serde(default)]
limit: Option<u32>,
#[serde(default)]
from: Option<String>,
}
/// Get the events for a room
pub async fn get_for_room(
client: MatrixClientExtractor,
path: web::Path<RoomIdInPath>,
query: web::Query<GetRoomEventsQuery>,
) -> 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?))
}