2023-04-04 06:08:50 +00:00
|
|
|
use crate::entities::parser::view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail};
|
2023-04-04 00:41:16 +00:00
|
|
|
use crate::view_ext::gen_view_id;
|
|
|
|
use collab_folder::core::{View, ViewLayout};
|
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
|
|
|
use flowy_error::ErrorCode;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
|
|
|
pub struct ViewPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2023-04-04 06:08:50 +00:00
|
|
|
pub parent_view_id: String,
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub create_time: i64,
|
|
|
|
|
|
|
|
#[pb(index = 5)]
|
|
|
|
pub belongings: Vec<ViewPB>,
|
|
|
|
|
|
|
|
#[pb(index = 6)]
|
2023-04-04 06:08:50 +00:00
|
|
|
pub layout: ViewLayoutPB,
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<View> for ViewPB {
|
|
|
|
fn from(view: View) -> Self {
|
|
|
|
ViewPB {
|
|
|
|
id: view.id,
|
2023-04-04 06:08:50 +00:00
|
|
|
parent_view_id: view.bid,
|
2023-04-04 00:41:16 +00:00
|
|
|
name: view.name,
|
|
|
|
create_time: view.created_at,
|
|
|
|
belongings: Default::default(),
|
|
|
|
layout: view.layout.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Debug, ProtoBuf_Enum, Clone)]
|
2023-04-04 06:08:50 +00:00
|
|
|
pub enum ViewLayoutPB {
|
2023-04-04 00:41:16 +00:00
|
|
|
Document = 0,
|
|
|
|
Grid = 3,
|
|
|
|
Board = 4,
|
|
|
|
Calendar = 5,
|
|
|
|
}
|
|
|
|
|
2023-04-04 06:08:50 +00:00
|
|
|
impl std::default::Default for ViewLayoutPB {
|
2023-04-04 00:41:16 +00:00
|
|
|
fn default() -> Self {
|
2023-04-04 06:08:50 +00:00
|
|
|
ViewLayoutPB::Grid
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 06:08:50 +00:00
|
|
|
impl ViewLayoutPB {
|
|
|
|
pub fn is_database(&self) -> bool {
|
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
ViewLayoutPB::Grid | ViewLayoutPB::Board | ViewLayoutPB::Calendar
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<ViewLayout> for ViewLayoutPB {
|
2023-04-04 00:41:16 +00:00
|
|
|
fn from(rev: ViewLayout) -> Self {
|
|
|
|
match rev {
|
2023-04-04 06:08:50 +00:00
|
|
|
ViewLayout::Grid => ViewLayoutPB::Grid,
|
|
|
|
ViewLayout::Board => ViewLayoutPB::Board,
|
|
|
|
ViewLayout::Document => ViewLayoutPB::Document,
|
|
|
|
ViewLayout::Calendar => ViewLayoutPB::Calendar,
|
2023-04-04 00:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone)]
|
|
|
|
pub struct RepeatedViewPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub items: Vec<ViewPB>,
|
|
|
|
}
|
|
|
|
|
2023-04-04 06:08:50 +00:00
|
|
|
impl std::convert::From<Vec<ViewPB>> for RepeatedViewPB {
|
|
|
|
fn from(items: Vec<ViewPB>) -> Self {
|
2023-04-04 00:41:16 +00:00
|
|
|
RepeatedViewPB { items }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for RepeatedViewPB {
|
|
|
|
type Target = Vec<ViewPB>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for RepeatedViewPB {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct RepeatedViewIdPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub items: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct CreateViewPayloadPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub belong_to_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub desc: String,
|
|
|
|
|
|
|
|
#[pb(index = 4, one_of)]
|
|
|
|
pub thumbnail: Option<String>,
|
|
|
|
|
|
|
|
#[pb(index = 5)]
|
2023-04-04 06:08:50 +00:00
|
|
|
pub layout: ViewLayoutPB,
|
2023-04-04 00:41:16 +00:00
|
|
|
|
|
|
|
#[pb(index = 6)]
|
|
|
|
pub initial_data: Vec<u8>,
|
|
|
|
|
|
|
|
#[pb(index = 7)]
|
|
|
|
pub ext: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CreateViewParams {
|
|
|
|
pub belong_to_id: String,
|
|
|
|
pub name: String,
|
|
|
|
pub desc: String,
|
2023-04-04 06:08:50 +00:00
|
|
|
pub layout: ViewLayoutPB,
|
2023-04-04 00:41:16 +00:00
|
|
|
pub view_id: String,
|
|
|
|
pub initial_data: Vec<u8>,
|
|
|
|
pub ext: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<CreateViewParams> for CreateViewPayloadPB {
|
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<CreateViewParams, Self::Error> {
|
|
|
|
let name = ViewName::parse(self.name)?.0;
|
2023-04-04 06:08:50 +00:00
|
|
|
let belong_to_id = ViewIdentify::parse(self.belong_to_id)?.0;
|
2023-04-04 00:41:16 +00:00
|
|
|
let view_id = gen_view_id();
|
|
|
|
|
|
|
|
Ok(CreateViewParams {
|
|
|
|
belong_to_id,
|
|
|
|
name,
|
|
|
|
desc: self.desc,
|
|
|
|
layout: self.layout,
|
|
|
|
view_id,
|
|
|
|
initial_data: self.initial_data,
|
|
|
|
ext: self.ext,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf, Clone, Debug)]
|
|
|
|
pub struct ViewIdPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<&str> for ViewIdPB {
|
|
|
|
fn from(value: &str) -> Self {
|
|
|
|
ViewIdPB {
|
|
|
|
value: value.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf, Clone, Debug)]
|
|
|
|
pub struct DeletedViewPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub view_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2, one_of)]
|
|
|
|
pub index: Option<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for ViewIdPB {
|
|
|
|
type Target = str;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct UpdateViewPayloadPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub view_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2, one_of)]
|
|
|
|
pub name: Option<String>,
|
|
|
|
|
|
|
|
#[pb(index = 3, one_of)]
|
|
|
|
pub desc: Option<String>,
|
|
|
|
|
|
|
|
#[pb(index = 4, one_of)]
|
|
|
|
pub thumbnail: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct UpdateViewParams {
|
|
|
|
pub view_id: String,
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub desc: Option<String>,
|
|
|
|
pub thumbnail: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<UpdateViewParams> for UpdateViewPayloadPB {
|
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
|
|
|
|
let view_id = ViewIdentify::parse(self.view_id)?.0;
|
|
|
|
|
|
|
|
let name = match self.name {
|
|
|
|
None => None,
|
|
|
|
Some(name) => Some(ViewName::parse(name)?.0),
|
|
|
|
};
|
|
|
|
|
|
|
|
let desc = match self.desc {
|
|
|
|
None => None,
|
|
|
|
Some(desc) => Some(ViewDesc::parse(desc)?.0),
|
|
|
|
};
|
|
|
|
|
|
|
|
let thumbnail = match self.thumbnail {
|
|
|
|
None => None,
|
|
|
|
Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(UpdateViewParams {
|
|
|
|
view_id,
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
thumbnail,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(ProtoBuf_Enum)]
|
|
|
|
pub enum MoveFolderItemType {
|
|
|
|
MoveApp = 0,
|
|
|
|
MoveView = 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::default::Default for MoveFolderItemType {
|
|
|
|
fn default() -> Self {
|
|
|
|
MoveFolderItemType::MoveApp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct MoveFolderItemPayloadPB {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub item_id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub from: i32,
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub to: i32,
|
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub ty: MoveFolderItemType,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MoveViewParams {
|
|
|
|
pub item_id: String,
|
|
|
|
pub from: usize,
|
|
|
|
pub to: usize,
|
|
|
|
pub ty: MoveFolderItemType,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<MoveViewParams> for MoveFolderItemPayloadPB {
|
|
|
|
type Error = ErrorCode;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<MoveViewParams, Self::Error> {
|
|
|
|
let view_id = ViewIdentify::parse(self.item_id)?.0;
|
|
|
|
Ok(MoveViewParams {
|
|
|
|
item_id: view_id,
|
|
|
|
from: self.from as usize,
|
|
|
|
to: self.to as usize,
|
|
|
|
ty: self.ty,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// impl<'de> Deserialize<'de> for ViewDataType {
|
|
|
|
// fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
|
|
|
|
// where
|
|
|
|
// D: Deserializer<'de>,
|
|
|
|
// {
|
|
|
|
// struct ViewTypeVisitor();
|
|
|
|
//
|
|
|
|
// impl<'de> Visitor<'de> for ViewTypeVisitor {
|
|
|
|
// type Value = ViewDataType;
|
|
|
|
// fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
// formatter.write_str("RichText, PlainText")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
|
|
|
|
// where
|
|
|
|
// E: de::Error,
|
|
|
|
// {
|
|
|
|
// let data_type;
|
|
|
|
// match v {
|
|
|
|
// 0 => {
|
|
|
|
// data_type = ViewDataType::RichText;
|
|
|
|
// }
|
|
|
|
// 1 => {
|
|
|
|
// data_type = ViewDataType::PlainText;
|
|
|
|
// }
|
|
|
|
// _ => {
|
|
|
|
// return Err(de::Error::invalid_value(Unexpected::Unsigned(v as u64), &self));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// Ok(data_type)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
|
|
|
// where
|
|
|
|
// E: de::Error,
|
|
|
|
// {
|
|
|
|
// let data_type;
|
|
|
|
// match s {
|
|
|
|
// "Doc" | "RichText" => {
|
|
|
|
// // Rename ViewDataType::Doc to ViewDataType::RichText, So we need to migrate the ViewType manually.
|
|
|
|
// data_type = ViewDataType::RichText;
|
|
|
|
// }
|
|
|
|
// "PlainText" => {
|
|
|
|
// data_type = ViewDataType::PlainText;
|
|
|
|
// }
|
|
|
|
// unknown => {
|
|
|
|
// return Err(de::Error::invalid_value(Unexpected::Str(unknown), &self));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// Ok(data_type)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// deserializer.deserialize_any(ViewTypeVisitor())
|
|
|
|
// }
|
|
|
|
// }
|