2022-06-25 16:12:23 +02:00
|
|
|
mod data;
|
2022-10-08 13:04:55 +02:00
|
|
|
|
2024-08-30 18:23:42 +02:00
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-08-30 18:23:42 +02:00
|
|
|
use conduit::{debug, Result};
|
2024-08-08 17:18:30 +00:00
|
|
|
use futures::Stream;
|
2024-08-30 18:23:42 +02:00
|
|
|
use ruma::{
|
|
|
|
|
events::{
|
|
|
|
|
receipt::{ReceiptEvent, ReceiptEventContent},
|
2024-08-08 17:18:30 +00:00
|
|
|
SyncEphemeralRoomEvent,
|
2024-08-30 18:23:42 +02:00
|
|
|
},
|
|
|
|
|
serde::Raw,
|
2024-08-08 17:18:30 +00:00
|
|
|
RoomId, UserId,
|
2024-08-30 18:23:42 +02:00
|
|
|
};
|
2022-09-07 13:25:51 +02:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
use self::data::{Data, ReceiptItem};
|
2024-07-18 06:37:47 +00:00
|
|
|
use crate::{sending, Dep};
|
2022-06-25 16:12:23 +02:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-07-18 06:37:47 +00:00
|
|
|
services: Services,
|
2024-05-27 03:17:20 +00:00
|
|
|
db: Data,
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
struct Services {
|
|
|
|
|
sending: Dep<sending::Service>,
|
|
|
|
|
}
|
|
|
|
|
|
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-07-18 06:37:47 +00:00
|
|
|
services: Services {
|
|
|
|
|
sending: args.depend::<sending::Service>("sending"),
|
|
|
|
|
},
|
|
|
|
|
db: Data::new(&args),
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Service {
|
2024-03-05 19:48:54 -05:00
|
|
|
/// Replaces the previous read receipt.
|
2024-08-08 17:18:30 +00:00
|
|
|
pub async fn readreceipt_update(&self, user_id: &UserId, room_id: &RoomId, event: &ReceiptEvent) {
|
|
|
|
|
self.db.readreceipt_update(user_id, room_id, event).await;
|
|
|
|
|
self.services
|
|
|
|
|
.sending
|
|
|
|
|
.flush_room(room_id)
|
|
|
|
|
.await
|
|
|
|
|
.expect("room flush failed");
|
2024-03-05 19:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over the most recent read_receipts in a room that
|
|
|
|
|
/// happened after the event with id `since`.
|
2024-08-08 17:18:30 +00:00
|
|
|
#[inline]
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn readreceipts_since<'a>(
|
2024-08-08 17:18:30 +00:00
|
|
|
&'a self, room_id: &'a RoomId, since: u64,
|
|
|
|
|
) -> impl Stream<Item = ReceiptItem> + Send + 'a {
|
2024-03-05 19:48:54 -05:00
|
|
|
self.db.readreceipts_since(room_id, since)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets a private read marker at `count`.
|
2024-08-08 17:18:30 +00:00
|
|
|
#[inline]
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-08-08 17:18:30 +00:00
|
|
|
pub fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) {
|
|
|
|
|
self.db.private_read_set(room_id, user_id, count);
|
2024-03-05 19:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the private read marker.
|
2024-08-08 17:18:30 +00:00
|
|
|
#[inline]
|
2024-07-07 19:03:15 +00:00
|
|
|
#[tracing::instrument(skip(self), level = "debug")]
|
2024-08-08 17:18:30 +00:00
|
|
|
pub async fn private_read_get(&self, room_id: &RoomId, user_id: &UserId) -> Result<u64> {
|
|
|
|
|
self.db.private_read_get(room_id, user_id).await
|
2024-03-05 19:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the count of the last typing update in this room.
|
2024-08-08 17:18:30 +00:00
|
|
|
#[inline]
|
|
|
|
|
pub async fn last_privateread_update(&self, user_id: &UserId, room_id: &RoomId) -> u64 {
|
|
|
|
|
self.db.last_privateread_update(user_id, room_id).await
|
2024-03-05 19:48:54 -05:00
|
|
|
}
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-08-30 18:23:42 +02:00
|
|
|
|
|
|
|
|
#[must_use]
|
2024-08-08 17:18:30 +00:00
|
|
|
pub fn pack_receipts<I>(receipts: I) -> Raw<SyncEphemeralRoomEvent<ReceiptEventContent>>
|
|
|
|
|
where
|
|
|
|
|
I: Iterator<Item = ReceiptItem>,
|
|
|
|
|
{
|
2024-08-30 18:23:42 +02:00
|
|
|
let mut json = BTreeMap::new();
|
2024-08-08 17:18:30 +00:00
|
|
|
for (_, _, value) in receipts {
|
2024-08-30 18:23:42 +02:00
|
|
|
let receipt = serde_json::from_str::<SyncEphemeralRoomEvent<ReceiptEventContent>>(value.json().get());
|
|
|
|
|
if let Ok(value) = receipt {
|
|
|
|
|
for (event, receipt) in value.content {
|
|
|
|
|
json.insert(event, receipt);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
debug!("failed to parse receipt: {:?}", receipt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let content = ReceiptEventContent::from_iter(json);
|
|
|
|
|
|
|
|
|
|
Raw::from_json(
|
|
|
|
|
serde_json::value::to_raw_value(&SyncEphemeralRoomEvent {
|
|
|
|
|
content,
|
|
|
|
|
})
|
|
|
|
|
.expect("received valid json"),
|
|
|
|
|
)
|
|
|
|
|
}
|