events: Add method to get user receipt in ReceiptEventContent

This commit is contained in:
Kévin Commaille 2022-03-30 00:05:47 +02:00 committed by GitHub
parent 77e44d6428
commit 0450f9d8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,20 @@ use crate::{receipt::ReceiptType, EventId, MilliSecondsSinceUnixEpoch, UserId};
#[ruma_event(type = "m.receipt", kind = EphemeralRoom)] #[ruma_event(type = "m.receipt", kind = EphemeralRoom)]
pub struct ReceiptEventContent(pub BTreeMap<Box<EventId>, Receipts>); pub struct ReceiptEventContent(pub BTreeMap<Box<EventId>, Receipts>);
impl ReceiptEventContent {
/// Get the receipt for the given user ID with the given receipt type, if it exists.
pub fn user_receipt(
&self,
user_id: &UserId,
receipt_type: ReceiptType,
) -> Option<(&EventId, &Receipt)> {
self.iter().find_map(|(event_id, receipts)| {
let receipt = receipts.get(&receipt_type)?.get(user_id)?;
Some((event_id.as_ref(), receipt))
})
}
}
impl Deref for ReceiptEventContent { impl Deref for ReceiptEventContent {
type Target = BTreeMap<Box<EventId>, Receipts>; type Target = BTreeMap<Box<EventId>, Receipts>;