parent
8d4745cc12
commit
79d556a830
5 changed files with 122 additions and 30 deletions
24
lib/controllers/account_management.dart
Normal file
24
lib/controllers/account_management.dart
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/models/account_management.dart";
|
||||
|
||||
/// Where the homeserver lets the user manage their account, if it says.
|
||||
class AccountManagementController extends AsyncNotifier<AccountManagement?> {
|
||||
@override
|
||||
Future<AccountManagement?> build() async {
|
||||
final homeserver = ref.watch(
|
||||
ClientStateController.provider.select((state) => state?.homeserverUrl),
|
||||
);
|
||||
if (homeserver == null) return null;
|
||||
|
||||
return ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.getAccountManagement(.parse(homeserver));
|
||||
}
|
||||
|
||||
static final provider =
|
||||
AsyncNotifierProvider<AccountManagementController, AccountManagement?>(
|
||||
AccountManagementController.new,
|
||||
);
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ import "package:nexus/models/requests/get_related_events.dart";
|
|||
import "package:nexus/models/requests/get_room_state.dart";
|
||||
import "package:nexus/models/requests/get_specific_room_state.dart";
|
||||
import "package:nexus/models/content/content.dart";
|
||||
import "package:nexus/models/account_management.dart";
|
||||
import "package:nexus/models/requests/get_space_hierarchy.dart";
|
||||
import "package:nexus/models/requests/get_room_directory.dart";
|
||||
import "package:nexus/models/requests/get_threads.dart";
|
||||
|
|
@ -281,24 +282,19 @@ class ClientController(final String? accountId) extends AsyncNotifier<int> {
|
|||
Future<void> resetEncryption(RecoveryKey key) =>
|
||||
sendCommand("reset_encryption", key.toJson());
|
||||
|
||||
Future<Uri?> getCrossSigningResetUrl(Uri homeserver) async {
|
||||
const action = "org.matrix.cross_signing_reset";
|
||||
final oauth = (await sendCommand("get_login_flows", {
|
||||
"homeserver_url": homeserver.toString(),
|
||||
}))["oauth"];
|
||||
|
||||
if (oauth?["account_management_uri"] case final String uri
|
||||
when (oauth["account_management_actions_supported"] as List?)?.contains(
|
||||
action,
|
||||
) ??
|
||||
false) {
|
||||
final parsed = Uri.parse(uri);
|
||||
return parsed.replace(
|
||||
queryParameters: {...parsed.queryParameters, "action": action},
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Future<AccountManagement?> getAccountManagement(Uri homeserver) async =>
|
||||
switch ((await sendCommand("get_login_flows", {
|
||||
"homeserver_url": homeserver.toString(),
|
||||
}))["oauth"]) {
|
||||
{"account_management_uri": final String uri} && final oauth => .new(
|
||||
.parse(uri),
|
||||
.new(
|
||||
(oauth["account_management_actions_supported"] as List? ?? [])
|
||||
.whereType<String>(),
|
||||
),
|
||||
),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
Future<String> joinRoom(JoinRoomRequest request) async =>
|
||||
(await sendCommand("join_room", request.toJson()))["room_id"];
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import "package:m3e_buttons/m3e_buttons.dart";
|
|||
import "package:multi_state_switch/multi_state_switch.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:nexus/controllers/account_data.dart";
|
||||
import "package:nexus/controllers/account_management.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/controllers/enabled_image_packs.dart";
|
||||
|
|
@ -24,6 +25,7 @@ import "package:nexus/helpers/extensions/distributor_name.dart";
|
|||
import "package:nexus/helpers/extensions/get_localpart.dart";
|
||||
import "package:nexus/helpers/extensions/room_image_packs.dart";
|
||||
import "package:nexus/helpers/hooks/saving.dart";
|
||||
import "package:nexus/helpers/launch_helper.dart";
|
||||
import "package:nexus/models/account_data.dart";
|
||||
import "package:nexus/models/client_state.dart";
|
||||
import "package:nexus/models/image_pack.dart";
|
||||
|
|
@ -50,6 +52,40 @@ class SettingsSectionsController
|
|||
@override
|
||||
Future<IMap<String, IList<SettingsCategory>>> build() async {
|
||||
final settings = await ref.watch(SettingsController.provider.future);
|
||||
// Not awaited, so a slow homeserver doesn't hold up the rest of settings
|
||||
final accountManagement = ref
|
||||
.watch(AccountManagementController.provider)
|
||||
.value;
|
||||
|
||||
// Opens [url], a page of the homeserver's account management
|
||||
Setting link(
|
||||
String title,
|
||||
String description,
|
||||
IconData icon,
|
||||
Uri url, {
|
||||
bool danger = false,
|
||||
}) => .new(
|
||||
title: title,
|
||||
description: description,
|
||||
icon: icon,
|
||||
builder: (title, description, icon) => Builder(
|
||||
builder: (context) {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
return M3EButton.icon(
|
||||
onPressed: () => ref.read(LaunchHelper.provider).launchUrl(url),
|
||||
label: Text(title),
|
||||
icon: Icon(icon),
|
||||
tooltip: description,
|
||||
decoration: danger
|
||||
? .styleFrom(
|
||||
backgroundColor: colorScheme.error,
|
||||
foregroundColor: colorScheme.onError,
|
||||
)
|
||||
: null,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
// Loads the enabled packs' state, so they can be shown
|
||||
ref.watch(EnabledImagePacksController.provider);
|
||||
|
|
@ -793,9 +829,23 @@ class SettingsSectionsController
|
|||
]),
|
||||
),
|
||||
.new(
|
||||
title: "Other",
|
||||
icon: Icons.key,
|
||||
title: "Account Management",
|
||||
icon: Icons.manage_accounts,
|
||||
settings: .new([
|
||||
if (accountManagement case final management?)
|
||||
link(
|
||||
"Manage Account",
|
||||
"Change your password, email, and other account details on your homeserver's website.",
|
||||
Icons.manage_accounts,
|
||||
management.uri,
|
||||
),
|
||||
if (accountManagement?.urlFor(.devicesList) case final url?)
|
||||
link(
|
||||
"Devices",
|
||||
"See and sign out the devices logged in to your account, on your homeserver's website.",
|
||||
Icons.devices,
|
||||
url,
|
||||
),
|
||||
.new(
|
||||
title: "Reset Encryption Identity",
|
||||
description: "Replace your encryption identity and recovery key with new ones, if you've lost your recovery key or your encryption is broken.",
|
||||
|
|
@ -811,6 +861,14 @@ class SettingsSectionsController
|
|||
),
|
||||
icon: Icons.key_off,
|
||||
),
|
||||
if (accountManagement?.urlFor(.accountDeactivate) case final url?)
|
||||
link(
|
||||
"Deactivate Account",
|
||||
"Permanently deactivate your account, on your homeserver's website. This can't be undone.",
|
||||
Icons.no_accounts,
|
||||
url,
|
||||
danger: true,
|
||||
),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
|
|
|
|||
19
lib/models/account_management.dart
Normal file
19
lib/models/account_management.dart
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
|
||||
// Later names first, then those from MSC4191, which servers may still use
|
||||
enum AccountManagementAction(final List<String> names) {
|
||||
devicesList(["org.matrix.devices_list", "org.matrix.sessions_list"]),
|
||||
accountDeactivate(["org.matrix.account_deactivate"]),
|
||||
crossSigningReset(["org.matrix.cross_signing_reset"]),
|
||||
}
|
||||
|
||||
class const AccountManagement(final Uri uri, final ISet<String> actions) {
|
||||
Uri? urlFor(AccountManagementAction action) =>
|
||||
switch (action.names.firstWhereOrNull(actions.contains)) {
|
||||
final name? => uri.replace(
|
||||
queryParameters: {...uri.queryParameters, "action": name},
|
||||
),
|
||||
null => null,
|
||||
};
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
|
|||
import "package:material_ui/material_ui.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/controllers/account_management.dart";
|
||||
import "package:nexus/helpers/launch_helper.dart";
|
||||
import "package:nexus/models/gomuks_exception.dart";
|
||||
import "package:nexus/models/recovery_key.dart";
|
||||
|
|
@ -41,15 +41,10 @@ class const RecoveryKeySheet(
|
|||
// Open the approval page straight away the first time.
|
||||
if (!needsApproval.value) {
|
||||
needsApproval.value = true;
|
||||
final homeserver = ref
|
||||
.read(ClientStateController.provider)
|
||||
?.homeserverUrl;
|
||||
final url = homeserver == null
|
||||
? null
|
||||
: await ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.getCrossSigningResetUrl(.parse(homeserver))
|
||||
.onError((_, _) => null);
|
||||
final url = await ref
|
||||
.read(AccountManagementController.provider.future)
|
||||
.then((management) => management?.urlFor(.crossSigningReset))
|
||||
.onError((_, _) => null);
|
||||
if (!context.mounted || url == null) return;
|
||||
|
||||
approvalUrl.value = url;
|
||||
|
|
|
|||
Loading…
Reference in a new issue