Start work on message events

This commit is contained in:
timokoesters 2020-03-30 00:10:15 +02:00
parent 533260edd8
commit b508b4d1e7
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
3 changed files with 25 additions and 27 deletions

View file

@ -1,7 +1,8 @@
use crate::utils; use crate::utils;
use directories::ProjectDirs; use directories::ProjectDirs;
use ruma_events::collections::all::RoomEvent; use log::debug;
use ruma_identifiers::UserId; use ruma_events::collections::all::Event;
use ruma_identifiers::{EventId, RoomId, UserId};
use std::convert::TryInto; use std::convert::TryInto;
const USERID_PASSWORD: &str = "userid_password"; const USERID_PASSWORD: &str = "userid_password";
@ -126,7 +127,8 @@ impl Data {
} }
/// Create a new room event. /// 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!(); todo!();
} }
} }

View file

@ -14,7 +14,8 @@ use ruma_client_api::{
}, },
unversioned::get_supported_versions, 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_identifiers::{EventId, UserId};
use ruma_wrapper::{MatrixResult, Ruma}; use ruma_wrapper::{MatrixResult, Ruma};
use serde_json::map::Map; use serde_json::map::Map;
@ -213,31 +214,19 @@ fn create_message_event_route(
_txn_id: String, _txn_id: String,
body: Ruma<create_message_event::Request>, body: Ruma<create_message_event::Request>,
) -> MatrixResult<create_message_event::Response> { ) -> MatrixResult<create_message_event::Response> {
// Check if content is valid // Generate event id
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,
}));
}
};
let event_id = EventId::try_from("$TODOrandomeventid:localhost").unwrap(); let event_id = EventId::try_from("$TODOrandomeventid:localhost").unwrap();
data.event_add(
data.room_event_add( &Event::RoomMessage(MessageEvent {
&MessageEvent { content: body.data.clone().into_result().unwrap(),
content,
event_id: event_id.clone(), event_id: event_id.clone(),
origin_server_ts: utils::millis_since_unix_epoch(), origin_server_ts: utils::millis_since_unix_epoch(),
room_id: Some(body.room_id.clone()), 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(), unsigned: Map::default(),
} }),
.into(), &body.room_id,
&event_id,
); );
MatrixResult(Ok(create_message_event::Response { event_id })) MatrixResult(Ok(create_message_event::Response { event_id }))

View file

@ -26,6 +26,7 @@ const MESSAGE_LIMIT: u64 = 65535;
pub struct Ruma<T: Outgoing> { pub struct Ruma<T: Outgoing> {
body: T::Incoming, body: T::Incoming,
pub user_id: Option<UserId>, pub user_id: Option<UserId>,
pub json_body: serde_json::Value,
} }
impl<T: Endpoint> FromDataSimple for Ruma<T> impl<T: Endpoint> FromDataSimple for Ruma<T>
@ -77,11 +78,17 @@ where
let mut body = Vec::new(); let mut body = Vec::new();
handle.read_to_end(&mut body).unwrap(); 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); log::info!("{:?}", http_request);
match T::Incoming::try_from(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) => { Err(e) => {
log::error!("{:?}", e); log::error!("{:?}", e);
Failure((Status::InternalServerError, ())) Failure((Status::InternalServerError, ()))