From 4d4cff712015651acf9579df33c14999f876cfb1 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 5 Apr 2020 23:06:43 +0200 Subject: [PATCH] Allow all kinds of messages in /send --- src/data.rs | 53 ++++++++++++++++++++++++++++++++++++++--------------- src/main.rs | 45 +++++++++++++++++---------------------------- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/data.rs b/src/data.rs index f0917ff4..86a7aa91 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,9 @@ use crate::{utils, Database, PduEvent}; use log::debug; -use ruma_events::{room::message::MessageEvent, EventType}; +use ruma_events::{ + room::message::{MessageEvent, MessageEventContent}, + EventType, +}; use ruma_federation_api::RoomV3Pdu; use ruma_identifiers::{EventId, RoomId, UserId}; use std::{ @@ -122,8 +125,7 @@ impl Data { }) } - // TODO: Make sure this isn't called twice in parallel - pub fn pdu_leaves_replace(&self, room_id: &RoomId, event_id: &EventId) -> Vec { + pub fn pdu_leaves_get(&self, room_id: &RoomId) -> Vec { let event_ids = self .db .roomid_pduleaves @@ -135,6 +137,10 @@ impl Data { }) .collect(); + event_ids + } + + pub fn pdu_leaves_replace(&self, room_id: &RoomId, event_id: &EventId) { self.db .roomid_pduleaves .clear(room_id.to_string().as_bytes()); @@ -143,15 +149,20 @@ impl Data { &room_id.to_string().as_bytes(), (*event_id.to_string()).into(), ); - - event_ids } /// Add a persisted data unit from this homeserver - pub fn pdu_append_message(&self, event_id: &EventId, room_id: &RoomId, event: MessageEvent) { + pub fn pdu_append( + &self, + room_id: RoomId, + sender: UserId, + event_type: EventType, + content: MessageEventContent, + ) -> EventId { // prev_events are the leaves of the current graph. This method removes all leaves from the // room and replaces them with our event - let prev_events = self.pdu_leaves_replace(room_id, event_id); + // TODO: Make sure this isn't called twice in parallel + let prev_events = self.pdu_leaves_get(&room_id); // Our depth is the maximum depth of prev_events + 1 let depth = prev_events @@ -166,26 +177,36 @@ impl Data { .unwrap_or(0_u64) + 1; - let pdu = PduEvent { - event_id: event_id.clone(), + let mut pdu = PduEvent { + event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(), room_id: room_id.clone(), - sender: event.sender, + sender: sender.clone(), origin: self.hostname.clone(), - origin_server_ts: event.origin_server_ts, - kind: EventType::RoomMessage, - content: serde_json::to_value(event.content).unwrap(), + origin_server_ts: utils::millis_since_unix_epoch(), + kind: event_type, + content: serde_json::to_value(content).expect("message content is valid json"), state_key: None, prev_events, depth: depth.try_into().unwrap(), auth_events: Vec::new(), redacts: None, - unsigned: Default::default(), + unsigned: Default::default(), // TODO hashes: ruma_federation_api::EventHash { sha256: "aaa".to_owned(), }, signatures: HashMap::new(), }; + // Generate event id + pdu.event_id = EventId::try_from(&*format!( + "${}", + ruma_signatures::reference_hash(&serde_json::to_value(&pdu).unwrap()) + .expect("ruma can calculate reference hashes") + )) + .expect("ruma's reference hashes are correct"); + + self.pdu_leaves_replace(&room_id, &pdu.event_id); + // The new value will need a new index. We store the last used index in 'n' + id let mut count_key: Vec = vec![b'n']; count_key.extend_from_slice(&room_id.to_string().as_bytes()); @@ -213,8 +234,10 @@ impl Data { self.db .eventid_pduid - .insert(event_id.to_string(), pdu_id.clone()) + .insert(pdu.event_id.to_string(), pdu_id.clone()) .unwrap(); + + pdu.event_id } /// Returns a vector of all PDUs. diff --git a/src/main.rs b/src/main.rs index 491a1b76..5a8c066b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,39 +213,28 @@ fn create_message_event_route( _txn_id: String, body: Ruma, ) -> MatrixResult { - // Construct event - let mut event = RoomEvent::RoomMessage(MessageEvent { - content: body.data.clone().into_result().unwrap(), - event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(), - origin_server_ts: utils::millis_since_unix_epoch(), - room_id: Some(body.room_id.clone()), - sender: body.user_id.clone().expect("user is authenticated"), - unsigned: Map::default(), - }); - - // Generate event id - let event_id = EventId::try_from(&*format!( - "${}", - ruma_signatures::reference_hash(&serde_json::to_value(&event).unwrap()) - .expect("ruma can calculate reference hashes") - )) - .expect("ruma's reference hashes are correct"); - - // Insert event id - if let RoomEvent::RoomMessage(message) = &mut event { - message.event_id = event_id.clone(); - data.pdu_append_message(&event_id, &body.room_id, message.clone()); + if let Ok(content) = body.data.clone().into_result() { + let event_id = data.pdu_append( + body.room_id.clone(), + body.user_id.clone().expect("user is authenticated"), + body.event_type.clone(), + content, + ); + MatrixResult(Ok(create_message_event::Response { event_id })) } else { - error!("only roommessages are handled currently"); + error!("No data found"); + MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "Room not found.".to_owned(), + status_code: http::StatusCode::NOT_FOUND, + })) } - - MatrixResult(Ok(create_message_event::Response { event_id })) } -#[get("/_matrix/client/r0/sync", data = "")] +#[get("/_matrix/client/r0/sync", data = "<_body>")] fn sync_route( data: State, - body: Ruma, + _body: Ruma, ) -> MatrixResult { let mut joined_rooms = HashMap::new(); { @@ -298,7 +287,7 @@ fn sync_route( fn options_route(_segments: PathBuf) -> MatrixResult { MatrixResult(Err(Error { kind: ErrorKind::NotFound, - message: "Room not found.".to_owned(), + message: "This is the options route.".to_owned(), status_code: http::StatusCode::NOT_FOUND, })) }