| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | import 'package:appflowy/core/config/kv.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/core/config/kv_keys.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/env/backend_env.dart'; | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | import 'package:appflowy/env/env.dart'; | 
					
						
							| 
									
										
										
										
											2023-08-03 08:48:04 +08:00
										 |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | import 'package:appflowy_backend/log.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Sets the cloud type for the application.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// This method updates the cloud type setting in the key-value storage
 | 
					
						
							|  |  |  | /// using the [KeyValueStorage] service. The cloud type is identified
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | /// by the [AuthenticatorType] enum.
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// [ty] - The type of cloud to be set. It must be one of the values from
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | /// [AuthenticatorType] enum. The corresponding integer value of the enum is stored:
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// - `CloudType.local` is stored as "0".
 | 
					
						
							|  |  |  | /// - `CloudType.supabase` is stored as "1".
 | 
					
						
							|  |  |  | /// - `CloudType.appflowyCloud` is stored as "2".
 | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  | Future<void> _setAuthenticatorType(AuthenticatorType ty) async { | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   switch (ty) { | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     case AuthenticatorType.local: | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |       await getIt<KeyValueStorage>().set(KVKeys.kCloudType, 0.toString()); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     case AuthenticatorType.supabase: | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |       await getIt<KeyValueStorage>().set(KVKeys.kCloudType, 1.toString()); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     case AuthenticatorType.appflowyCloud: | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |       await getIt<KeyValueStorage>().set(KVKeys.kCloudType, 2.toString()); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |     case AuthenticatorType.appflowyCloudSelfHost: | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |       await getIt<KeyValueStorage>().set(KVKeys.kCloudType, 3.toString()); | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |     case AuthenticatorType.appflowyCloudDevelop: | 
					
						
							|  |  |  |       await getIt<KeyValueStorage>().set(KVKeys.kCloudType, 4.toString()); | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  | const String kAppflowyCloudUrl = "https://beta.appflowy.cloud"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Retrieves the currently set cloud type.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// This method fetches the cloud type setting from the key-value storage
 | 
					
						
							|  |  |  | /// using the [KeyValueStorage] service and returns the corresponding
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | /// [AuthenticatorType] enum value.
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// Returns:
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | /// A Future that resolves to a [AuthenticatorType] enum value representing the
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// currently set cloud type. The default return value is `CloudType.local`
 | 
					
						
							|  |  |  | /// if no valid setting is found.
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | ///
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | Future<AuthenticatorType> getAuthenticatorType() async { | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   final value = await getIt<KeyValueStorage>().get(KVKeys.kCloudType); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   if (value == null && !integrationMode().isUnitTest) { | 
					
						
							| 
									
										
										
										
											2024-01-21 21:47:14 +08:00
										 |  |  |     // if the cloud type is not set, then set it to AppFlowy Cloud as default.
 | 
					
						
							| 
									
										
										
										
											2024-02-02 09:27:11 +08:00
										 |  |  |     await useAppFlowyBetaCloudWithURL( | 
					
						
							|  |  |  |       kAppflowyCloudUrl, | 
					
						
							|  |  |  |       AuthenticatorType.appflowyCloud, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |     return AuthenticatorType.appflowyCloud; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   switch (value ?? "0") { | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |     case "0": | 
					
						
							|  |  |  |       return AuthenticatorType.local; | 
					
						
							|  |  |  |     case "1": | 
					
						
							|  |  |  |       return AuthenticatorType.supabase; | 
					
						
							|  |  |  |     case "2": | 
					
						
							|  |  |  |       return AuthenticatorType.appflowyCloud; | 
					
						
							|  |  |  |     case "3": | 
					
						
							|  |  |  |       return AuthenticatorType.appflowyCloudSelfHost; | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |     case "4": | 
					
						
							|  |  |  |       return AuthenticatorType.appflowyCloudDevelop; | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2024-02-02 09:27:11 +08:00
										 |  |  |       await useAppFlowyBetaCloudWithURL( | 
					
						
							|  |  |  |         kAppflowyCloudUrl, | 
					
						
							|  |  |  |         AuthenticatorType.appflowyCloud, | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2024-01-13 03:08:07 +08:00
										 |  |  |       return AuthenticatorType.appflowyCloud; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Determines whether authentication is enabled.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// This getter evaluates if authentication should be enabled based on the
 | 
					
						
							|  |  |  | /// current integration mode and cloud type settings.
 | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  | ///
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Returns:
 | 
					
						
							|  |  |  | /// A boolean value indicating whether authentication is enabled. It returns
 | 
					
						
							|  |  |  | /// `true` if the application is in release or develop mode, and the cloud type
 | 
					
						
							|  |  |  | /// is not set to `CloudType.local`. Additionally, it checks if either the
 | 
					
						
							|  |  |  | /// AppFlowy Cloud or Supabase configuration is valid.
 | 
					
						
							|  |  |  | /// Returns `false` otherwise.
 | 
					
						
							|  |  |  | bool get isAuthEnabled { | 
					
						
							| 
									
										
										
										
											2023-12-21 08:12:40 +08:00
										 |  |  |   final env = getIt<AppFlowyCloudSharedEnv>(); | 
					
						
							|  |  |  |   if (env.authenticatorType == AuthenticatorType.supabase) { | 
					
						
							|  |  |  |     return env.supabaseConfig.isValid; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |   if (env.authenticatorType.isAppFlowyCloudEnabled) { | 
					
						
							| 
									
										
										
										
											2023-12-21 08:12:40 +08:00
										 |  |  |     return env.appflowyCloudConfig.isValid; | 
					
						
							| 
									
										
										
										
											2023-08-03 08:48:04 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-21 08:12:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2023-08-03 08:48:04 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Checks if Supabase is enabled.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// This getter evaluates if Supabase should be enabled based on the
 | 
					
						
							|  |  |  | /// current integration mode and cloud type setting.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Returns:
 | 
					
						
							|  |  |  | /// A boolean value indicating whether Supabase is enabled. It returns `true`
 | 
					
						
							|  |  |  | /// if the application is in release or develop mode and the current cloud type
 | 
					
						
							|  |  |  | /// is `CloudType.supabase`. Otherwise, it returns `false`.
 | 
					
						
							| 
									
										
										
										
											2023-10-07 09:58:44 +08:00
										 |  |  | bool get isSupabaseEnabled { | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |   return currentCloudType().isSupabaseEnabled; | 
					
						
							| 
									
										
										
										
											2023-10-07 09:58:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Determines if AppFlowy Cloud is enabled.
 | 
					
						
							| 
									
										
										
										
											2023-10-07 09:58:44 +08:00
										 |  |  | bool get isAppFlowyCloudEnabled { | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |   return currentCloudType().isAppFlowyCloudEnabled; | 
					
						
							| 
									
										
										
										
											2023-10-07 09:58:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | enum AuthenticatorType { | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   local, | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  |   supabase, | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |   appflowyCloud, | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |   appflowyCloudSelfHost, | 
					
						
							|  |  |  |   // The 'appflowyCloudDevelop' type is used for develop purposes only.
 | 
					
						
							|  |  |  |   appflowyCloudDevelop; | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:42:39 +08:00
										 |  |  |   bool get isLocal => this == AuthenticatorType.local; | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   bool get isAppFlowyCloudEnabled => | 
					
						
							|  |  |  |       this == AuthenticatorType.appflowyCloudSelfHost || | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       this == AuthenticatorType.appflowyCloudDevelop || | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |       this == AuthenticatorType.appflowyCloud; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool get isSupabaseEnabled => this == AuthenticatorType.supabase; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   int get value { | 
					
						
							|  |  |  |     switch (this) { | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |       case AuthenticatorType.local: | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |       case AuthenticatorType.supabase: | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |       case AuthenticatorType.appflowyCloud: | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |         return 2; | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |       case AuthenticatorType.appflowyCloudSelfHost: | 
					
						
							|  |  |  |         return 3; | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       case AuthenticatorType.appflowyCloudDevelop: | 
					
						
							|  |  |  |         return 4; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-08 20:01:54 +07:00
										 |  |  |   static AuthenticatorType fromValue(int value) { | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  |     switch (value) { | 
					
						
							|  |  |  |       case 0: | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |         return AuthenticatorType.local; | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  |       case 1: | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |         return AuthenticatorType.supabase; | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  |       case 2: | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |         return AuthenticatorType.appflowyCloud; | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |       case 3: | 
					
						
							|  |  |  |         return AuthenticatorType.appflowyCloudSelfHost; | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       case 4: | 
					
						
							|  |  |  |         return AuthenticatorType.appflowyCloudDevelop; | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |         return AuthenticatorType.local; | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | AuthenticatorType currentCloudType() { | 
					
						
							|  |  |  |   return getIt<AppFlowyCloudSharedEnv>().authenticatorType; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | Future<void> _setAppFlowyCloudUrl(String? url) async { | 
					
						
							|  |  |  |   await getIt<KeyValueStorage>().set(KVKeys.kAppflowyCloudBaseURL, url ?? ''); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  | Future<void> useSelfHostedAppFlowyCloudWithURL(String url) async { | 
					
						
							|  |  |  |   await _setAuthenticatorType(AuthenticatorType.appflowyCloudSelfHost); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   await _setAppFlowyCloudUrl(url); | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 09:27:11 +08:00
										 |  |  | Future<void> useAppFlowyBetaCloudWithURL( | 
					
						
							|  |  |  |   String url, | 
					
						
							|  |  |  |   AuthenticatorType authenticatorType, | 
					
						
							|  |  |  | ) async { | 
					
						
							|  |  |  |   await _setAuthenticatorType(authenticatorType); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   await _setAppFlowyCloudUrl(url); | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<void> useLocalServer() async { | 
					
						
							|  |  |  |   await _setAuthenticatorType(AuthenticatorType.local); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<void> useSupabaseCloud({ | 
					
						
							|  |  |  |   required String url, | 
					
						
							|  |  |  |   required String anonKey, | 
					
						
							|  |  |  | }) async { | 
					
						
							|  |  |  |   await _setAuthenticatorType(AuthenticatorType.supabase); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   await setSupabaseServer(url, anonKey); | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | /// Use getIt<AppFlowyCloudSharedEnv>() to get the shared environment.
 | 
					
						
							|  |  |  | class AppFlowyCloudSharedEnv { | 
					
						
							|  |  |  |   AppFlowyCloudSharedEnv({ | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     required AuthenticatorType authenticatorType, | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |     required this.appflowyCloudConfig, | 
					
						
							|  |  |  |     required this.supabaseConfig, | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |   }) : _authenticatorType = authenticatorType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   final AuthenticatorType _authenticatorType; | 
					
						
							|  |  |  |   final AppFlowyCloudConfiguration appflowyCloudConfig; | 
					
						
							|  |  |  |   final SupabaseConfiguration supabaseConfig; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |   AuthenticatorType get authenticatorType => _authenticatorType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static Future<AppFlowyCloudSharedEnv> fromEnv() async { | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |     // If [Env.enableCustomCloud] is true, then use the custom cloud configuration.
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     if (Env.enableCustomCloud) { | 
					
						
							|  |  |  |       // Use the custom cloud configuration.
 | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       var authenticatorType = await getAuthenticatorType(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-30 09:33:34 +08:00
										 |  |  |       final appflowyCloudConfig = authenticatorType.isAppFlowyCloudEnabled | 
					
						
							|  |  |  |           ? await getAppFlowyCloudConfig(authenticatorType) | 
					
						
							|  |  |  |           : AppFlowyCloudConfiguration.defaultConfig(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       final supabaseCloudConfig = authenticatorType.isSupabaseEnabled | 
					
						
							|  |  |  |           ? await getSupabaseCloudConfig() | 
					
						
							|  |  |  |           : SupabaseConfiguration.defaultConfig(); | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // In the backend, the value '2' represents the use of AppFlowy Cloud. However, in the frontend,
 | 
					
						
							|  |  |  |       // we distinguish between [AuthenticatorType.appflowyCloudSelfHost] and [AuthenticatorType.appflowyCloud].
 | 
					
						
							|  |  |  |       // When the cloud type is [AuthenticatorType.appflowyCloudSelfHost] in the frontend, it should be
 | 
					
						
							|  |  |  |       // converted to [AuthenticatorType.appflowyCloud] to align with the backend representation,
 | 
					
						
							|  |  |  |       // where both types are indicated by the value '2'.
 | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       if (authenticatorType.isAppFlowyCloudEnabled) { | 
					
						
							|  |  |  |         authenticatorType = AuthenticatorType.appflowyCloud; | 
					
						
							| 
									
										
										
										
											2024-01-12 14:34:59 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |       return AppFlowyCloudSharedEnv( | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |         authenticatorType: authenticatorType, | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |         appflowyCloudConfig: appflowyCloudConfig, | 
					
						
							|  |  |  |         supabaseConfig: supabaseCloudConfig, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |       // Using the cloud settings from the .env file.
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |       final appflowyCloudConfig = AppFlowyCloudConfiguration( | 
					
						
							|  |  |  |         base_url: Env.afCloudUrl, | 
					
						
							|  |  |  |         ws_base_url: await _getAppFlowyCloudWSUrl(Env.afCloudUrl), | 
					
						
							|  |  |  |         gotrue_url: await _getAppFlowyCloudGotrueUrl(Env.afCloudUrl), | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return AppFlowyCloudSharedEnv( | 
					
						
							|  |  |  |         authenticatorType: AuthenticatorType.fromValue(Env.authenticatorType), | 
					
						
							|  |  |  |         appflowyCloudConfig: appflowyCloudConfig, | 
					
						
							|  |  |  |         supabaseConfig: SupabaseConfiguration.defaultConfig(), | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-27 11:42:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   String toString() { | 
					
						
							|  |  |  |     return 'authenticator: $_authenticatorType\n' | 
					
						
							|  |  |  |         'appflowy: ${appflowyCloudConfig.toJson()}\n' | 
					
						
							|  |  |  |         'supabase: ${supabaseConfig.toJson()})\n'; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  | Future<AppFlowyCloudConfiguration> configurationFromUri( | 
					
						
							|  |  |  |   Uri baseUri, | 
					
						
							|  |  |  |   String baseUrl, | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |   AuthenticatorType authenticatorType, | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  | ) async { | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |   // In development mode, the app is configured to access the AppFlowy cloud server directly through specific ports.
 | 
					
						
							|  |  |  |   // This setup bypasses the need for Nginx, meaning that the AppFlowy cloud should be running without an Nginx server
 | 
					
						
							|  |  |  |   // in the development environment.
 | 
					
						
							|  |  |  |   if (authenticatorType == AuthenticatorType.appflowyCloudDevelop) { | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |     return AppFlowyCloudConfiguration( | 
					
						
							|  |  |  |       base_url: "$baseUrl:8000", | 
					
						
							| 
									
										
										
										
											2024-03-07 12:50:28 +08:00
										 |  |  |       ws_base_url: "ws://${baseUri.host}:8000/ws/v1", | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |       gotrue_url: "$baseUrl:9999", | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return AppFlowyCloudConfiguration( | 
					
						
							|  |  |  |       base_url: baseUrl, | 
					
						
							| 
									
										
										
										
											2023-12-04 14:20:07 -08:00
										 |  |  |       ws_base_url: await _getAppFlowyCloudWSUrl(baseUrl), | 
					
						
							|  |  |  |       gotrue_url: await _getAppFlowyCloudGotrueUrl(baseUrl), | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  | Future<AppFlowyCloudConfiguration> getAppFlowyCloudConfig( | 
					
						
							|  |  |  |   AuthenticatorType authenticatorType, | 
					
						
							|  |  |  | ) async { | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |   final baseURL = await getAppFlowyCloudUrl(); | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     final uri = Uri.parse(baseURL); | 
					
						
							| 
									
										
										
										
											2024-01-15 12:53:53 +08:00
										 |  |  |     return await configurationFromUri(uri, baseURL, authenticatorType); | 
					
						
							| 
									
										
										
										
											2023-11-30 13:47:12 -08:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     Log.error("Failed to parse AppFlowy Cloud URL: $e"); | 
					
						
							|  |  |  |     return AppFlowyCloudConfiguration.defaultConfig(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<String> getAppFlowyCloudUrl() async { | 
					
						
							|  |  |  |   final result = | 
					
						
							|  |  |  |       await getIt<KeyValueStorage>().get(KVKeys.kAppflowyCloudBaseURL); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   return result ?? kAppflowyCloudUrl; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | Future<String> _getAppFlowyCloudWSUrl(String baseURL) async { | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     final uri = Uri.parse(baseURL); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Construct the WebSocket URL directly from the parsed URI.
 | 
					
						
							|  |  |  |     final wsScheme = uri.isScheme('HTTPS') ? 'wss' : 'ws'; | 
					
						
							| 
									
										
										
										
											2024-04-26 20:09:55 +08:00
										 |  |  |     final wsUrl = | 
					
						
							|  |  |  |         Uri(scheme: wsScheme, host: uri.host, port: uri.port, path: '/ws/v1'); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return wsUrl.toString(); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     Log.error("Failed to get WebSocket URL: $e"); | 
					
						
							|  |  |  |     return ""; | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  | Future<String> _getAppFlowyCloudGotrueUrl(String baseURL) async { | 
					
						
							|  |  |  |   return "$baseURL/gotrue"; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | Future<void> setSupabaseServer( | 
					
						
							|  |  |  |   String? url, | 
					
						
							|  |  |  |   String? anonKey, | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | ) async { | 
					
						
							|  |  |  |   assert( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |     (url != null && anonKey != null) || (url == null && anonKey == null), | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |     "Either both Supabase URL and anon key must be set, or both should be unset", | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   if (url == null) { | 
					
						
							|  |  |  |     await getIt<KeyValueStorage>().remove(KVKeys.kSupabaseURL); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     await getIt<KeyValueStorage>().set(KVKeys.kSupabaseURL, url); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (anonKey == null) { | 
					
						
							|  |  |  |     await getIt<KeyValueStorage>().remove(KVKeys.kSupabaseAnonKey); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     await getIt<KeyValueStorage>().set(KVKeys.kSupabaseAnonKey, anonKey); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Future<SupabaseConfiguration> getSupabaseCloudConfig() async { | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   final url = await _getSupabaseUrl(); | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   final anonKey = await _getSupabaseAnonKey(); | 
					
						
							|  |  |  |   return SupabaseConfiguration( | 
					
						
							|  |  |  |     url: url, | 
					
						
							|  |  |  |     anon_key: anonKey, | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | Future<String> _getSupabaseUrl() async { | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  |   final result = await getIt<KeyValueStorage>().get(KVKeys.kSupabaseURL); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   return result ?? ''; | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 11:54:47 +08:00
										 |  |  | Future<String> _getSupabaseAnonKey() async { | 
					
						
							|  |  |  |   final result = await getIt<KeyValueStorage>().get(KVKeys.kSupabaseAnonKey); | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   return result ?? ''; | 
					
						
							| 
									
										
										
										
											2023-10-02 17:22:22 +08:00
										 |  |  | } |