Skip to content

Commit

Permalink
content: Handle internal links
Browse files Browse the repository at this point in the history
Integrates internal_links into link nodes
so that urls that resolve to internal Narrows
navigate to that instead of launching in an
external browser.

Fixes: zulip#73
  • Loading branch information
sirpengi committed Oct 12, 2023
1 parent c15a579 commit c9fb0a0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../model/store.dart';
import 'code_block.dart';
import 'dialog.dart';
import 'lightbox.dart';
import 'message_list.dart';
import 'store.dart';
import 'text.dart';

Expand Down Expand Up @@ -668,6 +669,14 @@ void _launchUrl(BuildContext context, String urlString) async {
return;
}

final internalNarrow = parseInternalLink(url, store);
if (internalNarrow != null) {
Navigator.push(context,
MessageListPage.buildRoute(context: context,
narrow: internalNarrow));
return;
}

bool launched = false;
String? errorMessage;
try {
Expand Down
80 changes: 80 additions & 0 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:zulip/api/core.dart';
import 'package:zulip/model/content.dart';
import 'package:zulip/model/narrow.dart';
import 'package:zulip/widgets/content.dart';
import 'package:zulip/widgets/message_list.dart';
import 'package:zulip/widgets/page.dart';
import 'package:zulip/widgets/store.dart';

import '../api/fake_api.dart';
import '../example_data.dart' as eg;
import '../model/binding.dart';
import '../model/message_list_test.dart';
import '../test_images.dart';
import '../test_navigation.dart';
import 'dialog_checks.dart';
import 'message_list_checks.dart';
import 'page_checks.dart';

void main() {
TestZulipBinding.ensureInitialized();
Expand Down Expand Up @@ -158,6 +166,78 @@ void main() {
});
});

group('Internal links', () {
Future<List<Route<dynamic>>> prepareContentWithNavigator(WidgetTester tester, {
required String html,
}) async {
addTearDown(testBinding.reset);
final pushedRoutes = <Route<dynamic>>[];
final testNavObserver = TestNavigatorObserver()
..onPushed = (route, prevRoute) => pushedRoutes.add(route);
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(
streams: [eg.stream(streamId: 1, name: 'check')],
));
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
final connection = store.connection as FakeApiConnection;
connection.prepare(json: newestResult(
foundOldest: true,
messages: [],
).toJson());
await tester.pumpWidget(GlobalStoreWidget(child: MaterialApp(
navigatorObservers: [testNavObserver],
home: PerAccountStoreWidget(accountId: eg.selfAccount.id,
child: BlockContentList(nodes: parseContent(html).nodes)))));
await tester.pump();
assert(pushedRoutes.length == 1);
pushedRoutes.removeLast();
return pushedRoutes;
}

testWidgets('internal links are resolved: StreamNarrow', (tester) async {
final pushedRoutes = await prepareContentWithNavigator(tester,
html: '<p><a href="/#narrow/stream/1-check">stream</a></p>');

await tester.tap(find.text('stream'));
check(testBinding.takeLaunchUrlCalls()).isEmpty();
check(pushedRoutes).single.isA<WidgetRoute>()
.page.isA<MessageListPage>()
.narrow.equals(const StreamNarrow(1));
});

testWidgets('internal links are resolved: TopicNarrow', (tester) async {
final pushedRoutes = await prepareContentWithNavigator(tester,
html: '<p><a href="/#narrow/stream/1-check/topic/my.20topic">topic</a></p>');

await tester.tap(find.text('topic'));
check(testBinding.takeLaunchUrlCalls()).isEmpty();
check(pushedRoutes).single.isA<WidgetRoute>()
.page.isA<MessageListPage>()
.narrow.equals(const TopicNarrow(1, 'my topic'));
});

testWidgets('internal links are resolved: DmNarrow', (tester) async {
final pushedRoutes = await prepareContentWithNavigator(tester,
html: '<p><a href="/#narrow/dm/1-123-group">dm</a></p>');

await tester.tap(find.text('dm'));
check(testBinding.takeLaunchUrlCalls()).isEmpty();
check(pushedRoutes).single.isA<WidgetRoute>()
.page.isA<MessageListPage>()
.narrow.equals(DmNarrow.withUser(1, selfUserId: eg.selfUser.userId));
});

testWidgets('invalid internal links are not followed', (tester) async {
final pushedRoutes = await prepareContentWithNavigator(tester,
html: '<p><a href="/#narrow/stream/1-check/topic">invalid</a></p>');

await tester.tap(find.text('invalid'));
final expectedUrl = Uri.parse('${eg.realmUrl}#narrow/stream/1-check/topic');
check(testBinding.takeLaunchUrlCalls())
.single.equals((url: expectedUrl, mode: LaunchMode.externalApplication));
check(pushedRoutes).isEmpty();
});
});

group('UnicodeEmoji', () {
Future<void> prepareContent(WidgetTester tester, String html) async {
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));
Expand Down

0 comments on commit c9fb0a0

Please sign in to comment.