Skip to main content

matrix_sdk/event_cache/caches/thread/
updates.rs

1// Copyright 2026 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
15use ruma::events::receipt::ReceiptEventContent;
16use tokio::sync::broadcast::{Receiver, Sender};
17
18use super::super::{super::RoomEventCacheGenericUpdate, TimelineVectorDiffs};
19
20/// An update related to events happened in a thread.
21#[derive(Debug, Clone)]
22pub enum ThreadEventCacheUpdate {
23    /// The thread has received updates for the timeline as _diffs_.
24    UpdateTimelineEvents(TimelineVectorDiffs),
25
26    /// The thread has received a new read receipt event.
27    AddReadReceiptEvent {
28        /// The event containing the receipts.
29        event: ReceiptEventContent,
30    },
31}
32
33/// A small type to send updates in all channels.
34#[derive(Clone)]
35pub struct ThreadEventCacheUpdateSender {
36    thread_sender: Sender<ThreadEventCacheUpdate>,
37    generic_sender: Sender<RoomEventCacheGenericUpdate>,
38}
39
40impl ThreadEventCacheUpdateSender {
41    /// Create a new [`ThreadEventCacheUpdateSender`].
42    pub fn new(generic_sender: Sender<RoomEventCacheGenericUpdate>) -> Self {
43        Self { thread_sender: Sender::new(32), generic_sender }
44    }
45
46    /// Send a [`TimelineVectorDiffs`].
47    pub fn send(
48        &self,
49        thread_update: ThreadEventCacheUpdate,
50        generic_update: Option<RoomEventCacheGenericUpdate>,
51    ) {
52        let _ = self.thread_sender.send(thread_update);
53
54        if let Some(generic_update) = generic_update {
55            let _ = self.generic_sender.send(generic_update);
56        }
57    }
58
59    /// Create a new [`Receiver`] of [`ThreadEventCacheUpdate`].
60    pub(super) fn new_thread_receiver(&self) -> Receiver<ThreadEventCacheUpdate> {
61        self.thread_sender.subscribe()
62    }
63}