From b508b4d1e7f55281e2561d50dcffcf8bf9134ab7 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Mon, 30 Mar 2020 00:10:15 +0200 Subject: [PATCH] Start work on message events --- src/data.rs | 8 +++++--- src/main.rs | 31 ++++++++++--------------------- src/ruma_wrapper.rs | 13 ++++++++++--- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/data.rs b/src/data.rs index 9a0a9c2..0fa24d4 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,7 +1,8 @@ use crate::utils; use directories::ProjectDirs; -use ruma_events::collections::all::RoomEvent; -use ruma_identifiers::UserId; +use log::debug; +use ruma_events::collections::all::Event; +use ruma_identifiers::{EventId, RoomId, UserId}; use std::convert::TryInto; const USERID_PASSWORD: &str = "userid_password"; @@ -126,7 +127,8 @@ impl Data { } /// Create a new room event. - pub fn room_event_add(&self, _room_event: &RoomEvent) { + pub fn event_add(&self, event: &Event, room_id: &RoomId, event_id: &EventId) { + debug!("{}", serde_json::to_string(event).unwrap()); todo!(); } } diff --git a/src/main.rs b/src/main.rs index 7cb7c67..06f7ca3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,8 @@ use ruma_client_api::{ }, unversioned::get_supported_versions, }; -use ruma_events::{room::message::MessageEvent, EventResult}; +use ruma_events::collections::all::Event; +use ruma_events::room::message::MessageEvent; use ruma_identifiers::{EventId, UserId}; use ruma_wrapper::{MatrixResult, Ruma}; use serde_json::map::Map; @@ -213,31 +214,19 @@ fn create_message_event_route( _txn_id: String, body: Ruma, ) -> MatrixResult { - // Check if content is valid - let content = match body.data.clone() { - EventResult::Ok(content) => content, - EventResult::Err(_) => { - debug!("No content."); - return MatrixResult(Err(Error { - kind: ErrorKind::NotFound, - message: "No content.".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - })); - } - }; - + // Generate event id let event_id = EventId::try_from("$TODOrandomeventid:localhost").unwrap(); - - data.room_event_add( - &MessageEvent { - content, + data.event_add( + &Event::RoomMessage(MessageEvent { + content: body.data.clone().into_result().unwrap(), event_id: event_id.clone(), origin_server_ts: utils::millis_since_unix_epoch(), room_id: Some(body.room_id.clone()), - sender: body.user_id.expect("user is authenticated"), + sender: body.user_id.clone().expect("user is authenticated"), unsigned: Map::default(), - } - .into(), + }), + &body.room_id, + &event_id, ); MatrixResult(Ok(create_message_event::Response { event_id })) diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 0b42ceb..e898137 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -26,6 +26,7 @@ const MESSAGE_LIMIT: u64 = 65535; pub struct Ruma { body: T::Incoming, pub user_id: Option, + pub json_body: serde_json::Value, } impl FromDataSimple for Ruma @@ -77,11 +78,17 @@ where let mut body = Vec::new(); handle.read_to_end(&mut body).unwrap(); - let http_request = http_request.body(body).unwrap(); - + let http_request = http_request.body(body.clone()).unwrap(); log::info!("{:?}", http_request); + match T::Incoming::try_from(http_request) { - Ok(t) => Success(Ruma { body: t, user_id }), + Ok(t) => Success(Ruma { + body: t, + user_id, + // TODO: Can we avoid parsing it again? + json_body: serde_json::from_slice(&body) + .expect("Ruma already parsed it successfuly"), + }), Err(e) => { log::error!("{:?}", e); Failure((Status::InternalServerError, ()))