1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-25 21:41:10 +00:00

all tests passing!

This commit is contained in:
Felix Ableitner 2023-12-01 14:04:51 +01:00
parent 7c789a1c06
commit d4772d35c2
18 changed files with 207 additions and 92 deletions

23
Cargo.lock generated
View file

@ -549,6 +549,12 @@ dependencies = [
"syn 2.0.39", "syn 2.0.39",
] ]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]] [[package]]
name = "diffy" name = "diffy"
version = "0.3.0" version = "0.3.0"
@ -686,6 +692,7 @@ dependencies = [
"env_logger", "env_logger",
"futures", "futures",
"once_cell", "once_cell",
"pretty_assertions",
"rand", "rand",
"reqwest", "reqwest",
"serde", "serde",
@ -1504,6 +1511,16 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "pretty_assertions"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
dependencies = [
"diff",
"yansi",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.69" version = "1.0.69"
@ -2561,3 +2578,9 @@ dependencies = [
"cfg-if", "cfg-if",
"windows-sys", "windows-sys",
] ]
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View file

@ -27,5 +27,6 @@ url = "2.4.1"
[dev-dependencies] [dev-dependencies]
once_cell = "1.18.0" once_cell = "1.18.0"
pretty_assertions = "1.4.0"
reqwest = "0.11.22" reqwest = "0.11.22"
serial_test = "2.0.0" serial_test = "2.0.0"

View file

@ -4,7 +4,6 @@ create table article (
text text not null, text text not null,
ap_id varchar(255) not null unique, ap_id varchar(255) not null unique,
instance_id varchar(255) not null, instance_id varchar(255) not null,
latest_version text not null,
local bool not null local bool not null
); );
@ -14,5 +13,6 @@ create table edit (
diff text not null, diff text not null,
article_id int REFERENCES article ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, article_id int REFERENCES article ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
version text not null, version text not null,
previous_version text not null,
local bool not null local bool not null
) )

View file

@ -44,13 +44,11 @@ pub struct CreateArticleData {
async fn create_article( async fn create_article(
data: Data<MyDataHandle>, data: Data<MyDataHandle>,
Form(create_article): Form<CreateArticleData>, Form(create_article): Form<CreateArticleData>,
) -> MyResult<Json<DbArticle>> { ) -> MyResult<Json<ArticleView>> {
dbg!(1);
let existing_article = DbArticle::read_local_title(&create_article.title, &data.db_connection); let existing_article = DbArticle::read_local_title(&create_article.title, &data.db_connection);
if existing_article.is_ok() { if existing_article.is_ok() {
return Err(anyhow!("A local article with this title already exists").into()); return Err(anyhow!("A local article with this title already exists").into());
} }
dbg!(2);
let instance_id = data.local_instance().ap_id; let instance_id = data.local_instance().ap_id;
let ap_id = ObjectId::parse(&format!( let ap_id = ObjectId::parse(&format!(
@ -58,24 +56,19 @@ async fn create_article(
instance_id.inner().domain().unwrap(), instance_id.inner().domain().unwrap(),
instance_id.inner().port().unwrap(), instance_id.inner().port().unwrap(),
create_article.title create_article.title
))? ))?;
.into();
let form = DbArticleForm { let form = DbArticleForm {
title: create_article.title, title: create_article.title,
text: String::new(), text: String::new(),
ap_id, ap_id,
latest_version: Default::default(),
instance_id, instance_id,
local: true, local: true,
}; };
dbg!(3); let article = DbArticle::create(&form, &data.db_connection)?;
let article = dbg!(DbArticle::create(&form, &data.db_connection))?;
dbg!(4);
CreateArticle::send_to_followers(article.clone(), &data).await?; CreateArticle::send_to_followers(article.clone(), &data).await?;
dbg!(5);
Ok(Json(article)) Ok(Json(DbArticle::read_view(article.id, &data.db_connection)?))
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
@ -122,30 +115,37 @@ async fn edit_article(
} }
lock.retain(|c| &c.id != resolve_conflict_id); lock.retain(|c| &c.id != resolve_conflict_id);
} }
let original_article = DbArticle::read(edit_form.article_id, &data.db_connection)?; let original_article = DbArticle::read_view(edit_form.article_id, &data.db_connection)?;
if edit_form.previous_version == original_article.latest_version { if edit_form.previous_version == original_article.latest_version {
// No intermediate changes, simply submit new version // No intermediate changes, simply submit new version
submit_article_update(&data, edit_form.new_text.clone(), &original_article).await?; submit_article_update(
&data,
edit_form.new_text.clone(),
edit_form.previous_version,
&original_article.article,
)
.await?;
Ok(Json(None)) Ok(Json(None))
} else { } else {
// There have been other changes since this edit was initiated. Get the common ancestor // There have been other changes since this edit was initiated. Get the common ancestor
// version and generate a diff to find out what exactly has changed. // version and generate a diff to find out what exactly has changed.
let edits = DbEdit::for_article(&original_article, &data.db_connection)?; let ancestor =
let ancestor = generate_article_version(&edits, &edit_form.previous_version)?; generate_article_version(&original_article.edits, &edit_form.previous_version)?;
let patch = create_patch(&ancestor, &edit_form.new_text); let patch = create_patch(&ancestor, &edit_form.new_text);
dbg!(&edit_form.previous_version);
let db_conflict = DbConflict { let db_conflict = DbConflict {
id: random(), id: random(),
diff: patch.to_string(), diff: patch.to_string(),
article_id: original_article.ap_id.clone().into(), article_id: original_article.article.ap_id.clone(),
previous_version: edit_form.previous_version, previous_version: edit_form.previous_version,
}; };
{ {
let mut lock = data.conflicts.lock().unwrap(); let mut lock = data.conflicts.lock().unwrap();
lock.push(db_conflict.clone()); lock.push(db_conflict.clone());
} }
Ok(Json(db_conflict.to_api_conflict(&data).await?)) Ok(Json(dbg!(db_conflict.to_api_conflict(&data).await)?))
} }
} }
@ -191,7 +191,12 @@ async fn resolve_article(
) -> MyResult<Json<ArticleView>> { ) -> MyResult<Json<ArticleView>> {
let article: DbArticle = ObjectId::from(query.id).dereference(&data).await?; let article: DbArticle = ObjectId::from(query.id).dereference(&data).await?;
let edits = DbEdit::for_article(&article, &data.db_connection)?; let edits = DbEdit::for_article(&article, &data.db_connection)?;
Ok(Json(ArticleView { article, edits })) let latest_version = edits.last().unwrap().version.clone();
Ok(Json(ArticleView {
article,
edits,
latest_version,
}))
} }
/// Retrieve the local instance info. /// Retrieve the local instance info.
@ -220,16 +225,21 @@ async fn follow_instance(
/// Get a list of all unresolved edit conflicts. /// Get a list of all unresolved edit conflicts.
#[debug_handler] #[debug_handler]
async fn edit_conflicts(data: Data<MyDataHandle>) -> MyResult<Json<Vec<ApiConflict>>> { async fn edit_conflicts(data: Data<MyDataHandle>) -> MyResult<Json<Vec<ApiConflict>>> {
dbg!("a");
let conflicts = { data.conflicts.lock().unwrap().to_vec() }; let conflicts = { data.conflicts.lock().unwrap().to_vec() };
dbg!(&conflicts);
dbg!("b");
let conflicts: Vec<ApiConflict> = try_join_all(conflicts.into_iter().map(|c| { let conflicts: Vec<ApiConflict> = try_join_all(conflicts.into_iter().map(|c| {
let data = data.reset_request_count(); let data = data.reset_request_count();
async move { c.to_api_conflict(&data).await } dbg!(&c.previous_version);
async move { dbg!(c.to_api_conflict(&data).await) }
})) }))
.await? .await?
.into_iter() .into_iter()
.flatten() .flatten()
.collect(); .collect();
Ok(Json(conflicts)) dbg!("c");
Ok(Json(dbg!(conflicts)))
} }
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
@ -276,19 +286,22 @@ async fn fork_article(
instance_id.inner().domain().unwrap(), instance_id.inner().domain().unwrap(),
instance_id.inner().port().unwrap(), instance_id.inner().port().unwrap(),
original_article.title original_article.title
))? ))?;
.into();
let form = DbArticleForm { let form = DbArticleForm {
title: original_article.title.clone(), title: original_article.title.clone(),
text: original_article.text.clone(), text: original_article.text.clone(),
ap_id, ap_id,
latest_version: original_article.latest_version.0.clone(),
instance_id, instance_id,
local: true, local: true,
}; };
let article = DbArticle::create(&form, &data.db_connection)?; let article = DbArticle::create(&form, &data.db_connection)?;
// TODO: need to copy edits separately with db query // copy edits to new article
let edits = DbEdit::for_article(&original_article, &data.db_connection)?;
for e in edits {
let form = e.copy_to_local_fork(&article)?;
DbEdit::create(&form, &data.db_connection)?;
}
CreateArticle::send_to_followers(article.clone(), &data).await?; CreateArticle::send_to_followers(article.clone(), &data).await?;

