Skip to content

Commit

Permalink
chore: save workspace member info to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Jun 14, 2024
1 parent e2ce274 commit 8e12bc0
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 28 deletions.
22 changes: 18 additions & 4 deletions frontend/rust-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/rust-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ collab-database = { version = "0.2" }
collab-plugins = { version = "0.2" }
collab-user = { version = "0.2" }
yrs = "0.18.8"
validator = { version = "0.16.1", features = ["derive"] }

# Please using the following command to update the revision id
# Current directory: frontend
Expand Down
2 changes: 1 addition & 1 deletion frontend/rust-lib/flowy-chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ uuid.workspace = true
strum_macros = "0.21"
protobuf.workspace = true
bytes.workspace = true
validator = { version = "0.16.0", features = ["derive"] }
validator = { workspace = true, features = ["derive"] }
lib-infra = { workspace = true, features = ["isolate_flutter"] }
flowy-chat-pub.workspace = true
dashmap = "5.5"
Expand Down
2 changes: 1 addition & 1 deletion frontend/rust-lib/flowy-database2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ chrono-tz = "0.8.2"
csv = "1.1.6"
strum = "0.25"
strum_macros = "0.25"
validator = { version = "0.16.0", features = ["derive"] }
validator = { workspace = true, features = ["derive"] }

[dev-dependencies]
event-integration-test = { path = "../event-integration-test", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion frontend/rust-lib/flowy-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protobuf.workspace = true
bytes.workspace = true
anyhow.workspace = true
thiserror = "1.0"
validator = "0.16.0"
validator.workspace = true
tokio = { workspace = true, features = ["sync", "rt"] }

fancy-regex = { version = "0.11.0" }
Expand Down
4 changes: 2 additions & 2 deletions frontend/rust-lib/flowy-folder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ strum_macros = "0.21"
protobuf.workspace = true
uuid.workspace = true
tokio-stream = { workspace = true, features = ["sync"] }
serde = { workspace = true, features = ["derive"]}
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
validator = "0.16.0"
validator.workspace = true
async-trait.workspace = true

[build-dependencies]
Expand Down
25 changes: 19 additions & 6 deletions frontend/rust-lib/flowy-sqlite/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,24 @@ diesel::table! {
}
}

diesel::table! {
workspace_members_table (email, workspace_id) {
email -> Text,
role -> Integer,
name -> Text,
avatar_url -> Nullable<Text>,
uid -> BigInt,
workspace_id -> Text,
updated_at -> Timestamp,
}
}

diesel::allow_tables_to_appear_in_same_query!(
chat_message_table,
chat_table,
collab_snapshot,
user_data_migration_records,
user_table,
user_workspace_table,
chat_message_table,
chat_table,
collab_snapshot,
user_data_migration_records,
user_table,
user_workspace_table,
workspace_members_table,
);
30 changes: 26 additions & 4 deletions frontend/rust-lib/flowy-user-pub/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,33 @@ pub enum UserTokenState {
}

// Workspace Role
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum Role {
Owner,
Member,
Guest,
Owner = 0,
Member = 1,
Guest = 2,
}

impl From<i32> for Role {
fn from(value: i32) -> Self {
match value {
0 => Role::Owner,
1 => Role::Member,
2 => Role::Guest,
_ => Role::Guest,
}
}
}

impl From<Role> for i32 {
fn from(value: Role) -> Self {
match value {
Role::Owner => 0,
Role::Member => 1,
Role::Guest => 2,
}
}
}

