Skip to content

Commit

Permalink
internal_link: Always include a "/" after hostname
Browse files Browse the repository at this point in the history
Fixes: zulip#845

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 authored and gnprice committed Dec 11, 2024
1 parent 223d674 commit fd91f72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/model/internal_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ Uri narrowLink(PerAccountStore store, Narrow narrow, {int? nearMessageId}) {
fragment.write('/near/$nearMessageId');
}

return store.realmUrl.replace(fragment: fragment.toString());
Uri result = store.realmUrl.replace(fragment: fragment.toString());
if (result.path.isEmpty) {
// Always ensure that there is a '/' right after the hostname.
// A generated URL without '/' looks odd,
// and if used in a Zulip message does not get automatically linkified.
result = result.replace(path: '/');
}
return result;
}

/// A [Narrow] from a given URL, on `store`'s realm.
Expand Down
17 changes: 17 additions & 0 deletions test/model/internal_link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ void main() {
'#narrow/dm/1,2-dm/near/12345',
'#narrow/pm-with/1,2-pm/near/12345');
});

test('normalize links to always include a "/" after hostname', () {
String narrowLinkFor({required String realmUrl}) {
final store = eg.store(
account: eg.account(user: eg.selfUser, realmUrl: Uri.parse(realmUrl)));
return narrowLink(store, const CombinedFeedNarrow()).toString();
}

check(narrowLinkFor(realmUrl: 'http://chat.example.com'))
.equals( 'http://chat.example.com/#narrow');
check(narrowLinkFor(realmUrl: 'http://chat.example.com/'))
.equals( 'http://chat.example.com/#narrow');
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path'))
.equals( 'http://chat.example.com/path#narrow');
check(narrowLinkFor(realmUrl: 'http://chat.example.com/path/'))
.equals( 'http://chat.example.com/path/#narrow');
});
});

final realmUrl = Uri.parse('https://example.com/');
Expand Down

0 comments on commit fd91f72

Please sign in to comment.