Merge pull request 'Moving to ruma-monorepo' (#133) from DevinR528/conduit:ruma-mono into master

Reviewed-on: https://git.koesters.xyz/timo/conduit/pulls/133
Reviewed-by: Timo Kösters <timo@koesters.xyz>
This commit is contained in:
Timo Kösters 2020-07-25 22:02:45 +02:00
commit 678f33acf9
13 changed files with 518 additions and 441 deletions

495
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -27,18 +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/timokoesters/ruma"
#rev = "baa87104569b45dc07a9a7a16d3c7592ab8f4d6b"
#path = "../ruma/ruma"
features = ["rand", "client-api", "federation-api"]
# These are required only until ruma-events and ruma-federation-api are merged into ruma/ruma
[patch.crates-io]
ruma-common = { git = "https://github.com/timokoesters/ruma" }
ruma-serde = { git = "https://github.com/timokoesters/ruma" }
ruma-identifiers = { git = "https://github.com/timokoesters/ruma" }
#ruma-common = { path = "../ruma/ruma-common" }
#ruma-serde = { path = "../ruma/ruma-serde" }
#ruma-identifiers = { path = "../ruma/ruma-identifiers" }
ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "08fbace" }

View File

@ -60,12 +60,12 @@ use ruma::{
unversioned::get_supported_versions,
},
events::{
collections::only::Event as EduEvent,
room::{
canonical_alias, guest_access, history_visibility, join_rules, member, name, redaction,
topic,
},
EventJson, EventType,
AnyBasicEvent, AnyEphemeralRoomEvent, AnyEvent, AnySyncEphemeralRoomEvent, EventJson,
EventType,
},
identifiers::{RoomAliasId, RoomId, RoomVersionId, UserId},
};
@ -169,14 +169,14 @@ pub fn register_route(
if let Some(auth) = &body.auth {
let (worked, uiaainfo) =
db.uiaa
.try_auth(&user_id, "", auth, &uiaainfo, &db.users, &db.globals)?;
.try_auth(&user_id, "".into(), auth, &uiaainfo, &db.users, &db.globals)?;
if !worked {
return Err(Error::Uiaa(uiaainfo));
}
// Success!
} else {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
db.uiaa.create(&user_id, "", &uiaainfo)?;
db.uiaa.create(&user_id, "".into(), &uiaainfo)?;
return Err(Error::Uiaa(uiaainfo));
}
@ -189,7 +189,7 @@ pub fn register_route(
let device_id = body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH));
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Generate new token for the device
let token = utils::random_string(TOKEN_LENGTH);
@ -221,7 +221,7 @@ pub fn register_route(
Ok(register::Response {
access_token: Some(token),
user_id,
device_id: Some(device_id),
device_id: Some(device_id.into()),
}
.into())
}
@ -269,7 +269,7 @@ pub fn login_route(
.body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH));
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Generate a new token for the device
let token = utils::random_string(TOKEN_LENGTH);
@ -286,7 +286,7 @@ pub fn login_route(
user_id,
access_token: token,
home_server: Some(db.globals.server_name().to_owned()),
device_id,
device_id: device_id.into(),
well_known: None,
}
.into())
@ -300,7 +300,7 @@ pub fn logout_route(
let user_id = body.user_id.as_ref().expect("user is authenticated");
let device_id = body.device_id.as_ref().expect("user is authenticated");
db.users.remove_device(&user_id, &device_id)?;
db.users.remove_device(&user_id, device_id)?;
Ok(logout::Response.into())
}
@ -340,14 +340,9 @@ pub fn change_password_route(
};
if let Some(auth) = &body.auth {
let (worked, uiaainfo) = db.uiaa.try_auth(
&user_id,
&device_id,
auth,
&uiaainfo,
&db.users,
&db.globals,
)?;
let (worked, uiaainfo) =
db.uiaa
.try_auth(&user_id, device_id, auth, &uiaainfo, &db.users, &db.globals)?;
if !worked {
return Err(Error::Uiaa(uiaainfo));
}
@ -452,11 +447,11 @@ pub fn deactivate_route(
pub fn get_capabilities_route() -> ConduitResult<get_capabilities::Response> {
let mut available = BTreeMap::new();
available.insert(
RoomVersionId::version_5(),
RoomVersionId::Version5,
get_capabilities::RoomVersionStability::Stable,
);
available.insert(
RoomVersionId::version_6(),
RoomVersionId::Version6,
get_capabilities::RoomVersionStability::Stable,
);
@ -480,7 +475,7 @@ pub fn get_pushrules_all_route(
) -> ConduitResult<get_pushrules_all::Response> {
let user_id = body.user_id.as_ref().expect("user is authenticated");
if let EduEvent::PushRules(pushrules) = db
if let AnyEvent::Basic(AnyBasicEvent::PushRules(pushrules)) = db
.account_data
.get(None, &user_id, &EventType::PushRules)?
.ok_or(Error::BadRequest(
@ -594,7 +589,7 @@ pub fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> {
let user_id = body.user_id.as_ref().expect("user is authenticated");
let data = db
let event = db
.account_data
.get(
None,
@ -603,6 +598,9 @@ pub fn get_global_account_data_route(
)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
let data = serde_json::from_str(event.json().get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
Ok(get_global_account_data::Response { account_data: data }.into())
}
@ -888,7 +886,7 @@ pub fn get_keys_route(
device_display_name: metadata.display_name,
});
container.insert(device_id.to_owned(), keys);
container.insert(device_id, keys);
}
}
device_keys.insert(user_id.clone(), container);
@ -1099,7 +1097,7 @@ pub fn set_read_marker_route(
content: ruma::events::fully_read::FullyReadEventContent {
event_id: body.fully_read.clone(),
},
room_id: Some(body.room_id.clone()),
room_id: body.room_id.clone(),
})
.expect("we just created a valid event")
.as_object_mut()
@ -1135,10 +1133,12 @@ pub fn set_read_marker_route(
db.rooms.edus.roomlatest_update(
&user_id,
&body.room_id,
EduEvent::Receipt(ruma::events::receipt::ReceiptEvent {
content: receipt_content,
room_id: None, // None because it can be inferred
}),
AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt(
ruma::events::receipt::ReceiptEvent {
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
room_id: body.room_id.clone(),
},
)),
&db.globals,
)?;
}
@ -1181,8 +1181,7 @@ pub fn create_room_route(
) -> ConduitResult<create_room::Response> {
let user_id = body.user_id.as_ref().expect("user is authenticated");
let room_id = RoomId::new(db.globals.server_name())
.map_err(|_| Error::bad_database("Server name is invalid."))?;
let room_id = RoomId::new(db.globals.server_name());
let alias = body
.room_alias_name
@ -1203,21 +1202,20 @@ pub fn create_room_route(
}
})?;
let mut content = ruma::events::room::create::CreateEventContent::new(user_id.clone());
content.federate = body.creation_content.as_ref().map_or(true, |c| c.federate);
content.predecessor = body
.creation_content
.as_ref()
.and_then(|c| c.predecessor.clone());
content.room_version = RoomVersionId::Version6;
// 1. The room create event
db.rooms.append_pdu(
room_id.clone(),
user_id.clone(),
EventType::RoomCreate,
serde_json::to_value(ruma::events::room::create::CreateEventContent {
creator: user_id.clone(),
federate: body.creation_content.as_ref().map_or(true, |c| c.federate),
predecessor: body
.creation_content
.as_ref()
.and_then(|c| c.predecessor.clone()),
room_version: RoomVersionId::version_6(),
})
.expect("event is valid, we just created it"),
serde_json::to_value(content).expect("event is valid, we just created it"),
None,
Some("".to_owned()),
None,
@ -1296,15 +1294,14 @@ pub fn create_room_route(
user_id.clone(),
EventType::RoomJoinRules,
match preset {
create_room::RoomPreset::PublicChat => {
serde_json::to_value(join_rules::JoinRulesEventContent {
join_rule: join_rules::JoinRule::Public,
})
.expect("event is valid, we just created it")
}
_ => serde_json::to_value(join_rules::JoinRulesEventContent {
join_rule: join_rules::JoinRule::Invite,
})
create_room::RoomPreset::PublicChat => serde_json::to_value(
join_rules::JoinRulesEventContent::new(join_rules::JoinRule::Public),
)
.expect("event is valid, we just created it"),
// according to spec "invite" is the default
_ => serde_json::to_value(join_rules::JoinRulesEventContent::new(
join_rules::JoinRule::Invite,
))
.expect("event is valid, we just created it"),
},
None,
@ -1318,9 +1315,9 @@ pub fn create_room_route(
room_id.clone(),
user_id.clone(),
EventType::RoomHistoryVisibility,
serde_json::to_value(history_visibility::HistoryVisibilityEventContent {
history_visibility: history_visibility::HistoryVisibility::Shared,
})
serde_json::to_value(history_visibility::HistoryVisibilityEventContent::new(
history_visibility::HistoryVisibility::Shared,
))
.expect("event is valid, we just created it"),
None,
Some("".to_owned()),
@ -1334,15 +1331,13 @@ pub fn create_room_route(
user_id.clone(),
EventType::RoomGuestAccess,
match preset {
create_room::RoomPreset::PublicChat => {
serde_json::to_value(guest_access::GuestAccessEventContent {
guest_access: guest_access::GuestAccess::Forbidden,
})
.expect("event is valid, we just created it")
}
_ => serde_json::to_value(guest_access::GuestAccessEventContent {
guest_access: guest_access::GuestAccess::CanJoin,
})
create_room::RoomPreset::PublicChat => serde_json::to_value(
guest_access::GuestAccessEventContent::new(guest_access::GuestAccess::Forbidden),
)
.expect("event is valid, we just created it"),
_ => serde_json::to_value(guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::CanJoin,
))
.expect("event is valid, we just created it"),
},
None,
@ -1533,7 +1528,7 @@ pub fn get_alias_route(
Ok(get_alias::Response {
room_id,
servers: vec![db.globals.server_name().to_owned()],
servers: vec![db.globals.server_name().to_string()],
}
.into())
}
@ -2521,7 +2516,7 @@ pub fn sync_route(
let room_events = pdus
.into_iter()
.map(|pdu| pdu.to_room_event())
.map(|pdu| pdu.to_sync_room_event())
.collect::<Vec<_>>();
let mut edus = db
@ -2539,7 +2534,7 @@ pub fn sync_route(
{
edus.push(
serde_json::from_str(
&serde_json::to_string(&EduEvent::Typing(
&serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing(
db.rooms.edus.roomactives_all(&room_id)?,
))
.expect("event is valid, we just created it"),
@ -2554,8 +2549,12 @@ pub fn sync_route(
.account_data
.changes_since(Some(&room_id), &user_id, since)?
.into_iter()
.map(|(_, v)| v)
.collect(),
.filter_map(|(_, v)| {
serde_json::from_str(v.json().get())
.map_err(|_| Error::bad_database("Invalid account event in database."))
.ok()
})
.collect::<Vec<_>>(),
},
summary: sync_events::RoomSummary {
heroes,
@ -2567,11 +2566,7 @@ pub fn sync_route(
notification_count,
},
timeline: sync_events::Timeline {
limited: if limited || joined_since_last_sync {
Some(true)
} else {
None
},
limited: limited || joined_since_last_sync,
prev_batch,
events: room_events,
},
@ -2581,7 +2576,7 @@ pub fn sync_route(
db.rooms
.room_state_full(&room_id)?
.into_iter()
.map(|(_, pdu)| pdu.to_state_event())
.map(|(_, pdu)| pdu.to_sync_state_event())
.collect()
} else {
Vec::new()
@ -2601,7 +2596,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())
.map(|pdu| pdu.to_sync_room_event())
.collect();
// TODO: Only until leave point
@ -2620,7 +2615,7 @@ pub fn sync_route(
{
edus.push(
serde_json::from_str(
&serde_json::to_string(&EduEvent::Typing(
&serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing(
db.rooms.edus.roomactives_all(&room_id)?,
))
.expect("event is valid, we just created it"),
@ -2632,7 +2627,7 @@ pub fn sync_route(
let left_room = sync_events::LeftRoom {
account_data: sync_events::AccountData { events: Vec::new() },
timeline: sync_events::Timeline {
limited: Some(false),
limited: false,
prev_batch: Some(next_batch.clone()),
events: room_events,
},
@ -2696,8 +2691,12 @@ pub fn sync_route(
.account_data
.changes_since(None, &user_id, since)?
.into_iter()
.map(|(_, v)| v)
.collect(),
.filter_map(|(_, v)| {
serde_json::from_str(v.json().get())
.map_err(|_| Error::bad_database("Invalid account event in database."))
.ok()
})
.collect::<Vec<_>>(),
},
device_lists: sync_events::DeviceLists {
changed: if since != 0 {
@ -2839,17 +2838,17 @@ pub fn get_message_events_route(
.clone()
.parse()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?;
let limit = body
.limit
.try_into()
.map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?;
match body.dir {
get_message_events::Direction::Forward => {
let events_after = db
.rooms
.pdus_after(&user_id, &body.room_id, from)
// Use limit or else 10
.take(body.limit.map_or(Ok::<_, Error>(10_usize), |l| {
Ok(u32::try_from(l).map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
})? as usize)
})?)
.take(limit)
.filter_map(|r| r.ok()) // Filter out buggy events
.collect::<Vec<_>>();
@ -2880,11 +2879,7 @@ pub fn get_message_events_route(
.rooms
.pdus_until(&user_id, &body.room_id, from)
// Use limit or else 10
.take(body.limit.map_or(Ok::<_, Error>(10_usize), |l| {
Ok(u32::try_from(l).map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
})? as usize)
})?)
.take(limit)
.filter_map(|r| r.ok()) // Filter out buggy events
.collect::<Vec<_>>();

View File

@ -1,7 +1,7 @@
use crate::{utils, Error, Result};
use ruma::{
api::client::error::ErrorKind,
events::{collections::only::Event as EduEvent, EventJson, EventType},
events::{AnyEvent as EduEvent, EventJson, EventType},
identifiers::{RoomId, UserId},
};
use std::{collections::HashMap, convert::TryFrom};

View File

@ -1,12 +1,14 @@
use crate::{utils, Error, Result};
use std::convert::TryInto;
use crate::{utils, Error, Result};
use ruma::identifiers::ServerName;
pub const COUNTER: &str = "c";
pub struct Globals {
pub(super) globals: sled::Tree,
keypair: ruma::signatures::Ed25519KeyPair,
reqwest_client: reqwest::Client,
server_name: String,
server_name: Box<ServerName>,
registration_disabled: bool,
}
@ -27,7 +29,9 @@ impl Globals {
server_name: config
.get_str("server_name")
.unwrap_or("localhost")
.to_owned(),
.to_string()
.try_into()
.map_err(|_| Error::BadConfig("Invalid server name found."))?,
registration_disabled: config.get_bool("registration_disabled").unwrap_or(false),
})
}
@ -59,8 +63,8 @@ impl Globals {
})
}
pub fn server_name(&self) -> &str {
&self.server_name
pub fn server_name(&self) -> &ServerName {
self.server_name.as_ref()
}
pub fn registration_disabled(&self) -> bool {

View File

@ -2,7 +2,7 @@ use crate::{utils, Error, Result};
use ruma::{
api::client::{
error::ErrorKind,
r0::backup::{get_backup_keys::Sessions, BackupAlgorithm, KeyData},
r0::backup::{BackupAlgorithm, KeyData, Sessions},
},
identifiers::{RoomId, UserId},
};

View File

@ -529,7 +529,7 @@ impl Rooms {
auth_events: Vec::new(),
redacts: redacts.clone(),
unsigned,
hashes: ruma::api::federation::EventHash {
hashes: ruma::events::pdu::EventHash {
sha256: "aaa".to_owned(),
},
signatures: HashMap::new(),
@ -547,7 +547,7 @@ impl Rooms {
let mut pdu_json = serde_json::to_value(&pdu).expect("event is valid, we just created it");
ruma::signatures::hash_and_sign_event(
globals.server_name(),
globals.server_name().as_str(),
globals.keypair(),
&mut pdu_json,
)

View File

@ -1,6 +1,6 @@
use crate::{utils, Error, Result};
use ruma::{
events::{collections::only::Event as EduEvent, EventJson},
events::{AnyEvent as EduEvent, EventJson, SyncEphemeralRoomEvent},
identifiers::{RoomId, UserId},
};
use std::convert::TryFrom;
@ -61,7 +61,8 @@ impl RoomEdus {
&self,
room_id: &RoomId,
since: u64,
) -> Result<impl Iterator<Item = Result<EventJson<EduEvent>>>> {
) -> Result<impl Iterator<Item = Result<EventJson<ruma::events::AnySyncEphemeralRoomEvent>>>>
{
let mut prefix = room_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
@ -208,8 +209,10 @@ impl RoomEdus {
.unwrap_or(0))
}
/// Returns an iterator over all active events (e.g. typing notifications).
pub fn roomactives_all(&self, room_id: &RoomId) -> Result<ruma::events::typing::TypingEvent> {
pub fn roomactives_all(
&self,
room_id: &RoomId,
) -> Result<SyncEphemeralRoomEvent<ruma::events::typing::TypingEventContent>> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
@ -233,9 +236,8 @@ impl RoomEdus {
user_ids.push(user_id?);
}
Ok(ruma::events::typing::TypingEvent {
Ok(SyncEphemeralRoomEvent {
content: ruma::events::typing::TypingEventContent { user_ids },
room_id: None, // Can be inferred
})
}

View File

@ -4,7 +4,7 @@ use ruma::{
error::ErrorKind,
r0::uiaa::{AuthData, UiaaInfo},
},
identifiers::UserId,
identifiers::{DeviceId, UserId},
};
pub struct Uiaa {
@ -13,14 +13,19 @@ pub struct Uiaa {
impl Uiaa {
/// Creates a new Uiaa session. Make sure the session token is unique.
pub fn create(&self, user_id: &UserId, device_id: &str, uiaainfo: &UiaaInfo) -> Result<()> {
pub fn create(
&self,
user_id: &UserId,
device_id: &DeviceId,
uiaainfo: &UiaaInfo,
) -> Result<()> {
self.update_uiaa_session(user_id, device_id, Some(uiaainfo))
}
pub fn try_auth(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
auth: &AuthData,
uiaainfo: &UiaaInfo,
users: &super::users::Users,
@ -130,7 +135,7 @@ impl Uiaa {
// UIAA was successful! Remove this session and return true
self.update_uiaa_session(user_id, device_id, None)?;
return Ok((true, uiaainfo));
Ok((true, uiaainfo))
} else {
panic!("FallbackAcknowledgement is not supported yet");
}
@ -139,12 +144,12 @@ impl Uiaa {
fn update_uiaa_session(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
uiaainfo: Option<&UiaaInfo>,
) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
if let Some(uiaainfo) = uiaainfo {
self.userdeviceid_uiaainfo.insert(
@ -161,12 +166,12 @@ impl Uiaa {
fn get_uiaa_session(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
session: &str,
) -> Result<UiaaInfo> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
let uiaainfo = serde_json::from_slice::<UiaaInfo>(
&self

View File

@ -8,8 +8,8 @@ use ruma::{
keys::{AlgorithmAndDeviceId, CrossSigningKey, DeviceKeys, KeyAlgorithm, OneTimeKey},
},
},
events::{to_device::AnyToDeviceEvent, EventJson, EventType},
identifiers::UserId,
events::{AnyToDeviceEvent, EventJson, EventType},
identifiers::{DeviceId, UserId},
};
use std::{collections::BTreeMap, convert::TryFrom, time::SystemTime};
@ -168,7 +168,7 @@ impl Users {
pub fn create_device(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
token: &str,
initial_device_display_name: Option<String>,
) -> Result<()> {
@ -177,12 +177,12 @@ impl Users {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
self.userdeviceid_metadata.insert(
userdeviceid,
serde_json::to_string(&Device {
device_id: device_id.to_owned(),
device_id: device_id.into(),
display_name: initial_device_display_name,
last_seen_ip: None, // TODO
last_seen_ts: Some(SystemTime::now()),
@ -191,16 +191,16 @@ impl Users {
.as_bytes(),
)?;
self.set_token(user_id, device_id, token)?;
self.set_token(user_id, &device_id, token)?;
Ok(())
}
/// Removes a device from a user.
pub fn remove_device(&self, user_id: &UserId, device_id: &str) -> Result<()> {
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
// Remove tokens
if let Some(old_token) = self.userdeviceid_token.remove(&userdeviceid)? {
@ -223,7 +223,7 @@ impl Users {
}
/// Returns an iterator over all device ids of this user.
pub fn all_device_ids(&self, user_id: &UserId) -> impl Iterator<Item = Result<String>> {
pub fn all_device_ids(&self, user_id: &UserId) -> impl Iterator<Item = Result<Box<DeviceId>>> {
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
// All devices have metadata
@ -237,17 +237,16 @@ impl Users {
.next()
.ok_or_else(|| Error::bad_database("UserDevice ID in db is invalid."))?,
)
.map_err(|_| {
Error::bad_database("Device ID in userdeviceid_metadata is invalid.")
})?)
.map_err(|_| Error::bad_database("Device ID in userdeviceid_metadata is invalid."))?
.into())
})
}
/// Replaces the access token of one device.
fn set_token(&self, user_id: &UserId, device_id: &str, token: &str) -> Result<()> {
fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
// All devices have metadata
assert!(self.userdeviceid_metadata.get(&userdeviceid)?.is_some());
@ -268,13 +267,13 @@ impl Users {
pub fn add_one_time_key(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
one_time_key_key: &AlgorithmAndDeviceId,
one_time_key_value: &OneTimeKey,
) -> Result<()> {
let mut key = user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.as_bytes());
key.extend_from_slice(device_id.as_str().as_bytes());
// All devices have metadata
// Only existing devices should be able to call this.
@ -301,12 +300,12 @@ impl Users {
pub fn take_one_time_key(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
key_algorithm: &KeyAlgorithm,
) -> Result<Option<(AlgorithmAndDeviceId, OneTimeKey)>> {
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(device_id.as_bytes());
prefix.extend_from_slice(device_id.as_str().as_bytes());
prefix.push(0xff);
prefix.push(b'"'); // Annoying quotation mark
prefix.extend_from_slice(key_algorithm.to_string().as_bytes());
@ -337,11 +336,11 @@ impl Users {
pub fn count_one_time_keys(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
) -> Result<BTreeMap<KeyAlgorithm, UInt>> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
let mut counts = BTreeMap::new();
@ -370,13 +369,13 @@ impl Users {
pub fn add_device_keys(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
device_keys: &DeviceKeys,
globals: &super::globals::Globals,
) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
self.keyid_key.insert(
&userdeviceid,
@ -550,10 +549,14 @@ impl Users {
})
}
pub fn get_device_keys(&self, user_id: &UserId, device_id: &str) -> Result<Option<DeviceKeys>> {
pub fn get_device_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
) -> Result<Option<DeviceKeys>> {
let mut key = user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(device_id.as_bytes());
key.extend_from_slice(device_id.as_str().as_bytes());
self.keyid_key.get(key)?.map_or(Ok(None), |bytes| {
Ok(Some(serde_json::from_slice(&bytes).map_err(|_| {
@ -633,14 +636,14 @@ impl Users {
&self,
sender: &UserId,
target_user_id: &UserId,
target_device_id: &str,
target_device_id: &DeviceId,
event_type: &EventType,
content: serde_json::Value,
globals: &super::globals::Globals,
) -> Result<()> {
let mut key = target_user_id.to_string().as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(target_device_id.as_bytes());
key.extend_from_slice(target_device_id.as_str().as_bytes());
key.push(0xff);
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
@ -660,14 +663,14 @@ impl Users {
pub fn take_to_device_events(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
max: usize,
) -> Result<Vec<EventJson<AnyToDeviceEvent>>> {
let mut events = Vec::new();
let mut prefix = user_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
prefix.extend_from_slice(device_id.as_bytes());
prefix.extend_from_slice(device_id.as_str().as_bytes());
prefix.push(0xff);
for result in self.todeviceid_events.scan_prefix(&prefix).take(max) {
@ -685,12 +688,12 @@ impl Users {
pub fn update_device_metadata(
&self,
user_id: &UserId,
device_id: &str,
device_id: &DeviceId,
device: &Device,
) -> Result<()> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
// Only existing devices should be able to call this.
assert!(self.userdeviceid_metadata.get(&userdeviceid)?.is_some());
@ -706,10 +709,14 @@ impl Users {
}
/// Get device metadata.
pub fn get_device_metadata(&self, user_id: &UserId, device_id: &str) -> Result<Option<Device>> {
pub fn get_device_metadata(
&self,
user_id: &UserId,
device_id: &DeviceId,
) -> Result<Option<Device>> {
let mut userdeviceid = user_id.to_string().as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
userdeviceid.extend_from_slice(device_id.as_str().as_bytes());
self.userdeviceid_metadata
.get(&userdeviceid)?

View File

@ -1,14 +1,12 @@
use crate::{Error, Result};
use js_int::UInt;
use ruma::{
api::federation::EventHash,
events::{
collections::all::{RoomEvent, StateEvent},
room::member::MemberEvent,
stripped::AnyStrippedStateEvent,
EventJson, EventType,
pdu::EventHash, room::member::MemberEventContent, AnyRoomEvent, AnyStateEvent,
AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventJson, EventType,
StateEvent,
},
identifiers::{EventId, RoomId, UserId},
identifiers::{EventId, RoomId, ServerName, UserId},
};
use serde::{Deserialize, Serialize};
use serde_json::json;
@ -19,7 +17,7 @@ pub struct PduEvent {
pub event_id: EventId,
pub room_id: RoomId,
pub sender: UserId,
pub origin: String,
pub origin: Box<ServerName>,
pub origin_server_ts: UInt,
#[serde(rename = "type")]
pub kind: EventType,
@ -81,24 +79,40 @@ impl PduEvent {
Ok(())
}
pub fn to_room_event(&self) -> EventJson<RoomEvent> {
pub fn to_sync_room_event(&self) -> EventJson<AnySyncRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<RoomEvent>>(&json)
.expect("EventJson::from_str always works")
serde_json::from_str::<AnySyncRoomEvent>(&json)
.map(EventJson::from)
.expect("AnySyncRoomEvent can always be built from a full PDU event")
}
pub fn to_state_event(&self) -> EventJson<StateEvent> {
pub fn to_room_event(&self) -> EventJson<AnyRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<StateEvent>>(&json)
.expect("EventJson::from_str always works")
serde_json::from_str::<AnyRoomEvent>(&json)
.map(EventJson::from)
.expect("AnyRoomEvent can always be built from a full PDU event")
}
pub fn to_state_event(&self) -> EventJson<AnyStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<AnyStateEvent>(&json)
.map(EventJson::from)
.expect("AnyStateEvent can always be built from a full PDU event")
}
pub fn to_sync_state_event(&self) -> EventJson<AnySyncStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<AnySyncStateEvent>(&json)
.map(EventJson::from)
.expect("AnySyncStateEvent can always be built from a full PDU event")
}
pub fn to_stripped_state_event(&self) -> EventJson<AnyStrippedStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyStrippedStateEvent>>(&json)
.expect("EventJson::from_str always works")
serde_json::from_str::<AnyStrippedStateEvent>(&json)
.map(EventJson::from)
.expect("AnyStrippedStateEvent can always be built from a full PDU event")
}
pub fn to_member_event(&self) -> EventJson<MemberEvent> {
pub fn to_member_event(&self) -> EventJson<StateEvent<MemberEventContent>> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<MemberEvent>>(&json)
.expect("EventJson::from_str always works")
serde_json::from_str::<StateEvent<MemberEventContent>>(&json)
.map(EventJson::from)
.expect("StateEvent<MemberEventContent> can always be built from a full PDU event")
}
}

View File

@ -1,45 +1,47 @@
use js_int::uint;
use ruma::{
events::push_rules::{ConditionalPushRule, PatternedPushRule, PushCondition, Ruleset},
identifiers::UserId,
push::{Action, Tweak},
push::{
Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule,
PatternedPushRuleInit, PushCondition, RoomMemberCountIs, Ruleset, Tweak,
},
};
pub fn default_pushrules(user_id: &UserId) -> Ruleset {
Ruleset {
content: vec![contains_user_name_rule(&user_id)],
override_: vec![
master_rule(),
suppress_notices_rule(),
invite_for_me_rule(),
member_event_rule(),
contains_display_name_rule(),
tombstone_rule(),
roomnotif_rule(),
],
room: vec![],
sender: vec![],
underride: vec![
call_rule(),
encrypted_room_one_to_one_rule(),
room_one_to_one_rule(),
message_rule(),
encrypted_rule(),
],
}
let mut rules = Ruleset::default();
rules.content = vec![contains_user_name_rule(&user_id)];
rules.override_ = vec![
master_rule(),
suppress_notices_rule(),
invite_for_me_rule(),
member_event_rule(),
contains_display_name_rule(),
tombstone_rule(),
roomnotif_rule(),
];
rules.underride = vec![
call_rule(),
encrypted_room_one_to_one_rule(),
room_one_to_one_rule(),
message_rule(),
encrypted_rule(),
];
rules
}
pub fn master_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: false,
rule_id: ".m.rule.master".to_owned(),
conditions: vec![],
}
.into()
}
pub fn suppress_notices_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: true,
@ -49,10 +51,11 @@ pub fn suppress_notices_rule() -> ConditionalPushRule {
pattern: "m.notice".to_owned(),
}],
}
.into()
}
pub fn invite_for_me_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
@ -66,10 +69,11 @@ pub fn invite_for_me_rule() -> ConditionalPushRule {
pattern: "m.invite".to_owned(),
}],
}
.into()
}
pub fn member_event_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: true,
@ -79,10 +83,11 @@ pub fn member_event_rule() -> ConditionalPushRule {
pattern: "type".to_owned(),
}],
}
.into()
}
pub fn contains_display_name_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
@ -93,10 +98,11 @@ pub fn contains_display_name_rule() -> ConditionalPushRule {
rule_id: ".m.rule.contains_display_name".to_owned(),
conditions: vec![PushCondition::ContainsDisplayName],
}
.into()
}
pub fn tombstone_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
@ -112,10 +118,11 @@ pub fn tombstone_rule() -> ConditionalPushRule {
},
],
}
.into()
}
pub fn roomnotif_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
@ -130,10 +137,11 @@ pub fn roomnotif_rule() -> ConditionalPushRule {
},
],
}
.into()
}
pub fn contains_user_name_rule(user_id: &UserId) -> PatternedPushRule {
PatternedPushRule {
PatternedPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
@ -144,10 +152,11 @@ pub fn contains_user_name_rule(user_id: &UserId) -> PatternedPushRule {
rule_id: ".m.rule.contains_user_name".to_owned(),
pattern: user_id.localpart().to_owned(),
}
.into()
}
pub fn call_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("ring".to_owned())),
@ -161,10 +170,11 @@ pub fn call_rule() -> ConditionalPushRule {
pattern: "m.call.invite".to_owned(),
}],
}
.into()
}
pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
@ -174,17 +184,20 @@ pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule {
enabled: true,
rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount { is: "2".to_owned() },
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(uint!(2)..),
},
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.encrypted".to_owned(),
},
],
}
.into()
}
pub fn room_one_to_one_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
@ -194,17 +207,20 @@ pub fn room_one_to_one_rule() -> ConditionalPushRule {
enabled: true,
rule_id: ".m.rule.room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount { is: "2".to_owned() },
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(uint!(2)..),
},
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.message".to_owned(),
},
],
}
.into()
}
pub fn message_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
@ -214,10 +230,11 @@ pub fn message_rule() -> ConditionalPushRule {
pattern: "m.room.message".to_owned(),
}],
}
.into()
}
pub fn encrypted_rule() -> ConditionalPushRule {
ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
@ -227,4 +244,5 @@ pub fn encrypted_rule() -> ConditionalPushRule {
pattern: "m.room.encrypted".to_owned(),
}],
}
.into()
}

View File

@ -7,7 +7,10 @@ use rocket::{
Outcome::*,
Request, State,
};
use ruma::{api::Endpoint, identifiers::UserId};
use ruma::{
api::Endpoint,
identifiers::{DeviceId, UserId},
};
use std::{convert::TryInto, io::Cursor, ops::Deref};
use tokio::io::AsyncReadExt;
@ -18,7 +21,7 @@ const MESSAGE_LIMIT: u64 = 20 * 1024 * 1024; // 20 MB
pub struct Ruma<T> {
pub body: T,
pub user_id: Option<UserId>,
pub device_id: Option<String>,
pub device_id: Option<Box<DeviceId>>,
pub json_body: Option<Box<serde_json::value::RawValue>>, // This is None when body is not a valid string
}
@ -63,7 +66,7 @@ impl<'a, T: Endpoint> FromTransformedData<'a> for Ruma<T> {
match db.users.find_from_token(&token).unwrap() {
// TODO: M_UNKNOWN_TOKEN
None => return Failure((Status::Unauthorized, ())),
Some((user_id, device_id)) => (Some(user_id), Some(device_id)),
Some((user_id, device_id)) => (Some(user_id), Some(device_id.into())),
}
} else {
(None, None)