pub struct WorkspaceMember {
Expand Down
4 changes: 2 additions & 2 deletions frontend/rust-lib/flowy-user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
flowy-derive.workspace = true
flowy-sqlite = { workspace = true }
flowy-encrypt = { workspace = true }
flowy-error = { workspace = true, features = ["impl_from_dispatch_error", "impl_from_sqlite", "impl_from_collab_folder", "impl_from_collab_persistence"] }
flowy-error = { workspace = true, features = ["impl_from_dispatch_error", "impl_from_sqlite", "impl_from_collab_folder", "impl_from_collab_persistence", "impl_from_collab_document"] }
flowy-folder-pub = { workspace = true }
lib-infra = { workspace = true }
flowy-notification = { workspace = true }
Expand Down Expand Up @@ -39,14 +39,14 @@ parking_lot.workspace = true
strum = "0.25"
strum_macros = "0.25.2"
tokio = { workspace = true, features = ["rt"] }
validator = "0.16.0"
unicode-segmentation = "1.10"
fancy-regex = "0.11.0"
uuid.workspace = true
chrono = { workspace = true, default-features = false, features = ["clock"] }
base64 = "^0.21"
tokio-stream = "0.1.14"
semver = "1.0.22"
validator = { workspace = true, features = ["derive"] }

[dev-dependencies]
nanoid = "0.4.0"
Expand Down
52 changes: 52 additions & 0 deletions frontend/rust-lib/flowy-user/src/services/sqlite_sql/member_sql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use diesel::{insert_into, RunQueryDsl};
use flowy_error::FlowyResult;

use flowy_sqlite::schema::workspace_members_table;

use flowy_sqlite::schema::workspace_members_table::dsl;
use flowy_sqlite::{query_dsl::*, DBConnection, ExpressionMethods};

#[derive(Queryable, Insertable, AsChangeset, Debug)]
#[diesel(table_name = workspace_members_table)]
#[diesel(primary_key(email, workspace_id))]
pub struct WorkspaceMemberTable {
pub email: String,
pub role: i32,
pub name: String,
pub avatar_url: Option<String>,
pub uid: i64,
pub workspace_id: String,
pub updated_at: chrono::NaiveDateTime,
}

pub fn upsert_workspace_member<T: Into<WorkspaceMemberTable>>(
mut conn: DBConnection,
member: T,
) -> FlowyResult<()> {
let member = member.into();

insert_into(workspace_members_table::table)
.values(&member)
.on_conflict((
workspace_members_table::email,
workspace_members_table::workspace_id,
))
.do_update()
.set(&member)
.execute(&mut conn)?;

Ok(())
}

pub fn select_workspace_member(
mut conn: DBConnection,
workspace_id: &str,
uid: i64,
) -> FlowyResult<WorkspaceMemberTable> {
let member = dsl::workspace_members_table
.filter(workspace_members_table::workspace_id.eq(workspace_id))
.filter(workspace_members_table::uid.eq(uid))
.first::<WorkspaceMemberTable>(&mut conn)?;

Ok(member)
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod member_sql;
pub(crate) mod user_sql;
pub(crate) mod workspace_sql;
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use chrono::{Duration, NaiveDateTime, Utc};
use std::convert::TryFrom;
use std::sync::Arc;

use collab_entity::{CollabObject, CollabType};
use collab_integrate::CollabKVDB;
use tracing::{error, info, instrument, warn};
use tracing::{error, info, instrument, trace, warn};

use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_folder_pub::entities::{AppFlowyData, ImportData};
Expand All @@ -23,6 +24,9 @@ use crate::notification::{send_notification, UserNotification};
use crate::services::data_import::{
generate_import_data, upload_collab_objects_data, ImportedFolder, ImportedSource,
};
use crate::services::sqlite_sql::member_sql::{
select_workspace_member, upsert_workspace_member, WorkspaceMemberTable,
};
use crate::services::sqlite_sql::workspace_sql::{
get_all_user_workspace_op, get_user_workspace_op, insert_new_workspaces_op, UserWorkspaceTable,
};
Expand Down Expand Up @@ -483,11 +487,54 @@ impl UserManager {
#[instrument(level = "debug", skip(self), err)]
pub async fn get_workspace_member_info(&self, uid: i64) -> FlowyResult<WorkspaceMember> {
let workspace_id = self.get_session()?.user_workspace.id.clone();
let db = self.authenticate_user.get_sqlite_connection(uid)?;
// Can opt in using memory cache
if let Ok(member_record) = select_workspace_member(db, &workspace_id, uid) {
if is_older_than_n_minutes(member_record.updated_at, 10) {
self
.get_workspace_member_info_from_remote(&workspace_id, uid)
.await?;
}

return Ok(WorkspaceMember {
email: member_record.email,
role: member_record.role.into(),
name: member_record.name,
avatar_url: member_record.avatar_url,
});
}

let member = self
.get_workspace_member_info_from_remote(&workspace_id, uid)
.await?;

Ok(member)
}

async fn get_workspace_member_info_from_remote(
&self,
workspace_id: &str,
uid: i64,
) -> FlowyResult<WorkspaceMember> {
trace!("get workspace member info from remote: {}", workspace_id);
let member = self
.cloud_services
.get_user_service()?
.get_workspace_member_info(&workspace_id, uid)
.await?;

let record = WorkspaceMemberTable {
email: member.email.clone(),
role: member.role.clone().into(),
name: member.name.clone(),
avatar_url: member.avatar_url.clone(),
uid,
workspace_id: workspace_id.to_string(),
updated_at: Utc::now().naive_utc(),
};

let db = self.authenticate_user.get_sqlite_connection(uid)?;
upsert_workspace_member(db, record)?;
Ok(member)
}
}
Expand Down Expand Up @@ -599,3 +646,11 @@ pub fn delete_user_workspaces(mut conn: DBConnection, workspace_id: &str) -> Flo
}
Ok(())
}

fn is_older_than_n_minutes(updated_at: NaiveDateTime, minutes: i64) -> bool {
let current_time: NaiveDateTime = Utc::now().naive_utc();
match current_time.checked_sub_signed(Duration::minutes(minutes)) {
Some(five_minutes_ago) => updated_at < five_minutes_ago,
None => false,
}
}
11 changes: 6 additions & 5 deletions frontend/rust-lib/lib-dispatch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ futures-core = { version = "0.3", default-features = false }
futures-channel = "0.3.26"
futures.workspace = true
futures-util = "0.3.26"
bytes = {version = "1.4", features = ["serde"]}
tokio = { workspace = true, features = ["rt", "sync"] }
bytes = { version = "1.4", features = ["serde"] }
nanoid = "0.4.0"

dyn-clone = "1.0"
Expand All @@ -25,16 +24,18 @@ serde_repr = { workspace = true, optional = true }
validator = "0.16.1"
tracing.workspace = true
parking_lot = "0.12"
bincode = { version = "1.3", optional = true}
bincode = { version = "1.3", optional = true }
protobuf = { workspace = true, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
thread-id = "3.3.0"
tokio = { workspace = true, features = ["full", "rt-multi-thread"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"]}
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen = { version = "0.2.89" }
wasm-bindgen-futures = "0.4"
tokio = { workspace = true, features = ["rt", "sync"] }

[dev-dependencies]
tokio = { workspace = true, features = ["rt"] }
Expand All @@ -43,7 +44,7 @@ futures-util = "0.3.26"
[features]
default = ["use_protobuf"]
use_serde = ["bincode", "serde_json", "serde", "serde_repr"]
use_protobuf= ["protobuf"]
use_protobuf = ["protobuf"]
local_set = []


2 changes: 1 addition & 1 deletion frontend/rust-lib/lib-infra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ md5 = "0.7.0"
anyhow.workspace = true
walkdir = "2.4.0"
tempfile = "3.8.1"
validator = "0.16.0"
validator = { version = "0.16.1", features = ["derive"] }
tracing.workspace = true
atomic_refcell = "0.1"
allo-isolate = { version = "^0.1", features = ["catch-unwind"], optional = true }
Expand Down

0 comments on commit 8e12bc0

Please sign in to comment.