2022-06-25 16:12:23 +02:00
|
|
|
mod data;
|
2024-06-28 22:51:39 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
use std::{
|
|
|
|
|
collections::{HashMap, HashSet},
|
2024-07-04 03:26:19 +00:00
|
|
|
fmt::Write,
|
|
|
|
|
sync::{Arc, Mutex},
|
2024-05-09 15:59:08 -07:00
|
|
|
};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
use conduit::{PduCount, Result};
|
2022-10-09 17:25:06 +02:00
|
|
|
use ruma::{DeviceId, OwnedDeviceId, OwnedRoomId, OwnedUserId, RoomId, UserId};
|
2022-01-04 14:30:13 +01:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
use self::data::Data;
|
2023-02-20 22:59:45 +01:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-07-28 04:02:45 +00:00
|
|
|
pub lazy_load_waiting: Mutex<LazyLoadWaiting>,
|
2024-06-28 22:51:39 +00:00
|
|
|
db: Data,
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 04:02:45 +00:00
|
|
|
type LazyLoadWaiting = HashMap<LazyLoadWaitingKey, LazyLoadWaitingVal>;
|
|
|
|
|
type LazyLoadWaitingKey = (OwnedUserId, OwnedDeviceId, OwnedRoomId, PduCount);
|
|
|
|
|
type LazyLoadWaitingVal = HashSet<OwnedUserId>;
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
|
Ok(Arc::new(Self {
|
2024-05-27 03:17:20 +00:00
|
|
|
lazy_load_waiting: Mutex::new(HashMap::new()),
|
2024-07-28 04:02:45 +00:00
|
|
|
db: Data::new(args.db),
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn memory_usage(&self, out: &mut dyn Write) -> Result<()> {
|
|
|
|
|
let lazy_load_waiting = self.lazy_load_waiting.lock().expect("locked").len();
|
|
|
|
|
writeln!(out, "lazy_load_waiting: {lazy_load_waiting}")?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn clear_cache(&self) { self.lazy_load_waiting.lock().expect("locked").clear(); }
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Service {
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn lazy_load_was_sent_before(
|
2022-01-04 14:30:13 +01:00
|
|
|
&self, user_id: &UserId, device_id: &DeviceId, room_id: &RoomId, ll_user: &UserId,
|
|
|
|
|
) -> Result<bool> {
|
2024-03-25 17:05:11 -04:00
|
|
|
self.db
|
|
|
|
|
.lazy_load_was_sent_before(user_id, device_id, room_id, ll_user)
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn lazy_load_mark_sent(
|
2022-10-09 17:25:06 +02:00
|
|
|
&self, user_id: &UserId, device_id: &DeviceId, room_id: &RoomId, lazy_load: HashSet<OwnedUserId>,
|
2023-02-20 22:59:45 +01:00
|
|
|
count: PduCount,
|
2022-01-04 14:30:13 +01:00
|
|
|
) {
|
|
|
|
|
self.lazy_load_waiting
|
|
|
|
|
.lock()
|
2024-07-04 03:26:19 +00:00
|
|
|
.expect("locked")
|
2022-01-04 14:30:13 +01:00
|
|
|
.insert((user_id.to_owned(), device_id.to_owned(), room_id.to_owned(), count), lazy_load);
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub async fn lazy_load_confirm_delivery(
|
2022-01-04 14:30:13 +01:00
|
|
|
&self, user_id: &UserId, device_id: &DeviceId, room_id: &RoomId, since: PduCount,
|
|
|
|
|
) -> Result<()> {
|
2024-07-04 03:26:19 +00:00
|
|
|
if let Some(user_ids) = self.lazy_load_waiting.lock().expect("locked").remove(&(
|
2022-10-05 12:45:54 +02:00
|
|
|
user_id.to_owned(),
|
|
|
|
|
device_id.to_owned(),
|
|
|
|
|
room_id.to_owned(),
|
|
|
|
|
since,
|
|
|
|
|
)) {
|
2024-03-25 17:05:11 -04:00
|
|
|
self.db
|
|
|
|
|
.lazy_load_confirm_delivery(user_id, device_id, room_id, &mut user_ids.iter().map(|u| &**u))?;
|
2022-10-05 12:45:54 +02:00
|
|
|
} else {
|
|
|
|
|
// Ignore
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
Ok(())
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn lazy_load_reset(&self, user_id: &UserId, device_id: &DeviceId, room_id: &RoomId) -> Result<()> {
|
2022-09-06 23:15:09 +02:00
|
|
|
self.db.lazy_load_reset(user_id, device_id, room_id)
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|