diff --git a/ubisync-lib/src/api/element.rs b/ubisync-lib/src/api/element.rs index 2155132..20829b8 100644 --- a/ubisync-lib/src/api/element.rs +++ b/ubisync-lib/src/api/element.rs @@ -1,7 +1,7 @@ use reqwest::Method; use serde::{Deserialize, Serialize}; -use crate::types::{Element, ElementContent, ElementId, PotId}; +use crate::types::{ContentUpdateStrategy, Element, ElementContent, ElementId, PotId}; use super::UbisyncRequest; @@ -9,6 +9,7 @@ use super::UbisyncRequest; pub struct ElementCreateRequest { pub content: ElementContent, pub pot: Option, + pub update_strategy: Option, } #[derive(Serialize, Deserialize)] diff --git a/ubisync/src/api/v0/element.rs b/ubisync/src/api/v0/element.rs index 05925b7..122d925 100644 --- a/ubisync/src/api/v0/element.rs +++ b/ubisync/src/api/v0/element.rs @@ -17,7 +17,6 @@ use ubisync_lib::{ types::{AppId, ElementId}, }; - pub(super) async fn get( Path(id): Path, app: Extension, @@ -51,7 +50,7 @@ pub(super) async fn create( Ok(_) => { warn!("Pot not found"); return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - }, + } Err(e) => { warn!("Element create request did not provide pot id, and no default pot for requesting app was found: {}", e); return StatusCode::INTERNAL_SERVER_ERROR.into_response(); @@ -59,7 +58,11 @@ pub(super) async fn create( }, }; - let element_id = s.create_element(req.content, pot_id); + let element_id = s.create_element( + req.content, + pot_id, + req.update_strategy.unwrap_or(Default::default()), + ); debug!("{:?}", element_id); match element_id { Ok(id) => ( diff --git a/ubisync/src/state/api_state.rs b/ubisync/src/state/api_state.rs index 9d0e9aa..fa350ca 100644 --- a/ubisync/src/state/api_state.rs +++ b/ubisync/src/state/api_state.rs @@ -57,10 +57,10 @@ impl ApiState { .map(|app_opt| app_opt.ok_or(Error::msg("Failed to find app")))? } - pub fn create_element(&self, content: ElementContent, pot: PotId) -> anyhow::Result { + pub fn create_element(&self, content: ElementContent, pot: PotId, update_strategy: ContentUpdateStrategy) -> anyhow::Result { let id = ElementId::new(); self.db() - .add_element(id.clone(), content.clone(), ContentUpdateStrategy::Overwrite, None, false, pot.clone())?; + .add_element(id.clone(), content.clone(), update_strategy, None, false, pot.clone())?; debug!("Added element {{{}}}", id.to_string()); self.state.send_to_peers( @@ -192,7 +192,7 @@ impl ApiState { #[cfg(test)] mod tests { use tracing::Level; - use ubisync_lib::types::ElementContent; + use ubisync_lib::types::{ContentUpdateStrategy, ElementContent}; use crate::state::State; @@ -224,6 +224,7 @@ mod tests { .create_element( ElementContent::Text("Test-text".to_string()), pot_id.clone(), + ContentUpdateStrategy::Overwrite, ) .unwrap(); let el = state.get_element(id, app_id).unwrap(); @@ -259,6 +260,7 @@ mod tests { .create_element( ElementContent::Text("Test-text".to_string()), pot_id.clone(), + ContentUpdateStrategy::Overwrite, ) .unwrap(); state diff --git a/ubisync/tests/api.rs b/ubisync/tests/api.rs index dbcb0b8..1c6757f 100644 --- a/ubisync/tests/api.rs +++ b/ubisync/tests/api.rs @@ -57,6 +57,7 @@ async fn two_nodes_element_creation() { ElementCreateRequest { content: test_element_content.clone(), pot: None, + update_strategy: None, }, (), )