Don't return content in response if it's unchanged
This commit is contained in:
parent
3ae0e7b896
commit
c41ce7ef68
5 changed files with 113 additions and 32 deletions
|
|
@ -12,10 +12,10 @@ use schemars::JsonSchema;
|
|||
use serde::Deserialize;
|
||||
use sync_lib::{base64_to_bytes, merge};
|
||||
|
||||
use super::{auth::auth, requests::CreateDocumentVersion};
|
||||
use super::{auth::auth, requests::CreateDocumentVersion, responses::DocumentUpdateResponse};
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
database::models::{DocumentVersion, StoredDocumentVersion, VaultId},
|
||||
database::models::{StoredDocumentVersion, VaultId},
|
||||
errors::{client_error, server_error, SyncServerError},
|
||||
};
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ pub async fn create_document(
|
|||
Path(PathParams { vault_id }): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<CreateDocumentVersion>,
|
||||
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
||||
) -> Result<Json<DocumentUpdateResponse>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
let mut transaction = state
|
||||
|
|
@ -56,22 +56,26 @@ pub async fn create_document(
|
|||
.map_err(server_error)?
|
||||
.and_then(|doc| if doc.is_deleted { None } else { Some(doc) });
|
||||
|
||||
let new_version = if let Some(existing_version) = maybe_existing_version {
|
||||
let content_bytes = base64_to_bytes(&request.content_base64)
|
||||
.context("Failed to decode base64 content in request")
|
||||
.map_err(client_error)?;
|
||||
let content_bytes = base64_to_bytes(&request.content_base64)
|
||||
.context("Failed to decode base64 content in request")
|
||||
.map_err(client_error)?;
|
||||
|
||||
let response = if let Some(existing_version) = maybe_existing_version {
|
||||
if content_bytes == existing_version.content {
|
||||
info!(
|
||||
"Content of the new version is the same as the existing version. Not creating a \
|
||||
new version."
|
||||
);
|
||||
|
||||
transaction
|
||||
.rollback()
|
||||
.await
|
||||
.context("Failed to rollback unecceseary transaction")
|
||||
.context("Failed to roll back unecceseary transaction")
|
||||
.map_err(server_error)?;
|
||||
return Ok(Json(existing_version.into()));
|
||||
|
||||
return Ok(Json(DocumentUpdateResponse::FastForwardUpdate(
|
||||
existing_version.into(),
|
||||
)));
|
||||
}
|
||||
|
||||
let merged_content = merge(
|
||||
|
|
@ -80,7 +84,7 @@ pub async fn create_document(
|
|||
&content_bytes,
|
||||
);
|
||||
|
||||
StoredDocumentVersion {
|
||||
let new_version = StoredDocumentVersion {
|
||||
vault_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
relative_path: request.relative_path,
|
||||
|
|
@ -89,27 +93,35 @@ pub async fn create_document(
|
|||
created_date: request.created_date,
|
||||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: false,
|
||||
}
|
||||
};
|
||||
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
DocumentUpdateResponse::MergingUpdate(new_version.into())
|
||||
} else {
|
||||
StoredDocumentVersion {
|
||||
let new_version = StoredDocumentVersion {
|
||||
vault_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
document_id: uuid::Uuid::new_v4(),
|
||||
relative_path: request.relative_path,
|
||||
content: base64_to_bytes(&request.content_base64)
|
||||
.context("Cannot convert base64 encoded content to bytes")
|
||||
.map_err(client_error)?,
|
||||
content: content_bytes,
|
||||
created_date: request.created_date,
|
||||
updated_date: chrono::Utc::now(),
|
||||
is_deleted: false,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
state
|
||||
.database
|
||||
.insert_document_version(&new_version, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
DocumentUpdateResponse::FastForwardUpdate(new_version.into())
|
||||
};
|
||||
|
||||
transaction
|
||||
.commit()
|
||||
|
|
@ -117,5 +129,5 @@ pub async fn create_document(
|
|||
.context("Failed to commit successful transaction")
|
||||
.map_err(server_error)?;
|
||||
|
||||
Ok(Json(new_version.into()))
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,40 @@
|
|||
use schemars::JsonSchema;
|
||||
use serde::{self, Serialize};
|
||||
|
||||
use crate::database::models::{DocumentVersionWithoutContent, VaultUpdateId};
|
||||
use crate::database::models::{DocumentVersion, DocumentVersionWithoutContent, VaultUpdateId};
|
||||
|
||||
/// Response to a ping request.
|
||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PingResponse {
|
||||
/// Semantic version of the server.
|
||||
pub server_version: String,
|
||||
|
||||
/// Whether the client is authenticated based on the sent Authorization
|
||||
/// header.
|
||||
pub is_authenticated: bool,
|
||||
}
|
||||
|
||||
/// Response to a fetch latest documents request.
|
||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FetchLatestDocumentsResponse {
|
||||
pub latest_documents: Vec<DocumentVersionWithoutContent>,
|
||||
|
||||
/// The update ID of the latest document in the response.
|
||||
pub last_update_id: VaultUpdateId,
|
||||
}
|
||||
|
||||
/// Response to a create/update document request.
|
||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum DocumentUpdateResponse {
|
||||
/// Returned when the created/updated document's content is the same as was
|
||||
/// sent in the create/update request and thus the response doesn't contain
|
||||
/// the content because the client must already have it.
|
||||
FastForwardUpdate(DocumentVersionWithoutContent),
|
||||
|
||||
/// Returned when the created/updated document's content is different from
|
||||
/// what was sent in the create/update request.
|
||||
MergingUpdate(DocumentVersion),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@ use schemars::JsonSchema;
|
|||
use serde::Deserialize;
|
||||
use sync_lib::{base64_to_bytes, merge};
|
||||
|
||||
use super::{auth::auth, requests::UpdateDocumentVersion};
|
||||
use super::{auth::auth, requests::UpdateDocumentVersion, responses::DocumentUpdateResponse};
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
database::models::{DocumentId, DocumentVersion, StoredDocumentVersion, VaultId},
|
||||
database::models::{DocumentId, StoredDocumentVersion, VaultId},
|
||||
errors::{client_error, not_found_error, server_error, SyncServerError},
|
||||
};
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ pub async fn update_document(
|
|||
}): Path<PathParams>,
|
||||
State(state): State<AppState>,
|
||||
Json(request): Json<UpdateDocumentVersion>,
|
||||
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
||||
) -> Result<Json<DocumentUpdateResponse>, SyncServerError> {
|
||||
auth(&state, auth_header.token())?;
|
||||
|
||||
// No need for a transaction as document versions are immutable
|
||||
|
|
@ -96,7 +96,9 @@ pub async fn update_document(
|
|||
.context("Failed to roll back transaction")
|
||||
.map_err(server_error)?;
|
||||
|
||||
return Ok(Json(latest_version.into()));
|
||||
return Ok(Json(DocumentUpdateResponse::FastForwardUpdate(
|
||||
latest_version.into(),
|
||||
)));
|
||||
}
|
||||
|
||||
let merged_content = merge(
|
||||
|
|
@ -104,6 +106,7 @@ pub async fn update_document(
|
|||
&latest_version.content,
|
||||
&content_bytes,
|
||||
);
|
||||
let is_different_from_request_content = merged_content != content_bytes;
|
||||
|
||||
// We can only update the relative path if we're the first one to do so
|
||||
let new_relative_path = if parent_document.relative_path == latest_version.relative_path {
|
||||
|
|
@ -135,5 +138,9 @@ pub async fn update_document(
|
|||
.context("Failed to commit successful transaction")
|
||||
.map_err(server_error)?;
|
||||
|
||||
Ok(Json(new_version.into()))
|
||||
Ok(Json(if is_different_from_request_content {
|
||||
DocumentUpdateResponse::MergingUpdate(new_version.into())
|
||||
} else {
|
||||
DocumentUpdateResponse::FastForwardUpdate(new_version.into())
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue