AppFlowy/frontend/appflowy_flutter/lib/startup/tasks/appflowy_cloud_task.dart
Bartosz Sypytkowski fd5299a13d
move to latest appflowy collab version (#5894)
* chore: move to latest appflowy collab version

* chore: filter mapping

* chore: remove mutex folder

* chore: cleanup borrow checker issues

* chore: fixed flowy user crate compilation errors

* chore: removed parking lot crate

* chore: adjusting non locking approach

* chore: remove with folder method

* chore: fix folder manager

* chore: fixed workspace database compilation errors

* chore: initialize database plugins

* chore: fix locks in flowy core

* chore: remove supabase

* chore: async traits

* chore: add mutexes in dart ffi

* chore: post rebase fixes

* chore: remove supabase dart code

* chore: fix deadlock

* chore: fix page_id is empty

* chore: use data source to init collab

* chore: fix user awareness test

* chore: fix database deadlock

* fix: initialize user awareness

* chore: fix open workspace test

* chore: fix import csv

* chore: fix update row meta deadlock

* chore: fix document size test

* fix: timestamp set/get type convert

* fix: calculation

* chore: revert Arc to Rc

* chore: attach plugin to database and database row

* chore: async get row

* chore: clippy

* chore: fix tauri build

* chore: clippy

* fix: duplicate view deadlock

* chore: fmt

* chore: tauri build

---------

Co-authored-by: nathan <nathan@appflowy.io>
2024-08-18 11:16:42 +08:00

224 lines
6.3 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:app_links/app_links.dart';
import 'package:appflowy/env/cloud_env.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/startup/tasks/app_widget.dart';
import 'package:appflowy/user/application/auth/auth_error.dart';
import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy/user/application/auth/device_id.dart';
import 'package:appflowy/user/application/user_auth_listener.dart';
import 'package:appflowy/workspace/application/subscription_success_listenable/subscription_success_listenable.dart';
import 'package:appflowy/workspace/presentation/home/toast.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:url_protocol/url_protocol.dart';
const appflowyDeepLinkSchema = 'appflowy-flutter';
class AppFlowyCloudDeepLink {
AppFlowyCloudDeepLink() {
if (_deeplinkSubscription == null) {
_deeplinkSubscription = _appLinks.uriLinkStream.listen(
(Uri? uri) async {
Log.info('onDeepLink: ${uri.toString()}');
await _handleUri(uri);
},
onError: (Object err, StackTrace stackTrace) {
Log.error('on DeepLink stream error: ${err.toString()}', stackTrace);
_deeplinkSubscription?.cancel();
_deeplinkSubscription = null;
},
);
if (Platform.isWindows) {
// register deep link for Windows
registerProtocolHandler(appflowyDeepLinkSchema);
}
} else {
_deeplinkSubscription?.resume();
}
}
final _appLinks = AppLinks();
ValueNotifier<DeepLinkResult?>? _stateNotifier = ValueNotifier(null);
Completer<FlowyResult<UserProfilePB, FlowyError>>? _completer;
// The AppLinks is a singleton, so we need to cancel the previous subscription
// before creating a new one.
static StreamSubscription<Uri?>? _deeplinkSubscription;
Future<void> dispose() async {
_deeplinkSubscription?.pause();
_stateNotifier?.dispose();
_stateNotifier = null;
}
void registerCompleter(
Completer<FlowyResult<UserProfilePB, FlowyError>> completer,
) {
_completer = completer;
}
VoidCallback subscribeDeepLinkLoadingState(
ValueChanged<DeepLinkResult> listener,
) {
void listenerFn() {
if (_stateNotifier?.value != null) {
listener(_stateNotifier!.value!);
}
}
_stateNotifier?.addListener(listenerFn);
return listenerFn;
}
void unsubscribeDeepLinkLoadingState(VoidCallback listener) =>
_stateNotifier?.removeListener(listener);
Future<void> _handleUri(
Uri? uri,
) async {
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.none);
if (uri == null) {
Log.error('onDeepLinkError: Unexpected empty deep link callback');
_completer?.complete(FlowyResult.failure(AuthError.emptyDeepLink));
_completer = null;
return;
}
if (_isPaymentSuccessUri(uri)) {
Log.debug("Payment success deep link: ${uri.toString()}");
final plan = uri.queryParameters['plan'];
return getIt<SubscriptionSuccessListenable>().onPaymentSuccess(plan);
}
return _isAuthCallbackDeepLink(uri).fold(
(_) async {
final deviceId = await getDeviceId();
final payload = OauthSignInPB(
authenticator: AuthenticatorPB.AppFlowyCloud,
map: {
AuthServiceMapKeys.signInURL: uri.toString(),
AuthServiceMapKeys.deviceId: deviceId,
},
);
_stateNotifier?.value = DeepLinkResult(state: DeepLinkState.loading);
final result = await UserEventOauthSignIn(payload).send();
_stateNotifier?.value = DeepLinkResult(
state: DeepLinkState.finish,
result: result,
);
// If there is no completer, runAppFlowy() will be called.
if (_completer == null) {
await result.fold(
(_) async {
await runAppFlowy();
},
(err) {
Log.error(err);
final context = AppGlobals.rootNavKey.currentState?.context;
if (context != null) {
showSnackBarMessage(
context,
err.msg,
);
}
},
);
} else {
_completer?.complete(result);
_completer = null;
}
},
(err) {
Log.error('onDeepLinkError: Unexpected deep link: $err');
if (_completer == null) {
final context = AppGlobals.rootNavKey.currentState?.context;
if (context != null) {
showSnackBarMessage(
context,
err.msg,
);
}
} else {
_completer?.complete(FlowyResult.failure(err));
_completer = null;
}
},
);
}
FlowyResult<void, FlowyError> _isAuthCallbackDeepLink(Uri uri) {
if (uri.fragment.contains('access_token')) {
return FlowyResult.success(null);
}
return FlowyResult.failure(
FlowyError.create()
..code = ErrorCode.MissingAuthField
..msg = uri.path,
);
}
bool _isPaymentSuccessUri(Uri uri) {
return uri.host == 'payment-success';
}
}
class InitAppFlowyCloudTask extends LaunchTask {
UserAuthStateListener? _authStateListener;
bool isLoggingOut = false;
@override
Future<void> initialize(LaunchContext context) async {
if (!isAppFlowyCloudEnabled) {
return;
}
_authStateListener = UserAuthStateListener();
_authStateListener?.start(
didSignIn: () {
isLoggingOut = false;
},
onInvalidAuth: (message) async {
Log.error(message);
if (!isLoggingOut) {
await runAppFlowy();
}
},
);
}
@override
Future<void> dispose() async {
await _authStateListener?.stop();
_authStateListener = null;
}
}
class DeepLinkResult {
DeepLinkResult({
required this.state,
this.result,
});
final DeepLinkState state;
final FlowyResult<UserProfilePB, FlowyError>? result;
}
enum DeepLinkState {
none,
loading,
finish,
}