Add a state for the VerificationRequest for when a request transitions

The `VerificationRequest` object is used to control the flow of the
verification but only up to a certain point.

Once we start handling of different specific verification flows (i.e.
SAS or QR code verification) the `VerificationRequest` object creates a
child object of the Verification type.

This patch adds a new `VerificationRequestState` variant called
`Transitioned` which holds the child verification object as associated
data.

This makes it much simpler to go through the whole verification flow by
allowing users to just listen to the `VerificationRequest::changes()`
method.
This commit is contained in:
Damir Jelić
2023-05-17 12:38:04 +02:00
parent 2cce236f4d
commit e9c3aa1a2e
3 changed files with 649 additions and 330 deletions

View File

@@ -550,26 +550,6 @@ pub enum VerificationRequestState {
},
}
impl From<RustVerificationRequestState> for VerificationRequestState {
fn from(value: RustVerificationRequestState) -> Self {
match value {
// The clients do not need to distinguish `Created` and `Requested` state
RustVerificationRequestState::Created { .. } => Self::Requested,
RustVerificationRequestState::Requested { .. } => Self::Requested,
RustVerificationRequestState::Ready {
their_methods,
our_methods,
other_device_id: _,
} => Self::Ready {
their_methods: their_methods.iter().map(|m| m.to_string()).collect(),
our_methods: our_methods.iter().map(|m| m.to_string()).collect(),
},
RustVerificationRequestState::Done => Self::Done,
RustVerificationRequestState::Cancelled(c) => Self::Cancelled { cancel_info: c.into() },
}
}
}
/// The verificatoin request object which then can transition into some concrete
/// verification method
#[derive(uniffi::Object)]
@@ -747,17 +727,57 @@ impl VerificationRequest {
pub fn set_changes_listener(&self, listener: Box<dyn VerificationRequestListener>) {
let stream = self.inner.changes();
self.runtime.spawn(Self::changes_listener(stream, listener));
self.runtime.spawn(Self::changes_listener(self.inner.to_owned(), stream, listener));
}
/// Get the current state of the verification request.
pub fn state(&self) -> VerificationRequestState {
self.inner.state().into()
Self::convert_verification_request(&self.inner, self.inner.state())
}
}
impl VerificationRequest {
fn convert_verification_request(
request: &InnerVerificationRequest,
value: RustVerificationRequestState,
) -> VerificationRequestState {
match value {
// The clients do not need to distinguish `Created` and `Requested` state
RustVerificationRequestState::Created { .. } => VerificationRequestState::Requested,
RustVerificationRequestState::Requested { .. } => VerificationRequestState::Requested,
RustVerificationRequestState::Ready {
their_methods,
our_methods,
other_device_id: _,
} => VerificationRequestState::Ready {
their_methods: their_methods.iter().map(|m| m.to_string()).collect(),
our_methods: our_methods.iter().map(|m| m.to_string()).collect(),
},
RustVerificationRequestState::Done => VerificationRequestState::Done,
RustVerificationRequestState::Transitioned { .. } => {
let their_methods = request
.their_supported_methods()
.expect("The transitioned state should know the other side's methods")
.into_iter()
.map(|m| m.to_string())
.collect();
let our_methods = request
.our_supported_methods()
.expect("The transitioned state should know our own supported methods")
.iter()
.map(|m| m.to_string())
.collect();
VerificationRequestState::Ready { their_methods, our_methods }
}
RustVerificationRequestState::Cancelled(c) => {
VerificationRequestState::Cancelled { cancel_info: c.into() }
}
}
}
async fn changes_listener(
request: InnerVerificationRequest,
mut stream: impl Stream<Item = RustVerificationRequestState> + std::marker::Unpin,
listener: Box<dyn VerificationRequestListener>,
) {
@@ -771,7 +791,9 @@ impl VerificationRequest {
| RustVerificationRequestState::Cancelled { .. }
);
listener.on_change(state.into());
let state = Self::convert_verification_request(&request, state);
listener.on_change(state);
if should_break {
break;

View File

@@ -1,5 +1,10 @@
# v0.7.0
- Add a new variant to the `VerificationRequestState` enum called
`Transitioned`. This enum variant is used when a `VerificationRequest`
transitions into a concrete `Verification` object. The concrete `Verification`
object is given as associated data in the `Transitioned` enum variant.
- Replace the libolm backup encryption code with a native Rust version. This
adds WASM support to the backups_v1 feature.

View File

File diff suppressed because it is too large Load Diff