diff options
author | Timo Kösters <timo@koesters.xyz> | 2022-10-30 21:22:32 +0100 |
---|---|---|
committer | Timo Kösters <timo@koesters.xyz> | 2022-10-30 21:22:32 +0100 |
commit | 5d691f405eb6cb13f40ec420b5fe99d99fe5feb9 (patch) | |
tree | 4058f31e7997836d2d1f08e4daef54259f2de803 /src/database | |
parent | c61914c8e1523a65a7feabe9a7c079cbb88fd194 (diff) | |
download | conduit-5d691f405eb6cb13f40ec420b5fe99d99fe5feb9.zip |
fix: stuck typing indicators
Diffstat (limited to 'src/database')
-rw-r--r-- | src/database/key_value/rooms/edus/typing.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/database/key_value/rooms/edus/typing.rs b/src/database/key_value/rooms/edus/typing.rs index 4a2f0f9..d50c000 100644 --- a/src/database/key_value/rooms/edus/typing.rs +++ b/src/database/key_value/rooms/edus/typing.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::mem; use ruma::{OwnedUserId, RoomId, UserId}; @@ -53,6 +54,48 @@ impl service::rooms::edus::typing::Data for KeyValueDatabase { Ok(()) } + fn typings_maintain( + &self, + room_id: &RoomId, + ) -> Result<()> { + let mut prefix = room_id.as_bytes().to_vec(); + prefix.push(0xff); + + let current_timestamp = utils::millis_since_unix_epoch(); + + let mut found_outdated = false; + + // Find all outdated edus before inserting a new one + for outdated_edu in self + .typingid_userid + .scan_prefix(prefix) + .map(|(key, _)| { + Ok::<_, Error>(( + key.clone(), + utils::u64_from_bytes( + &key.splitn(2, |&b| b == 0xff).nth(1).ok_or_else(|| { + Error::bad_database("RoomTyping has invalid timestamp or delimiters.") + })?[0..mem::size_of::<u64>()], + ) + .map_err(|_| Error::bad_database("RoomTyping has invalid timestamp bytes."))?, + )) + }) + .filter_map(|r| r.ok()) + .take_while(|&(_, timestamp)| timestamp < current_timestamp) + { + // This is an outdated edu (time > timestamp) + self.typingid_userid.remove(&outdated_edu.0)?; + found_outdated = true; + } + + if found_outdated { + self.roomid_lasttypingupdate + .insert(room_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?; + } + + Ok(()) + } + fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> { Ok(self .roomid_lasttypingupdate |