Nathan.fooo edc7933c66
feat: support pg storage (#2935)
* refactor: using tokio-postgres

* chore: update

* chore: update env

* chore: update

* chore: upgrade supabase and add logout button

* refactor: update

* chore: update

* refactor: using message queue to handle the pg connection

* refactor: move test

* refactor: update sql

* chore: create pg database when user login

* chore: update scheme

* chore: generic user service

* chore: update

* chore: create statistics

* chore: create snapshot

* chore: add test

* chore: add database cloud service

* chore: add document cloud service

* chore: update interface

* test: add document test

* refactor: document interface

* chore: fix test

* chore: update

* chore: update test

* test: add test

* test: add test

* test: add test

* chore: update collab rev

* fix: flutter analyzer

* chore: update

* chore: update

* chore: update

* fix: tests

* chore: update

* chore: update collab rev

* ci: rust fmt

---------

Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-07-05 20:57:09 +08:00

69 lines
1.7 KiB
Rust

use crate::document::document_event::*;
use flowy_document2::entities::*;
use nanoid::nanoid;
use serde_json::json;
use std::collections::HashMap;
pub fn gen_id() -> String {
nanoid!(10)
}
pub fn gen_text_block_data(text: &str) -> String {
json!({
"delta": [{
"insert": text
}]
})
.to_string()
}
pub struct ParseDocumentData {
pub doc_id: String,
pub page_id: String,
pub blocks: HashMap<String, BlockPB>,
pub children_map: HashMap<String, ChildrenPB>,
pub first_block_id: String,
}
pub fn parse_document_data(document: OpenDocumentData) -> ParseDocumentData {
let doc_id = document.id.clone();
let data = document.data;
let page_id = data.page_id;
let blocks = data.blocks;
let children_map = data.meta.children_map;
let page_block = blocks.get(&page_id).unwrap();
let children_id = page_block.children_id.clone();
let children = children_map.get(&children_id).unwrap();
let block_id = children.children.get(0).unwrap().to_string();
ParseDocumentData {
doc_id,
page_id,
blocks,
children_map,
first_block_id: block_id,
}
}
pub fn gen_insert_block_action(document: OpenDocumentData) -> BlockActionPB {
let parse_data = parse_document_data(document);
let first_block_id = parse_data.first_block_id;
let block = parse_data.blocks.get(&first_block_id).unwrap();
let page_id = parse_data.page_id;
let data = block.data.clone();
let new_block_id = gen_id();
let new_block = BlockPB {
id: new_block_id,
ty: block.ty.clone(),
data,
parent_id: page_id.clone(),
children_id: gen_id(),
};
BlockActionPB {
action: BlockActionTypePB::Insert,
payload: BlockActionPayloadPB {
block: new_block,
prev_id: Some(first_block_id),
parent_id: Some(page_id),
},
}
}