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

126 lines
2.8 KiB
Rust

use std::{collections::HashMap, vec};
use collab_document::blocks::{Block, DocumentData, DocumentMeta};
use nanoid::nanoid;
use crate::entities::{BlockPB, ChildrenPB, DocumentDataPB, MetaPB};
pub const PAGE: &str = "page";
pub const PARAGRAPH_BLOCK_TYPE: &str = "paragraph";
impl From<DocumentData> for DocumentDataPB {
fn from(data: DocumentData) -> Self {
let blocks = data
.blocks
.into_iter()
.map(|(id, block)| (id, block.into()))
.collect();
let children_map = data
.meta
.children_map
.into_iter()
.map(|(id, children)| (id, children.into()))
.collect();
let page_id = data.page_id;
Self {
page_id,
blocks,
meta: MetaPB { children_map },
}
}
}
impl From<DocumentDataPB> for DocumentData {
fn from(data: DocumentDataPB) -> Self {
let blocks = data
.blocks
.into_iter()
.map(|(id, block)| (id, block.into()))
.collect();
let children_map = data
.meta
.children_map
.into_iter()
.map(|(id, children)| (id, children.children))
.collect();
let page_id = data.page_id;
DocumentData {
page_id,
blocks,
meta: DocumentMeta { children_map },
}
}
}
/// The default document data.
/// The default document data is a document with a page block and a text block.
pub fn default_document_data() -> DocumentData {
let page_type = PAGE.to_string();
let text_type = PARAGRAPH_BLOCK_TYPE.to_string();
let mut blocks: HashMap<String, Block> = HashMap::new();
let mut meta: HashMap<String, Vec<String>> = HashMap::new();
// page block
let page_id = nanoid!(10);
let children_id = nanoid!(10);
let root = Block {
id: page_id.clone(),
ty: page_type,
parent: "".to_string(),
children: children_id.clone(),
external_id: None,
external_type: None,
data: HashMap::new(),
};
blocks.insert(page_id.clone(), root);
// text block
let text_block_id = nanoid!(10);
let text_block_children_id = nanoid!(10);
let text_block = Block {
id: text_block_id.clone(),
ty: text_type,
parent: page_id.clone(),
children: text_block_children_id.clone(),
external_id: None,
external_type: None,
data: HashMap::new(),
};
blocks.insert(text_block_id.clone(), text_block);
// meta
meta.insert(children_id, vec![text_block_id]);
meta.insert(text_block_children_id, vec![]);
DocumentData {
page_id,
blocks,
meta: DocumentMeta { children_map: meta },
}
}
impl From<Block> for BlockPB {
fn from(block: Block) -> Self {
Self {
id: block.id,
ty: block.ty,
data: serde_json::to_string(&block.data).unwrap_or_default(),
parent_id: block.parent,
children_id: block.children,
}
}
}
impl From<Vec<String>> for ChildrenPB {
fn from(children: Vec<String>) -> Self {
Self { children }
}
}