From 75959cdc10f5e2036107ad02d68ee782e452ab53 Mon Sep 17 00:00:00 2001 From: Devin R Date: Sat, 18 Jul 2020 08:21:25 -0400 Subject: [PATCH] Resolve stying review issues --- Cargo.toml | 6 +---- src/client_server.rs | 45 +++++++++++++++++++------------------- src/database.rs | 1 - src/database/globals.rs | 2 +- src/database/rooms/edus.rs | 36 ++++++++++++++++++++++++++++-- 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 514a178..80785c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,4 @@ reqwest = "0.10.6" base64 = "0.12.1" thiserror = "1.0.19" image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } - -[dependencies.ruma] -git = "https://github.com/ruma/ruma" -features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"] -rev = "848b225" +ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "848b225" } diff --git a/src/client_server.rs b/src/client_server.rs index 1300bff..c83f9e2 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -64,7 +64,7 @@ use ruma::{ canonical_alias, guest_access, history_visibility, join_rules, member, name, redaction, topic, }, - AnyBasicEvent, AnyEphemeralRoomEvent, AnyEvent as EduEvent, EventJson, EventType, + AnyBasicEvent, AnyEphemeralRoomEvent, AnyEvent, EventJson, EventType, }, identifiers::{RoomAliasId, RoomId, RoomVersionId, UserId}, }; @@ -285,7 +285,7 @@ pub fn login_route( user_id, access_token: token, home_server: Some(db.globals.server_name().to_string()), - device_id: device_id.to_string(), + device_id: device_id.into(), well_known: None, } .into()) @@ -479,7 +479,7 @@ pub fn get_pushrules_all_route( ) -> ConduitResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - if let EduEvent::Basic(AnyBasicEvent::PushRules(pushrules)) = db + if let AnyEvent::Basic(AnyBasicEvent::PushRules(pushrules)) = db .account_data .get(None, &user_id, &EventType::PushRules)? .ok_or(Error::BadRequest( @@ -602,13 +602,13 @@ pub fn get_global_account_data_route( )? .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; - let data: Result = data + let data: AnyEvent = data .deserialize() - .map_err(|_| Error::bad_database("Deserialization of account data failed")); + .map_err(|_| Error::bad_database("Deserialization of account data failed"))?; - if let EduEvent::Basic(data) = data? { + if let AnyEvent::Basic(data) = data { Ok(get_global_account_data::Response { - account_data: EventJson::from(data), + account_data: data.into(), } .into()) } else { @@ -1145,7 +1145,7 @@ pub fn set_read_marker_route( db.rooms.edus.roomlatest_update( &user_id, &body.room_id, - EduEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt( + AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt( ruma::events::receipt::ReceiptEvent { content: ruma::events::receipt::ReceiptEventContent(receipt_content), room_id: body.room_id.clone(), @@ -1221,6 +1221,7 @@ pub fn create_room_route( .as_ref() .and_then(|c| c.predecessor.clone()); content.room_version = RoomVersionId::version_6(); + // 1. The room create event db.rooms.append_pdu( room_id.clone(), @@ -2511,7 +2512,7 @@ pub fn sync_route( let room_events = pdus .into_iter() - .map(|pdu| pdu.to_room_event_stub()) + .map(|pdu| pdu.to_sync_room_event()) .collect::>(); let mut edus = db @@ -2529,7 +2530,7 @@ pub fn sync_route( { edus.push( serde_json::from_str( - &serde_json::to_string(&EduEvent::Ephemeral(AnyEphemeralRoomEvent::Typing( + &serde_json::to_string(&AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Typing( db.rooms.edus.roomactives_all(&room_id)?, ))) .expect("event is valid, we just created it"), @@ -2544,14 +2545,14 @@ pub fn sync_route( .account_data .changes_since(Some(&room_id), &user_id, since)? .into_iter() - .filter_map(|(_, v)| { - if let Ok(EduEvent::Basic(account_event)) = v.deserialize() { - Some(EventJson::from(account_event)) + .map(|(_, v)| { + if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { + Ok(EventJson::from(account_event)) } else { - None + Err(Error::bad_database("found invalid event")) } }) - .collect(), + .collect::, _>>()?, }, summary: sync_events::RoomSummary { heroes, @@ -2573,7 +2574,7 @@ pub fn sync_route( db.rooms .room_state_full(&room_id)? .into_iter() - .map(|(_, pdu)| pdu.to_state_event_stub()) + .map(|(_, pdu)| pdu.to_sync_state_event()) .collect() } else { Vec::new() @@ -2593,7 +2594,7 @@ pub fn sync_route( let pdus = db.rooms.pdus_since(&user_id, &room_id, since)?; let room_events = pdus .filter_map(|pdu| pdu.ok()) // Filter out buggy events - .map(|pdu| pdu.to_room_event_stub()) + .map(|pdu| pdu.to_sync_room_event()) .collect(); // TODO: Only until leave point @@ -2688,14 +2689,14 @@ pub fn sync_route( .account_data .changes_since(None, &user_id, since)? .into_iter() - .filter_map(|(_, v)| { - if let Ok(EduEvent::Basic(account_event)) = v.deserialize() { - Some(EventJson::from(account_event)) + .map(|(_, v)| { + if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { + Ok(EventJson::from(account_event)) } else { - None + Err(Error::bad_database("found invalid event")) } }) - .collect(), + .collect::, _>>()?, }, device_lists: sync_events::DeviceLists { changed: if since != 0 { diff --git a/src/database.rs b/src/database.rs index fcda5af..370fde7 100644 --- a/src/database.rs +++ b/src/database.rs @@ -62,7 +62,6 @@ impl Database { .to_owned()) })?; - println!("{:?}", path); let db = sled::open(&path)?; info!("Opened sled database at {}", path); diff --git a/src/database/globals.rs b/src/database/globals.rs index d307150..1654636 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -31,7 +31,7 @@ impl Globals { .unwrap_or("localhost") .to_string() .try_into() - .map_err(|_| crate::Error::bad_database("Private or public keys are invalid."))?, + .map_err(|_| Error::bad_database("Private or public keys are invalid."))?, registration_disabled: config.get_bool("registration_disabled").unwrap_or(false), }) } diff --git a/src/database/rooms/edus.rs b/src/database/rooms/edus.rs index 0b3ac57..3ee1e75 100644 --- a/src/database/rooms/edus.rs +++ b/src/database/rooms/edus.rs @@ -1,6 +1,6 @@ use crate::{utils, Error, Result}; use ruma::{ - events::{AnyEvent as EduEvent, EventJson}, + events::{AnyEvent as EduEvent, EventJson, SyncEphemeralRoomEvent}, identifiers::{RoomId, UserId}, }; use std::convert::TryFrom; @@ -236,9 +236,41 @@ impl RoomEdus { Ok(ruma::events::typing::TypingEvent { content: ruma::events::typing::TypingEventContent { user_ids }, - room_id: room_id.clone(), // Can be inferred + room_id: room_id.clone(), }) } + // REMOVE the above method and uncomment the bottom when ruma/ruma PR #141 is merged + // pub fn roomactives_all( + // &self, + // room_id: &RoomId, + // ) -> Result> { + // let mut prefix = room_id.to_string().as_bytes().to_vec(); + // prefix.push(0xff); + + // let mut user_ids = Vec::new(); + + // for user_id in self + // .roomactiveid_userid + // .scan_prefix(prefix) + // .values() + // .map(|user_id| { + // Ok::<_, Error>( + // UserId::try_from(utils::string_from_bytes(&user_id?).map_err(|_| { + // Error::bad_database("User ID in roomactiveid_userid is invalid unicode.") + // })?) + // .map_err(|_| { + // Error::bad_database("User ID in roomactiveid_userid is invalid.") + // })?, + // ) + // }) + // { + // user_ids.push(user_id?); + // } + + // Ok(SyncEphemeralRoomEvent { + // content: ruma::events::typing::TypingEventContent { user_ids }, + // }) + // } /// Sets a private read marker at `count`. pub fn room_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) -> Result<()> {