From 319f8aeb363aa01173cacdf6272d89e2712edb54 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Mon, 30 Nov 2026 02:46:24 -0500 Subject: [PATCH] feat(#1311): Share and Copy Link emit the offband.app link A meshcore:// link sent in a text or a chat can open the stock MeshCore app instead of Offband, since both answer it. Links meant to be sent now use https://offband.app/add/..., which Android opens in Offband once the domain verifies (#1309). QR codes stay meshcore://, so the stock app can still scan an Offband QR. - The channel QR dialog gets a Copy link button that copies the https link. The My contact QR dialog shows and copies it. - Channel.fromShareUri and Contact.fromShareUri read both forms, so the router, the add-contact paste field and the QR scanners all do. - The form conversion moves to lib/helpers/share_link_forms.dart, so the models can use it. A path typed in mixed case reads the same. - The router holds a contact link as received; the dialog reads either form. - New strings "Copy link" and "Channel link copied", English only for now. Agent: StormyElk (session 088ce922) --- lib/helpers/link_handler.dart | 1 + lib/helpers/share_link_forms.dart | 62 +++++++++ lib/l10n/app_en.arb | 2 + lib/l10n/app_localizations.dart | 12 ++ lib/l10n/app_localizations_bg.dart | 6 + lib/l10n/app_localizations_de.dart | 6 + lib/l10n/app_localizations_en.dart | 6 + lib/l10n/app_localizations_es.dart | 6 + lib/l10n/app_localizations_fr.dart | 6 + lib/l10n/app_localizations_hu.dart | 6 + lib/l10n/app_localizations_it.dart | 6 + lib/l10n/app_localizations_ja.dart | 6 + lib/l10n/app_localizations_ko.dart | 6 + lib/l10n/app_localizations_nl.dart | 6 + lib/l10n/app_localizations_pl.dart | 6 + lib/l10n/app_localizations_pt.dart | 6 + lib/l10n/app_localizations_ru.dart | 6 + lib/l10n/app_localizations_sk.dart | 6 + lib/l10n/app_localizations_sl.dart | 6 + lib/l10n/app_localizations_sv.dart | 6 + lib/l10n/app_localizations_uk.dart | 6 + lib/l10n/app_localizations_zh.dart | 6 + lib/models/channel.dart | 11 +- lib/models/contact.dart | 17 ++- lib/screens/channels_screen.dart | 2 + lib/services/link_router.dart | 32 +---- lib/widgets/my_contact_qr_dialog.dart | 17 ++- lib/widgets/qr_code_display.dart | 32 +++++ test/helpers/share_link_forms_test.dart | 146 ++++++++++++++++++++ test/services/link_router_test.dart | 21 ++- test/widgets/my_contact_qr_dialog_test.dart | 65 +++++++-- untranslated.json | 34 +++++ 32 files changed, 510 insertions(+), 52 deletions(-) create mode 100644 lib/helpers/share_link_forms.dart create mode 100644 test/helpers/share_link_forms_test.dart diff --git a/lib/helpers/link_handler.dart b/lib/helpers/link_handler.dart index 675113bf..d2856ce7 100644 --- a/lib/helpers/link_handler.dart +++ b/lib/helpers/link_handler.dart @@ -4,6 +4,7 @@ import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import '../l10n/l10n.dart'; import '../services/link_router.dart'; +import 'share_link_forms.dart'; import '../utils/platform_info.dart'; import '../helpers/snack_bar_builder.dart'; diff --git a/lib/helpers/share_link_forms.dart b/lib/helpers/share_link_forms.dart new file mode 100644 index 00000000..29b668eb --- /dev/null +++ b/lib/helpers/share_link_forms.dart @@ -0,0 +1,62 @@ +/// The two forms of a channel or contact share link (#1280). +/// +/// - `meshcore://channel/add?` and `meshcore://contact/add?`: +/// what the stock app reads, so QR codes keep using it. +/// - `https://offband.app/add/channel#` and `/add/contact#`: +/// what Share and Copy Link emit. The OS opens it in Offband, which the +/// stock app's claim on `meshcore://` gets in the way of. +/// +/// The fields are the same in both. In the https form they sit after `#`, +/// which browsers never send to the server, so a channel secret stays out +/// of the server's logs. +library; + +/// The hosts whose `/add/...` links are Offband share links. The app is +/// verified for both (#1309), so these open it instead of the browser. +const shareLinkHosts = {'offband.app', 'dev.offband.app'}; + +/// The host Share and Copy Link emit (#1311). +const shareLinkHost = 'offband.app'; + +final _meshcoreForm = RegExp(r'^meshcore://(channel|contact)/add\?(.+)$'); + +/// The `meshcore://` twin of an `https://offband.app/add/channel#...` or +/// `/add/contact#...` link, or null if [raw] is not one (#1309). +String? meshcoreFormOf(String raw) { + final uri = Uri.tryParse(raw.trim()); + if (uri == null || !isShareAddress(uri)) return null; + final path = _pathOf(uri); + if (path.length != 2) return null; + if (path[1] != 'channel' && path[1] != 'contact') return null; + if (uri.query.isNotEmpty || uri.fragment.isEmpty) return null; + return 'meshcore://${path[1]}/add?${uri.fragment}'; +} + +/// The path's segments, lowercased: a link typed by hand as `/Add/Channel` +/// is the same link. +List _pathOf(Uri uri) => [ + for (final s in uri.pathSegments) + if (s.isNotEmpty) s.toLowerCase(), +]; + +/// The https twin of a `meshcore://channel/add?...` or `contact/add?...` +/// link, on [shareLinkHost] (#1311). The fields are carried as they are, +/// already encoded. +String httpsFormOf(String meshcoreLink) { + final match = _meshcoreForm.firstMatch(meshcoreLink); + if (match == null) { + throw ArgumentError.value(meshcoreLink, 'meshcoreLink', 'not a share link'); + } + return 'https://$shareLinkHost/add/${match[1]}#${match[2]}'; +} + +/// [raw] in the `meshcore://` form, whichever form it arrived in, so one +/// parser reads both. +String toMeshcoreForm(String raw) => meshcoreFormOf(raw) ?? raw.trim(); + +/// True for any `https://offband.app/add/...` address, well formed or not. +bool isShareAddress(Uri uri) => + uri.scheme.toLowerCase() == 'https' && + !uri.hasPort && + shareLinkHosts.contains(uri.host.toLowerCase()) && + _pathOf(uri).firstOrNull == 'add'; diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index c65b1100..703ebb27 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -3186,6 +3186,8 @@ "contacts_myContactQr": "My contact QR", "contacts_myContactQrInstructions": "Have them scan this, or send them the link below. It works even if they have never heard your advert.", "contacts_contactLinkCopied": "Contact link copied", + "common_copyLink": "Copy link", + "channels_channelLinkCopied": "Channel link copied", "contacts_qrNeedsConnection": "Connect to a radio first, so the app knows your public key.", "contacts_scanContactQr": "Scan contact QR", "contacts_scanContactQrInstructions": "Point the camera at a MeshCore contact QR", diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index 3fdf1c75..c8f792a7 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -8836,6 +8836,18 @@ abstract class AppLocalizations { /// **'Contact link copied'** String get contacts_contactLinkCopied; + /// No description provided for @common_copyLink. + /// + /// In en, this message translates to: + /// **'Copy link'** + String get common_copyLink; + + /// No description provided for @channels_channelLinkCopied. + /// + /// In en, this message translates to: + /// **'Channel link copied'** + String get channels_channelLinkCopied; + /// No description provided for @contacts_qrNeedsConnection. /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index e85987a8..a111d7f5 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -5264,6 +5264,12 @@ class AppLocalizationsBg extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index 18d2095e..f3f5de92 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -5276,6 +5276,12 @@ class AppLocalizationsDe extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index a1653a2a..89933123 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -5198,6 +5198,12 @@ class AppLocalizationsEn extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index 7bd9e30a..0b113f6f 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -5265,6 +5265,12 @@ class AppLocalizationsEs extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index 9749cb44..90ec1839 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -5287,6 +5287,12 @@ class AppLocalizationsFr extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index ce93b4b3..71749bab 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -5282,6 +5282,12 @@ class AppLocalizationsHu extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index 7e2d8a3b..839c4ac8 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -5270,6 +5270,12 @@ class AppLocalizationsIt extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 6e491c39..634475f2 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -5067,6 +5067,12 @@ class AppLocalizationsJa extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index 244f29aa..fc4cfc83 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -5069,6 +5069,12 @@ class AppLocalizationsKo extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index 356d0c51..60d7de26 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -5250,6 +5250,12 @@ class AppLocalizationsNl extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 25f55d30..c03df7ad 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -5280,6 +5280,12 @@ class AppLocalizationsPl extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index c48fef97..202624f1 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -5262,6 +5262,12 @@ class AppLocalizationsPt extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index bdc29f7d..a992595c 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -5272,6 +5272,12 @@ class AppLocalizationsRu extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 052566a2..a8c1ebfc 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -5246,6 +5246,12 @@ class AppLocalizationsSk extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index 204a310b..2b3b3f48 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -5240,6 +5240,12 @@ class AppLocalizationsSl extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 8d593686..7740d3f5 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -5219,6 +5219,12 @@ class AppLocalizationsSv extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index 3938efb9..3b31ece6 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -5271,6 +5271,12 @@ class AppLocalizationsUk extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 4dc83082..f1f40342 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -4970,6 +4970,12 @@ class AppLocalizationsZh extends AppLocalizations { @override String get contacts_contactLinkCopied => 'Contact link copied'; + @override + String get common_copyLink => 'Copy link'; + + @override + String get channels_channelLinkCopied => 'Channel link copied'; + @override String get contacts_qrNeedsConnection => 'Connect to a radio first, so the app knows your public key.'; diff --git a/lib/models/channel.dart b/lib/models/channel.dart index 7d00cfea..82431e6c 100644 --- a/lib/models/channel.dart +++ b/lib/models/channel.dart @@ -4,6 +4,7 @@ import 'dart:typed_data'; import 'package:crypto/crypto.dart' as crypto; import '../connector/meshcore_protocol.dart'; +import '../helpers/share_link_forms.dart'; import 'community.dart'; enum ChannelType { public, private, hashtag, communityPublic, communityHashtag } @@ -112,14 +113,22 @@ class Channel { String toShareUri() => 'meshcore://channel/add?name=${Uri.encodeComponent(name)}&secret=$pskHex'; + /// The `https://offband.app/add/channel#...` twin of [toShareUri], for + /// Copy Link: it opens Offband, where the `meshcore://` form can land in + /// the stock app. QR codes keep [toShareUri], so the stock app can still + /// scan them. (#1311) + String toShareLink() => httpsFormOf(toShareUri()); + /// Parses a channel from a reference-app share URI, or null if malformed. /// [index] is the local slot the caller assigns on add. (#161) /// /// A hashtag channel's key is derived from its name, so a link naming one /// needs no `secret=`; "#public" means the real Public, as everywhere else /// (#1896). (#1289) + /// + /// The `https://offband.app/add/channel#...` form reads the same (#1311). static Channel? fromShareUri(String uri, {int index = 0}) { - final parsed = Uri.tryParse(uri.trim()); + final parsed = Uri.tryParse(toMeshcoreForm(uri)); if (parsed == null || parsed.scheme != 'meshcore') return null; if (parsed.host != 'channel') return null; if (parsed.path.replaceAll('/', '') != 'add') return null; diff --git a/lib/models/contact.dart b/lib/models/contact.dart index f51d85aa..61fa67fc 100644 --- a/lib/models/contact.dart +++ b/lib/models/contact.dart @@ -2,6 +2,7 @@ import 'dart:typed_data'; import 'package:meshcore_open/utils/app_logger.dart'; import '../connector/meshcore_protocol.dart'; +import '../helpers/share_link_forms.dart'; class Contact { final Uint8List publicKey; @@ -354,6 +355,18 @@ class Contact { '&public_key=$publicKeyHex' '&type=$type'; + /// The `https://offband.app/add/contact#...` twin of [buildShareUri], for + /// Share and Copy Link: it opens Offband, where the `meshcore://` form can + /// land in the stock app. QR codes keep [buildShareUri], so the stock app + /// can still scan them. (#1311) + static String buildShareLink({ + required String publicKeyHex, + required String name, + int type = advTypeChat, + }) => httpsFormOf( + buildShareUri(publicKeyHex: publicKeyHex, name: name, type: type), + ); + /// The body of one compact channel share, ``, captured /// without its delimiters. `[^>]*` cannot cross a closing bracket, so each /// card matches individually even when several sit in one message. @@ -468,8 +481,10 @@ class Contact { /// Returns null for the fork's older `meshcore://` form, /// whose host is the leading hex rather than `contact`. Callers keep handling /// that separately: it carries a full signed advert and is strictly richer. + /// + /// The `https://offband.app/add/contact#...` form reads the same (#1311). static Contact? fromShareUri(String uri) { - final parsed = Uri.tryParse(uri.trim()); + final parsed = Uri.tryParse(toMeshcoreForm(uri)); if (parsed == null || parsed.scheme != 'meshcore') return null; if (parsed.host != 'contact') return null; if (parsed.path.replaceAll('/', '') != 'add') return null; diff --git a/lib/screens/channels_screen.dart b/lib/screens/channels_screen.dart index 789c540b..d6adb612 100644 --- a/lib/screens/channels_screen.dart +++ b/lib/screens/channels_screen.dart @@ -2201,6 +2201,8 @@ class _ChannelsScreenState extends State QrCodeShareDialog.show( context: context, data: channel.toShareUri(), + link: channel.toShareLink(), + linkCopiedMessage: context.l10n.channels_channelLinkCopied, title: context.l10n.channels_shareQrTitle, instructions: context.l10n.channels_shareQrInstructions(channel.name), embeddedImage: Image.asset( diff --git a/lib/services/link_router.dart b/lib/services/link_router.dart index 903ca775..1db9463e 100644 --- a/lib/services/link_router.dart +++ b/lib/services/link_router.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import '../connector/meshcore_protocol.dart'; +import '../helpers/share_link_forms.dart'; import '../models/channel.dart'; import '../models/contact.dart'; import '../utils/address_bar.dart'; @@ -50,36 +51,9 @@ class AdvertShareLink extends ShareLink { // in one radio packet, so anything past 255 bytes is not one. final _advertLink = RegExp(r'^meshcore://((?:[0-9a-fA-F]{2}){98,255})$'); -/// The hosts whose `/add/...` links are Offband share links. The app is -/// verified for both (#1309), so these open it instead of the browser. -const shareLinkHosts = {'offband.app', 'dev.offband.app'}; - -/// The `meshcore://` twin of an `https://offband.app/add/channel#...` or -/// `/add/contact#...` link, or null if [raw] is not one (#1309). -/// -/// The fields sit after `#`, which browsers never send to the server, so a -/// channel secret stays out of the server's logs. They are the same fields -/// as the `meshcore://` link's query, so one parser reads both. -String? meshcoreFormOf(String raw) { - final uri = Uri.tryParse(raw.trim()); - if (uri == null || !_isShareAddress(uri)) return null; - final path = uri.pathSegments.where((s) => s.isNotEmpty).toList(); - if (path.length != 2) return null; - if (path[1] != 'channel' && path[1] != 'contact') return null; - if (uri.query.isNotEmpty || uri.fragment.isEmpty) return null; - return 'meshcore://${path[1]}/add?${uri.fragment}'; -} - -/// True for any `https://offband.app/add/...` address, well formed or not. -bool _isShareAddress(Uri uri) => - uri.scheme.toLowerCase() == 'https' && - !uri.hasPort && - shareLinkHosts.contains(uri.host.toLowerCase()) && - uri.pathSegments.where((s) => s.isNotEmpty).firstOrNull == 'add'; - /// Parses [raw] into a link Offband can add, or null if it is not one. ShareLink? parseShareLink(String raw) { - final trimmed = meshcoreFormOf(raw) ?? raw.trim(); + final trimmed = raw.trim(); final channel = Channel.fromShareUri(trimmed); if (channel != null) return ChannelShareLink(channel); final contact = Contact.fromShareUri(trimmed); @@ -136,7 +110,7 @@ class LinkRouter extends ChangeNotifier { void handleFromOs(Uri uri) { if (uri.scheme.toLowerCase() == 'meshcore') { handle(uri.toString()); - } else if (_isShareAddress(uri)) { + } else if (isShareAddress(uri)) { handle(uri.toString()); _clearAddressLink(); } diff --git a/lib/widgets/my_contact_qr_dialog.dart b/lib/widgets/my_contact_qr_dialog.dart index 289e8a9f..b56c1f6d 100644 --- a/lib/widgets/my_contact_qr_dialog.dart +++ b/lib/widgets/my_contact_qr_dialog.dart @@ -33,10 +33,19 @@ Future showMyContactQrDialog(BuildContext context) { return Future.value(); } + final name = connector.selfName ?? ''; + // This device is a companion. final uri = Contact.buildShareUri( publicKeyHex: keyHex, - name: connector.selfName ?? '', - // This device is a companion. + name: name, + type: advTypeChat, + ); + // What they are told to send: it opens Offband, where the meshcore:// form + // can land in the stock app. The QR keeps the meshcore:// form, which the + // stock app scans. (#1311) + final link = Contact.buildShareLink( + publicKeyHex: keyHex, + name: name, type: advTypeChat, ); @@ -55,7 +64,7 @@ Future showMyContactQrDialog(BuildContext context) { ), const SizedBox(height: 8), SelectableText( - uri, + link, style: Theme.of(dialogContext).textTheme.bodySmall?.copyWith( fontFamily: 'monospace', color: Theme.of(dialogContext).colorScheme.onSurfaceVariant, @@ -67,7 +76,7 @@ Future showMyContactQrDialog(BuildContext context) { actions: [ TextButton.icon( onPressed: () async { - await Clipboard.setData(ClipboardData(text: uri)); + await Clipboard.setData(ClipboardData(text: link)); if (!dialogContext.mounted) return; showDismissibleSnackBar( dialogContext, diff --git a/lib/widgets/qr_code_display.dart b/lib/widgets/qr_code_display.dart index dca94c28..0d7d1574 100644 --- a/lib/widgets/qr_code_display.dart +++ b/lib/widgets/qr_code_display.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:qr_flutter/qr_flutter.dart'; +import '../helpers/snack_bar_builder.dart'; import '../l10n/l10n.dart'; /// A reusable QR code display widget for sharing data. @@ -179,12 +181,20 @@ class QrCodeShareDialog extends StatelessWidget { final String? instructions; final Widget? embeddedImage; + /// A link to offer under the code, with a Copy link button, and the + /// message shown once it is copied. The QR keeps [data]: a link sent in + /// text can be a different form from the one a scanner reads. (#1311) + final String? link; + final String? linkCopiedMessage; + const QrCodeShareDialog({ super.key, required this.data, this.title, this.instructions, this.embeddedImage, + this.link, + this.linkCopiedMessage, }); @override @@ -203,6 +213,24 @@ class QrCodeShareDialog extends StatelessWidget { embeddedImage: embeddedImage, padding: EdgeInsets.zero, ), + if (link case final link?) ...[ + const SizedBox(height: 16), + SizedBox( + width: double.infinity, + child: OutlinedButton.icon( + onPressed: () async { + await Clipboard.setData(ClipboardData(text: link)); + if (!context.mounted) return; + showDismissibleSnackBar( + context, + content: Text(linkCopiedMessage ?? link), + ); + }, + icon: const Icon(Icons.link), + label: Text(context.l10n.common_copyLink), + ), + ), + ], const SizedBox(height: 24), SizedBox( width: double.infinity, @@ -224,6 +252,8 @@ class QrCodeShareDialog extends StatelessWidget { String? title, String? instructions, Widget? embeddedImage, + String? link, + String? linkCopiedMessage, }) { return showDialog( context: context, @@ -232,6 +262,8 @@ class QrCodeShareDialog extends StatelessWidget { title: title, instructions: instructions, embeddedImage: embeddedImage, + link: link, + linkCopiedMessage: linkCopiedMessage, ), ); } diff --git a/test/helpers/share_link_forms_test.dart b/test/helpers/share_link_forms_test.dart new file mode 100644 index 00000000..f95681d3 --- /dev/null +++ b/test/helpers/share_link_forms_test.dart @@ -0,0 +1,146 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/helpers/share_link_forms.dart'; +import 'package:meshcore_open/l10n/app_localizations.dart'; +import 'package:meshcore_open/models/channel.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/widgets/qr_code_display.dart'; + +// #1311: Share and Copy Link emit https://offband.app/add/..., which opens +// Offband; QR codes stay meshcore://, which the stock app scans. Every parser +// reads both. + +const _key = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; +const _secret = '5d25cf40b1f5b4a7eb0cf9703634a948'; + +void main() { + group('the two forms', () { + test('a channel link round-trips through both forms', () { + for (final name in ['Ops', '#echo', 'a&b', '50% off', 'one+two', 'a b']) { + final channel = Channel( + index: 0, + name: name, + psk: Channel.parsePskHex(_secret), + ); + final link = channel.toShareLink(); + expect(link, startsWith('https://offband.app/add/channel#')); + expect(meshcoreFormOf(link), channel.toShareUri()); + final back = Channel.fromShareUri(link)!; + expect(back.name, name, reason: link); + expect(back.pskHex, _secret, reason: link); + } + }); + + test('a contact link round-trips through both forms', () { + for (final name in ['Bob', 'Strycher-RK4', 'a&b', 'a b']) { + final link = Contact.buildShareLink( + publicKeyHex: _key, + name: name, + type: advTypeRepeater, + ); + expect(link, startsWith('https://offband.app/add/contact#')); + expect( + meshcoreFormOf(link), + Contact.buildShareUri( + publicKeyHex: _key, + name: name, + type: advTypeRepeater, + ), + ); + final back = Contact.fromShareUri(link)!; + expect(back.publicKeyHex, _key); + expect(back.name, name); + expect(back.type, advTypeRepeater); + } + }); + + test('the parsers still read the meshcore:// form', () { + final channel = Channel( + index: 0, + name: 'Ops', + psk: Channel.parsePskHex(_secret), + ); + expect(Channel.fromShareUri(channel.toShareUri())?.name, 'Ops'); + expect(Channel.isValidShareUri(channel.toShareUri()), isTrue); + expect(Channel.isValidShareUri(channel.toShareLink()), isTrue); + final uri = Contact.buildShareUri(publicKeyHex: _key, name: 'Bob'); + expect(Contact.isValidShareUri(uri), isTrue); + expect( + Contact.isValidShareUri( + Contact.buildShareLink(publicKeyHex: _key, name: 'Bob'), + ), + isTrue, + ); + }); + + test('httpsFormOf refuses what is not a share link', () { + for (final bad in [ + 'meshcore://${'ab' * 98}', + 'meshcore://channel/add', + 'https://offband.app/add/channel#name=x', + ]) { + expect(() => httpsFormOf(bad), throwsArgumentError, reason: bad); + } + }); + }); + + testWidgets('the channel QR stays meshcore://, Copy link copies https', ( + tester, + ) async { + final channel = Channel( + index: 0, + name: '#echo', + psk: Channel.parsePskHex(_secret), + ); + final copied = []; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + (call) async { + if (call.method == 'Clipboard.setData') { + copied.add((call.arguments as Map)['text'] as String); + } + return null; + }, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + null, + ), + ); + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: QrCodeShareDialog( + data: channel.toShareUri(), + link: channel.toShareLink(), + linkCopiedMessage: 'Channel link copied', + ), + ), + ), + ); + expect( + tester.widget(find.byType(QrCodeDisplay)).data, + channel.toShareUri(), + ); + await tester.tap(find.text('Copy link')); + await tester.pump(); + expect(copied, [channel.toShareLink()]); + expect(find.text('Channel link copied'), findsOneWidget); + }); + + testWidgets('a QR with no link offers no Copy link', (tester) async { + await tester.pumpWidget( + MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: const Scaffold(body: QrCodeShareDialog(data: '{"x":1}')), + ), + ); + expect(find.text('Copy link'), findsNothing); + }); +} diff --git a/test/services/link_router_test.dart b/test/services/link_router_test.dart index 1a057a00..7f16e0c4 100644 --- a/test/services/link_router_test.dart +++ b/test/services/link_router_test.dart @@ -1,5 +1,7 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/helpers/share_link_forms.dart'; import 'package:meshcore_open/models/channel.dart'; +import 'package:meshcore_open/models/contact.dart'; import 'package:meshcore_open/services/link_router.dart'; // #1289: every share link goes through LinkRouter, which holds it until its @@ -77,17 +79,22 @@ void main() { as ChannelShareLink; expect(hashtag.channel.name, '#example'); - final contact = - parseShareLink( - https(host, 'contact', 'name=Bob&public_key=$_key&type=1'), - ) - as ContactShareLink; + final link = https(host, 'contact', 'name=Bob&public_key=$_key&type=1'); + final contact = parseShareLink(link) as ContactShareLink; expect(contact.contact.name, 'Bob'); - // The add-contact dialog reads the link again, in the form it knows. - expect(contact.uri, _contactLink); + // Held as received; the add-contact dialog reads either form (#1311). + expect(contact.uri, link); + expect(Contact.fromShareUri(contact.uri)?.publicKeyHex, _key); } }); + test('read a hand-typed path in any case', () { + expect( + parseShareLink('https://offband.app/Add/Channel#name=%23example'), + isA(), + ); + }); + test('keep a name with reserved characters intact', () { const secret = '5d25cf40b1f5b4a7eb0cf9703634a948'; for (final name in ['a&b', 'x=y', '50% off', 'one+two', 'a b', '#c#d']) { diff --git a/test/widgets/my_contact_qr_dialog_test.dart b/test/widgets/my_contact_qr_dialog_test.dart index 7631a807..0668fc11 100644 --- a/test/widgets/my_contact_qr_dialog_test.dart +++ b/test/widgets/my_contact_qr_dialog_test.dart @@ -6,6 +6,7 @@ // that link parsing back into the right key, name and type. import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:provider/provider.dart'; import 'package:qr_flutter/qr_flutter.dart'; @@ -15,6 +16,7 @@ import 'package:meshcore_open/connector/meshcore_protocol.dart'; import 'package:meshcore_open/l10n/app_localizations.dart'; import 'package:meshcore_open/models/contact.dart'; import 'package:meshcore_open/widgets/my_contact_qr_dialog.dart'; +import 'package:meshcore_open/widgets/qr_code_display.dart'; const _key = 'aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899'; @@ -57,26 +59,69 @@ void main() { testWidgets('renders a QR plus the link it encodes', (tester) async { await _open(tester, _FakeConn(keyHex: _key, nodeName: 'Strycher-RK4')); - final expected = Contact.buildShareUri( - publicKeyHex: _key, - name: 'Strycher-RK4', - type: advTypeChat, - ); - expect(find.byType(QrImageView), findsOneWidget); + // The QR stays meshcore://, which the stock app scans (#1311). + expect( + tester.widget(find.byType(QrCodeDisplay)).data, + Contact.buildShareUri( + publicKeyHex: _key, + name: 'Strycher-RK4', + type: advTypeChat, + ), + ); // The link is shown so it can be copied or pasted into a chat, which is - // the path that works when the other person is not in the room. - expect(find.text(expected), findsOneWidget); + // the path that works when the other person is not in the room. It is + // the offband.app form, which opens Offband (#1311). + expect( + find.text( + Contact.buildShareLink( + publicKeyHex: _key, + name: 'Strycher-RK4', + type: advTypeChat, + ), + ), + findsOneWidget, + ); + }); + + testWidgets('Copy copies the offband.app link (#1311)', (tester) async { + final copied = []; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + (call) async { + if (call.method == 'Clipboard.setData') { + copied.add((call.arguments as Map)['text'] as String); + } + return null; + }, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + null, + ), + ); + await _open(tester, _FakeConn(keyHex: _key, nodeName: 'Strycher-RK4')); + await tester.tap(find.byIcon(Icons.copy)); + await tester.pump(); + expect(copied, [ + Contact.buildShareLink( + publicKeyHex: _key, + name: 'Strycher-RK4', + type: advTypeChat, + ), + ]); }); testWidgets('the rendered link parses back to this device', (tester) async { await _open(tester, _FakeConn(keyHex: _key, nodeName: 'Strycher-RK4')); - final expected = Contact.buildShareUri( + final expected = Contact.buildShareLink( publicKeyHex: _key, name: 'Strycher-RK4', type: advTypeChat, ); + expect(find.text(expected), findsOneWidget); final parsed = Contact.fromShareUri(expected); expect(parsed, isNotNull); @@ -91,7 +136,7 @@ void main() { ) async { await _open(tester, _FakeConn(keyHex: _key)); - final expected = Contact.buildShareUri( + final expected = Contact.buildShareLink( publicKeyHex: _key, name: '', type: advTypeChat, diff --git a/untranslated.json b/untranslated.json index 21ca628d..39961e4a 100644 --- a/untranslated.json +++ b/untranslated.json @@ -325,6 +325,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -799,6 +801,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -1273,6 +1277,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -1747,6 +1753,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -2221,6 +2229,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -2695,6 +2705,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -3169,6 +3181,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -3643,6 +3657,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -4117,6 +4133,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -4591,6 +4609,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -5065,6 +5085,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -5539,6 +5561,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -6013,6 +6037,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -6487,6 +6513,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -6961,6 +6989,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -7435,6 +7465,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions", @@ -7909,6 +7941,8 @@ "contacts_myContactQr", "contacts_myContactQrInstructions", "contacts_contactLinkCopied", + "common_copyLink", + "channels_channelLinkCopied", "contacts_qrNeedsConnection", "contacts_scanContactQr", "contacts_scanContactQrInstructions",