Add support for jumping to event links (#103)
Fixes #78. Reviewed-on: #103
This commit is contained in:
parent
597112f5d3
commit
1c4bf99be2
16 changed files with 169 additions and 113 deletions
|
|
@ -202,7 +202,7 @@ extension BuildEventOptions on Event {
|
|||
await Clipboard.setData(
|
||||
ClipboardData(
|
||||
text:
|
||||
"matrix:roomid/${room.metadata?.id.substring(1)}/e/$eventId$vias",
|
||||
"matrix:roomid/${room.metadata?.id.substring(1)}/e/${eventId.substring(1)}$vias",
|
||||
),
|
||||
);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,27 +1,28 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:flutter_riverpod/experimental/mutation.dart";
|
||||
import "package:nexus/controllers/contextual_event.dart";
|
||||
import "package:nexus/controllers/key.dart";
|
||||
import "package:nexus/controllers/spaces.dart";
|
||||
import "package:nexus/models/space.dart";
|
||||
|
||||
extension FocusRoom on MutationTarget {
|
||||
Future<bool> focusRoom(String id) async {
|
||||
Future<bool> focusRoom(String roomId, [int? eventRowId]) async {
|
||||
final spaces = container.read(SpacesController.provider);
|
||||
|
||||
if (spaces.firstWhereOrNull((space) => space.id == id) case Space _?) {
|
||||
if (spaces.firstWhereOrNull((space) => space.id == roomId) case Space _?) {
|
||||
await container
|
||||
.read(KeyController.provider(KeyController.spaceKey).notifier)
|
||||
.set(id);
|
||||
.set(roomId);
|
||||
return true;
|
||||
}
|
||||
|
||||
final parent = spaces.firstWhereOrNull(
|
||||
(space) =>
|
||||
space.children.any((room) => room.metadata?.id == id) ||
|
||||
space.children.any((room) => room.metadata?.id == roomId) ||
|
||||
space.subSpaces.any(
|
||||
(sub) =>
|
||||
sub.room.metadata?.id == id ||
|
||||
sub.children.any((room) => room.metadata?.id == id),
|
||||
sub.room.metadata?.id == roomId ||
|
||||
sub.children.any((room) => room.metadata?.id == roomId),
|
||||
),
|
||||
);
|
||||
if (parent == null) return false;
|
||||
|
|
@ -32,7 +33,13 @@ extension FocusRoom on MutationTarget {
|
|||
|
||||
await container
|
||||
.read(KeyController.provider(KeyController.roomKey).notifier)
|
||||
.set(id);
|
||||
.set(roomId);
|
||||
|
||||
if (eventRowId != null) {
|
||||
container
|
||||
.read(ContextualEventController.provider(roomId).notifier)
|
||||
.set(eventRowId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
70
lib/helpers/extensions/get_link_info.dart
Normal file
70
lib/helpers/extensions/get_link_info.dart
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
|
||||
/// A parsed Matrix link.
|
||||
///
|
||||
/// [identifier] is the room ID, room alias or user ID (e.g. "#room:matrix.org"),
|
||||
/// [eventId] is set for event permalinks (e.g. "$abc123"), and [via] holds any
|
||||
/// `via` servers from the link.
|
||||
typedef LinkInfo = ({String identifier, String? eventId, IList<String> via});
|
||||
|
||||
extension GetLinkInfo on String {
|
||||
/// Parses this string as a Matrix link.
|
||||
///
|
||||
/// Supports:
|
||||
/// - https://matrix.to/#/{id}[/{eventId}][?via=...]
|
||||
/// - matrix:roomid/{id}[/e/{eventId}][?via=...]
|
||||
/// - matrix:r/{alias}[/e/{eventId}][?via=...]
|
||||
/// - matrix:u/{userId}
|
||||
///
|
||||
/// Returns null if this is not a Matrix link.
|
||||
LinkInfo? get linkInfo {
|
||||
final uri = Uri.tryParse(trim());
|
||||
if (uri == null) return null;
|
||||
|
||||
if ((uri.scheme == "http" || uri.scheme == "https") &&
|
||||
uri.host == "matrix.to") {
|
||||
// matrix.to keeps everything, including the query, in the fragment,
|
||||
// so parse the fragment as its own URI.
|
||||
final inner = Uri.tryParse(uri.fragment);
|
||||
final segments = inner?.pathSegments.where((s) => s.isNotEmpty).toList();
|
||||
if (inner == null || segments == null || segments.isEmpty) return null;
|
||||
|
||||
return (
|
||||
identifier: segments.first,
|
||||
eventId: segments.length > 1 && segments[1].startsWith(r"$")
|
||||
? segments[1]
|
||||
: null,
|
||||
via: .new(inner.queryParametersAll["via"] ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
if (uri.scheme == "matrix") {
|
||||
final segments = uri.pathSegments;
|
||||
if (segments.length < 2 || segments[1].isEmpty) return null;
|
||||
|
||||
final sigil = switch (segments[0].toLowerCase()) {
|
||||
"r" => "#",
|
||||
"roomid" => "!",
|
||||
"u" => "@",
|
||||
_ => null,
|
||||
};
|
||||
if (sigil == null) return null;
|
||||
|
||||
// Event segment is "e/{eventId}" (without the "$" sigil) and is
|
||||
// only valid on room links.
|
||||
final hasEvent =
|
||||
sigil != "@" &&
|
||||
segments.length >= 4 &&
|
||||
segments[2].toLowerCase() == "e" &&
|
||||
segments[3].isNotEmpty;
|
||||
|
||||
return (
|
||||
identifier: "$sigil${segments[1]}",
|
||||
eventId: hasEvent ? "\$${segments[3]}" : null,
|
||||
via: .new(uri.queryParametersAll["via"] ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
|
||||
extension LinkToMention on String {
|
||||
/// Extracts a Matrix identifier from this string.
|
||||
///
|
||||
/// Supports:
|
||||
/// - https://matrix.to/#/...
|
||||
/// - matrix:roomid/...
|
||||
/// - matrix:r/...
|
||||
/// - matrix:u/...
|
||||
///
|
||||
/// Returns the decoded identifier (e.g. "#room:matrix.org")
|
||||
/// or null if this is not a Matrix link.
|
||||
/// TODO: Needs to be reworked to handle event links. Might be worth just rewriting, I don't like this code.
|
||||
String? get mention {
|
||||
final trimmed = trim();
|
||||
|
||||
final matrixTo = RegExp(
|
||||
r"^https?://matrix\.to/#/(.[^/?#]+)",
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
final matrixToMatch = matrixTo.firstMatch(trimmed);
|
||||
if (matrixToMatch != null) {
|
||||
return Uri.decodeComponent(matrixToMatch.group(1)!);
|
||||
}
|
||||
|
||||
if (trimmed.toLowerCase().startsWith("matrix:")) {
|
||||
try {
|
||||
final uri = Uri.parse(trimmed);
|
||||
|
||||
if (uri.pathSegments.isNotEmpty) {
|
||||
final identifier = uri.pathSegments.last;
|
||||
if (identifier.isNotEmpty) {
|
||||
return "${switch (uri.pathSegments.firstOrNull) {
|
||||
"r" => "#",
|
||||
"roomid" => "!",
|
||||
"u" => "@",
|
||||
_ => "",
|
||||
}}${Uri.decodeComponent(identifier)}";
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
IList<String> get via =>
|
||||
.new(Uri.tryParse(replaceAll("/#", ""))?.queryParametersAll["via"] ?? []);
|
||||
}
|
||||
Loading…
Reference in a new issue