From b24d977aa8856dbe9087b48b6830a0478b59548c Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 25 Sep 2024 14:41:39 +0700 Subject: [PATCH 1/4] TW-2036: Fix missing some thing part when use resetLocationPathWithLoginToken --- .../mixins/connect_page_mixin.dart | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index de2aeaf7a9..e3caa57417 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -216,9 +216,9 @@ mixin ConnectPageMixin { String _generatePostLogoutRedirectUrl() { if (kIsWeb) { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html'; + return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html'; } - return '${html.window.origin!}/web/auth.html'; + return '${getBaseUrlBeforeHash(html.window.location.href)}/web/auth.html'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect'; } @@ -230,9 +230,9 @@ mixin ConnectPageMixin { homeserverParam = '?homeserver=$homeserver'; } if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${html.window.origin!}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; + return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; } - return '${html.window.origin!}/web/auth.html$homeserverParam'; + return '${getBaseUrlBeforeHash(html.window.location.href)}/web/auth.html$homeserverParam'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login'; } @@ -295,6 +295,15 @@ mixin ConnectPageMixin { }) { final loginTokenExisted = getQueryParameter('loginToken') != null; if (!loginTokenExisted) return; - html.window.history.replaceState({}, '', '/#/${route ?? 'rooms'}'); + html.window.history.replaceState( + {}, + '', + '${getBaseUrlBeforeHash(html.window.location.href)}#/${route ?? 'rooms'}', + ); + } + + String getBaseUrlBeforeHash(String fullUrl) { + final fragmentIndex = fullUrl.indexOf('#/'); + return fragmentIndex != -1 ? fullUrl.substring(0, fragmentIndex) : fullUrl; } } From 0deaa10ab20b7258eb2e461c62de7340945d81c3 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 25 Sep 2024 14:58:20 +0700 Subject: [PATCH 2/4] fixup! TW-2036: Fix missing some thing part when use resetLocationPathWithLoginToken --- lib/presentation/mixins/connect_page_mixin.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index e3caa57417..ba5c5ece7a 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -218,7 +218,7 @@ mixin ConnectPageMixin { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}/web/auth.html'; + return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect'; } @@ -232,7 +232,7 @@ mixin ConnectPageMixin { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}/web/auth.html$homeserverParam'; + return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html$homeserverParam'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login'; } From 0c8f651f3955f4acbb4390cedc41eb1333f14061 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Thu, 26 Sep 2024 10:46:53 +0700 Subject: [PATCH 3/4] fixup! fixup! TW-2036: Fix missing some thing part when use resetLocationPathWithLoginToken --- .../mixins/connect_page_mixin.dart | 16 ++++----- lib/utils/string_extension.dart | 5 +++ test/utils/string_extension_test.dart | 36 +++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index ba5c5ece7a..62f3bf02c3 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -7,6 +7,7 @@ import 'package:fluffychat/pages/connect/sso_login_state.dart'; import 'package:fluffychat/utils/dialog/twake_dialog.dart'; import 'package:fluffychat/utils/exception/homeserver_exception.dart'; import 'package:fluffychat/utils/platform_infos.dart'; +import 'package:fluffychat/utils/string_extension.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -216,9 +217,9 @@ mixin ConnectPageMixin { String _generatePostLogoutRedirectUrl() { if (kIsWeb) { if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://redirect'; } @@ -230,9 +231,9 @@ mixin ConnectPageMixin { homeserverParam = '?homeserver=$homeserver'; } if (AppConfig.issueId != null && AppConfig.issueId!.isNotEmpty) { - return '${getBaseUrlBeforeHash(html.window.location.href)}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}/twake-on-matrix/${AppConfig.issueId}/auth.html$homeserverParam'; } - return '${getBaseUrlBeforeHash(html.window.location.href)}web/auth.html$homeserverParam'; + return '${html.window.location.href.getBaseUrlBeforeHash()}web/auth.html$homeserverParam'; } return '${AppConfig.appOpenUrlScheme.toLowerCase()}://login'; } @@ -298,12 +299,7 @@ mixin ConnectPageMixin { html.window.history.replaceState( {}, '', - '${getBaseUrlBeforeHash(html.window.location.href)}#/${route ?? 'rooms'}', + '${html.window.location.href.getBaseUrlBeforeHash()}#/${route ?? 'rooms'}', ); } - - String getBaseUrlBeforeHash(String fullUrl) { - final fragmentIndex = fullUrl.indexOf('#/'); - return fragmentIndex != -1 ? fullUrl.substring(0, fragmentIndex) : fullUrl; - } } diff --git a/lib/utils/string_extension.dart b/lib/utils/string_extension.dart index 02d326a305..2bde62a822 100644 --- a/lib/utils/string_extension.dart +++ b/lib/utils/string_extension.dart @@ -376,4 +376,9 @@ extension StringCasingExtension on String { final match = regex.firstMatch(this); return match?.group(1); } + + String getBaseUrlBeforeHash() { + final fragmentIndex = indexOf('#/'); + return fragmentIndex != -1 ? substring(0, fragmentIndex) : this; + } } diff --git a/test/utils/string_extension_test.dart b/test/utils/string_extension_test.dart index 96fcbda0c9..3c40f60404 100644 --- a/test/utils/string_extension_test.dart +++ b/test/utils/string_extension_test.dart @@ -598,5 +598,41 @@ void main() { expect(result[i].style, expectedSpans[i].style); } }); + + test('getBaseUrlBeforeHash handles URL with hash', () { + const url = 'https://example.com/web/f/#/test'; + const expectedUrl = 'https://example.com/web/f/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL without hash', () { + const url = 'https://example.com/test'; + const expectedUrl = 'https://example.com/test'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with multiple hashes', () { + const url = 'https://example.com/#/test#section'; + const expectedUrl = 'https://example.com/'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); + + test('getBaseUrlBeforeHash handles URL with query parameters and hash', () { + const url = 'https://example.com/test?query=1#/section'; + const expectedUrl = 'https://example.com/test?query=1'; + + final result = url.getBaseUrlBeforeHash(); + + expect(result, equals(expectedUrl)); + }); }); } From 84d8303d20ccad5d48f96e94da8dd095ea9a8dc2 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Fri, 27 Sep 2024 17:33:17 +0700 Subject: [PATCH 4/4] fixup! fixup! fixup! TW-2036: Fix missing some thing part when use resetLocationPathWithLoginToken --- test/utils/string_extension_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/utils/string_extension_test.dart b/test/utils/string_extension_test.dart index 3c40f60404..1d34c468da 100644 --- a/test/utils/string_extension_test.dart +++ b/test/utils/string_extension_test.dart @@ -598,7 +598,9 @@ void main() { expect(result[i].style, expectedSpans[i].style); } }); + }); + group('getBaseUrlBeforeHash test', () { test('getBaseUrlBeforeHash handles URL with hash', () { const url = 'https://example.com/web/f/#/test'; const expectedUrl = 'https://example.com/web/f/';