View file

@ -13,6 +13,7 @@ use diesel::{
PgTextExpressionMethods, QueryDsl, Queryable, RunQueryDsl, Selectable, PgTextExpressionMethods, QueryDsl, Queryable, RunQueryDsl, Selectable,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::Mutex; use std::sync::Mutex;
@ -24,8 +25,6 @@ pub struct DbArticle {
pub text: String, pub text: String,
pub ap_id: ObjectId<DbArticle>, pub ap_id: ObjectId<DbArticle>,
pub instance_id: ObjectId<DbInstance>, pub instance_id: ObjectId<DbInstance>,
// TODO: should read this from edits table instead of separate db field
pub latest_version: EditVersion,
pub local: bool, pub local: bool,
} }
@ -33,6 +32,7 @@ pub struct DbArticle {
#[diesel(table_name = article, check_for_backend(diesel::pg::Pg))] #[diesel(table_name = article, check_for_backend(diesel::pg::Pg))]
pub struct ArticleView { pub struct ArticleView {
pub article: DbArticle, pub article: DbArticle,
pub latest_version: EditVersion,
pub edits: Vec<DbEdit>, pub edits: Vec<DbEdit>,
} }
@ -44,8 +44,6 @@ pub struct DbArticleForm {
pub ap_id: ObjectId<DbArticle>, pub ap_id: ObjectId<DbArticle>,
// TODO: change to foreign key // TODO: change to foreign key
pub instance_id: ObjectId<DbInstance>, pub instance_id: ObjectId<DbInstance>,
// TODO: instead of this we can use latest entry in edits table
pub latest_version: String,
pub local: bool, pub local: bool,
} }
@ -77,10 +75,18 @@ impl DbArticle {
} }
pub fn read_view(id: i32, conn: &Mutex<PgConnection>) -> MyResult<ArticleView> { pub fn read_view(id: i32, conn: &Mutex<PgConnection>) -> MyResult<ArticleView> {
let article: DbArticle = {
let mut conn = conn.lock().unwrap(); let mut conn = conn.lock().unwrap();
let article: DbArticle = article::table.find(id).get_result(conn.deref_mut())?; article::table.find(id).get_result(conn.deref_mut())?
let edits = DbEdit::belonging_to(&article).get_results(conn.deref_mut())?; };
Ok(ArticleView { article, edits }) let latest_version = article.latest_edit_version(conn)?;
let mut conn = conn.lock().unwrap();
let edits: Vec<DbEdit> = DbEdit::belonging_to(&article).get_results(conn.deref_mut())?;
Ok(ArticleView {
article,
edits,
latest_version,
})
} }
pub fn read_from_ap_id( pub fn read_from_ap_id(
@ -122,4 +128,14 @@ impl DbArticle {
) )
.get_results(conn.deref_mut())?) .get_results(conn.deref_mut())?)
} }
// TODO: shouldnt have to read all edits from db
pub fn latest_edit_version(&self, conn: &Mutex<PgConnection>) -> MyResult<EditVersion> {
let mut conn = conn.lock().unwrap();
let edits: Vec<DbEdit> = DbEdit::belonging_to(&self).get_results(conn.deref_mut())?;
match edits.last().map(|e| e.version.clone()) {
Some(latest_version) => Ok(latest_version),
None => Ok(EditVersion::default()),
}
}
} }

View file

