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 matrix_sdk_base::deserialized_responses::ThreadSummary;
16use ruma::events::receipt::ReceiptEventContent;
17use tokio::sync::broadcast::{Receiver, Sender};
18
19use super::super::{super::RoomEventCacheGenericUpdate, TimelineVectorDiffs};
20
21/// An update related to events happened in a thread.
22#[derive(Debug, Clone)]
23pub enum ThreadEventCacheUpdate {
24    /// The thread has received updates for the timeline as _diffs_.
25    UpdateTimelineEvents(TimelineVectorDiffs),
26
27    /// The thread summary has been updated.
28    ///
29    /// One can either observe [`ThreadInfo`] with
30    /// [`ThreadEventCache::subscribe_to_thread_info`], or —if one is already
31    /// listening to these updates— one can use this particular update to see
32    /// new thread summary.
33    ///
34    /// [`ThreadInfo`]: matrix_sdk_base::event_cache::thread::ThreadInfo
35    /// [`ThreadEventCache::subscribe_to_thread_info`]: super::ThreadEventCache::subscribe_to_thread_info
36    UpdateSummary(ThreadSummary),
37
38    /// The thread has received a new read receipt event.
39    AddReadReceiptEvent {
40        /// The event containing the receipts.
41        event: ReceiptEventContent,
42    },
43}
44
45/// A small type to send updates in all channels.
46#[derive(Clone)]
47pub struct ThreadEventCacheUpdateSender {
48    thread_sender: Sender<ThreadEventCacheUpdate>,
49    generic_sender: Sender<RoomEventCacheGenericUpdate>,
50}
51
52impl ThreadEventCacheUpdateSender {
53    /// Create a new [`ThreadEventCacheUpdateSender`].
54    pub fn new(generic_sender: Sender<RoomEventCacheGenericUpdate>) -> Self {
55        Self { thread_sender: Sender::new(32), generic_sender }
56    }
57
58    /// Send a [`TimelineVectorDiffs`].
59    pub fn send(
60        &self,
61        thread_update: ThreadEventCacheUpdate,
62        generic_update: Option<RoomEventCacheGenericUpdate>,
63    ) {
64        let _ = self.thread_sender.send(thread_update);
65
66        if let Some(generic_update) = generic_update {
67            let _ = self.generic_sender.send(generic_update);
68        }
69    }
70
71    /// Create a new [`Receiver`] of [`ThreadEventCacheUpdate`].
72    pub(super) fn new_thread_receiver(&self) -> Receiver<ThreadEventCacheUpdate> {
73        self.thread_sender.subscribe()
74    }
75}