chore: clippy

This commit is contained in:
Nathan 2025-04-19 14:07:42 +08:00
parent edc5710e32
commit 3a05a4851f
4 changed files with 144 additions and 39 deletions

View File

@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE user_workspace_table
DROP COLUMN auth_type;

View File

@ -0,0 +1,24 @@
-- Your SQL goes here
ALTER TABLE user_workspace_table
ADD COLUMN auth_type INTEGER NOT NULL DEFAULT 1;
-- 2. Backfill from user_table.auth_type
UPDATE user_workspace_table
SET auth_type = (SELECT ut.auth_type
FROM user_table ut
WHERE ut.id = CAST(user_workspace_table.uid AS TEXT))
WHERE EXISTS (SELECT 1
FROM user_table ut
WHERE ut.id = CAST(user_workspace_table.uid AS TEXT));
ALTER TABLE user_table DROP COLUMN stability_ai_key;
ALTER TABLE user_table DROP COLUMN openai_key;
ALTER TABLE user_table DROP COLUMN workspace;
ALTER TABLE user_table DROP COLUMN encryption_type;
ALTER TABLE user_table DROP COLUMN ai_model;
CREATE TABLE workspace_setting_table (
id TEXT PRIMARY KEY NOT NULL ,
disable_search_indexing BOOLEAN DEFAULT FALSE NOT NULL ,
ai_model TEXT DEFAULT "" NOT NULL
);

View File

@ -0,0 +1,72 @@
use client_api::entity::AFWorkspaceSettings;
use flowy_error::FlowyError;
use flowy_sqlite::schema::workspace_setting_table;
use flowy_sqlite::schema::workspace_setting_table::dsl;
use flowy_sqlite::DBConnection;
use flowy_sqlite::{query_dsl::*, ExpressionMethods};
use uuid::Uuid;
#[derive(Clone, Default, Queryable, Identifiable, Insertable)]
#[diesel(table_name = workspace_setting_table)]
pub struct WorkspaceSettingsTable {
pub id: String,
pub disable_search_indexing: bool,
pub ai_model: String,
}
#[derive(AsChangeset, Identifiable, Default, Debug)]
#[diesel(table_name = workspace_setting_table)]
pub struct WorkspaceSettingsChangeset {
pub id: String,
pub disable_search_indexing: Option<bool>,
pub ai_model: Option<String>,
}
impl WorkspaceSettingsTable {
pub fn from_workspace_settings(workspace_id: &Uuid, settings: &AFWorkspaceSettings) -> Self {
Self {
id: workspace_id.to_string(),
disable_search_indexing: settings.disable_search_indexing,
ai_model: settings.ai_model.clone(),
}
}
}
pub fn update_workspace_setting(
conn: &mut DBConnection,
changeset: WorkspaceSettingsChangeset,
) -> Result<(), FlowyError> {
diesel::update(dsl::workspace_setting_table)
.filter(workspace_setting_table::id.eq(changeset.id.clone()))
.set(changeset)
.execute(conn)?;
Ok(())
}
/// Upserts a workspace setting into the database.
pub fn upsert_workspace_setting(
conn: &mut DBConnection,
settings: WorkspaceSettingsTable,
) -> Result<(), FlowyError> {
diesel::insert_into(dsl::workspace_setting_table)
.values(settings.clone())
.on_conflict(workspace_setting_table::id)
.do_update()
.set((
workspace_setting_table::disable_search_indexing.eq(settings.disable_search_indexing),
workspace_setting_table::ai_model.eq(settings.ai_model),
))
.execute(conn)?;
Ok(())
}
/// Selects a workspace setting by id from the database.
pub fn select_workspace_setting(
conn: &mut DBConnection,
id: &str,
) -> Result<WorkspaceSettingsTable, FlowyError> {
let setting = dsl::workspace_setting_table
.filter(workspace_setting_table::id.eq(id))
.first::<WorkspaceSettingsTable>(conn)?;
Ok(setting)
}

View File