@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha224}; use sha2::{Digest, Sha224};
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::Mutex; use std::sync::Mutex;
use url::Url;
/// Represents a single change to the article. /// Represents a single change to the article.
#[derive( #[derive(
@ -34,6 +33,8 @@ pub struct DbEdit {
pub diff: String, pub diff: String,
pub article_id: i32, pub article_id: i32,
pub version: EditVersion, pub version: EditVersion,
// TODO: could be an Option<DbEdit.id> instead
pub previous_version: EditVersion,
// TODO: there is already `local` field on article, do we need this? // TODO: there is already `local` field on article, do we need this?
pub local: bool, pub local: bool,
} }
@ -45,24 +46,40 @@ pub struct DbEditForm {
pub diff: String, pub diff: String,
pub article_id: i32, pub article_id: i32,
pub version: EditVersion, pub version: EditVersion,
pub previous_version: EditVersion,
pub local: bool, pub local: bool,
} }
impl DbEditForm { impl DbEditForm {
pub fn new(original_article: &DbArticle, updated_text: &str) -> MyResult<Self> { pub fn new(
original_article: &DbArticle,
updated_text: &str,
previous_version: EditVersion,
) -> MyResult<Self> {
let diff = create_patch(&original_article.text, updated_text); let diff = create_patch(&original_article.text, updated_text);
let mut sha224 = Sha224::new(); let (ap_id, hash) = Self::generate_ap_id_and_hash(original_article, diff.to_bytes())?;
sha224.update(diff.to_bytes());
let hash = format!("{:X}", sha224.finalize());
let edit_id = Url::parse(&format!("{}/{}", original_article.ap_id, hash))?;
Ok(DbEditForm { Ok(DbEditForm {
ap_id: edit_id.into(), ap_id,
diff: diff.to_string(), diff: diff.to_string(),
article_id: original_article.id, article_id: original_article.id,
version: EditVersion(hash), version: EditVersion(hash),
previous_version,
local: true, local: true,
}) })
} }
fn generate_ap_id_and_hash(
article: &DbArticle,
diff: Vec<u8>,
) -> MyResult<(ObjectId<DbEdit>, String)> {
let mut sha224 = Sha224::new();
sha224.update(diff);
let hash = format!("{:X}", sha224.finalize());
Ok((
ObjectId::parse(&format!("{}/{}", article.ap_id, hash))?,
hash,
))
}
} }
impl DbEdit { impl DbEdit {
@ -80,6 +97,18 @@ impl DbEdit {
let mut conn = conn.lock().unwrap(); let mut conn = conn.lock().unwrap();
Ok(DbEdit::belonging_to(&article).get_results(conn.deref_mut())?) Ok(DbEdit::belonging_to(&article).get_results(conn.deref_mut())?)
} }
pub fn copy_to_local_fork(self, article: &DbArticle) -> MyResult<DbEditForm> {
let (ap_id, _) =
DbEditForm::generate_ap_id_and_hash(article, self.diff.clone().into_bytes())?;
Ok(DbEditForm {
ap_id,
diff: self.diff,
article_id: article.id,
version: self.version,
previous_version: self.previous_version,
local: true,
})
}
} }
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, DieselNewType)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, DieselNewType)]

View file

@ -60,20 +60,28 @@ impl DbConflict {
data: &Data<MyDataHandle>, data: &Data<MyDataHandle>,
) -> MyResult<Option<ApiConflict>> { ) -> MyResult<Option<ApiConflict>> {
let original_article = let original_article =
DbArticle::read_from_ap_id(&self.article_id.clone().into(), &data.db_connection)?; DbArticle::read_from_ap_id(&self.article_id.clone(), &data.db_connection)?;
// create common ancestor version // create common ancestor version
let edits = DbEdit::for_article(&original_article, &data.db_connection)?; let edits = DbEdit::for_article(&original_article, &data.db_connection)?;
let ancestor = generate_article_version(&edits, &self.previous_version)?; let ancestor = generate_article_version(&edits, &self.previous_version)?;
dbg!(&ancestor, &self.previous_version);
dbg!(&self.diff);
let patch = Patch::from_str(&self.diff)?; let patch = Patch::from_str(&self.diff)?;
// apply self.diff to ancestor to get `ours` // apply self.diff to ancestor to get `ours`
let ours = apply(&ancestor, &patch)?; let ours = dbg!(apply(&ancestor, &patch))?;
match merge(&ancestor, &ours, &original_article.text) { match merge(&ancestor, &ours, &original_article.text) {
Ok(new_text) => { Ok(new_text) => {
// patch applies cleanly so we are done // patch applies cleanly so we are done
// federate the change // federate the change
submit_article_update(data, new_text, &original_article).await?; submit_article_update(
data,
new_text,
self.previous_version.clone(),
&original_article,
)
.await?;
// remove conflict from db // remove conflict from db
let mut lock = data.conflicts.lock().unwrap(); let mut lock = data.conflicts.lock().unwrap();
lock.retain(|c| c.id != self.id); lock.retain(|c| c.id != self.id);
@ -84,8 +92,8 @@ impl DbConflict {
Ok(Some(ApiConflict { Ok(Some(ApiConflict {
id: self.id, id: self.id,
three_way_merge, three_way_merge,
article_id: original_article.ap_id.into(), article_id: original_article.ap_id.clone(),
previous_version: original_article.latest_version, previous_version: original_article.latest_edit_version(&data.db_connection)?,
})) }))
} }
} }

View file

@ -9,7 +9,6 @@ diesel::table! {
ap_id -> Varchar, ap_id -> Varchar,
#[max_length = 255] #[max_length = 255]
instance_id -> Varchar, instance_id -> Varchar,
latest_version -> Text,
local -> Bool, local -> Bool,
} }
} }
@ -22,6 +21,7 @@ diesel::table! {
diff -> Text, diff -> Text,
article_id -> Int4, article_id -> Int4,
version -> Text, version -> Text,
previous_version -> Text,
local -> Bool, local -> Bool,
} }
} }

