improvement: device list works better
The only situation that isn't working yet is sending `left` events for users when the sender leaves the room
This commit is contained in:
parent
f23fb32e95
commit
4323cf5fec
5 changed files with 246 additions and 151 deletions
|
@ -945,11 +945,11 @@ impl Rooms {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn search_pdus(
|
||||
&self,
|
||||
pub fn search_pdus<'a>(
|
||||
&'a self,
|
||||
room_id: &RoomId,
|
||||
search_string: &str,
|
||||
) -> Result<(impl Iterator<Item = IVec>, Vec<String>)> {
|
||||
) -> Result<(impl Iterator<Item = IVec> + 'a, Vec<String>)> {
|
||||
let mut prefix = room_id.to_string().as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
||||
|
@ -958,7 +958,7 @@ impl Rooms {
|
|||
.map(str::to_lowercase)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut iterators = words.iter().map(|word| {
|
||||
let iterators = words.clone().into_iter().map(move |word| {
|
||||
let mut prefix2 = prefix.clone();
|
||||
prefix2.extend_from_slice(word.as_bytes());
|
||||
prefix2.push(0xff);
|
||||
|
@ -973,50 +973,56 @@ impl Rooms {
|
|||
.filter(|(_, &b)| b == 0xff)
|
||||
.nth(1)
|
||||
.ok_or_else(|| Error::bad_database("Invalid tokenid in db."))?
|
||||
.0 + 1; // +1 because the pdu id starts AFTER the separator
|
||||
.0
|
||||
+ 1; // +1 because the pdu id starts AFTER the separator
|
||||
|
||||
let pdu_id =
|
||||
key.subslice(pduid_index, key.len() - pduid_index);
|
||||
let pdu_id = key.subslice(pduid_index, key.len() - pduid_index);
|
||||
|
||||
Ok::<_, Error>(pdu_id)
|
||||
})
|
||||
.filter_map(|r| r.ok())
|
||||
.peekable()
|
||||
});
|
||||
|
||||
let first_iterator = match iterators.next() {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return Err(Error::BadRequest(
|
||||
ErrorKind::InvalidParam,
|
||||
"search_term needs to contain at least one word.",
|
||||
))
|
||||
}
|
||||
};
|
||||
Ok((utils::common_elements(iterators).unwrap(), words))
|
||||
}
|
||||
|
||||
let mut other_iterators = iterators.collect::<Vec<_>>();
|
||||
pub fn get_shared_rooms<'a>(
|
||||
&'a self,
|
||||
users: Vec<UserId>,
|
||||
) -> impl Iterator<Item = Result<RoomId>> + 'a {
|
||||
let iterators = users.into_iter().map(move |user_id| {
|
||||
let mut prefix = user_id.as_bytes().to_vec();
|
||||
prefix.push(0xff);
|
||||
|
||||
Ok((
|
||||
first_iterator.filter(move |target| {
|
||||
other_iterators
|
||||
.iter_mut()
|
||||
.map(|it| {
|
||||
while let Some(element) = it.peek() {
|
||||
if element > target {
|
||||
return false;
|
||||
} else if element == target {
|
||||
return true;
|
||||
} else {
|
||||
it.next();
|
||||
}
|
||||
}
|
||||
self.userroomid_joined
|
||||
.scan_prefix(&prefix)
|
||||
.keys()
|
||||
.filter_map(|r| r.ok())
|
||||
.map(|key| {
|
||||
let roomid_index = key
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, &b)| b == 0xff)
|
||||
.nth(0)
|
||||
.ok_or_else(|| Error::bad_database("Invalid userroomid_joined in db."))?
|
||||
.0
|
||||
+ 1; // +1 because the room id starts AFTER the separator
|
||||
|
||||
false
|
||||
})
|
||||
.all(|b| b)
|
||||
}),
|
||||
words,
|
||||
))
|
||||
let room_id = key.subslice(roomid_index, key.len() - roomid_index);
|
||||
|
||||
Ok::<_, Error>(room_id)
|
||||
})
|
||||
.filter_map(|r| r.ok())
|
||||
});
|
||||
|
||||
utils::common_elements(iterators)
|
||||
.expect("users is not empty")
|
||||
.map(|bytes| {
|
||||
RoomId::try_from(utils::string_from_bytes(&*bytes).map_err(|_| {
|
||||
Error::bad_database("Invalid RoomId bytes in userroomid_joined")
|
||||
})?)
|
||||
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an iterator over all joined members of a room.
|
||||
|
|
|
@ -408,19 +408,7 @@ impl Users {
|
|||
&*serde_json::to_string(&device_keys).expect("DeviceKeys::to_string always works"),
|
||||
)?;
|
||||
|
||||
let count = globals.next_count()?.to_be_bytes();
|
||||
for room_id in rooms.rooms_joined(&user_id) {
|
||||
let mut key = room_id?.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
}
|
||||
|
||||
let mut key = user_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
self.mark_device_key_update(user_id, rooms, globals)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -520,19 +508,7 @@ impl Users {
|
|||
.insert(&*user_id.to_string(), user_signing_key_key)?;
|
||||
}
|
||||
|
||||
let count = globals.next_count()?.to_be_bytes();
|
||||
for room_id in rooms.rooms_joined(&user_id) {
|
||||
let mut key = room_id?.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
}
|
||||
|
||||
let mut key = user_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
self.mark_device_key_update(user_id, rooms, globals)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -576,21 +552,7 @@ impl Users {
|
|||
)?;
|
||||
|
||||
// TODO: Should we notify about this change?
|
||||
let count = globals.next_count()?.to_be_bytes();
|
||||
for room_id in rooms.rooms_joined(&target_id) {
|
||||
let mut key = room_id?.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
|
||||
self.keychangeid_userid
|
||||
.insert(key, &*target_id.to_string())?;
|
||||
}
|
||||
|
||||
let mut key = target_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
self.keychangeid_userid
|
||||
.insert(key, &*target_id.to_string())?;
|
||||
self.mark_device_key_update(target_id, rooms, globals)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -628,6 +590,37 @@ impl Users {
|
|||
})
|
||||
}
|
||||
|
||||
fn mark_device_key_update(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
rooms: &super::rooms::Rooms,
|
||||
globals: &super::globals::Globals,
|
||||
) -> Result<()> {
|
||||
let count = globals.next_count()?.to_be_bytes();
|
||||
for room_id in rooms.rooms_joined(&user_id).filter_map(|r| r.ok()) {
|
||||
// Don't send key updates to unencrypted rooms
|
||||
if rooms
|
||||
.room_state_get(&room_id, &EventType::RoomEncryption, "")?
|
||||
.is_none()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut key = room_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
}
|
||||
|
||||
let mut key = user_id.to_string().as_bytes().to_vec();
|
||||
key.push(0xff);
|
||||
key.extend_from_slice(&count);
|
||||
self.keychangeid_userid.insert(key, &*user_id.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_device_keys(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue