Skip to content

Commit

Permalink
Add check for last revision ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmiegit committed Oct 8, 2024
1 parent aedad53 commit ab94456
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions deepwell/src/services/file/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl FileService {
page_id,
file_id,
user_id,
last_revision_id,
revision_comments,
bypass_filter,
body,
Expand All @@ -121,6 +122,8 @@ impl FileService {
let last_revision =
FileRevisionService::get_latest(ctx, site_id, page_id, file_id).await?;

check_last_revision(&last_revision, last_revision_id)?;

let EditFileBody {
name,
licensing,
Expand Down Expand Up @@ -203,6 +206,7 @@ impl FileService {
destination_page_id,
file_id,
user_id,
last_revision_id,
revision_comments,
}: MoveFile,
) -> Result<Option<MoveFileOutput>> {
Expand All @@ -211,6 +215,8 @@ impl FileService {
FileRevisionService::get_latest(ctx, site_id, current_page_id, file_id)
.await?;

check_last_revision(&last_revision, last_revision_id)?;

// Get destination filename
let name = name.unwrap_or_else(|| last_revision.name.clone());

Expand Down Expand Up @@ -261,6 +267,7 @@ impl FileService {
pub async fn delete(
ctx: &ServiceContext<'_>,
DeleteFile {
last_revision_id,
revision_comments,
site_id,
page_id,
Expand All @@ -284,6 +291,8 @@ impl FileService {
let last_revision =
FileRevisionService::get_latest(ctx, site_id, page_id, file_id).await?;

check_last_revision(&last_revision, last_revision_id)?;

// Create tombstone revision
// This outdates the page, etc
let output = FileRevisionService::create_tombstone(
Expand Down Expand Up @@ -567,3 +576,22 @@ impl FileService {
Ok(())
}
}

/// Verifies that the `last_revision_id` argument is the most recent.
///
/// See the helper function with the same name in `services/page/service.rs`.
fn check_last_revision(
last_revision_model: &FileRevisionModel,
arg_last_revision_id: i64,
) -> Result<()> {
if last_revision_model.revision_id != arg_last_revision_id {
error!(
"Latest revision ID in file table is {}, but user argument has ID {}",
last_revision_model.revision_id, arg_last_revision_id,
);

return Err(Error::NotLatestRevisionId);
}

Ok(())
}
3 changes: 3 additions & 0 deletions deepwell/src/services/file/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub struct EditFile {
pub page_id: i64,
pub file_id: i64,
pub user_id: i64,
pub last_revision_id: i64,
pub revision_comments: String,

#[serde(flatten)]
Expand All @@ -110,6 +111,7 @@ pub struct MoveFile {
pub site_id: i64,
pub file_id: i64,
pub user_id: i64,
pub last_revision_id: i64,
pub name: Option<String>,
pub current_page_id: i64,
pub destination_page_id: i64,
Expand All @@ -119,6 +121,7 @@ pub type MoveFileOutput = CreateFileRevisionOutput;

#[derive(Deserialize, Debug, Clone)]
pub struct DeleteFile<'a> {
pub last_revision_id: i64,
pub revision_comments: String,
pub site_id: i64,
pub page_id: i64,
Expand Down

0 comments on commit ab94456

Please sign in to comment.