chore: Follow up manage room aliases

pull/1031/head
krille-chan 1 year ago
parent 0684bb63ac
commit 4dbe85b278
No known key found for this signature in database

@ -2509,6 +2509,8 @@
"@passwordIsWrong": {}, "@passwordIsWrong": {},
"publicLink": "Public link", "publicLink": "Public link",
"@publicLink": {}, "@publicLink": {},
"publicLinks": "Public links",
"createNewLink": "Create new link",
"joinSpace": "Join space", "joinSpace": "Join space",
"@joinSpace": {}, "@joinSpace": {},
"publicSpaces": "Public spaces", "publicSpaces": "Public spaces",

@ -152,31 +152,76 @@ class ChatAccessSettingsController extends State<ChatAccessSettings> {
); );
} }
void setCanonicalAlias() async { Future<void> addAlias() async {
final domain = room.client.userID?.domain;
if (domain == null) {
throw Exception('userID or domain is null! This should never happen.');
}
final input = await showTextInputDialog( final input = await showTextInputDialog(
context: context, context: context,
title: L10n.of(context)!.editRoomAliases, title: L10n.of(context)!.editRoomAliases,
cancelLabel: L10n.of(context)!.cancel,
okLabel: L10n.of(context)!.ok,
textFields: [ textFields: [
DialogTextField( DialogTextField(
prefixText: '#', prefixText: '#',
suffixText: room.client.userID!.domain!, suffixText: domain,
initialText: room.canonicalAlias.localpart, hintText: L10n.of(context)!.alias,
), ),
], ],
); );
final newAliasLocalpart = input?.singleOrNull?.trim(); final aliasLocalpart = input?.singleOrNull?.trim();
if (newAliasLocalpart == null || newAliasLocalpart.isEmpty) return; if (aliasLocalpart == null || aliasLocalpart.isEmpty) return;
final alias = '#$aliasLocalpart:$domain';
final result = await showFutureLoadingDialog(
context: context,
future: () => room.client.setRoomAlias(alias, room.id),
);
if (result.error != null) return;
final canonicalAliasConsent = await showOkCancelAlertDialog(
context: context,
title: L10n.of(context)!.setAsCanonicalAlias,
message: alias,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.no,
);
final altAliases = room
.getState(EventTypes.RoomCanonicalAlias)
?.content
.tryGetList<String>('alt_aliases')
?.toSet() ??
{};
if (room.canonicalAlias.isNotEmpty) altAliases.add(room.canonicalAlias);
altAliases.add(alias);
if (canonicalAliasConsent == OkCancelResult.ok) {
altAliases.remove(alias);
} else {
altAliases.remove(room.canonicalAlias);
}
await showFutureLoadingDialog( await showFutureLoadingDialog(
context: context, context: context,
future: () => room.setCanonicalAlias( future: () => room.client.setRoomStateWithKey(
'#$newAliasLocalpart:${room.client.userID!.domain!}', room.id,
EventTypes.RoomCanonicalAlias,
'',
{
'alias': canonicalAliasConsent == OkCancelResult.ok
? alias
: room.canonicalAlias,
if (altAliases.isNotEmpty) 'alt_aliases': altAliases.toList(),
},
), ),
); );
} }
void deleteAlias(String alias) => showFutureLoadingDialog(
context: context,
future: () => room.client.deleteRoomAlias(alias),
);
void setChatVisibilityOnDirectory(bool? visibility) async { void setChatVisibilityOnDirectory(bool? visibility) async {
if (visibility == null) return; if (visibility == null) return;
setState(() { setState(() {

@ -23,7 +23,14 @@ class ChatAccessSettingsPageView extends StatelessWidget {
body: MaxWidthBody( body: MaxWidthBody(
child: StreamBuilder<Object>( child: StreamBuilder<Object>(
stream: room.onUpdate.stream, stream: room.onUpdate.stream,
builder: (context, snapshot) => Column( builder: (context, snapshot) {
final canonicalAlias = room.canonicalAlias;
final altAliases = room
.getState(EventTypes.RoomCanonicalAlias)
?.content
.tryGetList<String>('alt_aliases') ??
[];
return Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
ListTile( ListTile(
@ -58,16 +65,16 @@ class ChatAccessSettingsPageView extends StatelessWidget {
), ),
), ),
), ),
for (final joinRule for (final joinRule in JoinRules.values)
in JoinRules.values..remove(JoinRules.private)) if (joinRule != JoinRules.private)
RadioListTile<JoinRules>.adaptive( RadioListTile<JoinRules>.adaptive(
title: Text( title: Text(
joinRule.localizedString(L10n.of(context)!), joinRule.localizedString(L10n.of(context)!),
), ),
value: joinRule, value: joinRule,
groupValue: room.joinRules, groupValue: room.joinRules,
onChanged: onChanged: controller.joinRulesLoading ||
controller.joinRulesLoading || !room.canChangeJoinRules !room.canChangeJoinRules
? null ? null
: controller.setJoinRule, : controller.setJoinRule,
), ),
@ -86,8 +93,9 @@ class ChatAccessSettingsPageView extends StatelessWidget {
for (final guestAccess in GuestAccess.values) for (final guestAccess in GuestAccess.values)
RadioListTile<GuestAccess>.adaptive( RadioListTile<GuestAccess>.adaptive(
title: Text( title: Text(
guestAccess guestAccess.getLocalizedString(
.getLocalizedString(MatrixLocals(L10n.of(context)!)), MatrixLocals(L10n.of(context)!),
),
), ),
value: guestAccess, value: guestAccess,
groupValue: room.guestAccess, groupValue: room.guestAccess,
@ -97,48 +105,50 @@ class ChatAccessSettingsPageView extends StatelessWidget {
: controller.setGuestAccess, : controller.setGuestAccess,
), ),
Divider(color: Theme.of(context).dividerColor), Divider(color: Theme.of(context).dividerColor),
FutureBuilder( ListTile(
future: room.client.getRoomVisibilityOnDirectory(room.id),
builder: (context, snapshot) => SwitchListTile.adaptive(
value: snapshot.data == Visibility.public,
title: Text( title: Text(
L10n.of(context)!.chatCanBeDiscoveredViaSearchOnServer( L10n.of(context)!.publicLinks,
room.client.userID!.domain!, style: TextStyle(
color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold,
), ),
), ),
onChanged: controller.setChatVisibilityOnDirectory, trailing: IconButton(
icon: const Icon(Icons.add_outlined),
tooltip: L10n.of(context)!.createNewLink,
onPressed: controller.addAlias,
), ),
), ),
ListTile( if (canonicalAlias.isNotEmpty)
title: Text(L10n.of(context)!.publicLink), _AliasListTile(
subtitle: room.canonicalAlias.isEmpty alias: canonicalAlias,
? Text( onDelete: room.canChangeStateEvent(
L10n.of(context)!.noPublicLinkHasBeenCreatedYet, EventTypes.RoomCanonicalAlias,
style: const TextStyle( )
fontStyle: FontStyle.italic, ? () => controller.deleteAlias(canonicalAlias)
: null,
isCanonicalAlias: true,
), ),
for (final alias in altAliases)
_AliasListTile(
alias: alias,
onDelete: room.canChangeStateEvent(
EventTypes.RoomCanonicalAlias,
) )
: Text( ? () => controller.deleteAlias(alias)
'https://matrix.to/#/${room.canonicalAlias}', : null,
style: TextStyle(
decoration: TextDecoration.underline,
color: Theme.of(context).colorScheme.primary,
), ),
Divider(color: Theme.of(context).dividerColor),
FutureBuilder(
future: room.client.getRoomVisibilityOnDirectory(room.id),
builder: (context, snapshot) => SwitchListTile.adaptive(
value: snapshot.data == Visibility.public,
title: Text(
L10n.of(context)!.chatCanBeDiscoveredViaSearchOnServer(
room.client.userID!.domain!,
), ),
onTap: room.canChangeStateEvent(EventTypes.RoomCanonicalAlias)
? controller.setCanonicalAlias
: null,
trailing: room.canonicalAlias.isEmpty
? const Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0),
child: Icon(Icons.add),
)
: IconButton(
icon: Icon(Icons.adaptive.share_outlined),
onPressed: () => FluffyShare.share(
'https://matrix.to/#/${room.canonicalAlias}',
context,
), ),
onChanged: controller.setChatVisibilityOnDirectory,
), ),
), ),
], ],
@ -167,9 +177,56 @@ class ChatAccessSettingsPageView extends StatelessWidget {
: null, : null,
), ),
], ],
);
},
),
),
);
}
}
class _AliasListTile extends StatelessWidget {
const _AliasListTile({
required this.alias,
required this.onDelete,
this.isCanonicalAlias = false,
});
final String alias;
final void Function()? onDelete;
final bool isCanonicalAlias;
@override
Widget build(BuildContext context) {
return ListTile(
title: Row(
children: [
TextButton.icon(
onPressed: () => FluffyShare.share(
'https://matrix.to/#/$alias',
context,
),
icon: isCanonicalAlias
? const Icon(Icons.star)
: const Icon(Icons.link_outlined),
label: SelectableText(
'https://matrix.to/#/$alias',
style: TextStyle(
decoration: TextDecoration.underline,
decorationColor: Theme.of(context).colorScheme.primary,
color: Theme.of(context).colorScheme.primary,
), ),
), ),
), ),
const Spacer(),
],
),
trailing: onDelete != null
? IconButton(
icon: const Icon(Icons.delete_outlined),
onPressed: onDelete,
)
: null,
); );
} }
} }

Loading…
Cancel
Save