fix: send presence too often

next
timokoesters 2020-06-04 22:36:48 +02:00
parent 32da76b9a2
commit d404f902bf
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
4 changed files with 36 additions and 74 deletions

View File

@ -548,9 +548,8 @@ pub fn set_displayname_route(
// Presence update // Presence update
db.global_edus db.global_edus
.update_globallatest( .update_presence(
&user_id, ruma_events::presence::PresenceEvent {
EduEvent::Presence(ruma_events::presence::PresenceEvent {
content: ruma_events::presence::PresenceEventContent { content: ruma_events::presence::PresenceEventContent {
avatar_url: db.users.avatar_url(&user_id).unwrap(), avatar_url: db.users.avatar_url(&user_id).unwrap(),
currently_active: None, currently_active: None,
@ -560,7 +559,7 @@ pub fn set_displayname_route(
status_msg: None, status_msg: None,
}, },
sender: user_id.clone(), sender: user_id.clone(),
}), },
&db.globals, &db.globals,
) )
.unwrap(); .unwrap();
@ -640,9 +639,8 @@ pub fn set_avatar_url_route(
// Presence update // Presence update
db.global_edus db.global_edus
.update_globallatest( .update_presence(
&user_id, ruma_events::presence::PresenceEvent {
EduEvent::Presence(ruma_events::presence::PresenceEvent {
content: ruma_events::presence::PresenceEventContent { content: ruma_events::presence::PresenceEventContent {
avatar_url: db.users.avatar_url(&user_id).unwrap(), avatar_url: db.users.avatar_url(&user_id).unwrap(),
currently_active: None, currently_active: None,
@ -652,7 +650,7 @@ pub fn set_avatar_url_route(
status_msg: None, status_msg: None,
}, },
sender: user_id.clone(), sender: user_id.clone(),
}), },
&db.globals, &db.globals,
) )
.unwrap(); .unwrap();
@ -707,9 +705,8 @@ pub fn set_presence_route(
let user_id = body.user_id.as_ref().expect("user is authenticated"); let user_id = body.user_id.as_ref().expect("user is authenticated");
db.global_edus db.global_edus
.update_globallatest( .update_presence(
&user_id, ruma_events::presence::PresenceEvent {
EduEvent::Presence(ruma_events::presence::PresenceEvent {
content: ruma_events::presence::PresenceEventContent { content: ruma_events::presence::PresenceEventContent {
avatar_url: db.users.avatar_url(&user_id).unwrap(), avatar_url: db.users.avatar_url(&user_id).unwrap(),
currently_active: None, currently_active: None,
@ -719,7 +716,7 @@ pub fn set_presence_route(
status_msg: body.status_msg.clone(), status_msg: body.status_msg.clone(),
}, },
sender: user_id.clone(), sender: user_id.clone(),
}), },
&db.globals, &db.globals,
) )
.unwrap(); .unwrap();
@ -2435,24 +2432,16 @@ pub fn sync_route(
presence: sync_events::Presence { presence: sync_events::Presence {
events: db events: db
.global_edus .global_edus
.globallatests_since(since) .presence_since(since)
.unwrap() .unwrap()
.filter_map(|edu| { .map(|edu| {
// Only look for presence events let mut edu = edu.unwrap().deserialize().unwrap();
if let Ok(mut edu) = EventJson::<ruma_events::presence::PresenceEvent>::from( let timestamp = edu.content.last_active_ago.unwrap();
edu.unwrap().into_json(), let last_active_ago = js_int::UInt::try_from(utils::millis_since_unix_epoch())
) .unwrap()
.deserialize() - timestamp;
{ edu.content.last_active_ago = Some(last_active_ago);
let timestamp = edu.content.last_active_ago.unwrap(); edu.into()
edu.content.last_active_ago = Some(
js_int::UInt::try_from(utils::millis_since_unix_epoch()).unwrap()
- timestamp,
);
Some(edu.into())
} else {
None
}
}) })
.collect(), .collect(),
}, },

View File

@ -94,8 +94,7 @@ impl Database {
roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata").unwrap(), roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata").unwrap(),
}, },
global_edus: global_edus::GlobalEdus { global_edus: global_edus::GlobalEdus {
//globalallid_globalall: db.open_tree("globalallid_globalall").unwrap(), presenceid_presence: db.open_tree("presenceid_presence").unwrap(), // Presence
globallatestid_globallatest: db.open_tree("globallatestid_globallatest").unwrap(), // Presence
}, },
media: media::Media { media: media::Media {
mediaid_file: db.open_tree("mediaid_file").unwrap(), mediaid_file: db.open_tree("mediaid_file").unwrap(),

View File

@ -1,67 +1,53 @@
use crate::Result; use crate::Result;
use ruma_events::{collections::only::Event as EduEvent, EventJson}; use ruma_events::EventJson;
use ruma_identifiers::UserId;
pub struct GlobalEdus { pub struct GlobalEdus {
//pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Count //pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Count
pub(super) globallatestid_globallatest: sled::Tree, // Presence, GlobalLatestId = Count + UserId pub(super) presenceid_presence: sled::Tree, // Presence, PresenceId = Count + UserId
} }
impl GlobalEdus { impl GlobalEdus {
/// Adds a global event which will be saved until a new event replaces it (e.g. presence updates). /// Adds a global event which will be saved until a new event replaces it (e.g. presence updates).
pub fn update_globallatest( pub fn update_presence(
&self, &self,
user_id: &UserId, presence: ruma_events::presence::PresenceEvent,
event: EduEvent,
globals: &super::globals::Globals, globals: &super::globals::Globals,
) -> Result<()> { ) -> Result<()> {
// Remove old entry // Remove old entry
if let Some(old) = self if let Some(old) = self
.globallatestid_globallatest .presenceid_presence
.iter() .iter()
.keys() .keys()
.rev() .rev()
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.find(|key| { .find(|key| {
key.rsplit(|&b| b == 0xff).next().unwrap() == user_id.to_string().as_bytes() key.rsplit(|&b| b == 0xff).next().unwrap() == presence.sender.to_string().as_bytes()
}) })
{ {
// This is the old global_latest // This is the old global_latest
self.globallatestid_globallatest.remove(old)?; self.presenceid_presence.remove(old)?;
} }
let mut global_latest_id = globals.next_count()?.to_be_bytes().to_vec(); let mut presence_id = globals.next_count()?.to_be_bytes().to_vec();
global_latest_id.push(0xff); presence_id.push(0xff);
global_latest_id.extend_from_slice(&user_id.to_string().as_bytes()); presence_id.extend_from_slice(&presence.sender.to_string().as_bytes());
self.globallatestid_globallatest self.presenceid_presence
.insert(global_latest_id, &*serde_json::to_string(&event)?)?; .insert(presence_id, &*serde_json::to_string(&presence)?)?;
Ok(()) Ok(())
} }
/// Returns an iterator over the most recent presence updates that happened after the event with id `since`. /// Returns an iterator over the most recent presence updates that happened after the event with id `since`.
pub fn globallatests_since( pub fn presence_since(
&self, &self,
since: u64, since: u64,
) -> Result<impl Iterator<Item = Result<EventJson<EduEvent>>>> { ) -> Result<impl Iterator<Item = Result<EventJson<ruma_events::presence::PresenceEvent>>>> {
let first_possible_edu = since.to_be_bytes().to_vec(); let first_possible_edu = (since + 1).to_be_bytes().to_vec(); // +1 so we don't send the event at since
Ok(self Ok(self
.globallatestid_globallatest .presenceid_presence
.range(&*first_possible_edu..) .range(&*first_possible_edu..)
// Skip the first pdu if it's exactly at since, because we sent that last time
.skip(
if self
.globallatestid_globallatest
.get(first_possible_edu)?
.is_some()
{
1
} else {
0
},
)
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.map(|(_, v)| Ok(serde_json::from_slice(&v)?))) .map(|(_, v)| Ok(serde_json::from_slice(&v)?)))
} }

View File

@ -59,23 +59,11 @@ impl RoomEdus {
prefix.push(0xff); prefix.push(0xff);
let mut first_possible_edu = prefix.clone(); let mut first_possible_edu = prefix.clone();
first_possible_edu.extend_from_slice(&since.to_be_bytes()); first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
Ok(self Ok(self
.roomlatestid_roomlatest .roomlatestid_roomlatest
.range(&*first_possible_edu..) .range(&*first_possible_edu..)
// Skip the first pdu if it's exactly at since, because we sent that last time
.skip(
if self
.roomlatestid_roomlatest
.get(first_possible_edu)?
.is_some()
{
1
} else {
0
},
)
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.map(|(_, v)| Ok(serde_json::from_slice(&v)?))) .map(|(_, v)| Ok(serde_json::from_slice(&v)?)))