refactor presence to not involve rooms.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
885224ab76
commit
ca1c77d76b
11 changed files with 263 additions and 281 deletions
|
@ -31,7 +31,7 @@ pub struct Services<'a> {
|
|||
pub uiaa: uiaa::Service,
|
||||
pub users: users::Service,
|
||||
pub account_data: account_data::Service,
|
||||
pub presence: presence::Service,
|
||||
pub presence: Arc<presence::Service>,
|
||||
pub admin: Arc<admin::Service>,
|
||||
pub globals: globals::Service<'a>,
|
||||
pub key_backups: key_backups::Service,
|
||||
|
@ -155,9 +155,7 @@ impl Services<'_> {
|
|||
account_data: account_data::Service {
|
||||
db,
|
||||
},
|
||||
presence: presence::Service {
|
||||
db,
|
||||
},
|
||||
presence: presence::Service::build(db, config),
|
||||
admin: admin::Service::build(),
|
||||
key_backups: key_backups::Service {
|
||||
db,
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
use ruma::{events::presence::PresenceEvent, presence::PresenceState, OwnedUserId, RoomId, UInt, UserId};
|
||||
use ruma::{events::presence::PresenceEvent, presence::PresenceState, OwnedUserId, UInt, UserId};
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub trait Data: Send + Sync {
|
||||
/// Returns the latest presence event for the given user in the given room.
|
||||
fn get_presence(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<PresenceEvent>>;
|
||||
|
||||
/// Pings the presence of the given user in the given room, setting the
|
||||
/// specified state.
|
||||
fn ping_presence(&self, user_id: &UserId, new_state: PresenceState) -> Result<()>;
|
||||
/// Returns the latest presence event for the given user.
|
||||
fn get_presence(&self, user_id: &UserId) -> Result<Option<(u64, PresenceEvent)>>;
|
||||
|
||||
/// Adds a presence event which will be saved until a new event replaces it.
|
||||
fn set_presence(
|
||||
&self, room_id: &RoomId, user_id: &UserId, presence_state: PresenceState, currently_active: Option<bool>,
|
||||
&self, user_id: &UserId, presence_state: &PresenceState, currently_active: Option<bool>,
|
||||
last_active_ago: Option<UInt>, status_msg: Option<String>,
|
||||
) -> Result<()>;
|
||||
|
||||
|
@ -21,7 +17,5 @@ pub trait Data: Send + Sync {
|
|||
|
||||
/// Returns the most recent presence updates that happened after the event
|
||||
/// with id `since`.
|
||||
fn presence_since<'a>(
|
||||
&'a self, room_id: &RoomId, since: u64,
|
||||
) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a>;
|
||||
fn presence_since<'a>(&'a self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)> + 'a>;
|
||||
}
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
mod data;
|
||||
|
||||
use std::time::Duration;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
pub use data::Data;
|
||||
use futures_util::{stream::FuturesUnordered, StreamExt};
|
||||
use ruma::{
|
||||
events::presence::{PresenceEvent, PresenceEventContent},
|
||||
presence::PresenceState,
|
||||
OwnedUserId, RoomId, UInt, UserId,
|
||||
OwnedUserId, UInt, UserId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::{sync::mpsc, time::sleep};
|
||||
use tracing::debug;
|
||||
use tokio::{
|
||||
sync::{mpsc, Mutex},
|
||||
time::sleep,
|
||||
};
|
||||
use tracing::{debug, error};
|
||||
|
||||
use crate::{services, utils, Error, Result};
|
||||
use crate::{services, utils, Config, Error, Result};
|
||||
|
||||
/// Represents data required to be kept in order to implement the presence
|
||||
/// specification.
|
||||
|
@ -22,19 +25,15 @@ pub struct Presence {
|
|||
pub state: PresenceState,
|
||||
pub currently_active: bool,
|
||||
pub last_active_ts: u64,
|
||||
pub last_count: u64,
|
||||
pub status_msg: Option<String>,
|
||||
}
|
||||
|
||||
impl Presence {
|
||||
pub fn new(
|
||||
state: PresenceState, currently_active: bool, last_active_ts: u64, last_count: u64, status_msg: Option<String>,
|
||||
) -> Self {
|
||||
pub fn new(state: PresenceState, currently_active: bool, last_active_ts: u64, status_msg: Option<String>) -> Self {
|
||||
Self {
|
||||
state,
|
||||
currently_active,
|
||||
last_active_ts,
|
||||
last_count,
|
||||
status_msg,
|
||||
}
|
||||
}
|
||||
|
@ -72,27 +71,94 @@ impl Presence {
|
|||
|
||||
pub struct Service {
|
||||
pub db: &'static dyn Data,
|
||||
pub timer_sender: mpsc::UnboundedSender<(OwnedUserId, Duration)>,
|
||||
timer_receiver: Mutex<mpsc::UnboundedReceiver<(OwnedUserId, Duration)>>,
|
||||
timeout_remote_users: bool,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Returns the latest presence event for the given user in the given room.
|
||||
pub fn get_presence(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<PresenceEvent>> {
|
||||
self.db.get_presence(room_id, user_id)
|
||||
pub fn build(db: &'static dyn Data, config: &Config) -> Arc<Self> {
|
||||
let (timer_sender, timer_receiver) = mpsc::unbounded_channel();
|
||||
|
||||
Arc::new(Self {
|
||||
db,
|
||||
timer_sender,
|
||||
timer_receiver: Mutex::new(timer_receiver),
|
||||
timeout_remote_users: config.presence_timeout_remote_users,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn start_handler(self: &Arc<Self>) {
|
||||
let self_ = Arc::clone(self);
|
||||
tokio::spawn(async move {
|
||||
self_
|
||||
.handler()
|
||||
.await
|
||||
.expect("Failed to start presence handler");
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns the latest presence event for the given user.
|
||||
pub fn get_presence(&self, user_id: &UserId) -> Result<Option<PresenceEvent>> {
|
||||
if let Some((_, presence)) = self.db.get_presence(user_id)? {
|
||||
Ok(Some(presence))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pings the presence of the given user in the given room, setting the
|
||||
/// specified state.
|
||||
pub fn ping_presence(&self, user_id: &UserId, new_state: PresenceState) -> Result<()> {
|
||||
self.db.ping_presence(user_id, new_state)
|
||||
pub fn ping_presence(&self, user_id: &UserId, new_state: &PresenceState) -> Result<()> {
|
||||
let last_presence = self.db.get_presence(user_id)?;
|
||||
let state_changed = match last_presence {
|
||||
None => true,
|
||||
Some((_, ref presence)) => presence.content.presence != *new_state,
|
||||
};
|
||||
|
||||
let last_last_active_ago = match last_presence {
|
||||
None => 0_u64,
|
||||
Some((_, ref presence)) => presence.content.last_active_ago.unwrap_or_default().into(),
|
||||
};
|
||||
|
||||
const REFRESH_TIMEOUT: u64 = 60 * 25 * 1000;
|
||||
if !state_changed && last_last_active_ago < REFRESH_TIMEOUT {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let status_msg = match last_presence {
|
||||
Some((_, ref presence)) => presence.content.status_msg.clone(),
|
||||
None => Some(String::new()),
|
||||
};
|
||||
|
||||
let last_active_ago = UInt::new(0);
|
||||
let currently_active = *new_state == PresenceState::Online;
|
||||
self.set_presence(user_id, new_state, Some(currently_active), last_active_ago, status_msg)
|
||||
}
|
||||
|
||||
/// Adds a presence event which will be saved until a new event replaces it.
|
||||
pub fn set_presence(
|
||||
&self, room_id: &RoomId, user_id: &UserId, presence_state: PresenceState, currently_active: Option<bool>,
|
||||
&self, user_id: &UserId, presence_state: &PresenceState, currently_active: Option<bool>,
|
||||
last_active_ago: Option<UInt>, status_msg: Option<String>,
|
||||
) -> Result<()> {
|
||||
self.db
|
||||
.set_presence(room_id, user_id, presence_state, currently_active, last_active_ago, status_msg)
|
||||
.set_presence(user_id, presence_state, currently_active, last_active_ago, status_msg)?;
|
||||
|
||||
if self.timeout_remote_users || user_id.server_name() == services().globals.server_name() {
|
||||
let timeout = match presence_state {
|
||||
PresenceState::Online => services().globals.config.presence_idle_timeout_s,
|
||||
_ => services().globals.config.presence_offline_timeout_s,
|
||||
};
|
||||
|
||||
self.timer_sender
|
||||
.send((user_id.to_owned(), Duration::from_secs(timeout)))
|
||||
.map_err(|e| {
|
||||
error!("Failed to add presence timer: {}", e);
|
||||
Error::bad_database("Failed to add presence timer")
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes the presence record for the given user from the database.
|
||||
|
@ -100,29 +166,23 @@ impl Service {
|
|||
|
||||
/// Returns the most recent presence updates that happened after the event
|
||||
/// with id `since`.
|
||||
pub fn presence_since(
|
||||
&self, room_id: &RoomId, since: u64,
|
||||
) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)>> {
|
||||
self.db.presence_since(room_id, since)
|
||||
pub fn presence_since(&self, since: u64) -> Box<dyn Iterator<Item = (OwnedUserId, u64, PresenceEvent)>> {
|
||||
self.db.presence_since(since)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn presence_handler(
|
||||
mut presence_timer_receiver: mpsc::UnboundedReceiver<(OwnedUserId, Duration)>,
|
||||
) -> Result<()> {
|
||||
let mut presence_timers = FuturesUnordered::new();
|
||||
async fn handler(&self) -> Result<()> {
|
||||
let mut presence_timers = FuturesUnordered::new();
|
||||
let mut receiver = self.timer_receiver.lock().await;
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some((user_id, timeout)) = receiver.recv() => {
|
||||
debug!("Adding timer {}: {user_id} timeout:{timeout:?}", presence_timers.len());
|
||||
presence_timers.push(presence_timer(user_id, timeout));
|
||||
}
|
||||
|
||||
loop {
|
||||
debug!("Number of presence timers: {}", presence_timers.len());
|
||||
|
||||
tokio::select! {
|
||||
Some((user_id, timeout)) = presence_timer_receiver.recv() => {
|
||||
debug!("Adding timer for user '{user_id}': Timeout {timeout:?}");
|
||||
presence_timers.push(presence_timer(user_id, timeout));
|
||||
}
|
||||
|
||||
Some(user_id) = presence_timers.next() => {
|
||||
process_presence_timer(&user_id)?;
|
||||
Some(user_id) = presence_timers.next() => {
|
||||
process_presence_timer(&user_id)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,16 +202,12 @@ fn process_presence_timer(user_id: &OwnedUserId) -> Result<()> {
|
|||
let mut last_active_ago = None;
|
||||
let mut status_msg = None;
|
||||
|
||||
for room_id in services().rooms.state_cache.rooms_joined(user_id) {
|
||||
let presence_event = services().presence.get_presence(&room_id?, user_id)?;
|
||||
let presence_event = services().presence.get_presence(user_id)?;
|
||||
|
||||
if let Some(presence_event) = presence_event {
|
||||
presence_state = presence_event.content.presence;
|
||||
last_active_ago = presence_event.content.last_active_ago;
|
||||
status_msg = presence_event.content.status_msg;
|
||||
|
||||
break;
|
||||
}
|
||||
if let Some(presence_event) = presence_event {
|
||||
presence_state = presence_event.content.presence;
|
||||
last_active_ago = presence_event.content.last_active_ago;
|
||||
status_msg = presence_event.content.status_msg;
|
||||
}
|
||||
|
||||
let new_state = match (&presence_state, last_active_ago.map(u64::from)) {
|
||||
|
@ -163,16 +219,9 @@ fn process_presence_timer(user_id: &OwnedUserId) -> Result<()> {
|
|||
debug!("Processed presence timer for user '{user_id}': Old state = {presence_state}, New state = {new_state:?}");
|
||||
|
||||
if let Some(new_state) = new_state {
|
||||
for room_id in services().rooms.state_cache.rooms_joined(user_id) {
|
||||
services().presence.set_presence(
|
||||
&room_id?,
|
||||
user_id,
|
||||
new_state.clone(),
|
||||
Some(false),
|
||||
last_active_ago,
|
||||
status_msg.clone(),
|
||||
)?;
|
||||
}
|
||||
services()
|
||||
.presence
|
||||
.set_presence(user_id, &new_state, Some(false), last_active_ago, status_msg)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::{
|
||||
cmp,
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
fmt::Debug,
|
||||
sync::Arc,
|
||||
|
@ -413,7 +414,7 @@ impl Service {
|
|||
// Fail if a request has failed recently (exponential backoff)
|
||||
const MAX_DURATION: Duration = Duration::from_secs(60 * 60 * 24);
|
||||
let mut min_elapsed_duration = Duration::from_secs(self.timeout) * (*tries) * (*tries);
|
||||
min_elapsed_duration = std::cmp::min(min_elapsed_duration, MAX_DURATION);
|
||||
min_elapsed_duration = cmp::min(min_elapsed_duration, MAX_DURATION);
|
||||
if time.elapsed() < min_elapsed_duration {
|
||||
allow = false;
|
||||
} else {
|
||||
|
@ -448,9 +449,6 @@ impl Service {
|
|||
.filter_map(Result::ok)
|
||||
.filter(|user_id| user_id.server_name() == services().globals.server_name()),
|
||||
);
|
||||
if !select_edus_presence(&room_id, since, &mut max_edu_count, &mut events)? {
|
||||
break;
|
||||
}
|
||||
if !select_edus_receipts(&room_id, since, &mut max_edu_count, &mut events)? {
|
||||
break;
|
||||
}
|
||||
|
@ -472,29 +470,36 @@ impl Service {
|
|||
events.push(serde_json::to_vec(&edu).expect("json can be serialized"));
|
||||
}
|
||||
|
||||
if services().globals.allow_outgoing_presence() {
|
||||
select_edus_presence(server_name, since, &mut max_edu_count, &mut events)?;
|
||||
}
|
||||
|
||||
Ok((events, max_edu_count))
|
||||
}
|
||||
}
|
||||
|
||||
/// Look for presence [in this room] <--- XXX
|
||||
#[tracing::instrument(skip(room_id, since, max_edu_count, events))]
|
||||
/// Look for presence
|
||||
#[tracing::instrument(skip(server_name, since, max_edu_count, events))]
|
||||
pub fn select_edus_presence(
|
||||
room_id: &RoomId, since: u64, max_edu_count: &mut u64, events: &mut Vec<Vec<u8>>,
|
||||
server_name: &ServerName, since: u64, max_edu_count: &mut u64, events: &mut Vec<Vec<u8>>,
|
||||
) -> Result<bool> {
|
||||
if !services().globals.allow_outgoing_presence() {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// Look for presence updates in this room
|
||||
// Look for presence updates for this server
|
||||
let mut presence_updates = Vec::new();
|
||||
for (user_id, count, presence_event) in services().presence.presence_since(room_id, since) {
|
||||
if count > *max_edu_count {
|
||||
*max_edu_count = count;
|
||||
}
|
||||
for (user_id, count, presence_event) in services().presence.presence_since(since) {
|
||||
*max_edu_count = cmp::max(count, *max_edu_count);
|
||||
|
||||
if user_id.server_name() != services().globals.server_name() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !services()
|
||||
.rooms
|
||||
.state_cache
|
||||
.server_sees_user(server_name, &user_id)?
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
presence_updates.push(PresenceUpdate {
|
||||
user_id,
|
||||
presence: presence_event.content.presence,
|
||||
|
@ -524,10 +529,8 @@ pub fn select_edus_receipts(
|
|||
.readreceipts_since(room_id, since)
|
||||
{
|
||||
let (user_id, count, read_receipt) = r?;
|
||||
*max_edu_count = cmp::max(count, *max_edu_count);
|
||||
|
||||
if count > *max_edu_count {
|
||||
*max_edu_count = count;
|
||||
}
|
||||
if user_id.server_name() != services().globals.server_name() {
|
||||
continue;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue