Nathan.fooo 2cd88594e8
feat: migrate user data to cloud (#3078)
* refactor: weak passed-in params in handler

* refactor: rename struct

* chore: update tables

* chore: update schema

* chore: add permission

* chore: update tables

* chore: support transaction mode

* chore: workspace database id

* chore: add user workspace

* feat: return list of workspaces

* chore: add user to workspace

* feat: separate database row table

* refactor: update schema

* chore: partition table

* chore: use transaction

* refactor: dir

* refactor: collab db ref

* fix: collab db lock

* chore: rename files

* chore: add tables descriptions

* chore: update readme

* docs: update documentation

* chore: rename crate

* chore: update ref

* chore: update tests

* chore: update tests

* refactor: crate deps

* chore: update crate ref

* chore: remove unused deps

* chore: remove unused deps

* chore: update collab crate refs

* chore: replace client with transaction in pooler

* refactor: return error type

* refactor: use anyhow error in deps

* feat: supabase postgrest user signin (wip)

* fix: Cargo.toml source git deps, changed Error to anyhow::Error

* fix: uuid serialization

* chore: fix conflict

* chore: extend the response

* feat: add implementation place holders

* feat: impl get_user_workspaces

* feat: impl get_user_profile

* test: create workspace

* fix: postgrest: field names and alias

* chore: implement folder restful api

* chore: implement collab storate with restful api

* feat: added placeholders for impl: update_user_profile, check_user

* feat: impl: update_user_profile

* feat: impl: check_user

* fix: use UidResponse, add more debug info for serde serialization error

* fix: get_user_profile: use Optional<UserProfileResponse>

* chore: imple init sync

* chore: support soft delete

* feat: postgresql: add migration test

* feat: postgresql migration test: added UID display and colored output

* feat: postgresql migration test: workspace role

* feat: postgresql migration test: create shared common utils

* feat: postgresql migration test: fixed shebang

* chore: add flush_collab_update pg function

* chore: implement datbaase and document restful api

* chore: migrate to use restful api

* chore: update table schema

* chore: fix tests

* chore: remove unused code

* chore: format code

* chore: remove unused env

* fix: tauri build

* fix: tauri build

---------

Co-authored-by: Fu Zi Xiang <speed2exe@live.com.sg>
2023-07-29 09:46:24 +08:00

269 lines
8.5 KiB
Rust

/*
* The following code defines functions that handle creating, opening, and closing documents,
* as well as performing actions on documents. These functions make use of a DocumentManager,
* which you can think of as a higher-level interface to interact with documents.
*/
use std::sync::{Arc, Weak};
use collab_document::blocks::{
json_str_to_hashmap, Block, BlockAction, BlockActionPayload, BlockActionType, BlockEvent,
BlockEventPayload, DeltaType,
};
use flowy_error::{FlowyError, FlowyResult};
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
use crate::entities::*;
use crate::{manager::DocumentManager, parser::json::parser::JsonToDocumentParser};
fn upgrade_document(
document_manager: AFPluginState<Weak<DocumentManager>>,
) -> FlowyResult<Arc<DocumentManager>> {
let manager = document_manager
.upgrade()
.ok_or(FlowyError::internal().context("The document manager is already dropped"))?;
Ok(manager)
}
// Handler for creating a new document
pub(crate) async fn create_document_handler(
data: AFPluginData<CreateDocumentPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> FlowyResult<()> {
let manager = upgrade_document(manager)?;
let params: CreateDocumentParams = data.into_inner().try_into()?;
manager.create_document(&params.document_id, params.initial_data)?;
Ok(())
}
// Handler for opening an existing document
pub(crate) async fn open_document_handler(
data: AFPluginData<OpenDocumentPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<DocumentDataPB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: OpenDocumentParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document = manager.get_document(&doc_id).await?;
let document_data = document.lock().get_document_data()?;
data_result_ok(DocumentDataPB::from(document_data))
}
pub(crate) async fn close_document_handler(
data: AFPluginData<CloseDocumentPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> FlowyResult<()> {
let manager = upgrade_document(manager)?;
let params: CloseDocumentParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
manager.close_document(&doc_id)?;
Ok(())
}
// Get the content of the existing document,
// if the document does not exist, return an error.
pub(crate) async fn get_document_data_handler(
data: AFPluginData<OpenDocumentPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<DocumentDataPB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: OpenDocumentParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document_data = manager.get_document_data(&doc_id).await?;
data_result_ok(DocumentDataPB::from(document_data))
}
// Handler for applying an action to a document
pub(crate) async fn apply_action_handler(
data: AFPluginData<ApplyActionPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> FlowyResult<()> {
let manager = upgrade_document(manager)?;
let params: ApplyActionParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document = manager.get_document(&doc_id).await?;
let actions = params.actions;
document.lock().apply_action(actions);
Ok(())
}
pub(crate) async fn convert_data_to_document(
data: AFPluginData<ConvertDataPayloadPB>,
) -> DataResult<DocumentDataPB, FlowyError> {
let payload = data.into_inner();
let document = convert_data_to_document_internal(payload)?;
data_result_ok(document)
}
pub fn convert_data_to_document_internal(
payload: ConvertDataPayloadPB,
) -> Result<DocumentDataPB, FlowyError> {
let params: ConvertDataParams = payload.try_into()?;
let convert_type = params.convert_type;
let data = params.data;
match convert_type {
ConvertType::Json => {
let json_str = String::from_utf8(data).map_err(|_| FlowyError::invalid_data())?;
let document = JsonToDocumentParser::json_str_to_document(&json_str)?;
Ok(document)
},
}
}
pub(crate) async fn redo_handler(
data: AFPluginData<DocumentRedoUndoPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document = manager.get_document(&doc_id).await?;
let document = document.lock();
let redo = document.redo();
let can_redo = document.can_redo();
let can_undo = document.can_undo();
data_result_ok(DocumentRedoUndoResponsePB {
can_redo,
can_undo,
is_success: redo,
})
}
pub(crate) async fn undo_handler(
data: AFPluginData<DocumentRedoUndoPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document = manager.get_document(&doc_id).await?;
let document = document.lock();
let undo = document.undo();
let can_redo = document.can_redo();
let can_undo = document.can_undo();
data_result_ok(DocumentRedoUndoResponsePB {
can_redo,
can_undo,
is_success: undo,
})
}
pub(crate) async fn can_undo_redo_handler(
data: AFPluginData<DocumentRedoUndoPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<DocumentRedoUndoResponsePB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: DocumentRedoUndoParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let document = manager.get_document(&doc_id).await?;
let document = document.lock();
let can_redo = document.can_redo();
let can_undo = document.can_undo();
drop(document);
data_result_ok(DocumentRedoUndoResponsePB {
can_redo,
can_undo,
is_success: true,
})
}
pub(crate) async fn get_snapshot_handler(
data: AFPluginData<OpenDocumentPayloadPB>,
manager: AFPluginState<Weak<DocumentManager>>,
) -> DataResult<RepeatedDocumentSnapshotPB, FlowyError> {
let manager = upgrade_document(manager)?;
let params: OpenDocumentParams = data.into_inner().try_into()?;
let doc_id = params.document_id;
let snapshots = manager.get_document_snapshots(&doc_id).await?;
data_result_ok(RepeatedDocumentSnapshotPB { items: snapshots })
}
impl From<BlockActionPB> for BlockAction {
fn from(pb: BlockActionPB) -> Self {
Self {
action: pb.action.into(),
payload: pb.payload.into(),
}
}
}
impl From<BlockActionTypePB> for BlockActionType {
fn from(pb: BlockActionTypePB) -> Self {
match pb {
BlockActionTypePB::Insert => Self::Insert,
BlockActionTypePB::Update => Self::Update,
BlockActionTypePB::Delete => Self::Delete,
BlockActionTypePB::Move => Self::Move,
}
}
}
impl From<BlockActionPayloadPB> for BlockActionPayload {
fn from(pb: BlockActionPayloadPB) -> Self {
Self {
block: pb.block.into(),
parent_id: pb.parent_id,
prev_id: pb.prev_id,
}
}
}
impl From<BlockPB> for Block {
fn from(pb: BlockPB) -> Self {
// Use `json_str_to_hashmap()` from the `collab_document` crate to convert the JSON data to a hashmap
let data = json_str_to_hashmap(&pb.data).unwrap_or_default();
// Convert the protobuf `BlockPB` to our internal `Block` struct
Self {
id: pb.id,
ty: pb.ty,
children: pb.children_id,
parent: pb.parent_id,
data,
external_id: None,
external_type: None,
}
}
}
impl From<BlockEvent> for BlockEventPB {
fn from(payload: BlockEvent) -> Self {
// Convert each individual `BlockEvent` to a protobuf `BlockEventPB`, and collect the results into a `Vec`
Self {
event: payload.iter().map(|e| e.to_owned().into()).collect(),
}
}
}
impl From<BlockEventPayload> for BlockEventPayloadPB {
fn from(payload: BlockEventPayload) -> Self {
Self {
command: payload.command.into(),
path: payload.path,
id: payload.id,
value: payload.value,
}
}
}
impl From<DeltaType> for DeltaTypePB {
fn from(action: DeltaType) -> Self {
match action {
DeltaType::Inserted => Self::Inserted,
DeltaType::Updated => Self::Updated,
DeltaType::Removed => Self::Removed,
}
}
}
impl From<(&Vec<BlockEvent>, bool)> for DocEventPB {
fn from((events, is_remote): (&Vec<BlockEvent>, bool)) -> Self {
// Convert each individual `BlockEvent` to a protobuf `BlockEventPB`, and collect the results into a `Vec`
Self {
events: events.iter().map(|e| e.to_owned().into()).collect(),
is_remote,
}
}
}