Compare commits
3 Commits
bf119a34fb
...
4d72644a31
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d72644a31 | |||
| 0a395b0d26 | |||
| 639cc6c737 |
@@ -6,6 +6,10 @@ use futures_util::{StreamExt, stream};
|
|||||||
use matrix_sdk::Room;
|
use matrix_sdk::Room;
|
||||||
use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind};
|
use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind};
|
||||||
use matrix_sdk::room::MessagesOptions;
|
use matrix_sdk::room::MessagesOptions;
|
||||||
|
use matrix_sdk::room::edit::EditedContent;
|
||||||
|
use matrix_sdk::ruma::events::room::message::{
|
||||||
|
RoomMessageEventContent, RoomMessageEventContentWithoutRelation,
|
||||||
|
};
|
||||||
use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomId, UInt};
|
use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomId, UInt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::value::RawValue;
|
use serde_json::value::RawValue;
|
||||||
@@ -82,10 +86,95 @@ pub async fn get_for_room(
|
|||||||
path: web::Path<RoomIdInPath>,
|
path: web::Path<RoomIdInPath>,
|
||||||
query: web::Query<GetRoomEventsQuery>,
|
query: web::Query<GetRoomEventsQuery>,
|
||||||
) -> HttpResult {
|
) -> HttpResult {
|
||||||
let Some(room) = client.client.client.get_room(&path.id) else {
|
let Some(room) = client.client.client.get_room(&path.room_id) else {
|
||||||
return Ok(HttpResponse::NotFound().json("Room not found!"));
|
return Ok(HttpResponse::NotFound().json("Room not found!"));
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.json(get_events(&room, query.limit.unwrap_or(500), query.from.as_deref()).await?))
|
.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<RoomIdInPath>,
|
||||||
|
) -> HttpResult {
|
||||||
|
let req = client.auth.decode_json_body::<SendTextMessageRequest>()?;
|
||||||
|
|
||||||
|
let Some(room) = client.client.client.get_room(&path.room_id) else {
|
||||||
|
return Ok(HttpResponse::NotFound().json("Room not found!"));
|
||||||
|
};
|
||||||
|
|
||||||
|
room.send(RoomMessageEventContent::text_plain(req.content))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Accepted().finish())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
pub struct EventIdInPath {
|
||||||
|
pub(crate) event_id: OwnedEventId,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_text_content(
|
||||||
|
client: MatrixClientExtractor,
|
||||||
|
path: web::Path<RoomIdInPath>,
|
||||||
|
event_path: web::Path<EventIdInPath>,
|
||||||
|
) -> HttpResult {
|
||||||
|
let req = client.auth.decode_json_body::<SendTextMessageRequest>()?;
|
||||||
|
|
||||||
|
let Some(room) = client.client.client.get_room(&path.room_id) else {
|
||||||
|
return Ok(HttpResponse::NotFound().json("Room not found!"));
|
||||||
|
};
|
||||||
|
|
||||||
|
let edit_event = match room
|
||||||
|
.make_edit_event(
|
||||||
|
&event_path.event_id,
|
||||||
|
EditedContent::RoomMessage(RoomMessageEventContentWithoutRelation::text_plain(
|
||||||
|
req.content,
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(msg) => msg,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!(
|
||||||
|
"Failed to created edit message event {}: {e}",
|
||||||
|
event_path.event_id
|
||||||
|
);
|
||||||
|
return Ok(HttpResponse::InternalServerError()
|
||||||
|
.json(format!("Failed to create edit message event! {e}")));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(match room.send(edit_event).await {
|
||||||
|
Ok(_) => HttpResponse::Accepted().finish(),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to edit event message {}: {e}", event_path.event_id);
|
||||||
|
HttpResponse::InternalServerError().json(format!("Failed to edit event! {e}"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn redact_event(
|
||||||
|
client: MatrixClientExtractor,
|
||||||
|
path: web::Path<RoomIdInPath>,
|
||||||
|
event_path: web::Path<EventIdInPath>,
|
||||||
|
) -> HttpResult {
|
||||||
|
let Some(room) = client.client.client.get_room(&path.room_id) else {
|
||||||
|
return Ok(HttpResponse::NotFound().json("Room not found!"));
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(match room.redact(&event_path.event_id, None, None).await {
|
||||||
|
Ok(_) => HttpResponse::Accepted().finish(),
|
||||||
|
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to redact event {}: {e}", event_path.event_id);
|
||||||
|
HttpResponse::InternalServerError().json(format!("Failed to redact event! {e}"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ pub async fn get_joined_spaces(client: MatrixClientExtractor) -> HttpResult {
|
|||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
pub struct RoomIdInPath {
|
pub struct RoomIdInPath {
|
||||||
pub(crate) id: OwnedRoomId,
|
pub(crate) room_id: OwnedRoomId,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the list of joined rooms of the user
|
/// Get the list of joined rooms of the user
|
||||||
@@ -91,7 +91,7 @@ pub async fn single_room_info(
|
|||||||
client: MatrixClientExtractor,
|
client: MatrixClientExtractor,
|
||||||
path: web::Path<RoomIdInPath>,
|
path: web::Path<RoomIdInPath>,
|
||||||
) -> HttpResult {
|
) -> HttpResult {
|
||||||
Ok(match client.client.client.get_room(&path.id) {
|
Ok(match client.client.client.get_room(&path.room_id) {
|
||||||
None => HttpResponse::NotFound().json("Room not found"),
|
None => HttpResponse::NotFound().json("Room not found"),
|
||||||
Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r).await?),
|
Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r).await?),
|
||||||
})
|
})
|
||||||
@@ -103,7 +103,7 @@ pub async fn room_avatar(
|
|||||||
client: MatrixClientExtractor,
|
client: MatrixClientExtractor,
|
||||||
path: web::Path<RoomIdInPath>,
|
path: web::Path<RoomIdInPath>,
|
||||||
) -> HttpResult {
|
) -> HttpResult {
|
||||||
let Some(room) = client.client.client.get_room(&path.id) else {
|
let Some(room) = client.client.client.get_room(&path.room_id) else {
|
||||||
return Ok(HttpResponse::NotFound().json("Room not found"));
|
return Ok(HttpResponse::NotFound().json("Room not found"));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -148,11 +148,11 @@ async fn main() -> std::io::Result<()> {
|
|||||||
web::get().to(matrix_room_controller::get_joined_spaces),
|
web::get().to(matrix_room_controller::get_joined_spaces),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/matrix/room/{id}",
|
"/api/matrix/room/{room_id}",
|
||||||
web::get().to(matrix_room_controller::single_room_info),
|
web::get().to(matrix_room_controller::single_room_info),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/matrix/room/{id}/avatar",
|
"/api/matrix/room/{room_id}/avatar",
|
||||||
web::get().to(matrix_room_controller::room_avatar),
|
web::get().to(matrix_room_controller::room_avatar),
|
||||||
)
|
)
|
||||||
// Matrix profile controller
|
// Matrix profile controller
|
||||||
@@ -166,9 +166,21 @@ async fn main() -> std::io::Result<()> {
|
|||||||
)
|
)
|
||||||
// Matrix events controller
|
// Matrix events controller
|
||||||
.route(
|
.route(
|
||||||
"/api/matrix/room/{id}/events",
|
"/api/matrix/room/{room_id}/events",
|
||||||
web::get().to(matrix_event_controller::get_for_room),
|
web::get().to(matrix_event_controller::get_for_room),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/api/matrix/room/{room_id}/send_text_message",
|
||||||
|
web::post().to(matrix_event_controller::send_text_message),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/api/matrix/room/{room_id}/event/{event_id}/set_text_content",
|
||||||
|
web::post().to(matrix_event_controller::set_text_content),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/api/matrix/room/{room_id}/event/{event_id}",
|
||||||
|
web::delete().to(matrix_event_controller::redact_event),
|
||||||
|
)
|
||||||
// Matrix media controller
|
// Matrix media controller
|
||||||
.route(
|
.route(
|
||||||
"/api/matrix/media/{mxc}",
|
"/api/matrix/media/{mxc}",
|
||||||
|
|||||||
Reference in New Issue
Block a user