syncv3: read receipts extension (MSC3960)
This commit is contained in:
parent
bf9d498621
commit
9fde835673
3 changed files with 60 additions and 16 deletions
|
@ -2,17 +2,11 @@ use std::{mem::size_of, sync::Arc};
|
|||
|
||||
use conduit::{utils, Error, Result};
|
||||
use database::Map;
|
||||
use ruma::{
|
||||
events::{receipt::ReceiptEvent, AnySyncEphemeralRoomEvent},
|
||||
serde::Raw,
|
||||
CanonicalJsonObject, OwnedUserId, RoomId, UserId,
|
||||
};
|
||||
use ruma::{events::receipt::ReceiptEvent, serde::Raw, CanonicalJsonObject, RoomId, UserId};
|
||||
|
||||
use super::AnySyncEphemeralRoomEventIter;
|
||||
use crate::{globals, Dep};
|
||||
|
||||
type AnySyncEphemeralRoomEventIter<'a> =
|
||||
Box<dyn Iterator<Item = Result<(OwnedUserId, u64, Raw<AnySyncEphemeralRoomEvent>)>> + 'a>;
|
||||
|
||||
pub(super) struct Data {
|
||||
roomuserid_privateread: Arc<Map>,
|
||||
roomuserid_lastprivatereadupdate: Arc<Map>,
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
mod data;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use conduit::Result;
|
||||
use conduit::{debug, Result};
|
||||
use data::Data;
|
||||
use ruma::{events::receipt::ReceiptEvent, serde::Raw, OwnedUserId, RoomId, UserId};
|
||||
use ruma::{
|
||||
events::{
|
||||
receipt::{ReceiptEvent, ReceiptEventContent},
|
||||
AnySyncEphemeralRoomEvent, SyncEphemeralRoomEvent,
|
||||
},
|
||||
serde::Raw,
|
||||
OwnedUserId, RoomId, UserId,
|
||||
};
|
||||
|
||||
use crate::{sending, Dep};
|
||||
|
||||
|
@ -17,6 +24,9 @@ struct Services {
|
|||
sending: Dep<sending::Service>,
|
||||
}
|
||||
|
||||
type AnySyncEphemeralRoomEventIter<'a> =
|
||||
Box<dyn Iterator<Item = Result<(OwnedUserId, u64, Raw<AnySyncEphemeralRoomEvent>)>> + 'a>;
|
||||
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
|
@ -44,7 +54,7 @@ impl Service {
|
|||
#[tracing::instrument(skip(self), level = "debug")]
|
||||
pub fn readreceipts_since<'a>(
|
||||
&'a self, room_id: &RoomId, since: u64,
|
||||
) -> impl Iterator<Item = Result<(OwnedUserId, u64, Raw<ruma::events::AnySyncEphemeralRoomEvent>)>> + 'a {
|
||||
) -> impl Iterator<Item = Result<(OwnedUserId, u64, Raw<AnySyncEphemeralRoomEvent>)>> + 'a {
|
||||
self.db.readreceipts_since(room_id, since)
|
||||
}
|
||||
|
||||
|
@ -65,3 +75,26 @@ impl Service {
|
|||
self.db.last_privateread_update(user_id, room_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn pack_receipts(receipts: AnySyncEphemeralRoomEventIter<'_>) -> Raw<SyncEphemeralRoomEvent<ReceiptEventContent>> {
|
||||
let mut json = BTreeMap::new();
|
||||
for (_user, _count, value) in receipts.flatten() {
|
||||
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"),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue