2023-07-29 09:46:24 +08:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-10-10 19:05:55 +08:00
|
|
|
use collab_database::database::WatchStream;
|
2023-11-01 11:45:35 +08:00
|
|
|
use collab_folder::FolderData;
|
2023-05-17 09:49:39 +08:00
|
|
|
use strum_macros::Display;
|
|
|
|
|
2023-04-04 08:41:16 +08:00
|
|
|
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
|
|
|
|
use flowy_error::FlowyResult;
|
2023-08-24 14:00:34 +08:00
|
|
|
use flowy_user_deps::cloud::{UserCloudConfig, UserCloudService};
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_user_deps::entities::*;
|
2022-01-11 13:34:45 +08:00
|
|
|
use lib_dispatch::prelude::*;
|
2023-07-29 09:46:24 +08:00
|
|
|
use lib_infra::future::{to_fut, Fut};
|
2023-05-17 09:49:39 +08:00
|
|
|
|
2023-08-14 12:57:59 +08:00
|
|
|
use crate::errors::FlowyError;
|
2023-05-17 09:49:39 +08:00
|
|
|
use crate::event_handler::*;
|
2023-08-14 12:57:59 +08:00
|
|
|
use crate::manager::UserManager;
|
2021-07-09 14:02:42 +08:00
|
|
|
|
2023-10-25 21:35:47 +08:00
|
|
|
#[rustfmt::skip]
|
2023-08-14 12:57:59 +08:00
|
|
|
pub fn init(user_session: Weak<UserManager>) -> AFPlugin {
|
2023-08-06 11:51:03 +08:00
|
|
|
let store_preferences = user_session
|
|
|
|
.upgrade()
|
2023-08-09 12:43:03 +08:00
|
|
|
.map(|session| session.get_store_preferences())
|
2023-08-06 11:51:03 +08:00
|
|
|
.unwrap();
|
2023-02-13 09:29:49 +08:00
|
|
|
AFPlugin::new()
|
|
|
|
.name("Flowy-User")
|
|
|
|
.state(user_session)
|
2023-08-06 11:51:03 +08:00
|
|
|
.state(store_preferences)
|
2023-02-13 09:29:49 +08:00
|
|
|
.event(UserEvent::SignIn, sign_in)
|
|
|
|
.event(UserEvent::SignUp, sign_up)
|
|
|
|
.event(UserEvent::InitUser, init_user_handler)
|
|
|
|
.event(UserEvent::GetUserProfile, get_user_profile_handler)
|
2023-11-05 14:00:24 +08:00
|
|
|
.event(UserEvent::SignOut, sign_out_handler)
|
2023-02-13 09:29:49 +08:00
|
|
|
.event(UserEvent::UpdateUserProfile, update_user_profile_handler)
|
|
|
|
.event(UserEvent::SetAppearanceSetting, set_appearance_setting)
|
|
|
|
.event(UserEvent::GetAppearanceSetting, get_appearance_setting)
|
|
|
|
.event(UserEvent::GetUserSetting, get_user_setting)
|
2023-08-17 23:46:39 +08:00
|
|
|
.event(UserEvent::SetCloudConfig, set_cloud_config_handler)
|
|
|
|
.event(UserEvent::GetCloudConfig, get_cloud_config_handler)
|
|
|
|
.event(UserEvent::SetEncryptionSecret, set_encrypt_secret_handler)
|
|
|
|
.event(UserEvent::CheckEncryptionSign, check_encrypt_secret_handler)
|
2023-10-07 09:58:44 +08:00
|
|
|
.event(UserEvent::OauthSignIn, oauth_handler)
|
|
|
|
.event(UserEvent::GetSignInURL, get_sign_in_url_handler)
|
2023-10-25 21:35:47 +08:00
|
|
|
.event(UserEvent::GetOauthURLWithProvider, sign_in_with_provider_handler)
|
2023-11-05 14:00:24 +08:00
|
|
|
.event(UserEvent::GetAllWorkspace, get_all_workspace_handler)
|
2023-07-29 09:46:24 +08:00
|
|
|
.event(UserEvent::OpenWorkspace, open_workspace_handler)
|
|
|
|
.event(UserEvent::UpdateNetworkState, update_network_state_handler)
|
2023-08-07 22:24:04 +08:00
|
|
|
.event(UserEvent::GetHistoricalUsers, get_historical_users_handler)
|
|
|
|
.event(UserEvent::OpenHistoricalUser, open_historical_users_handler)
|
2023-08-12 17:36:31 +08:00
|
|
|
.event(UserEvent::PushRealtimeEvent, push_realtime_event_handler)
|
2023-08-14 12:57:59 +08:00
|
|
|
.event(UserEvent::CreateReminder, create_reminder_event_handler)
|
|
|
|
.event(UserEvent::GetAllReminders, get_all_reminder_event_handler)
|
2023-10-02 09:12:24 +02:00
|
|
|
.event(UserEvent::RemoveReminder, remove_reminder_event_handler)
|
|
|
|
.event(UserEvent::UpdateReminder, update_reminder_event_handler)
|
2023-08-22 00:19:15 +08:00
|
|
|
.event(UserEvent::ResetWorkspace, reset_workspace_handler)
|
2023-10-02 09:12:24 +02:00
|
|
|
.event(UserEvent::SetDateTimeSettings, set_date_time_settings)
|
|
|
|
.event(UserEvent::GetDateTimeSettings, get_date_time_settings)
|
2023-10-25 21:35:47 +08:00
|
|
|
.event(UserEvent::SetNotificationSettings, set_notification_settings)
|
|
|
|
.event(UserEvent::GetNotificationSettings, get_notification_settings)
|
|
|
|
// Workspace member
|
|
|
|
.event(UserEvent::AddWorkspaceMember, add_workspace_member_handler)
|
|
|
|
.event(UserEvent::RemoveWorkspaceMember, delete_workspace_member_handler)
|
|
|
|
.event(UserEvent::GetWorkspaceMember, get_workspace_member_handler)
|
2023-11-05 14:00:24 +08:00
|
|
|
.event(UserEvent::UpdateWorkspaceMember, update_workspace_member_handler)
|
2023-10-25 21:35:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
|
|
|
|
#[event_err = "FlowyError"]
|
|
|
|
pub enum UserEvent {
|
2023-11-12 18:00:07 +08:00
|
|
|
/// Only use when the [Authenticator] is Local or SelfHosted
|
2023-10-25 21:35:47 +08:00
|
|
|
/// Logging into an account using a register email and password
|
|
|
|
#[event(input = "SignInPayloadPB", output = "UserProfilePB")]
|
|
|
|
SignIn = 0,
|
|
|
|
|
2023-11-12 18:00:07 +08:00
|
|
|
/// Only use when the [Authenticator] is Local or SelfHosted
|
2023-10-25 21:35:47 +08:00
|
|
|
/// Creating a new account
|
|
|
|
#[event(input = "SignUpPayloadPB", output = "UserProfilePB")]
|
|
|
|
SignUp = 1,
|
|
|
|
|
|
|
|
/// Logging out fo an account
|
|
|
|
#[event()]
|
|
|
|
SignOut = 2,
|
|
|
|
|
|
|
|
/// Update the user information
|
|
|
|
#[event(input = "UpdateUserProfilePayloadPB")]
|
|
|
|
UpdateUserProfile = 3,
|
|
|
|
|
|
|
|
/// Get the user information
|
|
|
|
#[event(output = "UserProfilePB")]
|
|
|
|
GetUserProfile = 4,
|
|
|
|
|
|
|
|
/// Initialize resources for the current user after launching the application
|
|
|
|
///
|
|
|
|
#[event()]
|
|
|
|
InitUser = 6,
|
|
|
|
|
|
|
|
/// Change the visual elements of the interface, such as theme, font and more
|
|
|
|
#[event(input = "AppearanceSettingsPB")]
|
|
|
|
SetAppearanceSetting = 7,
|
|
|
|
|
|
|
|
/// Get the appearance setting
|
|
|
|
#[event(output = "AppearanceSettingsPB")]
|
|
|
|
GetAppearanceSetting = 8,
|
|
|
|
|
|
|
|
/// Get the settings of the user, such as the user storage folder
|
|
|
|
#[event(output = "UserSettingPB")]
|
|
|
|
GetUserSetting = 9,
|
|
|
|
|
|
|
|
#[event(input = "OauthSignInPB", output = "UserProfilePB")]
|
|
|
|
OauthSignIn = 10,
|
|
|
|
|
|
|
|
/// Get the OAuth callback url
|
2023-11-12 18:00:07 +08:00
|
|
|
/// Only use when the [Authenticator] is AFCloud
|
2023-10-25 21:35:47 +08:00
|
|
|
#[event(input = "SignInUrlPayloadPB", output = "SignInUrlPB")]
|
|
|
|
GetSignInURL = 11,
|
|
|
|
|
|
|
|
#[event(input = "OauthProviderPB", output = "OauthProviderDataPB")]
|
|
|
|
GetOauthURLWithProvider = 12,
|
|
|
|
|
|
|
|
#[event(input = "UpdateCloudConfigPB")]
|
|
|
|
SetCloudConfig = 13,
|
|
|
|
|
2023-11-17 15:38:56 +08:00
|
|
|
#[event(output = "CloudSettingPB")]
|
2023-10-25 21:35:47 +08:00
|
|
|
GetCloudConfig = 14,
|
|
|
|
|
|
|
|
#[event(input = "UserSecretPB")]
|
|
|
|
SetEncryptionSecret = 15,
|
|
|
|
|
|
|
|
#[event(output = "UserEncryptionConfigurationPB")]
|
|
|
|
CheckEncryptionSign = 16,
|
|
|
|
|
|
|
|
/// Return the all the workspaces of the user
|
2023-11-05 14:00:24 +08:00
|
|
|
#[event(output = "RepeatedUserWorkspacePB")]
|
|
|
|
GetAllWorkspace = 17,
|
2023-10-25 21:35:47 +08:00
|
|
|
|
2023-11-05 14:00:24 +08:00
|
|
|
#[event(input = "UserWorkspaceIdPB")]
|
2023-10-25 21:35:47 +08:00
|
|
|
OpenWorkspace = 21,
|
|
|
|
|
|
|
|
#[event(input = "NetworkStatePB")]
|
|
|
|
UpdateNetworkState = 24,
|
|
|
|
|
|
|
|
#[event(output = "RepeatedHistoricalUserPB")]
|
|
|
|
GetHistoricalUsers = 25,
|
|
|
|
|
|
|
|
#[event(input = "HistoricalUserPB")]
|
|
|
|
OpenHistoricalUser = 26,
|
|
|
|
|
|
|
|
/// Push a realtime event to the user. Currently, the realtime event
|
2023-11-12 18:00:07 +08:00
|
|
|
/// is only used when the auth type is: [Authenticator::Supabase].
|
2023-10-25 21:35:47 +08:00
|
|
|
///
|
|
|
|
#[event(input = "RealtimePayloadPB")]
|
|
|
|
PushRealtimeEvent = 27,
|
|
|
|
|
|
|
|
#[event(input = "ReminderPB")]
|
|
|
|
CreateReminder = 28,
|
|
|
|
|
|
|
|
#[event(output = "RepeatedReminderPB")]
|
|
|
|
GetAllReminders = 29,
|
|
|
|
|
|
|
|
#[event(input = "ReminderIdentifierPB")]
|
|
|
|
RemoveReminder = 30,
|
|
|
|
|
|
|
|
#[event(input = "ReminderPB")]
|
|
|
|
UpdateReminder = 31,
|
|
|
|
|
|
|
|
#[event(input = "ResetWorkspacePB")]
|
|
|
|
ResetWorkspace = 32,
|
|
|
|
|
|
|
|
/// Change the Date/Time formats globally
|
|
|
|
#[event(input = "DateTimeSettingsPB")]
|
|
|
|
SetDateTimeSettings = 33,
|
|
|
|
|
|
|
|
/// Retrieve the Date/Time formats
|
|
|
|
#[event(output = "DateTimeSettingsPB")]
|
|
|
|
GetDateTimeSettings = 34,
|
|
|
|
|
|
|
|
#[event(input = "NotificationSettingsPB")]
|
|
|
|
SetNotificationSettings = 35,
|
|
|
|
|
|
|
|
#[event(output = "NotificationSettingsPB")]
|
|
|
|
GetNotificationSettings = 36,
|
|
|
|
|
|
|
|
#[event(output = "AddWorkspaceMemberPB")]
|
|
|
|
AddWorkspaceMember = 37,
|
|
|
|
|
|
|
|
#[event(output = "RemoveWorkspaceMemberPB")]
|
|
|
|
RemoveWorkspaceMember = 38,
|
|
|
|
|
|
|
|
#[event(output = "UpdateWorkspaceMemberPB")]
|
|
|
|
UpdateWorkspaceMember = 39,
|
|
|
|
|
|
|
|
#[event(output = "QueryWorkspacePB")]
|
|
|
|
GetWorkspaceMember = 40,
|
2021-06-29 16:52:29 +08:00
|
|
|
}
|
2022-01-10 23:45:59 +08:00
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
pub struct SignUpContext {
|
|
|
|
/// Indicate whether the user is new or not.
|
|
|
|
pub is_new: bool,
|
|
|
|
/// If the user is sign in as guest, and the is_new is true, then the folder data will be not
|
|
|
|
/// None.
|
|
|
|
pub local_folder: Option<FolderData>,
|
2023-05-31 17:42:14 +08:00
|
|
|
}
|
|
|
|
|
2023-01-31 19:30:48 +08:00
|
|
|
pub trait UserStatusCallback: Send + Sync + 'static {
|
2023-11-12 18:00:07 +08:00
|
|
|
/// When the [Authenticator] changed, this method will be called. Currently, the auth type
|
2023-07-14 13:37:13 +08:00
|
|
|
/// will be changed when the user sign in or sign up.
|
2023-11-12 18:00:07 +08:00
|
|
|
fn authenticator_did_changed(&self, _authenticator: Authenticator) {}
|
2023-07-14 13:37:13 +08:00
|
|
|
/// This will be called after the application launches if the user is already signed in.
|
|
|
|
/// If the user is not signed in, this method will not be called
|
2023-08-12 17:36:31 +08:00
|
|
|
fn did_init(
|
|
|
|
&self,
|
|
|
|
user_id: i64,
|
2023-08-18 22:32:51 +08:00
|
|
|
cloud_config: &Option<UserCloudConfig>,
|
2023-08-12 17:36:31 +08:00
|
|
|
user_workspace: &UserWorkspace,
|
|
|
|
device_id: &str,
|
|
|
|
) -> Fut<FlowyResult<()>>;
|
2023-07-14 13:37:13 +08:00
|
|
|
/// Will be called after the user signed in.
|
2023-08-12 17:36:31 +08:00
|
|
|
fn did_sign_in(
|
|
|
|
&self,
|
|
|
|
user_id: i64,
|
|
|
|
user_workspace: &UserWorkspace,
|
|
|
|
device_id: &str,
|
|
|
|
) -> Fut<FlowyResult<()>>;
|
2023-07-14 13:37:13 +08:00
|
|
|
/// Will be called after the user signed up.
|
2023-07-29 09:46:24 +08:00
|
|
|
fn did_sign_up(
|
|
|
|
&self,
|
2023-08-28 13:28:24 +08:00
|
|
|
is_new_user: bool,
|
2023-07-29 09:46:24 +08:00
|
|
|
user_profile: &UserProfile,
|
|
|
|
user_workspace: &UserWorkspace,
|
2023-08-12 17:36:31 +08:00
|
|
|
device_id: &str,
|
2023-07-29 09:46:24 +08:00
|
|
|
) -> Fut<FlowyResult<()>>;
|
|
|
|
|
2023-04-04 08:41:16 +08:00
|
|
|
fn did_expired(&self, token: &str, user_id: i64) -> Fut<FlowyResult<()>>;
|
2023-07-29 09:46:24 +08:00
|
|
|
fn open_workspace(&self, user_id: i64, user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>>;
|
2023-08-12 17:36:31 +08:00
|
|
|
fn did_update_network(&self, _reachable: bool) {}
|
2023-01-31 19:30:48 +08:00
|
|
|
}
|
|
|
|
|
2023-05-21 18:53:59 +08:00
|
|
|
/// The user cloud service provider.
|
|
|
|
/// The provider can be supabase, firebase, aws, or any other cloud service.
|
|
|
|
pub trait UserCloudServiceProvider: Send + Sync + 'static {
|
2023-10-10 19:05:55 +08:00
|
|
|
fn set_token(&self, token: &str) -> Result<(), FlowyError>;
|
|
|
|
fn subscribe_token_state(&self) -> Option<WatchStream<UserTokenState>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-09-01 22:27:29 +08:00
|
|
|
fn set_enable_sync(&self, uid: i64, enable_sync: bool);
|
2023-08-17 23:46:39 +08:00
|
|
|
fn set_encrypt_secret(&self, secret: String);
|
2023-11-12 18:00:07 +08:00
|
|
|
fn set_authenticator(&self, authenticator: Authenticator);
|
|
|
|
fn get_authenticator(&self) -> Authenticator;
|
2023-08-24 14:00:34 +08:00
|
|
|
fn get_user_service(&self) -> Result<Arc<dyn UserCloudService>, FlowyError>;
|
2023-11-17 15:38:56 +08:00
|
|
|
fn service_url(&self) -> String;
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> UserCloudServiceProvider for Arc<T>
|
|
|
|
where
|
|
|
|
T: UserCloudServiceProvider,
|
|
|
|
{
|
2023-10-10 19:05:55 +08:00
|
|
|
fn set_token(&self, token: &str) -> Result<(), FlowyError> {
|
|
|
|
(**self).set_token(token)
|
|
|
|
}
|
|
|
|
|
2023-09-01 22:27:29 +08:00
|
|
|
fn set_enable_sync(&self, uid: i64, enable_sync: bool) {
|
|
|
|
(**self).set_enable_sync(uid, enable_sync)
|
2023-08-17 23:46:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_encrypt_secret(&self, secret: String) {
|
|
|
|
(**self).set_encrypt_secret(secret)
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:00:07 +08:00
|
|
|
fn set_authenticator(&self, authenticator: Authenticator) {
|
|
|
|
(**self).set_authenticator(authenticator)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_authenticator(&self) -> Authenticator {
|
|
|
|
(**self).get_authenticator()
|
2023-05-23 23:55:21 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 14:00:34 +08:00
|
|
|
fn get_user_service(&self) -> Result<Arc<dyn UserCloudService>, FlowyError> {
|
2023-07-29 09:46:24 +08:00
|
|
|
(**self).get_user_service()
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
2023-08-07 22:24:04 +08:00
|
|
|
|
2023-11-17 15:38:56 +08:00
|
|
|
fn service_url(&self) -> String {
|
|
|
|
(**self).service_url()
|
2023-08-07 22:24:04 +08:00
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
/// Acts as a placeholder [UserStatusCallback] for the user session, but does not perform any function
|
|
|
|
pub(crate) struct DefaultUserStatusCallback;
|
|
|
|
impl UserStatusCallback for DefaultUserStatusCallback {
|
2023-08-12 17:36:31 +08:00
|
|
|
fn did_init(
|
|
|
|
&self,
|
|
|
|
_user_id: i64,
|
2023-08-18 22:32:51 +08:00
|
|
|
_cloud_config: &Option<UserCloudConfig>,
|
2023-08-12 17:36:31 +08:00
|
|
|
_user_workspace: &UserWorkspace,
|
|
|
|
_device_id: &str,
|
|
|
|
) -> Fut<FlowyResult<()>> {
|
2023-07-14 13:37:13 +08:00
|
|
|
to_fut(async { Ok(()) })
|
|
|
|
}
|
|
|
|
|
2023-08-12 17:36:31 +08:00
|
|
|
fn did_sign_in(
|
|
|
|
&self,
|
|
|
|
_user_id: i64,
|
|
|
|
_user_workspace: &UserWorkspace,
|
|
|
|
_device_id: &str,
|
|
|
|
) -> Fut<FlowyResult<()>> {
|
2023-07-14 13:37:13 +08:00
|
|
|
to_fut(async { Ok(()) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn did_sign_up(
|
|
|
|
&self,
|
2023-08-28 13:28:24 +08:00
|
|
|
_is_new_user: bool,
|
2023-07-14 13:37:13 +08:00
|
|
|
_user_profile: &UserProfile,
|
2023-07-29 09:46:24 +08:00
|
|
|
_user_workspace: &UserWorkspace,
|
2023-08-12 17:36:31 +08:00
|
|
|
_device_id: &str,
|
2023-07-14 13:37:13 +08:00
|
|
|
) -> Fut<FlowyResult<()>> {
|
|
|
|
to_fut(async { Ok(()) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn did_expired(&self, _token: &str, _user_id: i64) -> Fut<FlowyResult<()>> {
|
|
|
|
to_fut(async { Ok(()) })
|
|
|
|
}
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
fn open_workspace(&self, _user_id: i64, _user_workspace: &UserWorkspace) -> Fut<FlowyResult<()>> {
|
|
|
|
to_fut(async { Ok(()) })
|
|
|
|
}
|
2023-07-14 13:37:13 +08:00
|
|
|
}
|