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#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
40pub struct ReadReceipts {
41    /// Does the room have unread messages?
42    pub num_unread: u64,
43
44    /// Does the room have unread events that should notify?
45    pub num_notifications: u64,
46
47    /// Does the room have messages causing highlights for the users? (aka
48    /// mentions)
49    pub num_mentions: u64,
50
51    /// The latest read receipt (main-threaded or unthreaded) known for the
52    /// room.
53    pub latest_active: Option<LatestReadReceipt>,
54
55    /// Read receipts that haven't been matched to their event.
56    ///
57    /// This might mean that the read receipt is in the past further than we
58    /// recall (i.e. before the first event we've ever cached), or in the
59    /// future (i.e. the event is lagging behind because of federation).
60    ///
61    /// Note: this contains event ids of the event *targets* of the receipts,
62    /// not the event ids of the receipt events themselves.
63    #[serde(default = "new_nonempty_ring_buffer")]
64    pub pending: RingBuffer<OwnedEventId>,
65}
66
67impl Default for ReadReceipts {
68    fn default() -> Self {
69        Self {
70            num_unread: Default::default(),
71            num_notifications: Default::default(),
72            num_mentions: Default::default(),
73            latest_active: Default::default(),
74            pending: new_nonempty_ring_buffer(),
75        }
76    }
77}
78
79fn new_nonempty_ring_buffer() -> RingBuffer<OwnedEventId> {
80    // 10 pending read receipts per room should be enough for everyone.
81    // SAFETY: `unwrap` is safe because 10 is not zero.
82    RingBuffer::new(NonZeroUsize::new(10).unwrap())
83}