Skip to main content

matrix_sdk_base/
read_receipts.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Basic types and data structures to allow tracking of read receipts in rooms.
16//!
17//! These are defined in this crate because they're used in the `RoomInfo`,
18//! which is also defined in this crate.
19//!
20//! All the fields are public by default, because they're mutated by other
21//! crates upwards in the dependency tree (notably by the event cache in the
22//! matrix-sdk crate).
23
24use std::num::NonZeroUsize;
25
26use matrix_sdk_common::ring_buffer::RingBuffer;
27use ruma::OwnedEventId;
28use serde::{Deserialize, Serialize};
29
30/// The latest read receipt known for a room.
31#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
32pub struct LatestReadReceipt {
33    /// The id of the event the read receipt is referring to. (Not the read
34    /// receipt event id.)
35    pub event_id: OwnedEventId,
36}
37
38/// Public data about read receipts collected during processing of that room.
39///
40/// Remember that each time a field of `RoomReadReceipts` is updated in
41/// `compute_unread_counts`, this function must return true!
42#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
43pub struct RoomReadReceipts {
44    /// Does the room have unread messages?
45    pub num_unread: u64,
46
47    /// Does the room have unread events that should notify?
48    pub num_notifications: u64,
49
50    /// Does the room have messages causing highlights for the users? (aka
51    /// mentions)
52    pub num_mentions: u64,
53
54    /// The latest read receipt (main-threaded or unthreaded) known for the
55    /// room.
56    pub latest_active: Option<LatestReadReceipt>,
57
58    /// Read receipts that haven't been matched to their event.
59    ///
60    /// This might mean that the read receipt is in the past further than we
61    /// recall (i.e. before the first event we've ever cached), or in the
62    /// future (i.e. the event is lagging behind because of federation).
63    ///
64    /// Note: this contains event ids of the event *targets* of the receipts,
65    /// not the event ids of the receipt events themselves.
66    #[serde(default = "new_nonempty_ring_buffer")]
67    pub pending: RingBuffer<OwnedEventId>,
68}
69
70impl Default for RoomReadReceipts {
71    fn default() -> Self {
72        Self {
73            num_unread: Default::default(),
74            num_notifications: Default::default(),
75            num_mentions: Default::default(),
76            latest_active: Default::default(),
77            pending: new_nonempty_ring_buffer(),
78        }
79    }
80}
81
82fn new_nonempty_ring_buffer() -> RingBuffer<OwnedEventId> {
83    // 10 pending read receipts per room should be enough for everyone.
84    // SAFETY: `unwrap` is safe because 10 is not zero.
85    RingBuffer::new(NonZeroUsize::new(10).unwrap())
86}