matrix_sdk_ui/timeline/
algorithms.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
15use std::{ops::Deref, sync::Arc};
16
17use imbl::Vector;
18use ruma::EventId;
19
20#[cfg(doc)]
21use super::controller::TimelineMetadata;
22use super::{
23    event_item::EventTimelineItemKind, item::TimelineUniqueId, EventTimelineItem,
24    ReactionsByKeyBySender, TimelineEventItemId, TimelineItem,
25};
26
27pub(super) struct EventTimelineItemWithId<'a> {
28    pub inner: &'a EventTimelineItem,
29    /// Internal identifier generated by [`TimelineMetadata`].
30    pub internal_id: &'a TimelineUniqueId,
31}
32
33impl EventTimelineItemWithId<'_> {
34    /// Create a clone of the underlying [`TimelineItem`] with the given kind.
35    pub fn with_inner_kind(&self, kind: impl Into<EventTimelineItemKind>) -> Arc<TimelineItem> {
36        TimelineItem::new(self.inner.with_kind(kind), self.internal_id.clone())
37    }
38
39    /// Create a clone of the underlying [`TimelineItem`] with the given
40    /// reactions.
41    pub fn with_reactions(&self, reactions: ReactionsByKeyBySender) -> Arc<TimelineItem> {
42        let content = self.inner.content().with_reactions(reactions);
43        let event_item = self.inner.with_content(content);
44        TimelineItem::new(event_item, self.internal_id.clone())
45    }
46}
47
48impl Deref for EventTimelineItemWithId<'_> {
49    type Target = EventTimelineItem;
50
51    fn deref(&self) -> &Self::Target {
52        self.inner
53    }
54}
55
56#[inline(always)]
57fn rfind_event_item_internal(
58    items: &Vector<Arc<TimelineItem>>,
59    mut f: impl FnMut(&EventTimelineItemWithId<'_>) -> bool,
60) -> Option<(usize, EventTimelineItemWithId<'_>)> {
61    items
62        .iter()
63        .enumerate()
64        .filter_map(|(idx, item)| {
65            Some((
66                idx,
67                EventTimelineItemWithId { inner: item.as_event()?, internal_id: &item.internal_id },
68            ))
69        })
70        .rfind(|(_, it)| f(it))
71}
72
73/// Finds an item in the vector of `items` given a predicate `f`.
74///
75/// WARNING/FIXME: this does a linear scan of the items, so this can be slow if
76/// there are many items in the timeline.
77pub(super) fn rfind_event_item(
78    items: &Vector<Arc<TimelineItem>>,
79    mut f: impl FnMut(&EventTimelineItem) -> bool,
80) -> Option<(usize, EventTimelineItemWithId<'_>)> {
81    rfind_event_item_internal(items, |item_with_id| f(item_with_id.inner))
82}
83
84/// Find the timeline item that matches the given event id, if any.
85///
86/// WARNING: Linear scan of the items, see documentation of
87/// [`rfind_event_item`].
88pub(super) fn rfind_event_by_id<'a>(
89    items: &'a Vector<Arc<TimelineItem>>,
90    event_id: &EventId,
91) -> Option<(usize, EventTimelineItemWithId<'a>)> {
92    rfind_event_item(items, |it| it.event_id() == Some(event_id))
93}
94
95/// Find the timeline item that matches the given item (event or transaction)
96/// id, if any.
97///
98/// WARNING: Linear scan of the items, see documentation of
99/// [`rfind_event_item`].
100pub(super) fn rfind_event_by_item_id<'a>(
101    items: &'a Vector<Arc<TimelineItem>>,
102    item_id: &TimelineEventItemId,
103) -> Option<(usize, EventTimelineItemWithId<'a>)> {
104    match item_id {
105        TimelineEventItemId::TransactionId(txn_id) => {
106            rfind_event_item(items, |item| match &item.kind {
107                EventTimelineItemKind::Local(local) => local.transaction_id == *txn_id,
108                EventTimelineItemKind::Remote(remote) => {
109                    remote.transaction_id.as_deref() == Some(txn_id)
110                }
111            })
112        }
113        TimelineEventItemId::EventId(event_id) => rfind_event_by_id(items, event_id),
114    }
115}