Files
continuwuity/src/service/rooms/read_receipt/data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.2 KiB
Rust
Raw Normal View History

2024-11-25 01:55:57 +00:00
use std::sync::Arc;
2024-05-26 21:29:19 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit::{
2024-11-25 01:55:57 +00:00
Result,
utils::{ReadyExt, stream::TryIgnore},
2024-08-08 17:18:30 +00:00
};
2024-10-07 17:54:27 +00:00
use database::{Deserialized, Json, Map};
2024-08-08 17:18:30 +00:00
use futures::{Stream, StreamExt};
use ruma::{
2024-11-25 01:55:57 +00:00
CanonicalJsonObject, RoomId, UserId,
events::{AnySyncEphemeralRoomEvent, receipt::ReceiptEvent},
serde::Raw,
2024-08-08 17:18:30 +00:00
};
use crate::{Dep, globals};
2024-03-05 19:48:54 -05:00
2024-05-27 03:17:20 +00:00
pub(super) struct Data {
2024-06-28 22:51:39 +00:00
roomuserid_privateread: Arc<Map>,
roomuserid_lastprivatereadupdate: Arc<Map>,
2024-07-18 06:37:47 +00:00
services: Services,
2024-06-28 22:51:39 +00:00
readreceiptid_readreceipt: Arc<Map>,
}
2024-05-26 21:29:19 +00:00
2024-07-18 06:37:47 +00:00
struct Services {
globals: Dep<globals::Service>,
}
2024-11-25 01:55:57 +00:00
pub(super) type ReceiptItem<'a> = (&'a UserId, u64, Raw<AnySyncEphemeralRoomEvent>);
2024-08-08 17:18:30 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-07-18 06:37:47 +00:00
pub(super) fn new(args: &crate::Args<'_>) -> Self {
let db = &args.db;
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
roomuserid_privateread: db["roomuserid_privateread"].clone(),
roomuserid_lastprivatereadupdate: db["roomuserid_lastprivatereadupdate"].clone(),
readreceiptid_readreceipt: db["readreceiptid_readreceipt"].clone(),
2024-07-18 06:37:47 +00:00
services: Services {
globals: args.depend::<globals::Service>("globals"),
},
2024-05-27 03:17:20 +00:00
}
}
pub(super) async fn readreceipt_update(
&self,
user_id: &UserId,
room_id: &RoomId,
event: &ReceiptEvent,
) {
2024-05-26 21:29:19 +00:00
// Remove old entry
2024-10-07 17:54:27 +00:00
let last_possible_key = (room_id, u64::MAX);
2024-08-08 17:18:30 +00:00
self.readreceiptid_readreceipt
2024-11-22 03:57:29 +00:00
.rev_keys_from_raw(&last_possible_key)
2024-08-08 17:18:30 +00:00
.ignore_err()
2024-11-22 03:57:29 +00:00
.ready_take_while(|key| key.starts_with(room_id.as_bytes()))
.ready_filter_map(|key| key.ends_with(user_id.as_bytes()).then_some(key))
.ready_for_each(|key| self.readreceiptid_readreceipt.del(key))
2024-08-08 17:18:30 +00:00
.await;
2024-05-26 21:29:19 +00:00
2024-10-07 17:54:27 +00:00
let count = self.services.globals.next_count().unwrap();
let latest_id = (room_id, count, user_id);
self.readreceiptid_readreceipt.put(latest_id, Json(event));
2024-05-26 21:29:19 +00:00
}
2024-08-08 17:18:30 +00:00
pub(super) fn readreceipts_since<'a>(
&'a self,
room_id: &'a RoomId,
since: u64,
) -> impl Stream<Item = ReceiptItem<'a>> + Send + 'a {
2024-11-25 01:55:57 +00:00
type Key<'a> = (&'a RoomId, u64, &'a UserId);
type KeyVal<'a> = (Key<'a>, CanonicalJsonObject);
2024-08-08 17:18:30 +00:00
let after_since = since.saturating_add(1); // +1 so we don't send the event at since
let first_possible_edu = (room_id, after_since);
self.readreceiptid_readreceipt
2024-11-25 01:55:57 +00:00
.stream_from(&first_possible_edu)
2024-08-08 17:18:30 +00:00
.ignore_err()
2024-11-25 01:55:57 +00:00
.ready_take_while(move |((r, ..), _): &KeyVal<'_>| *r == room_id)
.map(move |((_, count, user_id), mut json): KeyVal<'_>| {
2024-08-08 17:18:30 +00:00
json.remove("room_id");
2024-11-25 01:55:57 +00:00
let event = serde_json::value::to_raw_value(&json)?;
2024-08-08 17:18:30 +00:00
2024-11-25 01:55:57 +00:00
Ok((user_id, count, Raw::from_json(event)))
2024-08-08 17:18:30 +00:00
})
.ignore_err()
2024-05-26 21:29:19 +00:00
}
2024-12-10 22:54:19 -05:00
pub(super) fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, pdu_count: u64) {
2024-10-07 17:54:27 +00:00
let key = (room_id, user_id);
let next_count = self.services.globals.next_count().unwrap();
2024-05-26 21:29:19 +00:00
2024-12-10 22:54:19 -05:00
self.roomuserid_privateread.put(key, pdu_count);
2024-10-07 17:54:27 +00:00
self.roomuserid_lastprivatereadupdate.put(key, next_count);
2024-05-26 21:29:19 +00:00
}
pub(super) async fn private_read_get_count(
&self,
room_id: &RoomId,
user_id: &UserId,
) -> Result<u64> {
2024-08-08 17:18:30 +00:00
let key = (room_id, user_id);
self.roomuserid_privateread.qry(&key).await.deserialized()
2024-05-26 21:29:19 +00:00
}
pub(super) async fn last_privateread_update(
&self,
user_id: &UserId,
room_id: &RoomId,
) -> u64 {
2024-08-08 17:18:30 +00:00
let key = (room_id, user_id);
self.roomuserid_lastprivatereadupdate
.qry(&key)
.await
.deserialized()
.unwrap_or(0)
2024-05-26 21:29:19 +00:00
}
}