Skip to content

Commit

Permalink
Merge pull request #144 from humhub/143-support-for-both-pretty-and-n…
Browse files Browse the repository at this point in the history
…on-pretty-urls

143 support for both pretty and non pretty urls
  • Loading branch information
luke- authored Nov 30, 2023
2 parents ed63324 + 2a4e6a3 commit 4688626
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
9 changes: 5 additions & 4 deletions lib/models/manifest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class Manifest {
final String backgroundColor;
final String themeColor;

Manifest(this.display, this.startUrl, this.shortName, this.name,
this.backgroundColor, this.themeColor);
Manifest(this.display, this.startUrl, this.shortName, this.name, this.backgroundColor, this.themeColor);

String get baseUrl {
Uri url = Uri.parse(startUrl);
Expand All @@ -36,8 +35,10 @@ class Manifest {
'theme_color': themeColor,
};

static Future<Manifest> Function(Dio dio) get(String url) => (dio) async {
final res = await dio.get('$url/manifest.json');
static Future<Manifest> Function(Dio dio) get(String url, {bool isUriPretty = true}) => (dio) async {
Response<dynamic> res = !isUriPretty
? await dio.get('$url/index.php?r=web%2Fpwa-manifest%2Findex')
: await dio.get('$url/manifest.json');
return Manifest.fromJson(res.data);
};
}
18 changes: 12 additions & 6 deletions lib/util/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:humhub/util/const.dart';
import 'package:humhub/util/providers.dart';
import 'package:loggy/loggy.dart';
import 'package:webview_flutter/webview_flutter.dart';

// ignore_for_file: use_build_context_synchronously
extension MyCookies on WebViewCookieManager {
Future<void> setMyCookies(Manifest manifest) async {
Expand Down Expand Up @@ -55,21 +56,19 @@ extension MyWebViewController on InAppWebViewController {
var isHide = ref.read(humHubProvider).isHideDialog;
isHide
? SystemNavigator.pop()
: Navigator.of(context).pushNamedAndRemoveUntil(
Opener.path, (Route<dynamic> route) => false);
: Navigator.of(context).pushNamedAndRemoveUntil(Opener.path, (Route<dynamic> route) => false);
}
}

class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
try{
try {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF$hexColor";
}
return int.parse(hexColor, radix: 16);
}
catch(e){
} catch (e) {
logError("Color from manifest is not valid use primary color");
return primaryColor.value;
}
Expand All @@ -93,6 +92,13 @@ extension AsyncValueX<T> on AsyncValue<T> {
extension FutureAsyncValueX<T> on Future<AsyncValue<T>> {
Future<T?> get valueOrNull => then(
(asyncValue) => asyncValue.asData?.value,
);
);
}

extension PrettyUri on Uri {
bool isUriPretty() {
RegExp regex = RegExp(r'index\.php.*[?&]r=');
String path = Uri.decodeComponent(toString());
return !regex.hasMatch(path);
}
}
1 change: 0 additions & 1 deletion lib/util/notifications/channel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:humhub/pages/web_view.dart';
import 'package:humhub/util/universal_opener_controller.dart';
import 'package:humhub/util/router.dart';
Expand Down
21 changes: 13 additions & 8 deletions lib/util/opener_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart';
import 'package:humhub/models/hum_hub.dart';
import 'package:humhub/models/manifest.dart';
import 'package:humhub/util/extensions.dart';
import 'package:humhub/util/providers.dart';
import 'package:http/http.dart' as http;
import 'package:loggy/loggy.dart';
Expand All @@ -25,15 +26,19 @@ class OpenerController {

findManifest(String url) async {
Uri uri = assumeUrl(url);
for (var i = uri.pathSegments.length - 1; i >= 0; i--) {
String urlIn = "${uri.origin}/${uri.pathSegments.getRange(0, i).join('/')}";
asyncData = await APIProvider.of(ref).request(Manifest.get(i != 0 ? urlIn : uri.origin));
if (!asyncData!.hasError) break;
}
if (uri.pathSegments.isEmpty) {
asyncData = await APIProvider.of(ref).request(Manifest.get(uri.origin));
if (!uri.isUriPretty()) {
asyncData = await APIProvider.of(ref).request(Manifest.get(uri.origin, isUriPretty: false));
} else {
for (var i = uri.pathSegments.length - 1; i >= 0; i--) {
String urlIn = "${uri.origin}/${uri.pathSegments.getRange(0, i).join('/')}";
asyncData = await APIProvider.of(ref).request(Manifest.get(i != 0 ? urlIn : uri.origin));
if (!asyncData!.hasError) break;
}
if (uri.pathSegments.isEmpty) {
asyncData = await APIProvider.of(ref).request(Manifest.get(uri.origin));
}
}
if(asyncData!.hasError) return;
if (asyncData!.hasError) return;
await checkHumHubModuleView(asyncData!.value!.startUrl);
}

Expand Down
23 changes: 23 additions & 0 deletions test/pretty_urls_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:humhub/util/extensions.dart';

Future<void> main() async {
group("Testing pretty urls", () {
test("Community url", () {
Uri uri = Uri.parse("https://community.humhub.com/dashboard");
expect(uri.isUriPretty(), true);
});
test("Lado", () {
Uri uri = Uri.parse("https://labo-sphere.fr/index.php?r=user%2Fauth%2Flogin");
expect(uri.isUriPretty(), false);
});
test("Test12345", () {
Uri uri = Uri.parse("https://sometestproject12345.humhub.com/dashboard");
expect(uri.isUriPretty(), true);
});
test("Local", () {
Uri uri = Uri.parse("http://192.168.64.3/humhub/index.php?r=user%2Fauth%2Flogin");
expect(uri.isUriPretty(), false);
});
});
}

0 comments on commit 4688626

Please sign in to comment.