mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Fix/read app (#1808)
* fix: filter out the apps that are deleted * chore: format code style * chore: fix clippy wanrings
This commit is contained in:
@ -60,18 +60,16 @@ impl AppController {
|
||||
Ok(app.into())
|
||||
}
|
||||
|
||||
pub(crate) async fn read_app(&self, params: AppIdPB) -> Result<AppRevision, FlowyError> {
|
||||
pub(crate) async fn read_app(&self, params: AppIdPB) -> Result<Option<AppRevision>, FlowyError> {
|
||||
let app = self
|
||||
.persistence
|
||||
.begin_transaction(|transaction| {
|
||||
let app = transaction.read_app(¶ms.value)?;
|
||||
let trash_ids = self.trash_controller.read_trash_ids(&transaction)?;
|
||||
if trash_ids.contains(&app.id) {
|
||||
return Err(
|
||||
FlowyError::record_not_found().context(format!("Can not find the app:{}", params.value))
|
||||
);
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(app)
|
||||
Ok(Some(app))
|
||||
})
|
||||
.await?;
|
||||
self.read_app_on_server(params)?;
|
||||
@ -243,7 +241,7 @@ fn notify_apps_changed<'a>(
|
||||
trash_controller: Arc<TrashController>,
|
||||
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
||||
) -> FlowyResult<()> {
|
||||
let items = read_local_workspace_apps(workspace_id, trash_controller, transaction)?
|
||||
let items = read_workspace_apps(workspace_id, trash_controller, transaction)?
|
||||
.into_iter()
|
||||
.map(|app_rev| app_rev.into())
|
||||
.collect();
|
||||
@ -254,7 +252,7 @@ fn notify_apps_changed<'a>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_local_workspace_apps<'a>(
|
||||
pub fn read_workspace_apps<'a>(
|
||||
workspace_id: &str,
|
||||
trash_controller: Arc<TrashController>,
|
||||
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
||||
|
@ -51,8 +51,10 @@ pub(crate) async fn read_app_handler(
|
||||
view_controller: AFPluginState<Arc<ViewController>>,
|
||||
) -> DataResult<AppPB, FlowyError> {
|
||||
let params: AppIdPB = data.into_inner();
|
||||
let mut app_rev = app_controller.read_app(params.clone()).await?;
|
||||
app_rev.belongings = view_controller.read_views_belong_to(¶ms.value).await?;
|
||||
|
||||
data_result(app_rev.into())
|
||||
if let Some(mut app_rev) = app_controller.read_app(params.clone()).await? {
|
||||
app_rev.belongings = view_controller.read_views_belong_to(¶ms.value).await?;
|
||||
data_result(app_rev.into())
|
||||
} else {
|
||||
Err(FlowyError::record_not_found())
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![allow(clippy::unused_unit)]
|
||||
use bytes::Bytes;
|
||||
use flowy_error::{internal_error, FlowyResult};
|
||||
use flowy_revision::{RevisionSnapshot, RevisionSnapshotDiskCache};
|
||||
use flowy_revision::{RevisionSnapshotData, RevisionSnapshotPersistence};
|
||||
use flowy_sqlite::{
|
||||
prelude::*,
|
||||
schema::{folder_rev_snapshot, folder_rev_snapshot::dsl},
|
||||
@ -28,7 +28,7 @@ impl SQLiteFolderRevisionSnapshotPersistence {
|
||||
}
|
||||
}
|
||||
|
||||
impl RevisionSnapshotDiskCache for SQLiteFolderRevisionSnapshotPersistence {
|
||||
impl RevisionSnapshotPersistence for SQLiteFolderRevisionSnapshotPersistence {
|
||||
fn should_generate_snapshot_from_range(&self, start_rev_id: i64, current_rev_id: i64) -> bool {
|
||||
(current_rev_id - start_rev_id) >= 2
|
||||
}
|
||||
@ -51,7 +51,7 @@ impl RevisionSnapshotDiskCache for SQLiteFolderRevisionSnapshotPersistence {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_snapshot(&self, rev_id: i64) -> FlowyResult<Option<RevisionSnapshot>> {
|
||||
fn read_snapshot(&self, rev_id: i64) -> FlowyResult<Option<RevisionSnapshotData>> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let snapshot_id = self.gen_snapshot_id(rev_id);
|
||||
let record = dsl::folder_rev_snapshot
|
||||
@ -61,7 +61,7 @@ impl RevisionSnapshotDiskCache for SQLiteFolderRevisionSnapshotPersistence {
|
||||
Ok(Some(record.into()))
|
||||
}
|
||||
|
||||
fn read_last_snapshot(&self) -> FlowyResult<Option<RevisionSnapshot>> {
|
||||
fn read_last_snapshot(&self) -> FlowyResult<Option<RevisionSnapshotData>> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let latest_record = dsl::folder_rev_snapshot
|
||||
.filter(dsl::object_id.eq(&self.object_id))
|
||||
@ -85,9 +85,9 @@ struct FolderSnapshotRecord {
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl std::convert::From<FolderSnapshotRecord> for RevisionSnapshot {
|
||||
impl std::convert::From<FolderSnapshotRecord> for RevisionSnapshotData {
|
||||
fn from(record: FolderSnapshotRecord) -> Self {
|
||||
RevisionSnapshot {
|
||||
RevisionSnapshotData {
|
||||
rev_id: record.rev_id,
|
||||
base_rev_id: record.base_rev_id,
|
||||
timestamp: record.timestamp,
|
||||
|
@ -6,7 +6,7 @@ use crate::{
|
||||
notification::*,
|
||||
services::{
|
||||
persistence::{FolderPersistence, FolderPersistenceTransaction, WorkspaceChangeset},
|
||||
read_local_workspace_apps, TrashController,
|
||||
read_workspace_apps, TrashController,
|
||||
},
|
||||
};
|
||||
use flowy_sqlite::kv::KV;
|
||||
@ -69,7 +69,7 @@ impl WorkspaceController {
|
||||
.begin_transaction(|transaction| {
|
||||
transaction.update_workspace(changeset)?;
|
||||
let user_id = self.user.user_id()?;
|
||||
self.read_local_workspace(workspace_id.clone(), &user_id, &transaction)
|
||||
self.read_workspace(workspace_id.clone(), &user_id, &transaction)
|
||||
})
|
||||
.await?;
|
||||
|
||||
@ -89,7 +89,7 @@ impl WorkspaceController {
|
||||
.persistence
|
||||
.begin_transaction(|transaction| {
|
||||
transaction.delete_workspace(workspace_id)?;
|
||||
self.read_local_workspaces(None, &user_id, &transaction)
|
||||
self.read_workspaces(None, &user_id, &transaction)
|
||||
})
|
||||
.await?;
|
||||
send_notification(&token, FolderNotification::UserDeleteWorkspace)
|
||||
@ -104,7 +104,7 @@ impl WorkspaceController {
|
||||
if let Some(workspace_id) = params.value {
|
||||
let workspace = self
|
||||
.persistence
|
||||
.begin_transaction(|transaction| self.read_local_workspace(workspace_id, &user_id, &transaction))
|
||||
.begin_transaction(|transaction| self.read_workspace(workspace_id, &user_id, &transaction))
|
||||
.await?;
|
||||
set_current_workspace(&user_id, &workspace.id);
|
||||
Ok(workspace)
|
||||
@ -119,7 +119,7 @@ impl WorkspaceController {
|
||||
let app_revs = self
|
||||
.persistence
|
||||
.begin_transaction(|transaction| {
|
||||
read_local_workspace_apps(&workspace_id, self.trash_controller.clone(), &transaction)
|
||||
read_workspace_apps(&workspace_id, self.trash_controller.clone(), &transaction)
|
||||
})
|
||||
.await?;
|
||||
// TODO: read from server
|
||||
@ -127,38 +127,39 @@ impl WorkspaceController {
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(self, transaction), err)]
|
||||
pub(crate) fn read_local_workspaces<'a>(
|
||||
pub(crate) fn read_workspaces<'a>(
|
||||
&self,
|
||||
workspace_id: Option<String>,
|
||||
user_id: &str,
|
||||
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
||||
) -> Result<RepeatedWorkspacePB, FlowyError> {
|
||||
let workspace_id = workspace_id.to_owned();
|
||||
let trash_ids = self.trash_controller.read_trash_ids(transaction)?;
|
||||
let workspaces = transaction
|
||||
.read_workspaces(user_id, workspace_id)?
|
||||
.into_iter()
|
||||
.map(|workspace_rev| workspace_rev.into())
|
||||
.map(|mut workspace_rev| {
|
||||
workspace_rev.apps.retain(|app_rev| !trash_ids.contains(&app_rev.id));
|
||||
workspace_rev.into()
|
||||
})
|
||||
.collect();
|
||||
Ok(RepeatedWorkspacePB { items: workspaces })
|
||||
}
|
||||
|
||||
pub(crate) fn read_local_workspace<'a>(
|
||||
pub(crate) fn read_workspace<'a>(
|
||||
&self,
|
||||
workspace_id: String,
|
||||
user_id: &str,
|
||||
transaction: &'a (dyn FolderPersistenceTransaction + 'a),
|
||||
) -> Result<WorkspacePB, FlowyError> {
|
||||
let mut workspace_revs = transaction.read_workspaces(user_id, Some(workspace_id.clone()))?;
|
||||
if workspace_revs.is_empty() {
|
||||
let mut workspaces = self
|
||||
.read_workspaces(Some(workspace_id.clone()), user_id, transaction)?
|
||||
.items;
|
||||
if workspaces.is_empty() {
|
||||
return Err(FlowyError::record_not_found().context(format!("{} workspace not found", workspace_id)));
|
||||
}
|
||||
debug_assert_eq!(workspace_revs.len(), 1);
|
||||
let workspace = workspace_revs
|
||||
.drain(..1)
|
||||
.map(|workspace_rev| workspace_rev.into())
|
||||
.collect::<Vec<WorkspacePB>>()
|
||||
.pop()
|
||||
.unwrap();
|
||||
debug_assert_eq!(workspaces.len(), 1);
|
||||
let workspace = workspaces.drain(..1).collect::<Vec<WorkspacePB>>().pop().unwrap();
|
||||
Ok(workspace)
|
||||
}
|
||||
}
|
||||
@ -215,11 +216,10 @@ pub async fn notify_workspace_setting_did_change(
|
||||
let workspace_setting = folder_manager
|
||||
.persistence
|
||||
.begin_transaction(|transaction| {
|
||||
let workspace = folder_manager.workspace_controller.read_local_workspace(
|
||||
workspace_id.clone(),
|
||||
&user_id,
|
||||
&transaction,
|
||||
)?;
|
||||
let workspace =
|
||||
folder_manager
|
||||
.workspace_controller
|
||||
.read_workspace(workspace_id.clone(), &user_id, &transaction)?;
|
||||
|
||||
let setting = match transaction.read_view(view_id) {
|
||||
Ok(latest_view) => WorkspaceSettingPB {
|
||||
|
@ -7,7 +7,7 @@ use crate::{
|
||||
errors::FlowyError,
|
||||
manager::FolderManager,
|
||||
notification::{send_notification, FolderNotification},
|
||||
services::{get_current_workspace, read_local_workspace_apps, WorkspaceController},
|
||||
services::{get_current_workspace, read_workspace_apps, WorkspaceController},
|
||||
};
|
||||
use lib_dispatch::prelude::{data_result, AFPluginData, AFPluginState, DataResult};
|
||||
use std::{convert::TryInto, sync::Arc};
|
||||
@ -60,10 +60,9 @@ pub(crate) async fn read_workspaces_handler(
|
||||
let workspaces = folder
|
||||
.persistence
|
||||
.begin_transaction(|transaction| {
|
||||
let mut workspaces =
|
||||
workspace_controller.read_local_workspaces(params.value.clone(), &user_id, &transaction)?;
|
||||
let mut workspaces = workspace_controller.read_workspaces(params.value.clone(), &user_id, &transaction)?;
|
||||
for workspace in workspaces.iter_mut() {
|
||||
let apps = read_local_workspace_apps(&workspace.id, trash_controller.clone(), &transaction)?
|
||||
let apps = read_workspace_apps(&workspace.id, trash_controller.clone(), &transaction)?
|
||||
.into_iter()
|
||||
.map(|app_rev| app_rev.into())
|
||||
.collect();
|
||||
@ -91,7 +90,7 @@ pub async fn read_cur_workspace_handler(
|
||||
.begin_transaction(|transaction| {
|
||||
folder
|
||||
.workspace_controller
|
||||
.read_local_workspace(workspace_id, &user_id, &transaction)
|
||||
.read_workspace(workspace_id, &user_id, &transaction)
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
Reference in New Issue
Block a user