Compare commits

..

No commits in common. "1b1f1dfffa505482a5682fe031d5057e989e0fa3" and "4381cc82cba1385e45025a7706728e158e38bde7" have entirely different histories.

9 changed files with 48 additions and 70 deletions

View file

@ -24,7 +24,7 @@ pub enum MessageContent {
content: ElementContent, content: ElementContent,
pot: PotId, pot: PotId,
}, },
UpdateElement { SetElement {
id: ElementId, id: ElementId,
content: ElementContent, content: ElementContent,
}, },

View file

@ -5,22 +5,33 @@ use super::{ElementContent, ElementId, MessageId, PotId};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Element { pub struct Element {
// Uuid identifying the element itself // Uuid identifying the element itself
pub id: ElementId, id: ElementId,
pub pot: Option<PotId>, pot: Option<PotId>,
pub content: ElementContent, content: ElementContent,
pub update_strategy: ElementUpdateStrategy, latest_message: Option<MessageId>,
pub latest_message: Option<MessageId>, local_changes: bool,
pub local_changes: bool, }
impl From<(ElementId, Option<PotId>, ElementContent, Option<MessageId>, bool)> for Element {
fn from(value: (ElementId, Option<PotId>, ElementContent, Option<MessageId>, bool)) -> Self {
Element {
id: value.0,
pot: value.1,
content: value.2,
latest_message: value.3,
local_changes: value.4,
}
}
} }
impl Element { impl Element {
pub fn new(id: ElementId, content: ElementContent, update_strategy: ElementUpdateStrategy) -> Self { pub fn new(id: ElementId, content: ElementContent) -> Self {
// A new element with no latest message must have local changes // A new element with no latest message must have local changes
Element { Element {
id: id, id: id,
pot: None, pot: None,
content, content: content,
update_strategy,
latest_message: None, latest_message: None,
local_changes: true, local_changes: true,
} }
@ -42,16 +53,3 @@ impl Element {
self.local_changes self.local_changes
} }
} }
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum ElementUpdateStrategy {
Overwrite
}
impl Default for ElementUpdateStrategy {
fn default() -> Self {
Self::Overwrite
}
}

View file

@ -8,7 +8,7 @@ mod element_id;
pub use element_id::ElementId; pub use element_id::ElementId;
mod element; mod element;
pub use element::{Element, ElementUpdateStrategy}; pub use element::Element;
mod family_id; mod family_id;
pub use family_id::FamilyId; pub use family_id::FamilyId;

View file

@ -1,7 +1,7 @@
use tracing::debug; use tracing::debug;
use ubisync_lib::peer::Peer; use ubisync_lib::peer::Peer;
use ubisync_lib::types::{ElementUpdateStrategy, PeerId}; use ubisync_lib::types::PeerId;
use ubisync_lib::messages::{Message, MessageContent}; use ubisync_lib::messages::{Message, MessageContent};
@ -20,13 +20,12 @@ pub fn handle(state: &CommState, peer: &PeerId, message: Message) {
.add_received_element( .add_received_element(
id.to_owned(), id.to_owned(),
content.to_owned(), content.to_owned(),
ElementUpdateStrategy::Overwrite,
Some(message.id().to_owned()), Some(message.id().to_owned()),
pot.to_owned(), pot.to_owned(),
) )
.expect("State failed"); .expect("State failed");
} }
MessageContent::UpdateElement { id, content } => { MessageContent::SetElement { id, content } => {
state state
.update_element_content(id.to_owned(), content.to_owned(), message.id().to_owned()) .update_element_content(id.to_owned(), content.to_owned(), message.id().to_owned())
.expect("State failed"); .expect("State failed");

View file

@ -6,7 +6,7 @@ use tracing::debug;
use ubisync_lib::{ use ubisync_lib::{
api::events::AppEvent, api::events::AppEvent,
messages::MessageContent, messages::MessageContent,
types::{AppId, Element, ElementContent, ElementId, ElementUpdateStrategy, Pot, PotId}, types::{AppId, Element, ElementContent, ElementId, Pot, PotId},
}; };
use crate::api::v0::app::App; use crate::api::v0::app::App;
@ -60,7 +60,7 @@ impl ApiState {
pub fn create_element(&self, content: ElementContent, pot: PotId) -> anyhow::Result<ElementId> { pub fn create_element(&self, content: ElementContent, pot: PotId) -> anyhow::Result<ElementId> {
let id = ElementId::new(); let id = ElementId::new();
self.db() self.db()
.add_element(id.clone(), content.clone(), ElementUpdateStrategy::Overwrite, None, false, pot.clone())?; .add_element(id.clone(), content.clone(), None, false, pot.clone())?;
debug!("Added element {{{}}}", id.to_string()); debug!("Added element {{{}}}", id.to_string());
self.state.send_to_peers( self.state.send_to_peers(

View file

@ -5,7 +5,7 @@ use tracing::debug;
use ubisync_lib::{ use ubisync_lib::{
api::events::AppEvent, api::events::AppEvent,
peer::Peer, peer::Peer,
types::{Element, ElementContent, ElementId, ElementUpdateStrategy, MessageId, PotId}, types::{Element, ElementContent, ElementId, MessageId, PotId},
}; };
use crate::node_events::UbisyncNodeEvent; use crate::node_events::UbisyncNodeEvent;
@ -25,19 +25,11 @@ impl CommState {
&self, &self,
id: ElementId, id: ElementId,
content: ElementContent, content: ElementContent,
update_strategy: ElementUpdateStrategy,
latest_message: Option<MessageId>, latest_message: Option<MessageId>,
pot_id: PotId, pot_id: PotId,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
self.db() self.db()
.add_element( .add_element(id.clone(), content, latest_message, false, pot_id)
id.clone(),
content,
update_strategy,
latest_message,
false,
pot_id,
)
.inspect(|_| debug!("Added element {{{}}}", id.to_string())) .inspect(|_| debug!("Added element {{{}}}", id.to_string()))
} }
@ -110,7 +102,7 @@ mod tests {
use super::CommState; use super::CommState;
use tracing::Level; use tracing::Level;
use ubisync_lib::types::{ElementContent, ElementId, ElementUpdateStrategy, MessageId, PotId}; use ubisync_lib::types::{ElementContent, ElementId, MessageId, PotId};
#[tokio::test] #[tokio::test]
#[serial_test::serial] #[serial_test::serial]
@ -127,7 +119,6 @@ mod tests {
.add_received_element( .add_received_element(
id.clone(), id.clone(),
ElementContent::Text("Test-text".to_string()), ElementContent::Text("Test-text".to_string()),
ElementUpdateStrategy::Overwrite,
Some(MessageId::new()), Some(MessageId::new()),
pot_id, pot_id,
) )
@ -154,7 +145,6 @@ mod tests {
.add_received_element( .add_received_element(
id.clone(), id.clone(),
ElementContent::Text("Test-text".to_string()), ElementContent::Text("Test-text".to_string()),
ElementUpdateStrategy::Overwrite,
Some(MessageId::new()), Some(MessageId::new()),
pot_id, pot_id,
) )

View file

@ -113,7 +113,7 @@ impl StateDB {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ubisync_lib::types::{AppId, ElementContent, ElementId, ElementUpdateStrategy, Pot, PotId}; use ubisync_lib::types::{AppId, ElementContent, ElementId, Pot, PotId};
use crate::{api::v0::app::App, state::database::StateDB}; use crate::{api::v0::app::App, state::database::StateDB};
@ -259,7 +259,6 @@ mod tests {
db.add_element( db.add_element(
element_id.clone(), element_id.clone(),
ElementContent::Text("Text".to_string()), ElementContent::Text("Text".to_string()),
ElementUpdateStrategy::Overwrite,
None, None,
false, false,
pot_id.clone(), pot_id.clone(),

View file

@ -1,7 +1,7 @@
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use bonsaidb::core::schema::{Collection, SerializedCollection}; use bonsaidb::core::schema::{Collection, SerializedCollection};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ubisync_lib::types::{Element, ElementContent, ElementId, ElementUpdateStrategy, MessageId, PotId}; use ubisync_lib::types::{Element, ElementContent, ElementId, MessageId, PotId};
use crate::state::database::{as_key::AsKey, StateDB}; use crate::state::database::{as_key::AsKey, StateDB};
@ -11,7 +11,6 @@ pub(super) struct DbElement {
#[natural_id] #[natural_id]
pub(super) id: AsKey<ElementId>, pub(super) id: AsKey<ElementId>,
pub(super) content: ElementContent, pub(super) content: ElementContent,
pub(super) update_strategy: ElementUpdateStrategy,
pub(super) latest_message: Option<MessageId>, pub(super) latest_message: Option<MessageId>,
pub(super) local_changes: bool, pub(super) local_changes: bool,
pub(super) pot: PotId, pub(super) pot: PotId,
@ -19,14 +18,13 @@ pub(super) struct DbElement {
impl From<DbElement> for Element { impl From<DbElement> for Element {
fn from(value: DbElement) -> Self { fn from(value: DbElement) -> Self {
Element { Element::from((
id: (*value.id).clone(), (*value.id).clone(),
content: value.content, Some(value.pot),
update_strategy: value.update_strategy, value.content,
latest_message: value.latest_message, value.latest_message,
local_changes: value.local_changes, value.local_changes,
pot: Some(value.pot), ))
}
} }
} }
@ -35,7 +33,6 @@ impl StateDB {
&self, &self,
id: ElementId, id: ElementId,
content: ElementContent, content: ElementContent,
update_strategy: ElementUpdateStrategy,
latest_message: Option<MessageId>, latest_message: Option<MessageId>,
local_changes: bool, local_changes: bool,
pot: PotId, pot: PotId,
@ -44,7 +41,6 @@ impl StateDB {
DbElement { DbElement {
id: AsKey::new(id), id: AsKey::new(id),
content, content,
update_strategy,
latest_message, latest_message,
local_changes, local_changes,
pot, pot,
@ -108,7 +104,7 @@ impl StateDB {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ubisync_lib::types::{Element, ElementContent, ElementId, ElementUpdateStrategy, MessageId, PotId}; use ubisync_lib::types::{ElementContent, ElementId, MessageId, PotId};
use crate::state::database::StateDB; use crate::state::database::StateDB;
@ -120,7 +116,6 @@ mod tests {
db.add_element( db.add_element(
element_id.clone(), element_id.clone(),
ElementContent::Text("Content!!!".to_string()), ElementContent::Text("Content!!!".to_string()),
ElementUpdateStrategy::default(),
None, None,
false, false,
pot_id.clone(), pot_id.clone(),
@ -131,14 +126,14 @@ mod tests {
assert_eq!( assert_eq!(
Some( Some(
Element { (
id: element_id, element_id,
content: ElementContent::Text("Content!!!".to_string()), Some(pot_id),
update_strategy: ElementUpdateStrategy::default(), ElementContent::Text("Content!!!".to_string()),
latest_message: None, None,
local_changes: false, false
pot: Some(pot_id), )
} .into()
), ),
retrieved_element retrieved_element
) )
@ -151,7 +146,6 @@ mod tests {
db.add_element( db.add_element(
element_id.clone(), element_id.clone(),
ElementContent::Text("Content!!!".to_string()), ElementContent::Text("Content!!!".to_string()),
ElementUpdateStrategy::default(),
None, None,
false, false,
PotId::new(), PotId::new(),
@ -177,7 +171,6 @@ mod tests {
db.add_element( db.add_element(
element_id.clone(), element_id.clone(),
ElementContent::Text("Content!!!".to_string()), ElementContent::Text("Content!!!".to_string()),
ElementUpdateStrategy::default(),
None, None,
false, false,
PotId::new(), PotId::new(),
@ -213,7 +206,6 @@ mod tests {
db.add_element( db.add_element(
element_id.clone(), element_id.clone(),
ElementContent::Text("Content!!!".to_string()), ElementContent::Text("Content!!!".to_string()),
ElementUpdateStrategy::default(),
None, None,
false, false,
PotId::new(), PotId::new(),

View file

@ -88,7 +88,7 @@ impl State {
.inspect(|_| { .inspect(|_| {
//TODO: Get all peers interested in the element, e.g. because they subscribe to the element's pot, a share, etc. //TODO: Get all peers interested in the element, e.g. because they subscribe to the element's pot, a share, etc.
self.send_to_peers( self.send_to_peers(
MessageContent::UpdateElement { MessageContent::SetElement {
id: element_id.clone(), id: element_id.clone(),
content: content.clone(), content: content.clone(),
}, },