7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2025 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for that specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use std::{collections::HashMap, sync::Arc}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use matrix_sdk_ui::notification_client::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | NotificationClient as SdkNotificationClient, NotificationEvent as SdkNotificationEvent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | NotificationItem as SdkNotificationItem, NotificationStatus as SdkNotificationStatus, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | RawNotificationEvent as SdkRawNotificationEvent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | use crate::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | client::{Client, JoinRule}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | error::ClientError, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | event::TimelineEvent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | room::Room, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | pub enum NotificationEvent { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | Timeline { event: Arc<TimelineEvent> }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | Invite { sender: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | pub struct NotificationSenderInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | pub display_name: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | pub avatar_url: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | pub is_name_ambiguous: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | pub struct NotificationRoomInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | pub display_name: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | pub avatar_url: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | pub canonical_alias: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | pub topic: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | pub join_rule: Option<JoinRule>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | pub joined_members_count: u64, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | pub active_service_members_count: u64, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | pub service_members: Vec<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | pub is_encrypted: Option<bool>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | pub is_direct: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | pub is_space: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | pub is_dm: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | pub struct NotificationItem { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | pub event: NotificationEvent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | /// The raw JSON of the underlying event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | pub raw_event: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | pub sender_info: NotificationSenderInfo, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | pub room_info: NotificationRoomInfo, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | /// Is the notification supposed to be at the "noisy" level? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | /// Can be `None` if we couldn't determine this, because we lacked |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | /// information to create a push context. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | pub is_noisy: Option<bool>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | pub has_mention: Option<bool>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | pub thread_id: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | /// The push actions for this notification (notify, sound, highlight, etc.). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | pub actions: Option<Vec<crate::notification_settings::Action>>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | impl NotificationItem { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | fn from_inner(item: SdkNotificationItem) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | let event = match item.event { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | SdkNotificationEvent::Timeline(event) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | NotificationEvent::Timeline { event: Arc::new(TimelineEvent(event)) } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | SdkNotificationEvent::Invite(event) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | NotificationEvent::Invite { sender: event.sender.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | let raw_event = match &item.raw_event { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | SdkRawNotificationEvent::Timeline(raw) => raw.json().get().to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | SdkRawNotificationEvent::Invite(raw) => raw.json().get().to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | event, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | raw_event, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | sender_info: NotificationSenderInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | display_name: item.sender_display_name, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | avatar_url: item.sender_avatar_url, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | is_name_ambiguous: item.is_sender_name_ambiguous, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | room_info: NotificationRoomInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | display_name: item.room_computed_display_name, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | avatar_url: item.room_avatar_url, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | canonical_alias: item.room_canonical_alias, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | topic: item.room_topic, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | join_rule: item.room_join_rule.map(TryInto::try_into).transpose().ok().flatten(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | joined_members_count: item.joined_members_count, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | service_members: item.service_members, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | active_service_members_count: item.active_service_members_count, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | is_encrypted: item.is_room_encrypted, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | is_direct: item.is_direct_message_room, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | is_space: item.is_space, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | is_dm: item.room_is_dm, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | is_noisy: item.is_noisy, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | has_mention: item.has_mention, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | thread_id: item.thread_id.map(|t| t.to_string()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | actions: item |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | .actions |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | .map(|a| a.into_iter().filter_map(|action| action.try_into().ok()).collect()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | #[allow(clippy::large_enum_variant)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | pub enum NotificationStatus { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | /// The event has been found and was not filtered out. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | Event { item: NotificationItem }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | /// The event couldn't be found in the network queries used to find it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | EventNotFound, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | /// The event has been filtered out, either because of the user's push |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | /// rules, or because the user which triggered it is ignored by the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | /// current user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | EventFilteredOut, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | /// The event has been redacted. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | EventRedacted, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | impl From<SdkNotificationStatus> for NotificationStatus { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | fn from(item: SdkNotificationStatus) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | match item { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | SdkNotificationStatus::Event(item) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | NotificationStatus::Event { item: NotificationItem::from_inner(*item) } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | SdkNotificationStatus::EventNotFound => NotificationStatus::EventNotFound, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | SdkNotificationStatus::EventFilteredOut => NotificationStatus::EventFilteredOut, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | SdkNotificationStatus::EventRedacted => NotificationStatus::EventRedacted, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | #[allow(clippy::large_enum_variant)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | pub enum BatchNotificationResult { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | /// We have more detailed information about the notification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | Ok { status: NotificationStatus }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | /// An error occurred while trying to fetch the notification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | Error { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | /// The error message observed while handling a specific notification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 165 | message: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 166 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 167 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 168 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 169 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 170 | pub struct NotificationClient { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 171 | pub(crate) inner: SdkNotificationClient, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 172 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 173 | /// A reference to the FFI client. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 174 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 175 | /// Note: we do this to make it so that the FFI `NotificationClient` keeps |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 176 | /// the FFI `Client` and thus the SDK `Client` alive. Otherwise, we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 177 | /// would need to repeat the hack done in the FFI `Client::drop` method. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 178 | pub(crate) client: Arc<Client>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 179 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 180 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 181 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 182 | impl NotificationClient { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 183 | /// Fetches a room by its ID using the in-memory state store backed client. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 184 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 185 | /// Useful to retrieve room information after running the limited |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 186 | /// notification client sliding sync loop. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 187 | pub fn get_room(&self, room_id: String) -> Result<Option<Arc<Room>>, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 188 | let room_id = RoomId::parse(room_id)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 189 | let sdk_room = self.inner.get_room(&room_id); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 190 | let room = sdk_room |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 191 | .map(|room| Arc::new(Room::new(room, self.client.utd_hook_manager.get().cloned()))); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 192 | Ok(room) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 193 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 194 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 195 | /// Fetches the content of a notification. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 196 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 197 | /// This will first try to get the notification using a short-lived sliding |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 198 | /// sync, and if the sliding-sync can't find the event, then it'll use a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 199 | /// `/context` query to find the event with associated member information. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 200 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 201 | /// An error result means that we couldn't resolve the notification; in that |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 202 | /// case, a dummy notification may be displayed instead. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 203 | pub async fn get_notification( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 204 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 205 | room_id: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 206 | event_id: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 207 | ) -> Result<NotificationStatus, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 208 | let room_id = RoomId::parse(room_id)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 209 | let event_id = EventId::parse(event_id)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 210 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 211 | let item = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 212 | self.inner.get_notification(&room_id, &event_id).await.map_err(ClientError::from)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 213 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 214 | Ok(item.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 215 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 216 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 217 | /// Get several notification items in a single batch. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 218 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 219 | /// Returns an error if the flow failed when preparing to fetch the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 220 | /// notifications, and a [`HashMap`] containing either a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 221 | /// [`BatchNotificationResult`], that indicates if the notification was |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 222 | /// successfully fetched (in which case, it's a [`NotificationStatus`]), or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 223 | /// an error message if it couldn't be fetched. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 224 | pub async fn get_notifications( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 225 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 226 | requests: Vec<NotificationItemsRequest>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 227 | ) -> Result<HashMap<String, BatchNotificationResult>, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 228 | let requests = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 229 | requests.into_iter().map(TryInto::try_into).collect::<Result<Vec<_>, _>>()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 230 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 231 | let items = self.inner.get_notifications(&requests).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 232 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 233 | let mut batch_result = HashMap::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 234 | for (key, value) in items.into_iter() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 235 | let result = match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 236 | Ok(status) => BatchNotificationResult::Ok { status: status.into() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 237 | Err(error) => BatchNotificationResult::Error { message: error.to_string() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 238 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 239 | batch_result.insert(key.to_string(), result); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 240 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 241 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 242 | Ok(batch_result) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 243 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 244 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 245 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 246 | /// A request for notification items grouped by their room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 247 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 248 | pub struct NotificationItemsRequest { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 249 | room_id: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 250 | event_ids: Vec<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 251 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 252 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 253 | impl NotificationItemsRequest { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 254 | /// The parsed [`OwnedRoomId`] to use with the SDK crates. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 255 | pub fn room_id(&self) -> Result<OwnedRoomId, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 256 | RoomId::parse(&self.room_id).map_err(ClientError::from) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 257 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 258 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 259 | /// The parsed [`OwnedEventId`] list to use with the SDK crates. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 260 | pub fn event_ids(&self) -> Result<Vec<OwnedEventId>, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 261 | self.event_ids |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 262 | .iter() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 263 | .map(|id| EventId::parse(id).map_err(ClientError::from)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 264 | .collect::<Result<Vec<_>, _>>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 265 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 266 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 267 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 268 | impl TryFrom<NotificationItemsRequest> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 269 | for matrix_sdk_ui::notification_client::NotificationItemsRequest |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 270 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 271 | type Error = ClientError; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 272 | fn try_from(value: NotificationItemsRequest) -> Result<Self, Self::Error> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 273 | Ok(Self { room_id: value.room_id()?, event_ids: value.event_ids()? }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 274 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 275 | } |