feat: implement /keys/changes

This commit is contained in:
timokoesters 2020-07-29 17:37:26 +02:00
parent 0693387769
commit 66bc25fcd3
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
6 changed files with 96 additions and 50 deletions

View file

@ -611,8 +611,8 @@ impl Rooms {
self.pdus_since(user_id, room_id, 0)
}
/// Returns an iterator over all events in a room that happened after the event with id `since`
/// in reverse-chronological order.
/// Returns a double-ended iterator over all events in a room that happened after the event with id `since`
/// in chronological order.
pub fn pdus_since(
&self,
user_id: &UserId,
@ -624,7 +624,7 @@ impl Rooms {
// Skip the first pdu if it's exactly at since, because we sent that last time
let mut first_pdu_id = prefix.clone();
first_pdu_id.extend_from_slice(&(since+1).to_be_bytes());
first_pdu_id.extend_from_slice(&(since + 1).to_be_bytes());
let mut last_pdu_id = prefix.clone();
last_pdu_id.extend_from_slice(&u64::MAX.to_be_bytes());

View file

@ -352,7 +352,8 @@ impl RoomEdus {
.ok()?,
))
})
.take_while(|(_, timestamp)| current_timestamp - timestamp > 5 * 60_000) // 5 Minutes
.take_while(|(_, timestamp)| current_timestamp - timestamp > 5 * 60_000)
// 5 Minutes
{
self.userid_lastpresenceupdate.remove(&user_id_bytes)?;

View file

@ -9,7 +9,7 @@ use ruma::{
},
},
events::{AnyToDeviceEvent, EventType},
DeviceId, Raw, UserId, RoomId,
DeviceId, Raw, RoomId, UserId,
};
use std::{collections::BTreeMap, convert::TryFrom, mem, time::SystemTime};
@ -389,8 +389,7 @@ impl Users {
key.push(0xff);
key.extend_from_slice(&count);
self.keychangeid_userid
.insert(key, &*user_id.to_string())?;
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
}
Ok(())
@ -497,8 +496,7 @@ impl Users {
key.push(0xff);
key.extend_from_slice(&count);
self.keychangeid_userid
.insert(key, &*user_id.to_string())?;
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
}
Ok(())
@ -556,14 +554,23 @@ impl Users {
Ok(())
}
pub fn keys_changed(&self, room_id: &RoomId, since: u64) -> impl Iterator<Item = Result<UserId>> {
pub fn keys_changed(
&self,
room_id: &RoomId,
from: u64,
to: Option<u64>,
) -> impl Iterator<Item = Result<UserId>> {
let mut prefix = room_id.to_string().as_bytes().to_vec();
prefix.push(0xff);
let mut start = prefix.clone();
start.extend_from_slice(&(since + 1).to_be_bytes());
start.extend_from_slice(&(from + 1).to_be_bytes());
let mut end = prefix.clone();
end.extend_from_slice(&to.unwrap_or(u64::MAX).to_be_bytes());
self.keychangeid_userid
.range(start..)
.range(start..end)
.filter_map(|r| r.ok())
.take_while(move |(k, _)| k.starts_with(&prefix))
.map(|(_, bytes)| {