From 73e04e71d7715dda62197ae67267cbdeef0b32c0 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 29 Mar 2020 13:48:44 +0200 Subject: [PATCH] Start work on event creation --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/data.rs | 3 +++ src/main.rs | 23 ++++++++++++++++++++--- src/utils.rs | 9 +++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 91c24415..b1940929 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -489,11 +489,13 @@ version = "0.1.0" dependencies = [ "directories", "http", + "js_int", "log 0.4.8", "pretty_env_logger", "rocket", "ruma-api", "ruma-client-api", + "ruma-events", "ruma-identifiers", "sled", ] diff --git a/Cargo.toml b/Cargo.toml index 62322d77..b6933785 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ sled = "0.31.0" directories = "2.0.2" ruma-identifiers = "0.14.1" ruma-api = "0.15.0-dev.1" +ruma-events = "0.17.0" +js_int = "0.1.3" diff --git a/src/data.rs b/src/data.rs index 52fe7af9..de063ab0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,4 +1,5 @@ use directories::ProjectDirs; +use ruma_events::collections::all::RoomEvent; use ruma_identifiers::UserId; pub struct Data(sled::Db); @@ -36,4 +37,6 @@ impl Data { .insert(user_id.to_string(), &*password.unwrap_or_default()) .unwrap(); } + + pub fn room_event_add(&self, room_event: &RoomEvent) {} } diff --git a/src/main.rs b/src/main.rs index ba953195..8b488d5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![feature(proc_macro_hygiene, decl_macro)] mod data; mod ruma_wrapper; +mod utils; pub use data::Data; use log::debug; @@ -13,8 +14,10 @@ use ruma_client_api::{ }, unversioned::get_supported_versions, }; -use ruma_identifiers::UserId; +use ruma_events::room::message::MessageEvent; +use ruma_identifiers::{EventId, UserId}; use ruma_wrapper::{MatrixResult, Ruma}; +use std::convert::TryFrom; use std::{collections::HashMap, convert::TryInto}; #[get("/_matrix/client/versions")] @@ -153,14 +156,28 @@ fn join_room_by_id_route( data = "" )] fn create_message_event_route( + data: State, _room_id: String, _event_type: String, _txn_id: String, body: Ruma, ) -> MatrixResult { - dbg!(body); + dbg!(&body); + if let Ok(content) = body.data.clone().into_result() { + data.room_event_add( + &MessageEvent { + content, + event_id: EventId::try_from("$randomeventid:localhost").unwrap(), + origin_server_ts: utils::millis_since_unix_epoch(), + room_id: Some(body.room_id.clone()), + sender: UserId::try_from("@TODO:localhost").unwrap(), + unsigned: None, + } + .into(), + ); + } MatrixResult(Ok(create_message_event::Response { - event_id: "$randomeventid".try_into().unwrap(), + event_id: "$randomeventid:localhost".try_into().unwrap(), })) } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 00000000..29050886 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +pub fn millis_since_unix_epoch() -> js_int::UInt { + (SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() as u32) + .into() +}