mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-30 16:29:30 +01:00
Merge branch 'soru/fix-urlencoded-matrix-to' into 'main'
fix: Properly handle url encoding in matrix.to URLs See merge request famedly/fluffychat!322
This commit is contained in:
commit
7e450fce43
@ -1,24 +1,33 @@
|
||||
import '../app_config.dart';
|
||||
|
||||
extension MatrixIdentifierStringExtension on String {
|
||||
/// Separates room identifiers with an event id and possibly a query parameter into its components.
|
||||
MatrixIdentifierStringExtensionResults parseIdentifierIntoParts() {
|
||||
final match = RegExp(r'^([#!][^:]*:[^\/?]*)(?:\/(\$[^?]*))?(?:\?(.*))?$')
|
||||
.firstMatch(this);
|
||||
final isUrl = startsWith(AppConfig.inviteLinkPrefix);
|
||||
var s = this;
|
||||
if (isUrl) {
|
||||
// as we decode a component we may only call it on the url part *before* the "query" part
|
||||
final parts = replaceFirst(AppConfig.inviteLinkPrefix, '').split('?');
|
||||
s = Uri.decodeComponent(parts.removeAt(0)) + '?' + parts.join('?');
|
||||
}
|
||||
final match = RegExp(r'^([#!@+][^:]*:[^\/?]*)(?:\/(\$[^?]*))?(?:\?(.*))?$')
|
||||
.firstMatch(s);
|
||||
if (match == null) {
|
||||
return null;
|
||||
}
|
||||
return MatrixIdentifierStringExtensionResults(
|
||||
roomIdOrAlias: match.group(1),
|
||||
eventId: match.group(2),
|
||||
queryString: match.group(3),
|
||||
primaryIdentifier: match.group(1),
|
||||
secondaryIdentifier: match.group(2),
|
||||
queryString: match.group(3)?.isNotEmpty ?? false ? match.group(3) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MatrixIdentifierStringExtensionResults {
|
||||
final String roomIdOrAlias;
|
||||
final String eventId;
|
||||
final String primaryIdentifier;
|
||||
final String secondaryIdentifier;
|
||||
final String queryString;
|
||||
|
||||
MatrixIdentifierStringExtensionResults(
|
||||
{this.roomIdOrAlias, this.eventId, this.queryString});
|
||||
{this.primaryIdentifier, this.secondaryIdentifier, this.queryString});
|
||||
}
|
||||
|
@ -25,16 +25,18 @@ class UrlLauncher {
|
||||
|
||||
void openMatrixToUrl() async {
|
||||
final matrix = Matrix.of(context);
|
||||
final identifier = url.replaceAll(AppConfig.inviteLinkPrefix, '');
|
||||
if (identifier[0] == '#' || identifier[0] == '!') {
|
||||
// sometimes we have identifiers which have an event id and additional query parameters
|
||||
// we want to separate those.
|
||||
final identityParts = identifier.parseIdentifierIntoParts();
|
||||
// The identifier might be a matrix.to url and needs escaping. Or, it might have multiple
|
||||
// identifiers (room id & event id), or it might also have a query part.
|
||||
// All this needs parsing.
|
||||
final identityParts = url.parseIdentifierIntoParts();
|
||||
if (identityParts == null) {
|
||||
return; // no match, nothing to do
|
||||
}
|
||||
final roomIdOrAlias = identityParts.roomIdOrAlias;
|
||||
final event = identityParts.eventId;
|
||||
if (identityParts.primaryIdentifier.sigil == '#' ||
|
||||
identityParts.primaryIdentifier.sigil == '!') {
|
||||
// we got a room! Let's open that one
|
||||
final roomIdOrAlias = identityParts.primaryIdentifier;
|
||||
final event = identityParts.secondaryIdentifier;
|
||||
final query = identityParts.queryString;
|
||||
var room = matrix.client.getRoomByAlias(roomIdOrAlias) ??
|
||||
matrix.client.getRoomById(roomIdOrAlias);
|
||||
@ -42,7 +44,7 @@ class UrlLauncher {
|
||||
// we make the servers a set and later on convert to a list, so that we can easily
|
||||
// deduplicate servers added via alias lookup and query parameter
|
||||
var servers = <String>{};
|
||||
if (room == null && roomIdOrAlias.startsWith('#')) {
|
||||
if (room == null && roomIdOrAlias.sigil == '#') {
|
||||
// we were unable to find the room locally...so resolve it
|
||||
final response = await showFutureLoadingDialog(
|
||||
context: context,
|
||||
@ -81,6 +83,11 @@ class UrlLauncher {
|
||||
return;
|
||||
}
|
||||
if (roomIdOrAlias.sigil == '!') {
|
||||
if (await showOkCancelAlertDialog(
|
||||
context: context,
|
||||
title: 'Join room $roomIdOrAlias',
|
||||
) ==
|
||||
OkCancelResult.ok) {
|
||||
roomId = roomIdOrAlias;
|
||||
final response = await showFutureLoadingDialog(
|
||||
context: context,
|
||||
@ -100,20 +107,22 @@ class UrlLauncher {
|
||||
context, ChatView(response.result, scrollToEventId: event)),
|
||||
(r) => r.isFirst,
|
||||
);
|
||||
} else if (identifier.sigil == '#') {
|
||||
}
|
||||
} else {
|
||||
await Navigator.of(context).pushAndRemoveUntil(
|
||||
AppRoute.defaultRoute(
|
||||
context,
|
||||
DiscoverView(alias: identifier),
|
||||
DiscoverView(alias: roomIdOrAlias),
|
||||
),
|
||||
(r) => r.isFirst,
|
||||
);
|
||||
} else if (identifier.sigil == '@') {
|
||||
}
|
||||
} else if (identityParts.primaryIdentifier.sigil == '@') {
|
||||
final user = User(
|
||||
identifier,
|
||||
identityParts.primaryIdentifier,
|
||||
room: Room(id: '', client: matrix.client),
|
||||
);
|
||||
var roomId = matrix.client.getDirectChatFromUserId(identifier);
|
||||
var roomId = matrix.client.getDirectChatFromUserId(user.id);
|
||||
if (roomId != null) {
|
||||
await Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
@ -125,7 +134,7 @@ class UrlLauncher {
|
||||
|
||||
if (await showOkCancelAlertDialog(
|
||||
context: context,
|
||||
title: 'Message user $identifier',
|
||||
title: 'Message user ${user.id}',
|
||||
) ==
|
||||
OkCancelResult.ok) {
|
||||
roomId = (await showFutureLoadingDialog(
|
||||
@ -145,5 +154,4 @@ class UrlLauncher {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,27 +5,41 @@ void main() {
|
||||
group('Matrix Identifier String Extension', () {
|
||||
test('parseIdentifierIntoParts', () {
|
||||
var res = '#alias:beep'.parseIdentifierIntoParts();
|
||||
expect(res.roomIdOrAlias, '#alias:beep');
|
||||
expect(res.eventId, null);
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, null);
|
||||
expect(res.queryString, null);
|
||||
res = 'blha'.parseIdentifierIntoParts();
|
||||
expect(res, null);
|
||||
res = '#alias:beep/\$event'.parseIdentifierIntoParts();
|
||||
expect(res.roomIdOrAlias, '#alias:beep');
|
||||
expect(res.eventId, '\$event');
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, '\$event');
|
||||
expect(res.queryString, null);
|
||||
res = '#alias:beep?blubb'.parseIdentifierIntoParts();
|
||||
expect(res.roomIdOrAlias, '#alias:beep');
|
||||
expect(res.eventId, null);
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, null);
|
||||
expect(res.queryString, 'blubb');
|
||||
res = '#alias:beep/\$event?blubb'.parseIdentifierIntoParts();
|
||||
expect(res.roomIdOrAlias, '#alias:beep');
|
||||
expect(res.eventId, '\$event');
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, '\$event');
|
||||
expect(res.queryString, 'blubb');
|
||||
res = '#/\$?:beep/\$event?blubb?b'.parseIdentifierIntoParts();
|
||||
expect(res.roomIdOrAlias, '#/\$?:beep');
|
||||
expect(res.eventId, '\$event');
|
||||
expect(res.primaryIdentifier, '#/\$?:beep');
|
||||
expect(res.secondaryIdentifier, '\$event');
|
||||
expect(res.queryString, 'blubb?b');
|
||||
|
||||
res = 'https://matrix.to/#/#alias:beep'.parseIdentifierIntoParts();
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, null);
|
||||
expect(res.queryString, null);
|
||||
res = 'https://matrix.to/#/%23alias%3abeep'.parseIdentifierIntoParts();
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, null);
|
||||
expect(res.queryString, null);
|
||||
res = 'https://matrix.to/#/%23alias%3abeep?boop%F0%9F%A7%A1%F0%9F%A6%8A'
|
||||
.parseIdentifierIntoParts();
|
||||
expect(res.primaryIdentifier, '#alias:beep');
|
||||
expect(res.secondaryIdentifier, null);
|
||||
expect(res.queryString, 'boop%F0%9F%A7%A1%F0%9F%A6%8A');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user