add aliases for protobuf struct

This commit is contained in:
appflowy 2022-01-04 10:47:32 +08:00
parent 7b772a8a6e
commit 3fd3389f62
15 changed files with 123 additions and 93 deletions

View File

@ -12,16 +12,16 @@ use flowy_core_data_model::{
app::{AppDesc, AppName},
workspace::WorkspaceIdentify,
},
protobuf::{App, CreateAppParams, RepeatedView},
protobuf::{App as AppPB, CreateAppParams as CreateAppParamsPB, RepeatedView as RepeatedViewPB},
};
use sqlx::{postgres::PgArguments, Postgres};
use uuid::Uuid;
pub(crate) async fn create_app(
transaction: &mut DBTransaction<'_>,
mut params: CreateAppParams,
mut params: CreateAppParamsPB,
logged_user: LoggedUser,
) -> Result<App, ServerError> {
) -> Result<AppPB, ServerError> {
let name = AppName::parse(params.take_name()).map_err(invalid_params)?;
let workspace_id = WorkspaceIdentify::parse(params.take_workspace_id()).map_err(invalid_params)?;
let user_id = logged_user.as_uuid()?.to_string();
@ -44,7 +44,7 @@ pub(crate) async fn read_app(
transaction: &mut DBTransaction<'_>,
app_id: Uuid,
user: &LoggedUser,
) -> Result<App, ServerError> {
) -> Result<AppPB, ServerError> {
let table = read_app_table(app_id, transaction).await?;
let read_trash_ids = read_trash_ids(user, transaction).await?;
@ -52,14 +52,14 @@ pub(crate) async fn read_app(
return Err(ServerError::record_not_found());
}
let mut views = RepeatedView::default();
let mut views = RepeatedViewPB::default();
views.set_items(
read_view_belong_to_id(&table.id.to_string(), user, transaction as &mut DBTransaction<'_>)
.await?
.into(),
);
let mut app: App = table.into();
let mut app: AppPB = table.into();
app.set_belongings(views);
Ok(app)
}

View File

@ -3,7 +3,7 @@ use backend_service::errors::{invalid_params, ServerError};
use chrono::{DateTime, NaiveDateTime, Utc};
use flowy_core_data_model::{
parser::app::AppIdentify,
protobuf::{App, ColorStyle, RepeatedView},
protobuf::{App as AppPB, ColorStyle as ColorStylePB, RepeatedView as RepeatedViewPB},
};
use protobuf::Message;
use sqlx::postgres::PgArguments;
@ -35,7 +35,7 @@ impl NewAppSqlBuilder {
Self { table }
}
pub fn from_app(user_id: &str, app: App) -> Result<Self, ServerError> {
pub fn from_app(user_id: &str, app: AppPB) -> Result<Self, ServerError> {
let app_id = check_app_id(app.id)?;
let create_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(app.create_time, 0), Utc);
let modified_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(app.modified_time, 0), Utc);
@ -71,13 +71,13 @@ impl NewAppSqlBuilder {
self
}
pub fn color_style(mut self, color_style: ColorStyle) -> Self {
pub fn color_style(mut self, color_style: ColorStylePB) -> Self {
self.table.color_style = color_style.write_to_bytes().unwrap_or_else(|_| default_color_style());
self
}
pub fn build(self) -> Result<(String, PgArguments, App), ServerError> {
let app: App = self.table.clone().into();
pub fn build(self) -> Result<(String, PgArguments, AppPB), ServerError> {
let app: AppPB = self.table.clone().into();
let (sql, args) = SqlBuilder::create(APP_TABLE)
.add_field_with_arg("id", self.table.id)
@ -95,7 +95,7 @@ impl NewAppSqlBuilder {
}
fn default_color_style() -> Vec<u8> {
let style = ColorStyle::default();
let style = ColorStylePB::default();
match style.write_to_bytes() {
Ok(bytes) => bytes,
Err(e) => {
@ -123,14 +123,14 @@ pub struct AppTable {
pub(crate) create_time: chrono::DateTime<Utc>,
pub(crate) user_id: String,
}
impl std::convert::From<AppTable> for App {
impl std::convert::From<AppTable> for AppPB {
fn from(table: AppTable) -> Self {
let mut app = App::default();
let mut app = AppPB::default();
app.set_id(table.id.to_string());
app.set_workspace_id(table.workspace_id.to_string());
app.set_name(table.name.clone());
app.set_desc(table.description.clone());
app.set_belongings(RepeatedView::default());
app.set_belongings(RepeatedViewPB::default());
app.set_modified_time(table.modified_time.timestamp());
app.set_create_time(table.create_time.timestamp());

View File

@ -17,7 +17,7 @@ use backend_service::{
};
use flowy_core_data_model::{
parser::app::{AppDesc, AppName},
protobuf::{AppId, CreateAppParams, UpdateAppParams},
protobuf::{AppId as AppIdPB, CreateAppParams as CreateAppParamsPB, UpdateAppParams as UpdateAppParamsPB},
};
use protobuf::Message;
use sqlx::PgPool;
@ -27,7 +27,7 @@ pub async fn create_handler(
pool: Data<PgPool>,
logged_user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: CreateAppParams = parse_from_payload(payload).await?;
let params: CreateAppParamsPB = parse_from_payload(payload).await?;
let mut transaction = pool
.begin()
.await
@ -44,7 +44,7 @@ pub async fn create_handler(
}
pub async fn read_handler(payload: Payload, pool: Data<PgPool>, user: LoggedUser) -> Result<HttpResponse, ServerError> {
let params: AppId = parse_from_payload(payload).await?;
let params: AppIdPB = parse_from_payload(payload).await?;
let app_id = check_app_id(params.app_id)?;
let mut transaction = pool
@ -61,7 +61,7 @@ pub async fn read_handler(payload: Payload, pool: Data<PgPool>, user: LoggedUser
}
pub async fn update_handler(payload: Payload, pool: Data<PgPool>) -> Result<HttpResponse, ServerError> {
let params: UpdateAppParams = parse_from_payload(payload).await?;
let params: UpdateAppParamsPB = parse_from_payload(payload).await?;
let app_id = check_app_id(params.get_app_id().to_string())?;
let name = match params.has_name() {
false => None,
@ -96,7 +96,7 @@ pub async fn update_handler(payload: Payload, pool: Data<PgPool>) -> Result<Http
}
pub async fn delete_handler(payload: Payload, pool: Data<PgPool>) -> Result<HttpResponse, ServerError> {
let params: AppId = parse_from_payload(payload).await?;
let params: AppIdPB = parse_from_payload(payload).await?;
let app_id = check_app_id(params.app_id.to_owned())?;
let mut transaction = pool
.begin()

View File

@ -11,14 +11,14 @@ use bytes::Bytes;
use chrono::Utc;
use flowy_collaboration::{
entities::revision::{RepeatedRevision, Revision},
protobuf::CreateDocParams,
protobuf::CreateDocParams as CreateDocParamsPB,
};
use flowy_core_data_model::{
parser::{
app::AppIdentify,
view::{ViewDesc, ViewName, ViewThumbnail},
},
protobuf::{CreateViewParams, RepeatedView, View},
protobuf::{CreateViewParams as CreateViewParamsPB, RepeatedView as RepeatedViewPB, View as ViewPB},
};
use sqlx::{postgres::PgArguments, Postgres};
use std::{convert::TryInto, sync::Arc};
@ -69,9 +69,9 @@ pub(crate) async fn delete_view(
pub(crate) async fn create_view(
transaction: &mut DBTransaction<'_>,
kv_store: Arc<DocumentKVPersistence>,
params: CreateViewParams,
params: CreateViewParamsPB,
user_id: &str,
) -> Result<View, ServerError> {
) -> Result<ViewPB, ServerError> {
let view_id = check_view_id(params.view_id.clone())?;
let name = ViewName::parse(params.name).map_err(invalid_params)?;
let belong_to_id = AppIdentify::parse(params.belong_to_id).map_err(invalid_params)?;
@ -94,7 +94,7 @@ pub(crate) async fn create_view(
let md5 = format!("{:x}", md5::compute(&delta_data));
let revision = Revision::new(&view.id, 0, 0, delta_data, user_id, md5);
let repeated_revision = RepeatedRevision::new(vec![revision]);
let mut create_doc_params = CreateDocParams::new();
let mut create_doc_params = CreateDocParamsPB::new();
create_doc_params.set_revisions(repeated_revision.try_into().unwrap());
create_doc_params.set_id(view.id.clone());
let _ = create_document(&kv_store, create_doc_params).await?;
@ -106,7 +106,7 @@ pub(crate) async fn read_view(
user: &LoggedUser,
view_id: Uuid,
transaction: &mut DBTransaction<'_>,
) -> Result<View, ServerError> {
) -> Result<ViewPB, ServerError> {
let table = read_view_table(view_id, transaction as &mut DBTransaction<'_>).await?;
let read_trash_ids = read_trash_ids(user, transaction).await?;
@ -114,13 +114,13 @@ pub(crate) async fn read_view(
return Err(ServerError::record_not_found());
}
let mut views = RepeatedView::default();
let mut views = RepeatedViewPB::default();
views.set_items(
read_view_belong_to_id(&table.id.to_string(), &user, transaction)
.await?
.into(),
);
let mut view: View = table.into();
let mut view: ViewPB = table.into();
view.set_belongings(views);
Ok(view)
}
@ -147,7 +147,7 @@ pub(crate) async fn read_view_belong_to_id<'c>(
id: &str,
user: &LoggedUser,
transaction: &mut DBTransaction<'_>,
) -> Result<Vec<View>, ServerError> {
) -> Result<Vec<ViewPB>, ServerError> {
// TODO: add index for app_table
let (sql, args) = SqlBuilder::select(VIEW_TABLE)
.add_field("*")
@ -162,7 +162,7 @@ pub(crate) async fn read_view_belong_to_id<'c>(
let read_trash_ids = read_trash_ids(user, transaction).await?;
tables.retain(|table| !read_trash_ids.contains(&table.id.to_string()));
let views = tables.into_iter().map(|table| table.into()).collect::<Vec<View>>();
let views = tables.into_iter().map(|table| table.into()).collect::<Vec<ViewPB>>();
Ok(views)
}

View File

@ -3,7 +3,7 @@ use backend_service::errors::{invalid_params, ServerError};
use chrono::{DateTime, NaiveDateTime, Utc};
use flowy_core_data_model::{
parser::view::ViewIdentify,
protobuf::{RepeatedView, View, ViewType},
protobuf::{RepeatedView as RepeatedViewPB, View as ViewPB, ViewType as ViewTypePB},
};
use protobuf::ProtobufEnum;
use sqlx::postgres::PgArguments;
@ -27,13 +27,13 @@ impl NewViewSqlBuilder {
modified_time: time,
create_time: time,
thumbnail: "".to_string(),
view_type: ViewType::Doc.value(),
view_type: ViewTypePB::Doc.value(),
};
Self { table }
}
pub fn from_view(view: View) -> Result<Self, ServerError> {
pub fn from_view(view: ViewPB) -> Result<Self, ServerError> {
let view_id = ViewIdentify::parse(view.id).map_err(invalid_params)?;
let view_id = Uuid::parse_str(view_id.as_ref())?;
let create_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(view.create_time, 0), Utc);
@ -67,13 +67,13 @@ impl NewViewSqlBuilder {
self
}
pub fn view_type(mut self, view_type: ViewType) -> Self {
pub fn view_type(mut self, view_type: ViewTypePB) -> Self {
self.table.view_type = view_type.value();
self
}
pub fn build(self) -> Result<(String, PgArguments, View), ServerError> {
let view: View = self.table.clone().into();
pub fn build(self) -> Result<(String, PgArguments, ViewPB), ServerError> {
let view: ViewPB = self.table.clone().into();
let (sql, args) = SqlBuilder::create(VIEW_TABLE)
.add_field_with_arg("id", self.table.id)
@ -115,17 +115,17 @@ pub struct ViewTable {
pub(crate) thumbnail: String,
pub(crate) view_type: i32,
}
impl std::convert::From<ViewTable> for View {
impl std::convert::From<ViewTable> for ViewPB {
fn from(table: ViewTable) -> Self {
let view_type = ViewType::from_i32(table.view_type).unwrap_or(ViewType::Doc);
let view_type = ViewTypePB::from_i32(table.view_type).unwrap_or(ViewTypePB::Doc);
let mut view = View::default();
let mut view = ViewPB::default();
view.set_id(table.id.to_string());
view.set_belong_to_id(table.belong_to_id);
view.set_name(table.name);
view.set_desc(table.description);
view.set_view_type(view_type);
view.set_belongings(RepeatedView::default());
view.set_belongings(RepeatedViewPB::default());
view.set_create_time(table.create_time.timestamp());
view.set_modified_time(table.modified_time.timestamp());

View File

@ -21,7 +21,12 @@ use backend_service::{
};
use flowy_core_data_model::{
parser::view::{ViewDesc, ViewName, ViewThumbnail},
protobuf::{CreateViewParams, QueryViewRequest, UpdateViewParams, ViewId},
protobuf::{
CreateViewParams as CreateViewParamsPB,
QueryViewRequest as QueryViewRequestPB,
UpdateViewParams as UpdateViewParamsPB,
ViewId as ViewIdPB,
},
};
use sqlx::PgPool;
use std::sync::Arc;
@ -31,7 +36,7 @@ pub async fn create_handler(
persistence: Data<Arc<FlowyPersistence>>,
user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: CreateViewParams = parse_from_payload(payload).await?;
let params: CreateViewParamsPB = parse_from_payload(payload).await?;
let kv_store = persistence.kv_store();
let pool = persistence.pg_pool();
let mut transaction = pool
@ -50,7 +55,7 @@ pub async fn create_handler(
}
pub async fn read_handler(payload: Payload, pool: Data<PgPool>, user: LoggedUser) -> Result<HttpResponse, ServerError> {
let params: ViewId = parse_from_payload(payload).await?;
let params: ViewIdPB = parse_from_payload(payload).await?;
let view_id = check_view_ids(vec![params.view_id])?.pop().unwrap();
let mut transaction = pool
.begin()
@ -67,7 +72,7 @@ pub async fn read_handler(payload: Payload, pool: Data<PgPool>, user: LoggedUser
}
pub async fn update_handler(payload: Payload, pool: Data<PgPool>) -> Result<HttpResponse, ServerError> {
let params: UpdateViewParams = parse_from_payload(payload).await?;
let params: UpdateViewParamsPB = parse_from_payload(payload).await?;
let view_id = check_view_id(params.view_id.clone())?;
let name = match params.has_name() {
false => None,
@ -107,7 +112,7 @@ pub async fn delete_handler(
payload: Payload,
persistence: Data<Arc<FlowyPersistence>>,
) -> Result<HttpResponse, ServerError> {
let params: QueryViewRequest = parse_from_payload(payload).await?;
let params: QueryViewRequestPB = parse_from_payload(payload).await?;
let pool = persistence.pg_pool();
let kv_store = persistence.kv_store();
let view_ids = check_view_ids(params.view_ids.to_vec())?;

View File

@ -11,7 +11,7 @@ use anyhow::Context;
use backend_service::errors::{invalid_params, ServerError};
use flowy_core_data_model::{
parser::workspace::WorkspaceIdentify,
protobuf::{RepeatedApp, RepeatedWorkspace, Workspace},
protobuf::{RepeatedApp as RepeatedAppPB, RepeatedWorkspace as RepeatedWorkspacePB, Workspace as WorkspacePB},
};
use sqlx::{postgres::PgArguments, Postgres};
use uuid::Uuid;
@ -21,7 +21,7 @@ pub(crate) async fn create_workspace(
name: &str,
desc: &str,
logged_user: LoggedUser,
) -> Result<Workspace, ServerError> {
) -> Result<WorkspacePB, ServerError> {
let user_id = logged_user.as_uuid()?.to_string();
let (sql, args, workspace) = NewWorkspaceBuilder::new(&user_id).name(name).desc(desc).build()?;
@ -74,7 +74,7 @@ pub async fn read_workspaces(
transaction: &mut DBTransaction<'_>,
workspace_id: Option<String>,
logged_user: LoggedUser,
) -> Result<RepeatedWorkspace, ServerError> {
) -> Result<RepeatedWorkspacePB, ServerError> {
let user_id = logged_user.as_uuid()?.to_string();
let mut builder = SqlBuilder::select(WORKSPACE_TABLE)
@ -92,7 +92,7 @@ pub async fn read_workspaces(
.await
.map_err(map_sqlx_error)?;
let mut repeated_workspace = RepeatedWorkspace::default();
let mut repeated_workspace = RepeatedWorkspacePB::default();
let mut workspaces = vec![];
// Opti: combine the query
for table in tables {
@ -105,7 +105,7 @@ pub async fn read_workspaces(
.context("Get workspace app")
.unwrap_or_default();
let mut workspace: Workspace = table.into();
let mut workspace: WorkspacePB = table.into();
workspace.set_apps(apps);
workspaces.push(workspace);
}
@ -119,7 +119,7 @@ async fn read_workspace_apps<'c>(
user: &LoggedUser,
transaction: &mut DBTransaction<'_>,
workspace_id: &str,
) -> Result<RepeatedApp, ServerError> {
) -> Result<RepeatedAppPB, ServerError> {
let workspace_id = WorkspaceIdentify::parse(workspace_id.to_owned()).map_err(invalid_params)?;
let (sql, args) = SqlBuilder::select("app_table")
.add_field("*")
@ -138,7 +138,7 @@ async fn read_workspace_apps<'c>(
apps.push(app);
}
let mut repeated_app = RepeatedApp::default();
let mut repeated_app = RepeatedAppPB::default();
repeated_app.set_items(apps.into());
Ok(repeated_app)
}

View File

@ -1,7 +1,7 @@
use crate::util::sqlx_ext::SqlBuilder;
use backend_service::errors::{invalid_params, ServerError};
use chrono::{DateTime, NaiveDateTime, Utc};
use flowy_core_data_model::{parser::workspace::WorkspaceIdentify, protobuf::Workspace};
use flowy_core_data_model::{parser::workspace::WorkspaceIdentify, protobuf::Workspace as WorkspacePB};
use sqlx::postgres::PgArguments;
use uuid::Uuid;
@ -25,7 +25,7 @@ impl NewWorkspaceBuilder {
Self { table }
}
pub fn from_workspace(user_id: &str, workspace: Workspace) -> Result<Self, ServerError> {
pub fn from_workspace(user_id: &str, workspace: WorkspacePB) -> Result<Self, ServerError> {
let workspace_id = check_workspace_id(workspace.id)?;
let create_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(workspace.create_time, 0), Utc);
let modified_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(workspace.modified_time, 0), Utc);
@ -52,8 +52,8 @@ impl NewWorkspaceBuilder {
self
}
pub fn build(self) -> Result<(String, PgArguments, Workspace), ServerError> {
let workspace: Workspace = self.table.clone().into();
pub fn build(self) -> Result<(String, PgArguments, WorkspacePB), ServerError> {
let workspace: WorkspacePB = self.table.clone().into();
// TODO: use macro to fetch each field from struct
let (sql, args) = SqlBuilder::create(WORKSPACE_TABLE)
.add_field_with_arg("id", self.table.id)
@ -85,9 +85,9 @@ pub struct WorkspaceTable {
pub(crate) create_time: chrono::DateTime<Utc>,
pub(crate) user_id: String,
}
impl std::convert::From<WorkspaceTable> for Workspace {
impl std::convert::From<WorkspaceTable> for WorkspacePB {
fn from(table: WorkspaceTable) -> Self {
let mut workspace = Workspace::default();
let mut workspace = WorkspacePB::default();
workspace.set_id(table.id.to_string());
workspace.set_name(table.name.clone());
workspace.set_desc(table.description.clone());

View File

@ -20,7 +20,11 @@ use backend_service::{
};
use flowy_core_data_model::{
parser::workspace::{WorkspaceDesc, WorkspaceName},
protobuf::{CreateWorkspaceParams, UpdateWorkspaceParams, WorkspaceId},
protobuf::{
CreateWorkspaceParams as CreateWorkspaceParamsPB,
UpdateWorkspaceParams as UpdateWorkspaceParamsPB,
WorkspaceId as WorkspaceIdPB,
},
};
use sqlx::PgPool;
@ -29,7 +33,7 @@ pub async fn create_handler(
pool: Data<PgPool>,
logged_user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: CreateWorkspaceParams = parse_from_payload(payload).await?;
let params: CreateWorkspaceParamsPB = parse_from_payload(payload).await?;
let name = WorkspaceName::parse(params.get_name().to_owned()).map_err(invalid_params)?;
let desc = WorkspaceDesc::parse(params.get_desc().to_owned()).map_err(invalid_params)?;
let mut transaction = pool
@ -50,7 +54,7 @@ pub async fn read_handler(
pool: Data<PgPool>,
logged_user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: WorkspaceId = parse_from_payload(payload).await?;
let params: WorkspaceIdPB = parse_from_payload(payload).await?;
let mut transaction = pool
.begin()
.await
@ -76,7 +80,7 @@ pub async fn delete_handler(
pool: Data<PgPool>,
_logged_user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: WorkspaceId = parse_from_payload(payload).await?;
let params: WorkspaceIdPB = parse_from_payload(payload).await?;
let workspace_id = check_workspace_id(params.get_workspace_id().to_owned())?;
let mut transaction = pool
.begin()
@ -97,7 +101,7 @@ pub async fn update_handler(
pool: Data<PgPool>,
_logged_user: LoggedUser,
) -> Result<HttpResponse, ServerError> {
let params: UpdateWorkspaceParams = parse_from_payload(payload).await?;
let params: UpdateWorkspaceParamsPB = parse_from_payload(payload).await?;
let workspace_id = check_workspace_id(params.get_id().to_owned())?;
let name = match params.has_name() {
false => None,

View File

@ -8,16 +8,21 @@ use actix_web::{
HttpResponse,
};
use backend_service::{errors::ServerError, response::FlowyResponse};
use flowy_collaboration::protobuf::{CreateDocParams, DocumentId, ResetDocumentParams};
use flowy_collaboration::sync::ServerDocumentManager;
use flowy_collaboration::{
protobuf::{
CreateDocParams as CreateDocParamsPB,
DocumentId as DocumentIdPB,
ResetDocumentParams as ResetDocumentParamsPB,
},
sync::ServerDocumentManager,
};
use std::sync::Arc;
pub async fn create_document_handler(
payload: Payload,
persistence: Data<Arc<FlowyPersistence>>,
) -> Result<HttpResponse, ServerError> {
let params: CreateDocParams = parse_from_payload(payload).await?;
let params: CreateDocParamsPB = parse_from_payload(payload).await?;
let kv_store = persistence.kv_store();
let _ = create_document(&kv_store, params).await?;
Ok(FlowyResponse::success().into())
@ -28,7 +33,7 @@ pub async fn read_document_handler(
payload: Payload,
persistence: Data<Arc<FlowyPersistence>>,
) -> Result<HttpResponse, ServerError> {
let params: DocumentId = parse_from_payload(payload).await?;
let params: DocumentIdPB = parse_from_payload(payload).await?;
let kv_store = persistence.kv_store();
let doc = read_document(&kv_store, params).await?;
let response = FlowyResponse::success().pb(doc)?;
@ -39,7 +44,7 @@ pub async fn reset_document_handler(
payload: Payload,
document_manager: Data<Arc<ServerDocumentManager>>,
) -> Result<HttpResponse, ServerError> {
let params: ResetDocumentParams = parse_from_payload(payload).await?;
let params: ResetDocumentParamsPB = parse_from_payload(payload).await?;
let _ = reset_document(document_manager.get_ref(), params).await?;
Ok(FlowyResponse::success().into())
}

View File

@ -8,7 +8,11 @@ use async_stream::stream;
use backend_service::errors::{internal_error, Result, ServerError};
use flowy_collaboration::{
protobuf::{DocumentClientWSData, DocumentClientWSDataType, Revision},
protobuf::{
DocumentClientWSData as DocumentClientWSDataPB,
DocumentClientWSDataType as DocumentClientWSDataTypePB,
Revision as RevisionPB,
},
sync::{RevisionUser, ServerDocumentManager, SyncResponse},
};
use futures::stream::StreamExt;
@ -68,7 +72,7 @@ impl DocumentWebSocketActor {
async fn handle_client_data(&self, client_data: WSClientData, persistence: Arc<FlowyPersistence>) -> Result<()> {
let WSClientData { user, socket, data } = client_data;
let document_client_data = spawn_blocking(move || parse_from_bytes::<DocumentClientWSData>(&data))
let document_client_data = spawn_blocking(move || parse_from_bytes::<DocumentClientWSDataPB>(&data))
.await
.map_err(internal_error)??;
@ -86,14 +90,14 @@ impl DocumentWebSocketActor {
});
match &document_client_data.ty {
DocumentClientWSDataType::ClientPushRev => {
DocumentClientWSDataTypePB::ClientPushRev => {
let _ = self
.doc_manager
.handle_client_revisions(user, document_client_data)
.await
.map_err(internal_error)?;
},
DocumentClientWSDataType::ClientPing => {
DocumentClientWSDataTypePB::ClientPing => {
let _ = self
.doc_manager
.handle_client_ping(user, document_client_data)
@ -107,9 +111,9 @@ impl DocumentWebSocketActor {
}
#[allow(dead_code)]
fn verify_md5(revision: &Revision) -> Result<()> {
fn verify_md5(revision: &RevisionPB) -> Result<()> {
if md5(&revision.delta_data) != revision.md5 {
return Err(ServerError::internal().context("Revision md5 not match"));
return Err(ServerError::internal().context("RevisionPB md5 not match"));
}
Ok(())
}

View File

@ -17,11 +17,18 @@ use backend_service::{
use chrono::Utc;
use flowy_user_data_model::{
parser::{UserEmail, UserName, UserPassword},
protobuf::{SignInParams, SignInResponse, SignUpParams, SignUpResponse, UpdateUserParams, UserProfile},
protobuf::{
SignInParams as SignInParamsPB,
SignInResponse as SignInResponsePB,
SignUpParams as SignUpParamsPB,
SignUpResponse as SignUpResponsePB,
UpdateUserParams as UpdateUserParamsPB,
UserProfile as UserProfilePB,
},
};
use sqlx::{PgPool, Postgres};
pub async fn sign_in(pool: &PgPool, params: SignInParams) -> Result<SignInResponse, ServerError> {
pub async fn sign_in(pool: &PgPool, params: SignInParamsPB) -> Result<SignInResponsePB, ServerError> {
let email = UserEmail::parse(params.email).map_err(|e| ServerError::params_invalid().context(e))?;
let password = UserPassword::parse(params.password).map_err(|e| ServerError::params_invalid().context(e))?;
@ -40,7 +47,7 @@ pub async fn sign_in(pool: &PgPool, params: SignInParams) -> Result<SignInRespon
let logged_user = LoggedUser::new(&user.id.to_string());
AUTHORIZED_USERS.store_auth(logged_user, true);
let mut response_data = SignInResponse::default();
let mut response_data = SignInResponsePB::default();
response_data.set_user_id(user.id.to_string());
response_data.set_name(user.name);
response_data.set_email(user.email);
@ -54,7 +61,7 @@ pub async fn sign_out(logged_user: LoggedUser) -> Result<FlowyResponse, ServerEr
Ok(FlowyResponse::success())
}
pub async fn register_user(pool: &PgPool, params: SignUpParams) -> Result<FlowyResponse, ServerError> {
pub async fn register_user(pool: &PgPool, params: SignUpParamsPB) -> Result<FlowyResponse, ServerError> {
let name = UserName::parse(params.name).map_err(|e| ServerError::params_invalid().context(e))?;
let email = UserEmail::parse(params.email).map_err(|e| ServerError::params_invalid().context(e))?;
let password = UserPassword::parse(params.password).map_err(|e| ServerError::params_invalid().context(e))?;
@ -105,7 +112,7 @@ pub(crate) async fn get_user_profile(
// update the user active time
AUTHORIZED_USERS.store_auth(logged_user, true);
let mut user_profile = UserProfile::default();
let mut user_profile = UserProfilePB::default();
user_profile.set_id(user_table.id.to_string());
user_profile.set_email(user_table.email);
user_profile.set_name(user_table.name);
@ -116,7 +123,7 @@ pub(crate) async fn get_user_profile(
pub(crate) async fn set_user_profile(
pool: &PgPool,
logged_user: LoggedUser,
params: UpdateUserParams,
params: UpdateUserParamsPB,
) -> Result<FlowyResponse, ServerError> {
let mut transaction = pool
.begin()
@ -204,7 +211,7 @@ async fn insert_new_user(
name: &str,
email: &str,
password: &str,
) -> Result<SignUpResponse, ServerError> {
) -> Result<SignUpResponsePB, ServerError> {
let uuid = uuid::Uuid::new_v4();
let token = Token::create_token(&uuid.to_string())?;
let password = hash_password(password)?;
@ -223,7 +230,7 @@ async fn insert_new_user(
.await
.map_err(|e| ServerError::internal().context(e))?;
let mut response = SignUpResponse::default();
let mut response = SignUpResponsePB::default();
response.set_user_id(uuid.to_string());
response.set_name(name.to_string());
response.set_email(email.to_string());

View File

@ -10,11 +10,15 @@ use actix_web::{
HttpResponse,
};
use backend_service::{errors::ServerError, response::FlowyResponse};
use flowy_user_data_model::protobuf::{SignInParams, SignUpParams, UpdateUserParams};
use flowy_user_data_model::protobuf::{
SignInParams as SignInParamsPB,
SignUpParams as SignUpParamsPB,
UpdateUserParams as UpdateUserParamsPB,
};
use sqlx::PgPool;
pub async fn sign_in_handler(payload: Payload, id: Identity, pool: Data<PgPool>) -> Result<HttpResponse, ServerError> {
let params: SignInParams = parse_from_payload(payload).await?;
let params: SignInParamsPB = parse_from_payload(payload).await?;
let data = sign_in(pool.get_ref(), params).await?;
id.remember(data.token.clone());
let response = FlowyResponse::success().pb(data)?;
@ -42,13 +46,13 @@ pub async fn set_user_profile_handler(
pool: Data<PgPool>,
payload: Payload,
) -> Result<HttpResponse, ServerError> {
let params: UpdateUserParams = parse_from_payload(payload).await?;
let params: UpdateUserParamsPB = parse_from_payload(payload).await?;
let response = set_user_profile(pool.get_ref(), logged_user, params).await?;
Ok(response.into())
}
pub async fn register_handler(payload: Payload, pool: Data<PgPool>) -> Result<HttpResponse, ServerError> {
let params: SignUpParams = parse_from_payload(payload).await?;
let params: SignUpParamsPB = parse_from_payload(payload).await?;
let resp = register_user(pool.get_ref(), params).await?;
Ok(resp.into())

View File

@ -10,11 +10,12 @@ use std::sync::Arc;
use bytes::Bytes;
use tokio::time::{sleep, Duration};
use crate::util::helper::{spawn_server, TestServer};
use flowy_collaboration::{entities::doc::DocumentId, protobuf::ResetDocumentParams};
use flowy_collaboration::{entities::doc::DocumentId, protobuf::ResetDocumentParams as ResetDocumentParamsPB};
use lib_ot::rich_text::{RichTextAttribute, RichTextDelta};
use parking_lot::RwLock;
use backend::services::document::persistence::{read_document, reset_document};
use flowy_collaboration::entities::revision::{RepeatedRevision, Revision};
use flowy_collaboration::protobuf::{RepeatedRevision as RepeatedRevisionPB, DocumentId as DocumentIdPB};
use flowy_collaboration::sync::ServerDocumentManager;
use lib_ot::core::Interval;
@ -115,7 +116,7 @@ async fn run_scripts(context: Arc<RwLock<ScriptContext>>, scripts: Vec<DocScript
DocScript::AssertServer(s, rev_id) => {
sleep(Duration::from_millis(2000)).await;
let persistence = Data::new(context.read().server.app_ctx.persistence.kv_store());
let doc_identifier: flowy_collaboration::protobuf::DocumentId = DocumentId {
let doc_identifier: DocumentIdPB = DocumentId {
doc_id
}.try_into().unwrap();
@ -171,8 +172,8 @@ async fn create_doc(flowy_test: &FlowySDKTest) -> String {
}
async fn reset_doc(doc_id: &str, repeated_revision: RepeatedRevision, document_manager: &Arc<ServerDocumentManager>) {
let pb: flowy_collaboration::protobuf::RepeatedRevision = repeated_revision.try_into().unwrap();
let mut params = ResetDocumentParams::new();
let pb: RepeatedRevisionPB = repeated_revision.try_into().unwrap();
let mut params = ResetDocumentParamsPB::new();
params.set_doc_id(doc_id.to_owned());
params.set_revisions(pb);
let _ = reset_document(document_manager, params).await.unwrap();

View File

@ -16,5 +16,5 @@ scheduled-thread-pool = "0.2.5"
error-chain = "=0.12.0"
log = "0.4.11"
[features]
windows = ["libsqlite3-sys/bundled-windows"]
#[features]
#windows = ["libsqlite3-sys/bundled-windows"]