From 0450f9d8f7a6c190e34e367658be6bee620a6d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= <76261501+zecakeh@users.noreply.github.com> Date: Wed, 30 Mar 2022 00:05:47 +0200 Subject: [PATCH] events: Add method to get user receipt in ReceiptEventContent --- crates/ruma-common/src/events/receipt.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/ruma-common/src/events/receipt.rs b/crates/ruma-common/src/events/receipt.rs index 1047f38f..9011a6f6 100644 --- a/crates/ruma-common/src/events/receipt.rs +++ b/crates/ruma-common/src/events/receipt.rs @@ -23,6 +23,20 @@ use crate::{receipt::ReceiptType, EventId, MilliSecondsSinceUnixEpoch, UserId}; #[ruma_event(type = "m.receipt", kind = EphemeralRoom)] pub struct ReceiptEventContent(pub BTreeMap, 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 { type Target = BTreeMap, Receipts>;