Skip to content

Commit

Permalink
Use filter_map() to remove Option from VerificationEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfriend committed Dec 11, 2024
1 parent 2e26894 commit 8ff8191
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:acter/common/extensions/options.dart';
import 'package:acter_flutter_sdk/acter_flutter_sdk_ffi.dart';
import 'package:logging/logging.dart';
import 'package:riverpod/riverpod.dart';
Expand All @@ -18,8 +17,8 @@ class VerificationNotifier extends StateNotifier<VerificationState> {
final Ref ref;
final Client client;

late Stream<OptionVerificationEvent>? _listener;
late StreamSubscription<OptionVerificationEvent>? _poller;
late Stream<VerificationEvent>? _listener;
late StreamSubscription<VerificationEvent>? _poller;

VerificationNotifier({
required this.ref,
Expand All @@ -31,13 +30,7 @@ class VerificationNotifier extends StateNotifier<VerificationState> {
void _init() {
_listener = client.verificationEventRx(); // keep it resident in memory
_poller = _listener?.listen(
(event) {
final data = event.data();
data.map(
(event) => _handleEvent(event),
orElse: () => _log.info('invalid verification event'),
);
},
_handleEvent,
onError: (e, s) {
_log.severe('stream errored', e, s);
},
Expand Down
7 changes: 1 addition & 6 deletions native/acter/api.rsh
Original file line number Diff line number Diff line change
Expand Up @@ -2803,7 +2803,7 @@ object Client {
fn logout() -> Future<Result<bool>>;

/// Get the verification event receiver
fn verification_event_rx() -> Stream<OptionVerificationEvent>;
fn verification_event_rx() -> Stream<VerificationEvent>;

/// Get session manager that returns all/verified/unverified/inactive session list
fn session_manager() -> SessionManager;
Expand Down Expand Up @@ -3136,11 +3136,6 @@ object VerificationEvent {
fn mismatch_sas_verification() -> Future<Result<bool>>;
}

object OptionVerificationEvent {
/// get data object
fn data() -> Option<VerificationEvent>;
}

object VerificationEmoji {
/// binary representation of emoji unicode
fn symbol() -> u32;
Expand Down
4 changes: 1 addition & 3 deletions native/acter/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ pub use tasks::{
};
pub use typing::TypingEvent;
pub use utils::parse_markdown;
pub use verification::{
OptionVerificationEvent, SessionManager, VerificationEmoji, VerificationEvent,
};
pub use verification::{SessionManager, VerificationEmoji, VerificationEvent};

pub type DeviceId = matrix_sdk_base::ruma::OwnedDeviceId;
pub type EventId = matrix_sdk_base::ruma::OwnedEventId;
Expand Down
24 changes: 6 additions & 18 deletions native/acter/src/api/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use matrix_sdk_base::ruma::{
};
use std::{
collections::HashMap,
marker::Unpin,
ops::Deref,
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
Expand Down Expand Up @@ -307,21 +308,6 @@ impl VerificationEvent {
}
}

#[derive(Clone)]
pub struct OptionVerificationEvent {
data: Option<VerificationEvent>,
}

impl OptionVerificationEvent {
pub(crate) fn new(data: Option<VerificationEvent>) -> Self {
OptionVerificationEvent { data }
}

pub fn data(&self) -> Option<VerificationEvent> {
self.data.clone()
}
}

#[derive(Clone, Debug)]
pub struct VerificationEmoji {
symbol: u32,
Expand Down Expand Up @@ -1127,9 +1113,11 @@ impl SessionManager {
}

impl Client {
pub fn verification_event_rx(&self) -> impl Stream<Item = OptionVerificationEvent> {
BroadcastStream::new(self.verification_controller.event_rx.resubscribe())
.map(|o| OptionVerificationEvent::new(o.ok()))
// this return value should be able to unpin, because wait_for_verification_event calls pin_mut internally
// this return value should be wrapped in Box::pin, to make unpin possible
pub fn verification_event_rx(&self) -> impl Stream<Item = VerificationEvent> + Unpin {
let mut stream = BroadcastStream::new(self.verification_controller.event_rx.resubscribe());
Box::pin(stream.filter_map(|o| async move { o.ok() }))

Check warning on line 1120 in native/acter/src/api/verification.rs

View check run for this annotation

Codecov / codecov/patch

native/acter/src/api/verification.rs#L1118-L1120

Added lines #L1118 - L1120 were not covered by tests
}

pub fn session_manager(&self) -> SessionManager {
Expand Down
10 changes: 4 additions & 6 deletions native/test/src/tests/verification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use acter::api::{OptionVerificationEvent, VerificationEvent};
use acter::api::VerificationEvent;
use anyhow::Result;
use futures::{
pin_mut,
Expand All @@ -10,16 +10,14 @@ use tracing::info;
use crate::utils::random_user;

fn wait_for_verification_event(
rx: impl Stream<Item = OptionVerificationEvent>,
rx: impl Stream<Item = VerificationEvent>,
name: &str,
) -> VerificationEvent {
pin_mut!(rx);
loop {
if let Some(event) = rx.next().now_or_never().flatten() {
if let Some(data) = event.data() {
if data.event_type() == name {
return data;
}
if event.event_type() == name {
return event;
}
}
}
Expand Down
57 changes: 4 additions & 53 deletions packages/rust_sdk/lib/acter_flutter_sdk_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15954,7 +15954,7 @@ class Api {
return tmp9;
}

OptionVerificationEvent? __clientVerificationEventRxStreamPoll(
VerificationEvent? __clientVerificationEventRxStreamPoll(
int boxed,
int postCobject,
int port,
Expand Down Expand Up @@ -15984,9 +15984,9 @@ class Api {
return null;
}
final ffi.Pointer<ffi.Void> tmp11_0 = ffi.Pointer.fromAddress(tmp11);
final tmp11_1 = _Box(this, tmp11_0, "drop_box_OptionVerificationEvent");
final tmp11_1 = _Box(this, tmp11_0, "drop_box_VerificationEvent");
tmp11_1._finalizer = this._registerFinalizer(tmp11_1);
final tmp9 = OptionVerificationEvent._(this, tmp11_1);
final tmp9 = VerificationEvent._(this, tmp11_1);
return tmp9;
}

Expand Down Expand Up @@ -28362,17 +28362,6 @@ class Api {
int Function(
int,
)>();
late final _optionVerificationEventDataPtr = _lookup<
ffi.NativeFunction<
_OptionVerificationEventDataReturn Function(
ffi.IntPtr,
)>>("__OptionVerificationEvent_data");

late final _optionVerificationEventData =
_optionVerificationEventDataPtr.asFunction<
_OptionVerificationEventDataReturn Function(
int,
)>();
late final _verificationEmojiSymbolPtr = _lookup<
ffi.NativeFunction<
ffi.Uint32 Function(
Expand Down Expand Up @@ -55254,7 +55243,7 @@ class Client {
}

/// Get the verification event receiver
Stream<OptionVerificationEvent> verificationEventRx() {
Stream<VerificationEvent> verificationEventRx() {
var tmp0 = 0;
tmp0 = _box.borrow();
final tmp1 = _api._clientVerificationEventRx(
Expand Down Expand Up @@ -58091,37 +58080,6 @@ class VerificationEvent {
}
}

class OptionVerificationEvent {
final Api _api;
final _Box _box;

OptionVerificationEvent._(this._api, this._box);

/// get data object
VerificationEvent? data() {
var tmp0 = 0;
tmp0 = _box.borrow();
final tmp1 = _api._optionVerificationEventData(
tmp0,
);
final tmp3 = tmp1.arg0;
final tmp4 = tmp1.arg1;
if (tmp3 == 0) {
return null;
}
final ffi.Pointer<ffi.Void> tmp4_0 = ffi.Pointer.fromAddress(tmp4);
final tmp4_1 = _Box(_api, tmp4_0, "drop_box_VerificationEvent");
tmp4_1._finalizer = _api._registerFinalizer(tmp4_1);
final tmp2 = VerificationEvent._(_api, tmp4_1);
return tmp2;
}

/// Manually drops the object and unregisters the FinalizableHandle.
void drop() {
_box.drop();
}
}

class VerificationEmoji {
final Api _api;
final _Box _box;
Expand Down Expand Up @@ -61627,13 +61585,6 @@ class _VerificationEventGetContentReturn extends ffi.Struct {
external int arg3;
}

class _OptionVerificationEventDataReturn extends ffi.Struct {
@ffi.Uint8()
external int arg0;
@ffi.IntPtr()
external int arg1;
}

class _VerificationEmojiDescriptionReturn extends ffi.Struct {
@ffi.IntPtr()
external int arg0;
Expand Down

0 comments on commit 8ff8191

Please sign in to comment.