From cc315e919a86e02c400984400ea444c8434c9ce7 Mon Sep 17 00:00:00 2001 From: dennis Date: Thu, 16 Jul 2026 04:33:24 +0800 Subject: [PATCH] Handler for lemmyverse.link shared links. --- .../shell/navigation/link_navigation_utils.dart | 2 +- .../domain/utils/instance_link_utils.dart | 16 ++++++++++++++++ .../utils/threadiverse_link_parser_utils.dart | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/src/app/shell/navigation/link_navigation_utils.dart b/lib/src/app/shell/navigation/link_navigation_utils.dart index 2dee8dbe1..c58bbb0b4 100644 --- a/lib/src/app/shell/navigation/link_navigation_utils.dart +++ b/lib/src/app/shell/navigation/link_navigation_utils.dart @@ -157,7 +157,7 @@ void handleLink(BuildContext context, {required String url, bool forceOpenInBrow } // Try navigating to post - int? postId = await getLemmyPostId(context, url); + int? postId = await getLemmyPostId(context, checkEmbeddedInstance(url)); if (postId != null) { try { // Show the loading page while we fetch the post diff --git a/lib/src/features/instance/domain/utils/instance_link_utils.dart b/lib/src/features/instance/domain/utils/instance_link_utils.dart index 6f739280f..07cb0948f 100644 --- a/lib/src/features/instance/domain/utils/instance_link_utils.dart +++ b/lib/src/features/instance/domain/utils/instance_link_utils.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:thunder/src/features/instance/data/constants/known_instances.dart'; import 'package:thunder/src/foundation/primitives/primitives.dart'; import 'package:thunder/src/features/search/search.dart'; import 'package:thunder/src/features/session/api.dart'; @@ -33,6 +34,21 @@ Future getLemmyUser(String text) async { return result?.qualified; } +// Check for URLs from instance-agnostic shareable links like lemmyverse.link and threadiverse.link. +// Users and communities are already parsed correctly, this adds detection and handling for posts. +String checkEmbeddedInstance(String text) { + final uri = Uri.tryParse(text); + if (uri == null || uri.host.isEmpty) return text; + if (uri.pathSegments.isEmpty) return text; + + final first = uri.pathSegments.first; + // Only rewrite if the first path segment exactly matches a known instance host. + if (!knownInstances.containsKey(first)) return text; + + final remaining = uri.pathSegments.sublist(1); + return uri.replace(host: first, pathSegments: remaining).toString(); +} + /// Gets the post ID from a Lemmy/PieFed URL. /// If the URL is from a different instance, it will attempt to resolve it. Future getLemmyPostId(BuildContext context, String text) async { diff --git a/lib/src/foundation/utils/threadiverse_link_parser_utils.dart b/lib/src/foundation/utils/threadiverse_link_parser_utils.dart index 5c5dd2ddf..6bb905776 100644 --- a/lib/src/foundation/utils/threadiverse_link_parser_utils.dart +++ b/lib/src/foundation/utils/threadiverse_link_parser_utils.dart @@ -34,7 +34,9 @@ final RegExp _lemmyUserMention = RegExp(r'^@?(https?:\/\/)?((?:(?!\/u\/u).)*)@(. /// Matches instance.tld/post/123 /// Groups: 2=instance, 3=postId -final RegExp _lemmyPostUrl = RegExp(r'^(https?:\/\/)(.*)/post/([0-9]+)$'); +// final RegExp _lemmyPostUrl = RegExp(r'^(https?:\/\/)(.*)/post/([0-9]+)$'); +// Modified to allow query parameters, e.g. /post/123?foo=bar +final RegExp _lemmyPostUrl = RegExp(r'^(https?:\/\/)(.*)/post/([0-9]+)(?:\?.*)?$'); /// Matches instance.tld/comment/123 /// Groups: 2=instance, 3=commentId