fix: room exists panic

This commit is contained in:
Timo Kösters 2021-08-14 08:26:45 +02:00
parent 3eabaa2a95
commit 3cf0145bc5
No known key found for this signature in database
GPG key ID: 356E705610F626D5

View file

@ -249,7 +249,10 @@ impl Rooms {
/// Checks if a room exists.
pub fn exists(&self, room_id: &RoomId) -> Result<bool> {
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = match self.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(),
None => return Ok(false),
};
// Look for PDUs in that room.
Ok(self
@ -262,7 +265,7 @@ impl Rooms {
/// Checks if a room exists.
pub fn first_pdu_in_room(&self, room_id: &RoomId) -> Result<Option<Arc<PduEvent>>> {
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
// Look for PDUs in that room.
self.pduid_pdu
@ -627,12 +630,12 @@ impl Rooms {
})
}
pub fn get_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
let bytes = self
pub fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>> {
self
.roomid_shortroomid
.get(&room_id.as_bytes())?
.expect("every room has a shortroomid");
utils::u64_from_bytes(&bytes).map_err(|_| Error::bad_database("Invalid shortroomid in db."))
.map(|bytes|
utils::u64_from_bytes(&bytes).map_err(|_| Error::bad_database("Invalid shortroomid in db."))).transpose()
}
pub fn get_shortstatekey(
@ -781,7 +784,7 @@ impl Rooms {
}
pub fn latest_pdu_count(&self, room_id: &RoomId) -> Result<u64> {
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
let mut last_possible_key = prefix.clone();
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
@ -1021,7 +1024,7 @@ impl Rooms {
leaves: &[EventId],
db: &Database,
) -> Result<Vec<u8>> {
let shortroomid = self.get_shortroomid(&pdu.room_id)?;
let shortroomid = self.get_shortroomid(&pdu.room_id)?.expect("room exists");
// Make unsigned fields correct. This is not properly documented in the spec, but state
// events need to have previous content in the unsigned field, so clients can easily
@ -1846,7 +1849,7 @@ impl Rooms {
room_id: &RoomId,
since: u64,
) -> Result<impl Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a> {
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
// Skip the first pdu if it's exactly at since, because we sent that last time
let mut first_pdu_id = prefix.clone();
@ -1878,7 +1881,7 @@ impl Rooms {
until: u64,
) -> Result<impl Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a> {
// Create the first part of the full pdu id
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
let mut current = prefix.clone();
current.extend_from_slice(&(until.saturating_sub(1)).to_be_bytes()); // -1 because we don't want event at `until`
@ -1911,7 +1914,7 @@ impl Rooms {
from: u64,
) -> Result<impl Iterator<Item = Result<(Vec<u8>, PduEvent)>> + 'a> {
// Create the first part of the full pdu id
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
let mut current = prefix.clone();
current.extend_from_slice(&(from + 1).to_be_bytes()); // +1 so we don't send the base event
@ -2462,7 +2465,7 @@ impl Rooms {
room_id: &RoomId,
search_string: &str,
) -> Result<(impl Iterator<Item = Vec<u8>> + 'a, Vec<String>)> {
let prefix = self.get_shortroomid(room_id)?.to_be_bytes().to_vec();
let prefix = self.get_shortroomid(room_id)?.expect("room exists").to_be_bytes().to_vec();
let prefix_clone = prefix.clone();
let words = search_string