mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 07:41:08 +00:00
all tests passing!
This commit is contained in:
parent
7c789a1c06
commit
d4772d35c2
18 changed files with 207 additions and 92 deletions
23
Cargo.lock
generated
23
Cargo.lock
generated
|
@ -549,6 +549,12 @@ dependencies = [
|
|||
"syn 2.0.39",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "diff"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||
|
||||
[[package]]
|
||||
name = "diffy"
|
||||
version = "0.3.0"
|
||||
|
@ -686,6 +692,7 @@ dependencies = [
|
|||
"env_logger",
|
||||
"futures",
|
||||
"once_cell",
|
||||
"pretty_assertions",
|
||||
"rand",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
@ -1504,6 +1511,16 @@ dependencies = [
|
|||
"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]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.69"
|
||||
|
@ -2561,3 +2578,9 @@ dependencies = [
|
|||
"cfg-if",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yansi"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
||||
|
|
|
@ -27,5 +27,6 @@ url = "2.4.1"
|
|||
|
||||
[dev-dependencies]
|
||||
once_cell = "1.18.0"
|
||||
pretty_assertions = "1.4.0"
|
||||
reqwest = "0.11.22"
|
||||
serial_test = "2.0.0"
|
||||
|
|
|
@ -4,7 +4,6 @@ create table article (
|
|||
text text not null,
|
||||
ap_id varchar(255) not null unique,
|
||||
instance_id varchar(255) not null,
|
||||
latest_version text not null,
|
||||
local bool not null
|
||||
);
|
||||
|
||||
|
@ -14,5 +13,6 @@ create table edit (
|
|||
diff text not null,
|
||||
article_id int REFERENCES article ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
|
||||
version text not null,
|
||||
previous_version text not null,
|
||||
local bool not null
|
||||
)
|
61
src/api.rs
61
src/api.rs
|
@ -44,13 +44,11 @@ pub struct CreateArticleData {
|
|||
async fn create_article(
|
||||
data: Data<MyDataHandle>,
|
||||
Form(create_article): Form<CreateArticleData>,
|
||||
) -> MyResult<Json<DbArticle>> {
|
||||
dbg!(1);
|
||||
) -> MyResult<Json<ArticleView>> {
|
||||
let existing_article = DbArticle::read_local_title(&create_article.title, &data.db_connection);
|
||||
if existing_article.is_ok() {
|
||||
return Err(anyhow!("A local article with this title already exists").into());
|
||||
}
|
||||
dbg!(2);
|
||||
|
||||
let instance_id = data.local_instance().ap_id;
|
||||
let ap_id = ObjectId::parse(&format!(
|
||||
|
@ -58,24 +56,19 @@ async fn create_article(
|
|||
instance_id.inner().domain().unwrap(),
|
||||
instance_id.inner().port().unwrap(),
|
||||
create_article.title
|
||||
))?
|
||||
.into();
|
||||
))?;
|
||||
let form = DbArticleForm {
|
||||
title: create_article.title,
|
||||
text: String::new(),
|
||||
ap_id,
|
||||
latest_version: Default::default(),
|
||||
instance_id,
|
||||
local: true,
|
||||
};
|
||||
dbg!(3);
|
||||
let article = dbg!(DbArticle::create(&form, &data.db_connection))?;
|
||||
let article = DbArticle::create(&form, &data.db_connection)?;
|
||||
|
||||
dbg!(4);
|
||||
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)]
|
||||
|
@ -122,30 +115,37 @@ async fn edit_article(
|
|||
}
|
||||
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 {
|
||||
// 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))
|
||||
} else {
|
||||
// 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.
|
||||
let edits = DbEdit::for_article(&original_article, &data.db_connection)?;
|
||||
let ancestor = generate_article_version(&edits, &edit_form.previous_version)?;
|
||||
let ancestor =
|
||||
generate_article_version(&original_article.edits, &edit_form.previous_version)?;
|
||||
let patch = create_patch(&ancestor, &edit_form.new_text);
|
||||
dbg!(&edit_form.previous_version);
|
||||
|
||||
let db_conflict = DbConflict {
|
||||
id: random(),
|
||||
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,
|
||||
};
|
||||
{
|
||||
let mut lock = data.conflicts.lock().unwrap();
|
||||
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>> {
|
||||
let article: DbArticle = ObjectId::from(query.id).dereference(&data).await?;
|
||||
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.
|
||||
|
@ -220,16 +225,21 @@ async fn follow_instance(
|
|||
/// Get a list of all unresolved edit conflicts.
|
||||
#[debug_handler]
|
||||
async fn edit_conflicts(data: Data<MyDataHandle>) -> MyResult<Json<Vec<ApiConflict>>> {
|
||||
dbg!("a");
|
||||
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 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?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect();
|
||||
Ok(Json(conflicts))
|
||||
dbg!("c");
|
||||
Ok(Json(dbg!(conflicts)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
|
@ -276,19 +286,22 @@ async fn fork_article(
|
|||
instance_id.inner().domain().unwrap(),
|
||||
instance_id.inner().port().unwrap(),
|
||||
original_article.title
|
||||
))?
|
||||
.into();
|
||||
))?;
|
||||
let form = DbArticleForm {
|
||||
title: original_article.title.clone(),
|
||||
text: original_article.text.clone(),
|
||||
ap_id,
|
||||
latest_version: original_article.latest_version.0.clone(),
|
||||
instance_id,
|
||||
local: true,
|
||||
};
|
||||
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?;
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ use diesel::{
|
|||
PgTextExpressionMethods, QueryDsl, Queryable, RunQueryDsl, Selectable,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use std::ops::DerefMut;
|
||||
use std::sync::Mutex;
|
||||
|
||||
|
@ -24,8 +25,6 @@ pub struct DbArticle {
|
|||
pub text: String,
|
||||
pub ap_id: ObjectId<DbArticle>,
|
||||
pub instance_id: ObjectId<DbInstance>,
|
||||
// TODO: should read this from edits table instead of separate db field
|
||||
pub latest_version: EditVersion,
|
||||
pub local: bool,
|
||||
}
|
||||
|
||||
|
@ -33,6 +32,7 @@ pub struct DbArticle {
|
|||
#[diesel(table_name = article, check_for_backend(diesel::pg::Pg))]
|
||||
pub struct ArticleView {
|
||||
pub article: DbArticle,
|
||||
pub latest_version: EditVersion,
|
||||
pub edits: Vec<DbEdit>,
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,6 @@ pub struct DbArticleForm {
|
|||
pub ap_id: ObjectId<DbArticle>,
|
||||
// TODO: change to foreign key
|
||||
pub instance_id: ObjectId<DbInstance>,
|
||||
// TODO: instead of this we can use latest entry in edits table
|
||||
pub latest_version: String,
|
||||
pub local: bool,
|
||||
}
|
||||
|
||||
|
@ -77,10 +75,18 @@ impl DbArticle {
|
|||
}
|
||||
|
||||
pub fn read_view(id: i32, conn: &Mutex<PgConnection>) -> MyResult<ArticleView> {
|
||||
let article: DbArticle = {
|
||||
let mut conn = conn.lock().unwrap();
|
||||
article::table.find(id).get_result(conn.deref_mut())?
|
||||
};
|
||||
let latest_version = article.latest_edit_version(conn)?;
|
||||
let mut conn = conn.lock().unwrap();
|
||||
let article: DbArticle = 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 edits: Vec<DbEdit> = DbEdit::belonging_to(&article).get_results(conn.deref_mut())?;
|
||||
Ok(ArticleView {
|
||||
article,
|
||||
edits,
|
||||
latest_version,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_from_ap_id(
|
||||
|
@ -122,4 +128,14 @@ impl DbArticle {
|
|||
)
|
||||
.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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize};
|
|||
use sha2::{Digest, Sha224};
|
||||
use std::ops::DerefMut;
|
||||
use std::sync::Mutex;
|
||||
use url::Url;
|
||||
|
||||
/// Represents a single change to the article.
|
||||
#[derive(
|
||||
|
@ -34,6 +33,8 @@ pub struct DbEdit {
|
|||
pub diff: String,
|
||||
pub article_id: i32,
|
||||
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?
|
||||
pub local: bool,
|
||||
}
|
||||
|
@ -45,24 +46,40 @@ pub struct DbEditForm {
|
|||
pub diff: String,
|
||||
pub article_id: i32,
|
||||
pub version: EditVersion,
|
||||
pub previous_version: EditVersion,
|
||||
pub local: bool,
|
||||
}
|
||||
|
||||
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 mut sha224 = Sha224::new();
|
||||
sha224.update(diff.to_bytes());
|
||||
let hash = format!("{:X}", sha224.finalize());
|
||||
let edit_id = Url::parse(&format!("{}/{}", original_article.ap_id, hash))?;
|
||||
let (ap_id, hash) = Self::generate_ap_id_and_hash(original_article, diff.to_bytes())?;
|
||||
Ok(DbEditForm {
|
||||
ap_id: edit_id.into(),
|
||||
ap_id,
|
||||
diff: diff.to_string(),
|
||||
article_id: original_article.id,
|
||||
version: EditVersion(hash),
|
||||
previous_version,
|
||||
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 {
|
||||
|
@ -80,6 +97,18 @@ impl DbEdit {
|
|||
let mut conn = conn.lock().unwrap();
|
||||
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)]
|
||||
|
|
|
@ -60,20 +60,28 @@ impl DbConflict {
|
|||
data: &Data<MyDataHandle>,
|
||||
) -> MyResult<Option<ApiConflict>> {
|
||||
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
|
||||
let edits = DbEdit::for_article(&original_article, &data.db_connection)?;
|
||||
let ancestor = generate_article_version(&edits, &self.previous_version)?;
|
||||
dbg!(&ancestor, &self.previous_version);
|
||||
|
||||
dbg!(&self.diff);
|
||||
let patch = Patch::from_str(&self.diff)?;
|
||||
// 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) {
|
||||
Ok(new_text) => {
|
||||
// patch applies cleanly so we are done
|
||||
// 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
|
||||
let mut lock = data.conflicts.lock().unwrap();
|
||||
lock.retain(|c| c.id != self.id);
|
||||
|
@ -84,8 +92,8 @@ impl DbConflict {
|
|||
Ok(Some(ApiConflict {
|
||||
id: self.id,
|
||||
three_way_merge,
|
||||
article_id: original_article.ap_id.into(),
|
||||
previous_version: original_article.latest_version,
|
||||
article_id: original_article.ap_id.clone(),
|
||||
previous_version: original_article.latest_edit_version(&data.db_connection)?,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ diesel::table! {
|
|||
ap_id -> Varchar,
|
||||
#[max_length = 255]
|
||||
instance_id -> Varchar,
|
||||
latest_version -> Text,
|
||||
local -> Bool,
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +21,7 @@ diesel::table! {
|
|||
diff -> Text,
|
||||
article_id -> Int4,
|
||||
version -> Text,
|
||||
previous_version -> Text,
|
||||
local -> Bool,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use crate::database::article::DbArticle;
|
||||
use crate::database::edit::{DbEdit, DbEditForm};
|
||||
use crate::database::edit::{DbEdit, DbEditForm, EditVersion};
|
||||
use crate::database::MyDataHandle;
|
||||
use crate::error::Error;
|
||||
use crate::federation::activities::update_local_article::UpdateLocalArticle;
|
||||
use crate::federation::activities::update_remote_article::UpdateRemoteArticle;
|
||||
use crate::federation::objects::instance::DbInstance;
|
||||
use activitypub_federation::config::Data;
|
||||
use activitypub_federation::fetch::object_id::ObjectId;
|
||||
|
||||
pub mod accept;
|
||||
pub mod create_article;
|
||||
|
@ -18,17 +17,30 @@ pub mod update_remote_article;
|
|||
pub async fn submit_article_update(
|
||||
data: &Data<MyDataHandle>,
|
||||
new_text: String,
|
||||
previous_version: EditVersion,
|
||||
original_article: &DbArticle,
|
||||
) -> Result<(), Error> {
|
||||
let form = DbEditForm::new(original_article, &new_text)?;
|
||||
let edit = DbEdit::create(&form, &data.db_connection)?;
|
||||
let form = DbEditForm::new(original_article, &new_text, previous_version)?;
|
||||
if original_article.local {
|
||||
let edit = DbEdit::create(&form, &data.db_connection)?;
|
||||
let updated_article =
|
||||
DbArticle::update_text(edit.article_id, &new_text, &data.db_connection)?;
|
||||
|
||||
UpdateLocalArticle::send(updated_article, vec![], data).await?;
|
||||
} 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)
|
||||
.await?;
|
||||
UpdateRemoteArticle::send(edit, instance, data).await?;
|
||||
|
|
|
@ -66,6 +66,7 @@ impl ActivityHandler for RejectEdit {
|
|||
}
|
||||
|
||||
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
|
||||
let mut lock = data.conflicts.lock().unwrap();
|
||||
let conflict = DbConflict {
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
use crate::database::MyDataHandle;
|
||||
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::instance::DbInstance;
|
||||
use crate::utils::generate_activity_id;
|
||||
|
@ -12,11 +16,6 @@ use activitypub_federation::{
|
|||
traits::{ActivityHandler, Object},
|
||||
};
|
||||
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 url::Url;
|
||||
|
||||
|
@ -48,6 +47,9 @@ impl UpdateRemoteArticle {
|
|||
kind: Default::default(),
|
||||
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
|
||||
.send(update, vec![article_instance.inbox], data)
|
||||
.await?;
|
||||
|
|
|
@ -45,16 +45,14 @@ impl Object for DbArticle {
|
|||
}
|
||||
|
||||
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
|
||||
let instance: DbInstance = ObjectId::from(self.instance_id.clone())
|
||||
.dereference_local(data)
|
||||
.await?;
|
||||
let instance: DbInstance = self.instance_id.clone().dereference_local(data).await?;
|
||||
Ok(ApubArticle {
|
||||
kind: Default::default(),
|
||||
id: self.ap_id.clone().into(),
|
||||
attributed_to: instance.ap_id.clone().into(),
|
||||
id: self.ap_id.clone(),
|
||||
attributed_to: instance.ap_id.clone(),
|
||||
to: vec![public(), instance.followers_url()?],
|
||||
edits: self.edits_id()?,
|
||||
latest_version: self.latest_version,
|
||||
latest_version: self.latest_edit_version(&data.db_connection)?,
|
||||
content: self.text,
|
||||
name: self.title,
|
||||
})
|
||||
|
@ -73,10 +71,9 @@ impl Object for DbArticle {
|
|||
let form = DbArticleForm {
|
||||
title: json.name,
|
||||
text: json.content,
|
||||
ap_id: json.id.into(),
|
||||
latest_version: json.latest_version.0,
|
||||
ap_id: json.id,
|
||||
local: false,
|
||||
instance_id: json.attributed_to.into(),
|
||||
instance_id: json.attributed_to,
|
||||
};
|
||||
let article = DbArticle::create(&form, &data.db_connection)?;
|
||||
|
||||
|
|
|
@ -42,12 +42,12 @@ impl Object for DbEdit {
|
|||
let article = DbArticle::read(self.article_id, &data.db_connection)?;
|
||||
Ok(ApubEdit {
|
||||
kind: EditType::Edit,
|
||||
id: self.ap_id.into(),
|
||||
id: self.ap_id,
|
||||
content: self.diff,
|
||||
version: self.version,
|
||||
// TODO: this is wrong
|
||||
previous_version: article.latest_version,
|
||||
object: article.ap_id.into(),
|
||||
previous_version: self.previous_version,
|
||||
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> {
|
||||
let article = json.object.dereference(data).await?;
|
||||
let form = DbEditForm {
|
||||
ap_id: json.id.into(),
|
||||
ap_id: json.id,
|
||||
diff: json.content,
|
||||
article_id: article.id,
|
||||
version: json.version,
|
||||
previous_version: json.previous_version,
|
||||
local: false,
|
||||
};
|
||||
let edit = DbEdit::create(&form, &data.db_connection)?;
|
||||
|
|
|
@ -26,7 +26,6 @@ const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
|||
pub async fn start(hostname: &str, database_url: &str) -> MyResult<()> {
|
||||
let fake_db = create_fake_db(hostname).await?;
|
||||
|
||||
dbg!(database_url);
|
||||
let db_connection = Arc::new(Mutex::new(PgConnection::establish(database_url)?));
|
||||
db_connection
|
||||
.lock()
|
||||
|
|
|
@ -10,6 +10,6 @@ pub async fn main() -> MyResult<()> {
|
|||
.filter_module("fediwiki", LevelFilter::Info)
|
||||
.init();
|
||||
let database_url = "postgres://fediwiki:password@localhost:5432/fediwiki";
|
||||
start("localhost:8131", &database_url).await?;
|
||||
start("localhost:8131", database_url).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::error::MyResult;
|
|||
use anyhow::anyhow;
|
||||
use diffy::{apply, Patch};
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
|
||||
use url::{ParseError, Url};
|
||||
|
||||
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
|
||||
pub fn generate_article_version(edits: &Vec<DbEdit>, version: &EditVersion) -> MyResult<String> {
|
||||
let mut generated = String::new();
|
||||
if version == &EditVersion::default() {
|
||||
return Ok(generated);
|
||||
}
|
||||
for e in edits {
|
||||
dbg!(&e);
|
||||
let patch = Patch::from_str(&e.diff)?;
|
||||
generated = apply(&generated, &patch)?;
|
||||
if &e.version == version {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use fediwiki::api::{
|
||||
ApiConflict, CreateArticleData, EditArticleData, FollowInstance, GetArticleData, ResolveObject,
|
||||
};
|
||||
use fediwiki::database::article::{ArticleView, DbArticle};
|
||||
use fediwiki::database::article::ArticleView;
|
||||
use fediwiki::error::MyResult;
|
||||
use fediwiki::federation::objects::instance::DbInstance;
|
||||
use fediwiki::start;
|
||||
|
@ -96,10 +96,10 @@ pub async fn create_article(hostname: &str, title: String) -> MyResult<ArticleVi
|
|||
let create_form = CreateArticleData {
|
||||
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)
|
||||
let edit_form = EditArticleData {
|
||||
article_id: article.id,
|
||||
article_id: article.article.id,
|
||||
new_text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
|
||||
previous_version: article.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
|
|
|
@ -12,8 +12,9 @@ use fediwiki::api::{
|
|||
};
|
||||
use fediwiki::database::article::{ArticleView, DbArticle};
|
||||
use fediwiki::error::MyResult;
|
||||
use fediwiki::federation::objects::edit::ApubEdit;
|
||||
|
||||
use fediwiki::federation::objects::instance::DbInstance;
|
||||
use pretty_assertions::{assert_eq, assert_ne};
|
||||
use serial_test::serial;
|
||||
use url::Url;
|
||||
|
||||
|
@ -42,7 +43,7 @@ async fn test_create_read_and_edit_article() -> MyResult<()> {
|
|||
let edit_form = EditArticleData {
|
||||
article_id: create_res.article.id,
|
||||
new_text: "Lorem Ipsum 2".to_string(),
|
||||
previous_version: get_res.article.latest_version,
|
||||
previous_version: get_res.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
};
|
||||
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 {
|
||||
article_id: create_res.article.id,
|
||||
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,
|
||||
};
|
||||
edit_article(&data.alpha.hostname, &edit_form).await?;
|
||||
|
@ -171,7 +172,7 @@ async fn test_edit_local_article() -> MyResult<()> {
|
|||
let edit_form = EditArticleData {
|
||||
article_id: create_res.article.id,
|
||||
new_text: "Lorem Ipsum 2".to_string(),
|
||||
previous_version: get_res.article.latest_version,
|
||||
previous_version: get_res.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
};
|
||||
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 {
|
||||
article_id: create_res.article.id,
|
||||
new_text: "Lorem Ipsum 2".to_string(),
|
||||
previous_version: get_res.article.latest_version,
|
||||
previous_version: get_res.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
};
|
||||
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 {
|
||||
article_id: create_res.article.id,
|
||||
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,
|
||||
};
|
||||
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 {
|
||||
article_id: create_res.article.id,
|
||||
new_text: "Ipsum Lorem\n".to_string(),
|
||||
previous_version: create_res.article.latest_version,
|
||||
previous_version: create_res.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
};
|
||||
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 {
|
||||
id: create_res.article.ap_id.inner().clone(),
|
||||
};
|
||||
let resolve_res: DbArticle = get_query(
|
||||
let resolve_res: ArticleView = get_query(
|
||||
&data.gamma.hostname,
|
||||
"resolve_article",
|
||||
Some(resolve_object),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(create_res.article.text, resolve_res.text);
|
||||
assert_eq!(create_res.article.text, resolve_res.article.text);
|
||||
|
||||
// alpha edits article
|
||||
let edit_form = EditArticleData {
|
||||
article_id: create_res.article.id,
|
||||
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,
|
||||
};
|
||||
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
|
||||
// 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 {
|
||||
article_id: create_res.article.id,
|
||||
new_text: "aaaa\n".to_string(),
|
||||
previous_version: create_res.article.latest_version,
|
||||
previous_version: create_res.latest_version,
|
||||
resolve_conflict_id: None,
|
||||
};
|
||||
dbg!(1);
|
||||
let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?;
|
||||
assert_ne!(edit_form.new_text, edit_res.article.text);
|
||||
assert_eq!(2, edit_res.edits.len());
|
||||
assert!(!edit_res.article.local);
|
||||
|
||||
dbg!(2);
|
||||
let conflicts: Vec<ApiConflict> =
|
||||
get_query(&data.gamma.hostname, "edit_conflicts", None::<()>).await?;
|
||||
assert_eq!(1, conflicts.len());
|
||||
|
@ -364,13 +368,16 @@ async fn test_federated_edit_conflict() -> MyResult<()> {
|
|||
previous_version: conflicts[0].previous_version.clone(),
|
||||
resolve_conflict_id: Some(conflicts[0].id),
|
||||
};
|
||||
dbg!(3);
|
||||
let edit_res = edit_article(&data.gamma.hostname, &edit_form).await?;
|
||||
assert_eq!(edit_form.new_text, edit_res.article.text);
|
||||
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?;
|
||||
assert_eq!(0, conflicts.len());
|
||||
dbg!(5);
|
||||
|
||||
data.stop()
|
||||
}
|
||||
|
@ -390,7 +397,7 @@ async fn test_overlapping_edits_no_conflict() -> MyResult<()> {
|
|||
let edit_form = EditArticleData {
|
||||
article_id: create_res.article.id,
|
||||
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,
|
||||
};
|
||||
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 {
|
||||
article_id: create_res.article.id,
|
||||
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,
|
||||
};
|
||||
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;
|
||||
assert_eq!(resolved_article.title, forked_article.title);
|
||||
assert_eq!(resolved_article.text, forked_article.text);
|
||||
assert_eq!(resolve_res.edits, fork_res.edits);
|
||||
assert_eq!(
|
||||
resolved_article.latest_version,
|
||||
forked_article.latest_version
|
||||
);
|
||||
assert_eq!(resolve_res.edits.len(), fork_res.edits.len());
|
||||
assert_eq!(resolve_res.edits[0].diff, fork_res.edits[0].diff);
|
||||
assert_eq!(resolve_res.edits[0].version, fork_res.edits[0].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!(forked_article.local);
|
||||
|
||||
|
|
Loading…
Reference in a new issue