refactor: Make UIA Request null safe

This commit is contained in:
Christian Pauly 2021-08-30 09:20:33 +02:00
parent efca583cf5
commit 62694248b9
3 changed files with 21 additions and 24 deletions

View File

@ -22,7 +22,7 @@ import 'package:matrix/encryption/utils/olm_session.dart';
import 'package:matrix/encryption/utils/outbound_group_session.dart'; import 'package:matrix/encryption/utils/outbound_group_session.dart';
import 'package:matrix/encryption/utils/ssss_cache.dart'; import 'package:matrix/encryption/utils/ssss_cache.dart';
import 'package:matrix/encryption/utils/stored_inbound_group_session.dart'; import 'package:matrix/encryption/utils/stored_inbound_group_session.dart';
import 'package:matrix/src/utils/QueuedToDeviceEvent.dart'; import 'package:matrix/src/utils/queued_to_device_event.dart';
import '../../matrix.dart'; import '../../matrix.dart';
@ -80,7 +80,7 @@ abstract class DatabaseApi {
Future<List<Event>> getEventList(int clientId, Room room); Future<List<Event>> getEventList(int clientId, Room room);
Future<Uint8List?> getFile(String mxcUri); Future<Uint8List?> getFile(Uri mxcUri);
Future storeFile(Uri mxcUri, Uint8List bytes, int time); Future storeFile(Uri mxcUri, Uint8List bytes, int time);

View File

@ -328,7 +328,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
} }
@override @override
Future<Uint8List?> getFile(String mxcUri) async { Future<Uint8List?> getFile(Uri mxcUri) async {
return null; return null;
} }

View File

@ -1,4 +1,3 @@
// @dart=2.9
/* /*
* Famedly Matrix SDK * Famedly Matrix SDK
* Copyright (C) 2020, 2021 Famedly GmbH * Copyright (C) 2020, 2021 Famedly GmbH
@ -35,12 +34,12 @@ enum UiaRequestState {
/// Wrapper to handle User interactive authentication requests /// Wrapper to handle User interactive authentication requests
class UiaRequest<T> { class UiaRequest<T> {
void Function(UiaRequestState state) onUpdate; void Function(UiaRequestState state)? onUpdate;
final Future<T> Function(AuthenticationData auth) request; final Future<T> Function(AuthenticationData? auth) request;
String session; String? session;
UiaRequestState _state = UiaRequestState.loading; UiaRequestState _state = UiaRequestState.loading;
T result; T? result;
Exception error; Exception? error;
Set<String> nextStages = <String>{}; Set<String> nextStages = <String>{};
Map<String, dynamic> params = <String, dynamic>{}; Map<String, dynamic> params = <String, dynamic>{};
@ -52,11 +51,11 @@ class UiaRequest<T> {
onUpdate?.call(newState); onUpdate?.call(newState);
} }
UiaRequest({this.onUpdate, this.request}) { UiaRequest({this.onUpdate, required this.request}) {
_run(); _run();
} }
Future<T> _run([AuthenticationData auth]) async { Future<T?> _run([AuthenticationData? auth]) async {
state = UiaRequestState.loading; state = UiaRequestState.loading;
try { try {
final res = await request(auth); final res = await request(auth);
@ -68,7 +67,7 @@ class UiaRequest<T> {
rethrow; rethrow;
} }
session ??= err.session; session ??= err.session;
final completed = err.completedAuthenticationFlows ?? <String>[]; final completed = err.completedAuthenticationFlows;
final flows = err.authenticationFlows ?? <AuthenticationFlow>[]; final flows = err.authenticationFlows ?? <AuthenticationFlow>[];
params = err.authenticationParams ?? <String, dynamic>{}; params = err.authenticationParams ?? <String, dynamic>{};
nextStages = getNextStages(flows, completed); nextStages = getNextStages(flows, completed);
@ -87,10 +86,10 @@ class UiaRequest<T> {
} }
} }
Future<T> completeStage(AuthenticationData auth) => _run(auth); Future<T?> completeStage(AuthenticationData auth) => _run(auth);
/// Cancel this uia request for example if the app can not handle this stage. /// Cancel this uia request for example if the app can not handle this stage.
void cancel([Exception err]) { void cancel([Exception? err]) {
error = err ?? Exception('Request has been canceled'); error = err ?? Exception('Request has been canceled');
state = UiaRequestState.fail; state = UiaRequestState.fail;
} }
@ -101,7 +100,6 @@ class UiaRequest<T> {
for (final flow in flows) { for (final flow in flows) {
final stages = flow.stages; final stages = flow.stages;
final nextStage = stages[completed.length]; final nextStage = stages[completed.length];
if (nextStage != null) {
var stagesValid = true; var stagesValid = true;
for (var i = 0; i < completed.length; i++) { for (var i = 0; i < completed.length; i++) {
if (stages[i] != completed[i]) { if (stages[i] != completed[i]) {
@ -113,7 +111,6 @@ class UiaRequest<T> {
nextStages.add(nextStage); nextStages.add(nextStage);
} }
} }
}
return nextStages; return nextStages;
} }
} }