mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-16 00:49:31 +01:00
fix: Properly handle url encoding in matrix.to URLs
This commit is contained in:
parent
e2791de69b
commit
baccd0aa42
@ -1,24 +1,33 @@
|
|||||||
|
import '../app_config.dart';
|
||||||
|
|
||||||
extension MatrixIdentifierStringExtension on String {
|
extension MatrixIdentifierStringExtension on String {
|
||||||
/// Separates room identifiers with an event id and possibly a query parameter into its components.
|
/// Separates room identifiers with an event id and possibly a query parameter into its components.
|
||||||
MatrixIdentifierStringExtensionResults parseIdentifierIntoParts() {
|
MatrixIdentifierStringExtensionResults parseIdentifierIntoParts() {
|
||||||
final match = RegExp(r'^([#!][^:]*:[^\/?]*)(?:\/(\$[^?]*))?(?:\?(.*))?$')
|
final isUrl = startsWith(AppConfig.inviteLinkPrefix);
|
||||||
.firstMatch(this);
|
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) {
|
if (match == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return MatrixIdentifierStringExtensionResults(
|
return MatrixIdentifierStringExtensionResults(
|
||||||
roomIdOrAlias: match.group(1),
|
primaryIdentifier: match.group(1),
|
||||||
eventId: match.group(2),
|
secondaryIdentifier: match.group(2),
|
||||||
queryString: match.group(3),
|
queryString: match.group(3)?.isNotEmpty ?? false ? match.group(3) : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MatrixIdentifierStringExtensionResults {
|
class MatrixIdentifierStringExtensionResults {
|
||||||
final String roomIdOrAlias;
|
final String primaryIdentifier;
|
||||||
final String eventId;
|
final String secondaryIdentifier;
|
||||||
final String queryString;
|
final String queryString;
|
||||||
|
|
||||||
MatrixIdentifierStringExtensionResults(
|
MatrixIdentifierStringExtensionResults(
|
||||||
{this.roomIdOrAlias, this.eventId, this.queryString});
|
{this.primaryIdentifier, this.secondaryIdentifier, this.queryString});
|
||||||
}
|
}
|
||||||
|
@ -25,16 +25,18 @@ class UrlLauncher {
|
|||||||
|
|
||||||
void openMatrixToUrl() async {
|
void openMatrixToUrl() async {
|
||||||
final matrix = Matrix.of(context);
|
final matrix = Matrix.of(context);
|
||||||
final identifier = url.replaceAll(AppConfig.inviteLinkPrefix, '');
|
// The identifier might be a matrix.to url and needs escaping. Or, it might have multiple
|
||||||
if (identifier[0] == '#' || identifier[0] == '!') {
|
// identifiers (room id & event id), or it might also have a query part.
|
||||||
// sometimes we have identifiers which have an event id and additional query parameters
|
// All this needs parsing.
|
||||||
// we want to separate those.
|
final identityParts = url.parseIdentifierIntoParts();
|
||||||
final identityParts = identifier.parseIdentifierIntoParts();
|
|
||||||
if (identityParts == null) {
|
if (identityParts == null) {
|
||||||
return; // no match, nothing to do
|
return; // no match, nothing to do
|
||||||
}
|
}
|
||||||
final roomIdOrAlias = identityParts.roomIdOrAlias;
|
if (identityParts.primaryIdentifier.sigil == '#' ||
|
||||||
final event = identityParts.eventId;
|
identityParts.primaryIdentifier.sigil == '!') {
|
||||||
|
// we got a room! Let's open that one
|
||||||
|
final roomIdOrAlias = identityParts.primaryIdentifier;
|
||||||
|
final event = identityParts.secondaryIdentifier;
|
||||||
final query = identityParts.queryString;
|
final query = identityParts.queryString;
|
||||||
var room = matrix.client.getRoomByAlias(roomIdOrAlias) ??
|
var room = matrix.client.getRoomByAlias(roomIdOrAlias) ??
|
||||||
matrix.client.getRoomById(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
|
// 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
|
// deduplicate servers added via alias lookup and query parameter
|
||||||
var servers = <String>{};
|
var servers = <String>{};
|
||||||
if (room == null && roomIdOrAlias.startsWith('#')) {
|
if (room == null && roomIdOrAlias.sigil == '#') {
|
||||||
// we were unable to find the room locally...so resolve it
|
// we were unable to find the room locally...so resolve it
|
||||||
final response = await showFutureLoadingDialog(
|
final response = await showFutureLoadingDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@ -81,6 +83,11 @@ class UrlLauncher {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (roomIdOrAlias.sigil == '!') {
|
if (roomIdOrAlias.sigil == '!') {
|
||||||
|
if (await showOkCancelAlertDialog(
|
||||||
|
context: context,
|
||||||
|
title: 'Join room $roomIdOrAlias',
|
||||||
|
) ==
|
||||||
|
OkCancelResult.ok) {
|
||||||
roomId = roomIdOrAlias;
|
roomId = roomIdOrAlias;
|
||||||
final response = await showFutureLoadingDialog(
|
final response = await showFutureLoadingDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@ -100,20 +107,22 @@ class UrlLauncher {
|
|||||||
context, ChatView(response.result, scrollToEventId: event)),
|
context, ChatView(response.result, scrollToEventId: event)),
|
||||||
(r) => r.isFirst,
|
(r) => r.isFirst,
|
||||||
);
|
);
|
||||||
} else if (identifier.sigil == '#') {
|
}
|
||||||
|
} else {
|
||||||
await Navigator.of(context).pushAndRemoveUntil(
|
await Navigator.of(context).pushAndRemoveUntil(
|
||||||
AppRoute.defaultRoute(
|
AppRoute.defaultRoute(
|
||||||
context,
|
context,
|
||||||
DiscoverView(alias: identifier),
|
DiscoverView(alias: roomIdOrAlias),
|
||||||
),
|
),
|
||||||
(r) => r.isFirst,
|
(r) => r.isFirst,
|
||||||
);
|
);
|
||||||
} else if (identifier.sigil == '@') {
|
}
|
||||||
|
} else if (identityParts.primaryIdentifier.sigil == '@') {
|
||||||
final user = User(
|
final user = User(
|
||||||
identifier,
|
identityParts.primaryIdentifier,
|
||||||
room: Room(id: '', client: matrix.client),
|
room: Room(id: '', client: matrix.client),
|
||||||
);
|
);
|
||||||
var roomId = matrix.client.getDirectChatFromUserId(identifier);
|
var roomId = matrix.client.getDirectChatFromUserId(user.id);
|
||||||
if (roomId != null) {
|
if (roomId != null) {
|
||||||
await Navigator.pushAndRemoveUntil(
|
await Navigator.pushAndRemoveUntil(
|
||||||
context,
|
context,
|
||||||
@ -125,7 +134,7 @@ class UrlLauncher {
|
|||||||
|
|
||||||
if (await showOkCancelAlertDialog(
|
if (await showOkCancelAlertDialog(
|
||||||
context: context,
|
context: context,
|
||||||
title: 'Message user $identifier',
|
title: 'Message user ${user.id}',
|
||||||
) ==
|
) ==
|
||||||
OkCancelResult.ok) {
|
OkCancelResult.ok) {
|
||||||
roomId = (await showFutureLoadingDialog(
|
roomId = (await showFutureLoadingDialog(
|
||||||
@ -145,5 +154,4 @@ class UrlLauncher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,27 +5,41 @@ void main() {
|
|||||||
group('Matrix Identifier String Extension', () {
|
group('Matrix Identifier String Extension', () {
|
||||||
test('parseIdentifierIntoParts', () {
|
test('parseIdentifierIntoParts', () {
|
||||||
var res = '#alias:beep'.parseIdentifierIntoParts();
|
var res = '#alias:beep'.parseIdentifierIntoParts();
|
||||||
expect(res.roomIdOrAlias, '#alias:beep');
|
expect(res.primaryIdentifier, '#alias:beep');
|
||||||
expect(res.eventId, null);
|
expect(res.secondaryIdentifier, null);
|
||||||
expect(res.queryString, null);
|
expect(res.queryString, null);
|
||||||
res = 'blha'.parseIdentifierIntoParts();
|
res = 'blha'.parseIdentifierIntoParts();
|
||||||
expect(res, null);
|
expect(res, null);
|
||||||
res = '#alias:beep/\$event'.parseIdentifierIntoParts();
|
res = '#alias:beep/\$event'.parseIdentifierIntoParts();
|
||||||
expect(res.roomIdOrAlias, '#alias:beep');
|
expect(res.primaryIdentifier, '#alias:beep');
|
||||||
expect(res.eventId, '\$event');
|
expect(res.secondaryIdentifier, '\$event');
|
||||||
expect(res.queryString, null);
|
expect(res.queryString, null);
|
||||||
res = '#alias:beep?blubb'.parseIdentifierIntoParts();
|
res = '#alias:beep?blubb'.parseIdentifierIntoParts();
|
||||||
expect(res.roomIdOrAlias, '#alias:beep');
|
expect(res.primaryIdentifier, '#alias:beep');
|
||||||
expect(res.eventId, null);
|
expect(res.secondaryIdentifier, null);
|
||||||
expect(res.queryString, 'blubb');
|
expect(res.queryString, 'blubb');
|
||||||
res = '#alias:beep/\$event?blubb'.parseIdentifierIntoParts();
|
res = '#alias:beep/\$event?blubb'.parseIdentifierIntoParts();
|
||||||
expect(res.roomIdOrAlias, '#alias:beep');
|
expect(res.primaryIdentifier, '#alias:beep');
|
||||||
expect(res.eventId, '\$event');
|
expect(res.secondaryIdentifier, '\$event');
|
||||||
expect(res.queryString, 'blubb');
|
expect(res.queryString, 'blubb');
|
||||||
res = '#/\$?:beep/\$event?blubb?b'.parseIdentifierIntoParts();
|
res = '#/\$?:beep/\$event?blubb?b'.parseIdentifierIntoParts();
|
||||||
expect(res.roomIdOrAlias, '#/\$?:beep');
|
expect(res.primaryIdentifier, '#/\$?:beep');
|
||||||
expect(res.eventId, '\$event');
|
expect(res.secondaryIdentifier, '\$event');
|
||||||
expect(res.queryString, 'blubb?b');
|
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