improvement: optimize /sync response

This commit is contained in:
timokoesters 2020-06-06 22:34:08 +02:00
parent 0067f49d52
commit b4d65ab67d
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
5 changed files with 95 additions and 92 deletions

17
Cargo.lock generated
View file

@ -1263,7 +1263,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.1.0"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"ruma-api",
"ruma-client-api",
@ -1277,7 +1277,7 @@ dependencies = [
[[package]]
name = "ruma-api"
version = "0.16.1"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"http",
"percent-encoding 2.1.0",
@ -1292,7 +1292,7 @@ dependencies = [
[[package]]
name = "ruma-api-macros"
version = "0.16.1"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"proc-macro2 1.0.18",
"quote 1.0.6",
@ -1302,7 +1302,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.9.0"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"http",
"js_int",
@ -1319,7 +1319,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.1.3"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"matches",
"ruma-serde",
@ -1356,8 +1356,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff655a4cb7d43b60b18e07a601889836c1c12854bb16f4c083826b664fdc55aa"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"js_int",
"matches",
@ -1382,7 +1381,7 @@ dependencies = [
[[package]]
name = "ruma-serde"
version = "0.2.2"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"dtoa",
"itoa",
@ -1395,7 +1394,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.6.0-dev.1"
source = "git+https://github.com/ruma/ruma?rev=f6fb971329a4a5a7faeebf7ea47a86cd19e580f4#f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
source = "git+https://github.com/ruma/ruma?rev=12388c3fbc8ba2a685cbf0fe810c633c827f5b2c#12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
dependencies = [
"base64 0.12.1",
"ring",

View file

@ -30,12 +30,13 @@ image = { version = "0.23.4", default-features = false, features = ["jpeg", "png
[dependencies.ruma]
git = "https://github.com/ruma/ruma"
rev = "f6fb971329a4a5a7faeebf7ea47a86cd19e580f4"
rev = "12388c3fbc8ba2a685cbf0fe810c633c827f5b2c"
#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-api = { git = "https://github.com/ruma/ruma", rev = "f6fb971329a4a5a7faeebf7ea47a86cd19e580f4" }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "f6fb971329a4a5a7faeebf7ea47a86cd19e580f4" }
ruma-events = { git = "https://github.com/ruma/ruma-events", rev = "c1ee72d" }
ruma-serde = { git = "https://github.com/ruma/ruma", rev = "f6fb971329a4a5a7faeebf7ea47a86cd19e580f4" }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "12388c3fbc8ba2a685cbf0fe810c633c827f5b2c" }
ruma-serde = { git = "https://github.com/ruma/ruma", rev = "12388c3fbc8ba2a685cbf0fe810c633c827f5b2c" }
#ruma-common = { path = "../ruma/ruma-common" }
#ruma-serde = { path = "../ruma/ruma-serde" }

View file

@ -2338,50 +2338,51 @@ pub fn sync_route(
);
}
joined_rooms.insert(
room_id.clone().try_into().unwrap(),
sync_events::JoinedRoom {
account_data: Some(sync_events::AccountData {
events: db
.account_data
.changes_since(Some(&room_id), &user_id, since)
.unwrap()
.into_iter()
.map(|(_, v)| v)
.collect(),
}),
summary: sync_events::RoomSummary {
heroes,
joined_member_count: joined_member_count.map(|n| (n as u32).into()),
invited_member_count: invited_member_count.map(|n| (n as u32).into()),
},
unread_notifications: sync_events::UnreadNotificationsCount {
highlight_count: None,
notification_count,
},
timeline: sync_events::Timeline {
limited: if limited || joined_since_last_sync {
Some(true)
} else {
None
},
prev_batch,
events: room_events,
},
// TODO: state before timeline
state: sync_events::State {
events: if joined_since_last_sync {
state
.into_iter()
.map(|(_, pdu)| pdu.to_state_event())
.collect()
} else {
Vec::new()
},
},
ephemeral: sync_events::Ephemeral { events: edus },
let joined_room = sync_events::JoinedRoom {
account_data: sync_events::AccountData {
events: db
.account_data
.changes_since(Some(&room_id), &user_id, since)
.unwrap()
.into_iter()
.map(|(_, v)| v)
.collect(),
},
);
summary: sync_events::RoomSummary {
heroes,
joined_member_count: joined_member_count.map(|n| (n as u32).into()),
invited_member_count: invited_member_count.map(|n| (n as u32).into()),
},
unread_notifications: sync_events::UnreadNotificationsCount {
highlight_count: None,
notification_count,
},
timeline: sync_events::Timeline {
limited: if limited || joined_since_last_sync {
Some(true)
} else {
None
},
prev_batch,
events: room_events,
},
// TODO: state before timeline
state: sync_events::State {
events: if joined_since_last_sync {
state
.into_iter()
.map(|(_, pdu)| pdu.to_state_event())
.collect()
} else {
Vec::new()
},
},
ephemeral: sync_events::Ephemeral { events: edus },
};
if !joined_room.is_empty() {
joined_rooms.insert(room_id.clone().try_into().unwrap(), joined_room);
}
}
let mut left_rooms = BTreeMap::new();
@ -2390,6 +2391,7 @@ pub fn sync_route(
let pdus = db.rooms.pdus_since(&room_id, since).unwrap();
let room_events = pdus.map(|pdu| pdu.unwrap().to_room_event()).collect();
// TODO: Only until leave point
let mut edus = db
.rooms
.edus
@ -2416,38 +2418,40 @@ pub fn sync_route(
);
}
left_rooms.insert(
room_id.clone().try_into().unwrap(),
sync_events::LeftRoom {
account_data: Some(sync_events::AccountData { events: Vec::new() }),
timeline: sync_events::Timeline {
limited: Some(false),
prev_batch: Some(next_batch.clone()),
events: room_events,
},
state: sync_events::State { events: Vec::new() },
let left_room = sync_events::LeftRoom {
account_data: sync_events::AccountData { events: Vec::new() },
timeline: sync_events::Timeline {
limited: Some(false),
prev_batch: Some(next_batch.clone()),
events: room_events,
},
);
state: sync_events::State { events: Vec::new() },
};
if !left_room.is_empty() {
left_rooms.insert(room_id.clone().try_into().unwrap(), left_room);
}
}
let mut invited_rooms = BTreeMap::new();
for room_id in db.rooms.rooms_invited(&user_id) {
let room_id = room_id.unwrap();
invited_rooms.insert(
room_id.clone(),
sync_events::InvitedRoom {
invite_state: sync_events::InviteState {
events: db
.rooms
.room_state(&room_id)
.unwrap()
.into_iter()
.map(|(_, pdu)| pdu.to_stripped_state_event())
.collect(),
},
let invited_room = sync_events::InvitedRoom {
invite_state: sync_events::InviteState {
events: db
.rooms
.room_state(&room_id)
.unwrap()
.into_iter()
.map(|(_, pdu)| pdu.to_stripped_state_event())
.collect(),
},
);
};
if !invited_room.is_empty() {
invited_rooms.insert(room_id.clone(), invited_room);
}
}
MatrixResult(Ok(sync_events::Response {
@ -2482,17 +2486,16 @@ pub fn sync_route(
.map(|(_, v)| v)
.collect(),
},
device_lists: if since != 0 {
Some(sync_events::DeviceLists {
changed: db
.users
device_lists: sync_events::DeviceLists {
changed: if since != 0 {
db.users
.device_keys_changed(since)
.map(|u| u.unwrap())
.collect(),
left: Vec::new(), // TODO
})
} else {
None // TODO: left
.collect()
} else {
Vec::new()
},
left: Vec::new(), // TODO
},
device_one_time_keys_count: Default::default(), // TODO
to_device: sync_events::ToDevice {

View file

@ -421,7 +421,7 @@ impl Rooms {
auth_events: Vec::new(),
redacts: redacts.clone(),
unsigned,
hashes: ruma::api::federation::EventHash {
hashes: ruma::api::federation::pdu::EventHash {
sha256: "aaa".to_owned(),
},
signatures: HashMap::new(),

View file

@ -1,6 +1,6 @@
use js_int::UInt;
use ruma::{
api::federation::EventHash,
api::federation::pdu::EventHash,
events::{
collections::all::{RoomEvent, StateEvent},
stripped::AnyStrippedStateEvent,