Skip to content

Commit

Permalink
Merge pull request #250 from humhub/f-brevo-provider
Browse files Browse the repository at this point in the history
Add Brevo provider
  • Loading branch information
marc-farre authored Oct 25, 2024
2 parents 6643251 + f04974a commit 80a4650
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
11 changes: 6 additions & 5 deletions lib/flavored/util/intent_plugin.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/flavored/web_view.f.dart';
import 'package:humhub/util/const.dart';
import 'package:humhub/util/init_from_url.dart';
import 'package:humhub/util/intent/mail_link_provider.dart';
import 'package:humhub/util/loading_provider.dart';
import 'package:loggy/loggy.dart';
import 'package:uni_links/uni_links.dart';
Expand Down Expand Up @@ -55,9 +56,9 @@ class IntentPluginFState extends ConsumerState<IntentPluginF> {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
_sub = uriLinkStream.listen((Uri? uri) async {
if (!mounted) return;
_latestUri = uri;
String? redirectUrl = uri?.toString();
if (!mounted && uri == null) return;
_latestUri = await MailProviderHandler.handleUniversalLink(uri!) ?? uri;
String? redirectUrl = _latestUri?.toString();
if (redirectUrl != null && navigatorKey.currentState != null) {
tryNavigateWithOpener(redirectUrl);
}
Expand Down Expand Up @@ -87,8 +88,8 @@ class IntentPluginFState extends ConsumerState<IntentPluginF> {
Uri? uri = await getInitialUri();
if (uri == null) return;
setState(() => _initialUri = uri);
_latestUri = uri;
String? redirectUrl = uri.toString();
_latestUri = await MailProviderHandler.handleUniversalLink(uri) ?? uri;
String? redirectUrl = _latestUri.toString();
if (navigatorKey.currentState != null) {
tryNavigateWithOpener(redirectUrl);
} else {
Expand Down
13 changes: 7 additions & 6 deletions lib/util/intent/intent_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:humhub/pages/web_view.dart';
import 'package:humhub/util/const.dart';
import 'package:humhub/util/intent/mail_link_provider.dart';
import 'package:humhub/util/loading_provider.dart';
import 'package:humhub/util/openers/universal_opener_controller.dart';
import 'package:loggy/loggy.dart';
Expand Down Expand Up @@ -56,10 +57,10 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
_sub = uriLinkStream.listen((Uri? uri) async {
if (!mounted) return;
_latestUri = uri;
String? redirectUrl = uri!.queryParameters['url'];
if (redirectUrl != null && navigatorKey.currentState != null) {
if (!mounted && uri == null) return;
_latestUri = await MailProviderHandler.handleUniversalLink(uri!) ?? uri;
String redirectUrl = _latestUri.toString();
if (navigatorKey.currentState != null) {
tryNavigateWithOpener(redirectUrl);
}
_err = null;
Expand Down Expand Up @@ -89,8 +90,8 @@ class IntentPluginState extends ConsumerState<IntentPlugin> {
final uri = await getInitialUri();
if (uri == null || !mounted) return;
setState(() => _initialUri = uri);
_latestUri = uri;
String? redirectUrl = uri.toString();
_latestUri = await MailProviderHandler.handleUniversalLink(uri) ?? uri;
String? redirectUrl = _latestUri.toString();
if (navigatorKey.currentState != null) {
tryNavigateWithOpener(redirectUrl);
} else {
Expand Down
36 changes: 36 additions & 0 deletions lib/util/intent/mail_link_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:loggy/loggy.dart';

class MailProviderHandler {
static Future<Uri?> handleUniversalLink(Uri url) async {
if (_isBrevoUrl(url)) {
return await _handleUniversalLinkBrevo(url);
} else {
return null;
}
}

static bool _isBrevoUrl(Uri url) {
return url.toString().contains('r.mail.inforisque.fr');
}

static Future<Uri?> _handleUniversalLinkBrevo(Uri url) async {
try {
final response = await http.get(
url,
headers: {'Accept': 'application/json'},
);
if (response.statusCode == 200) {
final Map<String, dynamic> data = jsonDecode(response.body);
return Uri.parse(data['url']);
} else {
logError('Failed to retrieve deep link. Status code: ${response.statusCode}');
return null;
}
} catch (e) {
logError('Error while handling universal link: $e');
return null;
}
}
}

0 comments on commit 80a4650

Please sign in to comment.