Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: Notify when the identity status of a user changes #4022

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bindings/matrix-sdk-ffi/src/identity_status_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[derive(uniffi::Record)]
pub struct IdentityStatusChange {
/// The user ID of the user whose identity status changed
pub user_id: String,

/// The new state of the identity of the user. One of:
///
/// * Verified
/// * Pinned
/// * PinViolation
/// * PreviouslyVerified
///
/// See
/// [`matrix_sdk_crypto::identities::room_identity_state::RoomIdentityState`]
/// for details.
pub changed_to: String,
}
1 change: 1 addition & 0 deletions bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod encryption;
mod error;
mod event;
mod helpers;
mod identity_status_change;
mod notification;
mod notification_settings;
mod platform;
Expand Down
42 changes: 42 additions & 0 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use anyhow::{Context, Result};
use futures_util::StreamExt;
use matrix_sdk::{
crypto::LocalTrust,
event_cache::paginator::PaginatorError,
Expand Down Expand Up @@ -35,6 +36,7 @@ use crate::{
chunk_iterator::ChunkIterator,
error::{ClientError, MediaInfoError, RoomError},
event::{MessageLikeEventType, StateEventType},
identity_status_change::IdentityStatusChange,
room_info::RoomInfo,
room_member::RoomMember,
ruma::{ImageInfo, Mentions, NotifyType},
Expand Down Expand Up @@ -582,6 +584,41 @@ impl Room {
})))
}

pub fn subscribe_to_identity_status_changes(
&self,
listener: Box<dyn IdentityStatusChangeListener>,
) -> Arc<TaskHandle> {
let room = self.inner.clone();
Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
let status_changes = room.subscribe_to_identity_status_changes().await;
if let Ok(mut status_changes) = status_changes {
// TODO: what to do with failures?
while let Some(identity_status_changes) = status_changes.next().await {
listener.call(
identity_status_changes
.iter()
.map(|change| {
let user_id = change.user_id.to_string();
let changed_to = match change.changed_to {
matrix_sdk::crypto::IdentityState::Verified => "Verified",
matrix_sdk::crypto::IdentityState::Pinned => "Pinned",
matrix_sdk::crypto::IdentityState::PinViolation => {
"PinViolation"
}
matrix_sdk::crypto::IdentityState::PreviouslyVerified => {
"PreviouslyVerified"
}
}
.to_owned();
IdentityStatusChange { user_id, changed_to }
})
.collect(),
);
}
}
})))
}

/// Set (or unset) a flag on the room to indicate that the user has
/// explicitly marked it as unread.
pub async fn set_unread_flag(&self, new_value: bool) -> Result<(), ClientError> {
Expand Down Expand Up @@ -898,6 +935,11 @@ pub trait TypingNotificationsListener: Sync + Send {
fn call(&self, typing_user_ids: Vec<String>);
}

#[uniffi::export(callback_interface)]
pub trait IdentityStatusChangeListener: Sync + Send {
fn call(&self, identity_status_change: Vec<IdentityStatusChange>);
}

#[derive(uniffi::Object)]
pub struct RoomMembersIterator {
chunk_iterator: ChunkIterator<matrix_sdk::room::RoomMember>,
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/src/identities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
//! `/keys/query` API call.
pub(crate) mod device;
pub(crate) mod manager;
pub(crate) mod room_identity_state;
pub(crate) mod user;

use std::sync::{
Expand Down
Loading
Loading