@ -5,17 +5,6 @@ use client_api::entity::billing_dto::{SubscriptionPlan, WorkspaceUsageAndLimit};
use std::str::FromStr;
use std::sync::Arc;
use collab_integrate::CollabKVDB;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_folder_pub::entities::{ImportFrom, ImportedCollabData, ImportedFolderData};
use flowy_sqlite::schema::user_workspace_table;
use flowy_sqlite::{query_dsl::*, DBConnection, ExpressionMethods};
use flowy_user_pub::entities::{
AuthType, Role, UserWorkspace, WorkspaceInvitation, WorkspaceInvitationStatus, WorkspaceMember,
};
use tracing::{error, info, instrument, trace, warn};
use uuid::Uuid;
use crate::entities::{
RepeatedUserWorkspacePB, SubscribeWorkspacePB, SuccessWorkspaceSubscriptionPB,
UpdateUserWorkspaceSettingPB, UserWorkspacePB, WorkspaceSettingsPB, WorkspaceSubscriptionInfoPB,
@ -38,7 +27,18 @@ use crate::services::sqlite_sql::workspace_sql::{
UserWorkspaceChangeset, UserWorkspaceTable,
};
use crate::user_manager::UserManager;
use collab_integrate::CollabKVDB;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_folder_pub::entities::{ImportFrom, ImportedCollabData, ImportedFolderData};
use flowy_sqlite::schema::user_workspace_table;
use flowy_sqlite::{query_dsl::*, ConnectionPool, DBConnection, ExpressionMethods};
use flowy_user_pub::cloud::UserCloudServiceProvider;
use flowy_user_pub::entities::{
AuthType, Role, UserWorkspace, WorkspaceInvitation, WorkspaceInvitationStatus, WorkspaceMember,
};
use flowy_user_pub::session::Session;
use tracing::{error, info, instrument, trace, warn};
use uuid::Uuid;
impl UserManager {
/// Import appflowy data from the given path.
@ -561,51 +561,29 @@ impl UserManager {
Ok(workspace_settings) => {
trace!("workspace settings found in local db");
let pb = WorkspaceSettingsPB::from(workspace_settings);
let old_pb = pb.clone();
let workspace_id = *workspace_id;
// Spawn a task to sync remote settings using the helper
let pool = self.db_pool(uid)?;
let cloud_service = self.cloud_service.clone();
tokio::spawn(async move {
let cloud_service = cloud_service.get_user_service()?;
let settings = cloud_service.get_workspace_setting(&workspace_id).await?;
let new_pb = WorkspaceSettingsPB::from(&settings);
if new_pb != old_pb {
trace!("workspace settings updated");
send_notification(
&uid.to_string(),
UserNotification::DidUpdateWorkspaceSetting,
)
.payload(new_pb)
.send();
if let Ok(mut conn) = pool.get() {
upsert_workspace_setting(
&mut conn,
WorkspaceSettingsTable::from_workspace_settings(&workspace_id, &settings),
)?;
}
}
Ok::<_, FlowyError>(())
let _ = sync_workspace_settings(cloud_service, workspace_id, old_pb, uid, pool).await;
});
Ok(pb)
},
Err(err) => {
if err.is_record_not_found() {
trace!("No workspace settings found, fetch from remote");
let settings = self
.cloud_service
.get_user_service()?
.get_workspace_setting(&workspace_id)
.await?;
let service = self.cloud_service.get_user_service()?;
let settings = service.get_workspace_setting(&workspace_id).await?;
let pb = WorkspaceSettingsPB::from(&settings);
let mut conn = self.db_connection(uid)?;
upsert_workspace_setting(
&mut conn,
WorkspaceSettingsTable::from_workspace_settings(&workspace_id, &settings),
)?;
Ok::<_, FlowyError>(pb.clone())
Ok(pb)
} else {
Err(err)
}
@ -777,3 +755,31 @@ fn is_older_than_n_minutes(updated_at: NaiveDateTime, minutes: i64) -> bool {
None => false,
}
}
async fn sync_workspace_settings(
cloud_service: Arc<dyn UserCloudServiceProvider>,
workspace_id: Uuid,
old_pb: WorkspaceSettingsPB,
uid: i64,
pool: Arc<ConnectionPool>,
) -> FlowyResult<()> {
let service = cloud_service.get_user_service()?;
let settings = service.get_workspace_setting(&workspace_id).await?;
let new_pb = WorkspaceSettingsPB::from(&settings);
if new_pb != old_pb {
trace!("workspace settings updated");
send_notification(
&uid.to_string(),
UserNotification::DidUpdateWorkspaceSetting,
)
.payload(new_pb)
.send();
if let Ok(mut conn) = pool.get() {
upsert_workspace_setting(
&mut conn,
WorkspaceSettingsTable::from_workspace_settings(&workspace_id, &settings),
)?;
}
}
Ok(())
}