View file

@ -1,12 +1,11 @@
use crate::database::article::DbArticle; use crate::database::article::DbArticle;
use crate::database::edit::{DbEdit, DbEditForm}; use crate::database::edit::{DbEdit, DbEditForm, EditVersion};
use crate::database::MyDataHandle; use crate::database::MyDataHandle;
use crate::error::Error; use crate::error::Error;
use crate::federation::activities::update_local_article::UpdateLocalArticle; use crate::federation::activities::update_local_article::UpdateLocalArticle;
use crate::federation::activities::update_remote_article::UpdateRemoteArticle; use crate::federation::activities::update_remote_article::UpdateRemoteArticle;
use crate::federation::objects::instance::DbInstance; use crate::federation::objects::instance::DbInstance;
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
pub mod accept; pub mod accept;
pub mod create_article; pub mod create_article;
@ -18,17 +17,30 @@ pub mod update_remote_article;
pub async fn submit_article_update( pub async fn submit_article_update(
data: &Data<MyDataHandle>, data: &Data<MyDataHandle>,
new_text: String, new_text: String,
previous_version: EditVersion,
original_article: &DbArticle, original_article: &DbArticle,
) -> Result<(), Error> { ) -> Result<(), Error> {
let form = DbEditForm::new(original_article, &new_text)?; let form = DbEditForm::new(original_article, &new_text, previous_version)?;
let edit = DbEdit::create(&form, &data.db_connection)?;
if original_article.local { if original_article.local {
let edit = DbEdit::create(&form, &data.db_connection)?;
let updated_article = let updated_article =
DbArticle::update_text(edit.article_id, &new_text, &data.db_connection)?; DbArticle::update_text(edit.article_id, &new_text, &data.db_connection)?;
UpdateLocalArticle::send(updated_article, vec![], data).await?; UpdateLocalArticle::send(updated_article, vec![], data).await?;
} else { } else {
let instance: DbInstance = ObjectId::from(original_article.instance_id.clone()) // dont insert edit into db, might be invalid in case of conflict
let edit = DbEdit {
id: 0,
ap_id: form.ap_id,
diff: form.diff,
article_id: form.article_id,
version: form.version,
previous_version: form.previous_version,
local: form.local,
};
let instance: DbInstance = original_article
.instance_id
.clone()
.dereference(data) .dereference(data)
.await?; .await?;
UpdateRemoteArticle::send(edit, instance, data).await?; UpdateRemoteArticle::send(edit, instance, data).await?;

View file

@ -66,6 +66,7 @@ impl ActivityHandler for RejectEdit {
} }
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> { async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
dbg!(&self);
// cant convert this to DbEdit as it tries to apply patch and fails // cant convert this to DbEdit as it tries to apply patch and fails
let mut lock = data.conflicts.lock().unwrap(); let mut lock = data.conflicts.lock().unwrap();
let conflict = DbConflict { let conflict = DbConflict {

View file

@ -1,6 +1,10 @@
use crate::database::MyDataHandle; use crate::database::MyDataHandle;
use crate::error::MyResult; use crate::error::MyResult;
use crate::database::article::DbArticle;
use crate::database::edit::DbEdit;
use crate::federation::activities::reject::RejectEdit;
use crate::federation::activities::update_local_article::UpdateLocalArticle;
use crate::federation::objects::edit::ApubEdit; use crate::federation::objects::edit::ApubEdit;
use crate::federation::objects::instance::DbInstance; use crate::federation::objects::instance::DbInstance;
use crate::utils::generate_activity_id; use crate::utils::generate_activity_id;
@ -12,11 +16,6 @@ use activitypub_federation::{
traits::{ActivityHandler, Object}, traits::{ActivityHandler, Object},
}; };
use diffy::{apply, Patch}; use diffy::{apply, Patch};
use crate::database::article::DbArticle;
use crate::database::edit::DbEdit;
use crate::federation::activities::reject::RejectEdit;
use crate::federation::activities::update_local_article::UpdateLocalArticle;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -48,6 +47,9 @@ impl UpdateRemoteArticle {
kind: Default::default(), kind: Default::default(),
id, id,
}; };
// TODO: this is wrong and causes test failure. need to take previous_version from api param,
// or put previous_version in DbEdit
dbg!(&update.object.previous_version);
local_instance local_instance
.send(update, vec![article_instance.inbox], data) .send(update, vec![article_instance.inbox], data)
.await?; .await?;

View file

@ -45,16 +45,14 @@ impl Object for DbArticle {
} }
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> { async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
let instance: DbInstance = ObjectId::from(self.instance_id.clone()) let instance: DbInstance = self.instance_id.clone().dereference_local(data).await?;
.dereference_local(data)
.await?;
Ok(ApubArticle { Ok(ApubArticle {
kind: Default::default(), kind: Default::default(),
id: self.ap_id.clone().into(), id: self.ap_id.clone(),
attributed_to: instance.ap_id.clone().into(), attributed_to: instance.ap_id.clone(),
to: vec![public(), instance.followers_url()?], to: vec![public(), instance.followers_url()?],
edits: self.edits_id()?, edits: self.edits_id()?,
latest_version: self.latest_version, latest_version: self.latest_edit_version(&data.db_connection)?,
content: self.text, content: self.text,
name: self.title, name: self.title,
}) })
@ -73,10 +71,9 @@ impl Object for DbArticle {
let form = DbArticleForm { let form = DbArticleForm {
title: json.name, title: json.name,
text: json.content, text: json.content,
ap_id: json.id.into(), ap_id: json.id,
latest_version: json.latest_version.0,
local: false, local: false,
instance_id: json.attributed_to.into(), instance_id: json.attributed_to,
}; };
let article = DbArticle::create(&form, &data.db_connection)?; let article = DbArticle::create(&form, &data.db_connection)?;

View file

@ -42,12 +42,12 @@ impl Object for DbEdit {
let article = DbArticle::read(self.article_id, &data.db_connection)?; let article = DbArticle::read(self.article_id, &data.db_connection)?;
Ok(ApubEdit { Ok(ApubEdit {
kind: EditType::Edit, kind: EditType::Edit,
id: self.ap_id.into(), id: self.ap_id,
content: self.diff, content: self.diff,
version: self.version, version: self.version,
// TODO: this is wrong // TODO: this is wrong
previous_version: article.latest_version, previous_version: self.previous_version,
object: article.ap_id.into(), object: article.ap_id,
}) })
} }
@ -62,10 +62,11 @@ impl Object for DbEdit {
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> { async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
let article = json.object.dereference(data).await?; let article = json.object.dereference(data).await?;
let form = DbEditForm { let form = DbEditForm {
ap_id: json.id.into(), ap_id: json.id,
diff: json.content, diff: json.content,
article_id: article.id, article_id: article.id,
version: json.version, version: json.version,
previous_version: json.previous_version,
local: false, local: false,
}; };
let edit = DbEdit::create(&form, &data.db_connection)?; let edit = DbEdit::create(&form, &data.db_connection)?;

View file

@ -26,7 +26,6 @@ const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
pub async fn start(hostname: &str, database_url: &str) -> MyResult<()> { pub async fn start(hostname: &str, database_url: &str) -> MyResult<()> {
let fake_db = create_fake_db(hostname).await?; let fake_db = create_fake_db(hostname).await?;
dbg!(database_url);
let db_connection = Arc::new(Mutex::new(PgConnection::establish(database_url)?)); let db_connection = Arc::new(Mutex::new(PgConnection::establish(database_url)?));
db_connection db_connection
.lock() .lock()

View file

@ -10,6 +10,6 @@ pub async fn main() -> MyResult<()> {
.filter_module("fediwiki", LevelFilter::Info) .filter_module("fediwiki", LevelFilter::Info)
.init(); .init();
let database_url = "postgres://fediwiki:password@localhost:5432/fediwiki"; let database_url = "postgres://fediwiki:password@localhost:5432/fediwiki";
start("localhost:8131", &database_url).await?; start("localhost:8131", database_url).await?;
Ok(()) Ok(())
} }

View file

@ -4,6 +4,7 @@ use crate::error::MyResult;
use anyhow::anyhow; use anyhow::anyhow;
use diffy::{apply, Patch}; use diffy::{apply, Patch};
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use url::{ParseError, Url}; use url::{ParseError, Url};
pub fn generate_activity_id(domain: &Url) -> Result<Url, ParseError> { pub fn generate_activity_id(domain: &Url) -> Result<Url, ParseError> {
@ -24,7 +25,11 @@ pub fn generate_activity_id(domain: &Url) -> Result<Url, ParseError> {
/// TODO: should cache all these generated versions /// TODO: should cache all these generated versions
pub fn generate_article_version(edits: &Vec<DbEdit>, version: &EditVersion) -> MyResult<String> { pub fn generate_article_version(edits: &Vec<DbEdit>, version: &EditVersion) -> MyResult<String> {
let mut generated = String::new(); let mut generated = String::new();
if version == &EditVersion::default() {
return Ok(generated);
}
for e in edits { for e in edits {
dbg!(&e);
let patch = Patch::from_str(&e.diff)?; let patch = Patch::from_str(&e.diff)?;
generated = apply(&generated, &patch)?; generated = apply(&generated, &patch)?;
if &e.version == version { if &e.version == version {

View file

@ -1,7 +1,7 @@
use fediwiki::api::{ use fediwiki::api::{
ApiConflict, CreateArticleData, EditArticleData, FollowInstance, GetArticleData, ResolveObject, ApiConflict, CreateArticleData, EditArticleData, FollowInstance, GetArticleData, ResolveObject,
}; };
use fediwiki::database::article::{ArticleView, DbArticle}; use fediwiki::database::article::ArticleView;
use fediwiki::error::MyResult; use fediwiki::error::MyResult;
use fediwiki::federation::objects::instance::DbInstance; use fediwiki::federation::objects::instance::DbInstance;
use fediwiki::start; use fediwiki::start;
@ -96,10 +96,10 @@ pub async fn create_article(hostname: &str, title: String) -> MyResult<ArticleVi
let create_form = CreateArticleData { let create_form = CreateArticleData {
title: title.clone(), title: title.clone(),
}; };
let article: DbArticle = post(hostname, "article", &create_form).await?; let article: ArticleView = post(hostname, "article", &create_form).await?;
// create initial edit to ensure that conflicts are generated (there are no conflicts on empty file) // create initial edit to ensure that conflicts are generated (there are no conflicts on empty file)
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: article.id, article_id: article.article.id,
new_text: TEST_ARTICLE_DEFAULT_TEXT.to_string(), new_text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
previous_version: article.latest_version, previous_version: article.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,

View file

@ -12,8 +12,9 @@ use fediwiki::api::{
}; };
use fediwiki::database::article::{ArticleView, DbArticle}; use fediwiki::database::article::{ArticleView, DbArticle};
use fediwiki::error::MyResult; use fediwiki::error::MyResult;
use fediwiki::federation::objects::edit::ApubEdit;
use fediwiki::federation::objects::instance::DbInstance; use fediwiki::federation::objects::instance::DbInstance;
use pretty_assertions::{assert_eq, assert_ne};
use serial_test::serial; use serial_test::serial;
use url::Url; use url::Url;
@ -42,7 +43,7 @@ async fn test_create_read_and_edit_article() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum 2".to_string(), new_text: "Lorem Ipsum 2".to_string(),
previous_version: get_res.article.latest_version, previous_version: get_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -116,7 +117,7 @@ async fn test_synchronize_articles() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum 2\n".to_string(), new_text: "Lorem Ipsum 2\n".to_string(),
previous_version: create_res.article.latest_version, previous_version: create_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
edit_article(&data.alpha.hostname, &edit_form).await?; edit_article(&data.alpha.hostname, &edit_form).await?;
@ -171,7 +172,7 @@ async fn test_edit_local_article() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum 2".to_string(), new_text: "Lorem Ipsum 2".to_string(),
previous_version: get_res.article.latest_version, previous_version: get_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.beta.hostname, &edit_form).await?; let edit_res = edit_article(&data.beta.hostname, &edit_form).await?;
@ -218,7 +219,7 @@ async fn test_edit_remote_article() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum 2".to_string(), new_text: "Lorem Ipsum 2".to_string(),
previous_version: get_res.article.latest_version, previous_version: get_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -259,7 +260,7 @@ async fn test_local_edit_conflict() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum\n".to_string(), new_text: "Lorem Ipsum\n".to_string(),
previous_version: create_res.article.latest_version.clone(), previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -270,7 +271,7 @@ async fn test_local_edit_conflict() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Ipsum Lorem\n".to_string(), new_text: "Ipsum Lorem\n".to_string(),
previous_version: create_res.article.latest_version, previous_version: create_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article_with_conflict(&data.alpha.hostname, &edit_form) let edit_res = edit_article_with_conflict(&data.alpha.hostname, &edit_form)
@ -316,19 +317,19 @@ async fn test_federated_edit_conflict() -> MyResult<()> {
let resolve_object = ResolveObject { let resolve_object = ResolveObject {
id: create_res.article.ap_id.inner().clone(), id: create_res.article.ap_id.inner().clone(),
}; };
let resolve_res: DbArticle = get_query( let resolve_res: ArticleView = get_query(
&data.gamma.hostname, &data.gamma.hostname,
"resolve_article", "resolve_article",
Some(resolve_object), Some(resolve_object),
) )
.await?; .await?;
assert_eq!(create_res.article.text, resolve_res.text); assert_eq!(create_res.article.text, resolve_res.article.text);
// alpha edits article // alpha edits article
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "Lorem Ipsum\n".to_string(), new_text: "Lorem Ipsum\n".to_string(),
previous_version: create_res.article.latest_version.clone(), previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -342,17 +343,20 @@ async fn test_federated_edit_conflict() -> MyResult<()> {
// gamma also edits, as its not the latest version there is a conflict. local version should // gamma also edits, as its not the latest version there is a conflict. local version should
// not be updated with this conflicting version, instead user needs to handle the conflict // not be updated with this conflicting version, instead user needs to handle the conflict
dbg!(&create_res.article.text, &create_res.latest_version);
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "aaaa\n".to_string(), new_text: "aaaa\n".to_string(),
previous_version: create_res.article.latest_version, previous_version: create_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
dbg!(1);
let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?; let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?;
assert_ne!(edit_form.new_text, edit_res.article.text); assert_ne!(edit_form.new_text, edit_res.article.text);
assert_eq!(2, edit_res.edits.len()); assert_eq!(2, edit_res.edits.len());
assert!(!edit_res.article.local); assert!(!edit_res.article.local);
dbg!(2);
let conflicts: Vec<ApiConflict> = let conflicts: Vec<ApiConflict> =
get_query(&data.gamma.hostname, "edit_conflicts", None::<()>).await?; get_query(&data.gamma.hostname, "edit_conflicts", None::<()>).await?;
assert_eq!(1, conflicts.len()); assert_eq!(1, conflicts.len());
@ -364,13 +368,16 @@ async fn test_federated_edit_conflict() -> MyResult<()> {
previous_version: conflicts[0].previous_version.clone(), previous_version: conflicts[0].previous_version.clone(),
resolve_conflict_id: Some(conflicts[0].id), resolve_conflict_id: Some(conflicts[0].id),
}; };
dbg!(3);
let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?; let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?;
assert_eq!(edit_form.new_text, edit_res.article.text); assert_eq!(edit_form.new_text, edit_res.article.text);
assert_eq!(3, edit_res.edits.len()); assert_eq!(3, edit_res.edits.len());
let conflicts: Vec<ApubEdit> = dbg!(4);
let conflicts: Vec<ApiConflict> =
get_query(&data.gamma.hostname, "edit_conflicts", None::<()>).await?; get_query(&data.gamma.hostname, "edit_conflicts", None::<()>).await?;
assert_eq!(0, conflicts.len()); assert_eq!(0, conflicts.len());
dbg!(5);
data.stop() data.stop()
} }
@ -390,7 +397,7 @@ async fn test_overlapping_edits_no_conflict() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "my\nexample\ntext\n".to_string(), new_text: "my\nexample\ntext\n".to_string(),
previous_version: create_res.article.latest_version.clone(), previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -401,7 +408,7 @@ async fn test_overlapping_edits_no_conflict() -> MyResult<()> {
let edit_form = EditArticleData { let edit_form = EditArticleData {
article_id: create_res.article.id, article_id: create_res.article.id,
new_text: "some\nexample\narticle\n".to_string(), new_text: "some\nexample\narticle\n".to_string(),
previous_version: create_res.article.latest_version, previous_version: create_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?; let edit_res = edit_article(&data.alpha.hostname, &edit_form).await?;
@ -442,11 +449,12 @@ async fn test_fork_article() -> MyResult<()> {
let forked_article = fork_res.article; let forked_article = fork_res.article;
assert_eq!(resolved_article.title, forked_article.title); assert_eq!(resolved_article.title, forked_article.title);
assert_eq!(resolved_article.text, forked_article.text); assert_eq!(resolved_article.text, forked_article.text);
assert_eq!(resolve_res.edits, fork_res.edits); assert_eq!(resolve_res.edits.len(), fork_res.edits.len());
assert_eq!( assert_eq!(resolve_res.edits[0].diff, fork_res.edits[0].diff);
resolved_article.latest_version, assert_eq!(resolve_res.edits[0].version, fork_res.edits[0].version);
forked_article.latest_version assert_ne!(resolve_res.edits[0].id, fork_res.edits[0].id);
); assert!(fork_res.edits[0].local);
assert_eq!(resolve_res.latest_version, fork_res.latest_version);
assert_ne!(resolved_article.ap_id, forked_article.ap_id); assert_ne!(resolved_article.ap_id, forked_article.ap_id);
assert!(forked_article.local); assert!(forked_article.local);