From 42b27f2f60d93084129240704099f494eab63630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kub=C3=ADk?= Date: Fri, 18 Nov 2022 22:39:05 +0100 Subject: [PATCH] style(presence): reformat with cargo --- src/api/client_server/presence.rs | 8 +- src/api/client_server/profile.rs | 4 +- src/api/client_server/sync.rs | 7 +- src/api/server_server.rs | 2 +- src/database/key_value/rooms/edus/presence.rs | 92 +++++++++++++------ src/service/rooms/edus/presence/data.rs | 7 +- src/service/rooms/edus/presence/mod.rs | 21 +++-- 7 files changed, 93 insertions(+), 48 deletions(-) diff --git a/src/api/client_server/presence.rs b/src/api/client_server/presence.rs index f363933e..583b8798 100644 --- a/src/api/client_server/presence.rs +++ b/src/api/client_server/presence.rs @@ -1,5 +1,9 @@ use crate::{services, Result, Ruma}; -use ruma::{api::client::presence::{get_presence, set_presence}, uint, presence::PresenceState}; +use ruma::{ + api::client::presence::{get_presence, set_presence}, + presence::PresenceState, + uint, +}; use std::time::Duration; /// # `PUT /_matrix/client/r0/presence/{userId}/status` @@ -27,7 +31,7 @@ pub async fn set_presence_route( }, sender: sender_user.clone(), }, - true + true, )?; } diff --git a/src/api/client_server/profile.rs b/src/api/client_server/profile.rs index 0e667290..09f1a5e8 100644 --- a/src/api/client_server/profile.rs +++ b/src/api/client_server/profile.rs @@ -109,7 +109,7 @@ pub async fn set_displayname_route( }, sender: sender_user.clone(), }, - true + true, )?; } @@ -245,7 +245,7 @@ pub async fn set_avatar_url_route( }, sender: sender_user.clone(), }, - true + true, )?; } diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index e3c250c1..03ef17a1 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -170,12 +170,7 @@ async fn sync_helper( .rooms .edus .presence - .ping_presence( - &sender_user, - false, - true, - true - )?; + .ping_presence(&sender_user, false, true, true)?; // Setup watchers, so if there's no response, we can wait for them let watcher = services().globals.watch(&sender_user, &sender_device); diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 564843a6..543bd837 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -770,7 +770,7 @@ pub async fn send_transaction_message_route( }, sender: user_id.clone(), }, - true + true, )?; } } diff --git a/src/database/key_value/rooms/edus/presence.rs b/src/database/key_value/rooms/edus/presence.rs index 159edca6..e23370a0 100644 --- a/src/database/key_value/rooms/edus/presence.rs +++ b/src/database/key_value/rooms/edus/presence.rs @@ -1,6 +1,10 @@ use futures_util::{stream::FuturesUnordered, StreamExt}; use ruma::user_id; -use std::{collections::{HashMap, hash_map::Entry}, time::Duration, mem}; +use std::{ + collections::{hash_map::Entry, HashMap}, + mem, + time::Duration, +}; use tracing::{error, info}; use ruma::{ @@ -23,16 +27,24 @@ pub struct PresenceUpdate { impl PresenceUpdate { fn to_be_bytes(&self) -> Vec { - [self.count.to_be_bytes(), self.prev_timestamp.to_be_bytes(), self.curr_timestamp.to_be_bytes()].concat() + [ + self.count.to_be_bytes(), + self.prev_timestamp.to_be_bytes(), + self.curr_timestamp.to_be_bytes(), + ] + .concat() } fn from_be_bytes(bytes: &[u8]) -> Result { let (count_bytes, timestamps_bytes) = bytes.split_at(mem::size_of::()); - let (prev_timestamp_bytes, curr_timestamp_bytes) = timestamps_bytes.split_at(mem::size_of::()); + let (prev_timestamp_bytes, curr_timestamp_bytes) = + timestamps_bytes.split_at(mem::size_of::()); Ok(Self { count: u64_from_bytes(count_bytes).expect("count bytes from DB are valid"), - prev_timestamp: u64_from_bytes(prev_timestamp_bytes).expect("timestamp bytes from DB are valid"), - curr_timestamp: u64_from_bytes(curr_timestamp_bytes).expect("timestamp bytes from DB are valid"), + prev_timestamp: u64_from_bytes(prev_timestamp_bytes) + .expect("timestamp bytes from DB are valid"), + curr_timestamp: u64_from_bytes(curr_timestamp_bytes) + .expect("timestamp bytes from DB are valid"), }) } } @@ -69,33 +81,47 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { Ok(()) } - fn ping_presence(&self, user_id: &UserId, update_count: bool, update_timestamp: bool) -> Result<()> { + fn ping_presence( + &self, + user_id: &UserId, + update_count: bool, + update_timestamp: bool, + ) -> Result<()> { let now = millis_since_unix_epoch(); - let presence = self.userid_presenceupdate + let presence = self + .userid_presenceupdate .get(user_id.as_bytes())? .map(|presence_bytes| PresenceUpdate::from_be_bytes(&presence_bytes)) .transpose()?; let new_presence = match presence { - Some(presence) => { - PresenceUpdate { - count: if update_count { services().globals.next_count()? } else { presence.count }, - prev_timestamp: if update_timestamp { presence.curr_timestamp } else { presence.prev_timestamp }, - curr_timestamp: if update_timestamp { now } else { presence.curr_timestamp } - } + Some(presence) => PresenceUpdate { + count: if update_count { + services().globals.next_count()? + } else { + presence.count + }, + prev_timestamp: if update_timestamp { + presence.curr_timestamp + } else { + presence.prev_timestamp + }, + curr_timestamp: if update_timestamp { + now + } else { + presence.curr_timestamp + }, }, None => PresenceUpdate { count: services().globals.current_count()?, prev_timestamp: now, curr_timestamp: now, - } + }, }; - self.userid_presenceupdate.insert( - user_id.as_bytes(), - &*new_presence.to_be_bytes(), - )?; + self.userid_presenceupdate + .insert(user_id.as_bytes(), &*new_presence.to_be_bytes())?; Ok(()) } @@ -103,7 +129,10 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { fn last_presence_update(&self, user_id: &UserId) -> Result> { self.userid_presenceupdate .get(user_id.as_bytes())? - .map(|bytes| PresenceUpdate::from_be_bytes(&bytes).map(|update| (update.prev_timestamp, update.curr_timestamp))) + .map(|bytes| { + PresenceUpdate::from_be_bytes(&bytes) + .map(|update| (update.prev_timestamp, update.curr_timestamp)) + }) .transpose() } @@ -132,15 +161,16 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { Some(( UserId::parse( utils::string_from_bytes(&user_id_bytes) - .expect("UserID bytes are a valid string") - ).expect("UserID bytes from database are a valid UserID"), + .expect("UserID bytes are a valid string"), + ) + .expect("UserID bytes from database are a valid UserID"), PresenceUpdate::from_be_bytes(&update_bytes) - .expect("PresenceUpdate bytes from database are a valid PresenceUpdate"), + .expect("PresenceUpdate bytes from database are a valid PresenceUpdate"), )) }) .filter_map(|(user_id, presence_update)| { if presence_update.count <= since - || !services() + || !services() .rooms .state_cache .is_joined(&user_id, room_id) @@ -157,12 +187,15 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { self.roomuserid_presenceevent .scan_prefix(room_id.as_bytes().to_vec()) .filter_map(|(roomuserid_bytes, presence_bytes)| { - let user_id_bytes = roomuserid_bytes.split(|byte| *byte == 0xff as u8).last()?; + let user_id_bytes = + roomuserid_bytes.split(|byte| *byte == 0xff as u8).last()?; Some(( UserId::parse( - utils::string_from_bytes(&user_id_bytes) - .expect("UserID bytes are a valid string") - ).expect("UserID bytes from database are a valid UserID").to_owned(), + utils::string_from_bytes(&user_id_bytes) + .expect("UserID bytes are a valid string"), + ) + .expect("UserID bytes from database are a valid UserID") + .to_owned(), presence_bytes, )) }) @@ -172,8 +205,9 @@ impl service::rooms::edus::presence::Data for KeyValueDatabase { Some(( user_id, - parse_presence_event(&presence_bytes, *timestamp) - .expect("PresenceEvent bytes from database are a valid PresenceEvent"), + parse_presence_event(&presence_bytes, *timestamp).expect( + "PresenceEvent bytes from database are a valid PresenceEvent", + ), )) }, ), diff --git a/src/service/rooms/edus/presence/data.rs b/src/service/rooms/edus/presence/data.rs index d90eaece..02c93714 100644 --- a/src/service/rooms/edus/presence/data.rs +++ b/src/service/rooms/edus/presence/data.rs @@ -15,7 +15,12 @@ pub trait Data: Send + Sync { ) -> Result<()>; /// Resets the presence timeout, so the user will stay in their current presence state. - fn ping_presence(&self, user_id: &UserId, update_count: bool, update_timestamp: bool) -> Result<()>; + fn ping_presence( + &self, + user_id: &UserId, + update_count: bool, + update_timestamp: bool, + ) -> Result<()>; /// Returns the timestamp of the last presence update of this user in millis since the unix epoch. fn last_presence_update(&self, user_id: &UserId) -> Result>; diff --git a/src/service/rooms/edus/presence/mod.rs b/src/service/rooms/edus/presence/mod.rs index 427c4fd1..8d3e46aa 100644 --- a/src/service/rooms/edus/presence/mod.rs +++ b/src/service/rooms/edus/presence/mod.rs @@ -21,19 +21,26 @@ impl Service { db, timer_sender: sender, }; - + service.presence_maintain(receiver)?; Ok(service) } /// Resets the presence timeout, so the user will stay in their current presence state. - pub fn ping_presence(&self, user_id: &UserId, update_count: bool, update_timestamp: bool, spawn_timer: bool) -> Result<()> { + pub fn ping_presence( + &self, + user_id: &UserId, + update_count: bool, + update_timestamp: bool, + spawn_timer: bool, + ) -> Result<()> { if spawn_timer { self.spawn_timer(user_id)?; } - self.db.ping_presence(user_id, update_count, update_timestamp) + self.db + .ping_presence(user_id, update_count, update_timestamp) } /// Adds a presence event which will be saved until a new event replaces it. @@ -45,7 +52,7 @@ impl Service { user_id: &UserId, room_id: &RoomId, presence: PresenceEvent, - spawn_timer: bool + spawn_timer: bool, ) -> Result<()> { if spawn_timer { self.spawn_timer(user_id)?; @@ -85,9 +92,9 @@ impl Service { /// Spawns a task maintaining presence data fn presence_maintain( - &self, - timer_receiver: mpsc::UnboundedReceiver, - ) -> Result<()> { + &self, + timer_receiver: mpsc::UnboundedReceiver, + ) -> Result<()> { self.db.presence_maintain(timer_receiver) }