mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 17:41:09 +00:00
store edits inside edit.rs, avoid code duplication
This commit is contained in:
parent
920ec258bc
commit
abb5ee0ce4
5 changed files with 36 additions and 45 deletions
12
src/api.rs
12
src/api.rs
|
@ -1,15 +1,15 @@
|
||||||
use crate::database::DatabaseHandle;
|
use crate::database::DatabaseHandle;
|
||||||
use crate::error::{Error, MyResult};
|
use crate::error::{MyResult};
|
||||||
use crate::federation::activities::create_article::CreateArticle;
|
use crate::federation::activities::create_article::CreateArticle;
|
||||||
use crate::federation::activities::update_article::UpdateArticle;
|
use crate::federation::activities::update_article::UpdateArticle;
|
||||||
use crate::federation::objects::article::{ApubArticle, DbArticle};
|
use crate::federation::objects::article::{DbArticle};
|
||||||
use crate::federation::objects::edit::DbEdit;
|
use crate::federation::objects::edit::DbEdit;
|
||||||
use crate::federation::objects::instance::{ApubInstance, DbInstance};
|
use crate::federation::objects::instance::{DbInstance};
|
||||||
use activitypub_federation::config::Data;
|
use activitypub_federation::config::Data;
|
||||||
use activitypub_federation::fetch::object_id::ObjectId;
|
use activitypub_federation::fetch::object_id::ObjectId;
|
||||||
use activitypub_federation::traits::Object;
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use async_trait::async_trait;
|
|
||||||
use axum::extract::Query;
|
use axum::extract::Query;
|
||||||
use axum::routing::{get, post};
|
use axum::routing::{get, post};
|
||||||
use axum::{Form, Json, Router};
|
use axum::{Form, Json, Router};
|
||||||
|
@ -65,7 +65,7 @@ async fn create_article(
|
||||||
Ok(Json(article))
|
Ok(Json(article))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct EditArticleData {
|
pub struct EditArticleData {
|
||||||
pub ap_id: ObjectId<DbArticle>,
|
pub ap_id: ObjectId<DbArticle>,
|
||||||
pub new_text: String,
|
pub new_text: String,
|
||||||
|
|
|
@ -11,7 +11,7 @@ use activitypub_federation::{
|
||||||
protocol::helpers::deserialize_one_or_many,
|
protocol::helpers::deserialize_one_or_many,
|
||||||
traits::{ActivityHandler, Object},
|
traits::{ActivityHandler, Object},
|
||||||
};
|
};
|
||||||
use diffy::{apply, Patch};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -21,8 +21,7 @@ pub struct UpdateArticle {
|
||||||
pub actor: ObjectId<DbInstance>,
|
pub actor: ObjectId<DbInstance>,
|
||||||
#[serde(deserialize_with = "deserialize_one_or_many")]
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
||||||
pub to: Vec<Url>,
|
pub to: Vec<Url>,
|
||||||
pub object: ObjectId<DbArticle>,
|
pub object: ApubEdit,
|
||||||
pub result: ApubEdit,
|
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub kind: CreateType,
|
pub kind: CreateType,
|
||||||
pub id: Url,
|
pub id: Url,
|
||||||
|
@ -40,8 +39,7 @@ impl UpdateArticle {
|
||||||
let update = UpdateArticle {
|
let update = UpdateArticle {
|
||||||
actor: local_instance.ap_id.clone(),
|
actor: local_instance.ap_id.clone(),
|
||||||
to: local_instance.follower_ids(),
|
to: local_instance.follower_ids(),
|
||||||
object: article.ap_id,
|
object: edit.into_json(data).await?,
|
||||||
result: edit.into_json(data).await?,
|
|
||||||
kind: Default::default(),
|
kind: Default::default(),
|
||||||
id,
|
id,
|
||||||
};
|
};
|
||||||
|
@ -51,8 +49,7 @@ impl UpdateArticle {
|
||||||
let update = UpdateArticle {
|
let update = UpdateArticle {
|
||||||
actor: local_instance.ap_id.clone(),
|
actor: local_instance.ap_id.clone(),
|
||||||
to: vec![article_instance.ap_id.into_inner()],
|
to: vec![article_instance.ap_id.into_inner()],
|
||||||
object: article.ap_id,
|
object: edit.into_json(data).await?,
|
||||||
result: edit.into_json(data).await?,
|
|
||||||
kind: Default::default(),
|
kind: Default::default(),
|
||||||
id,
|
id,
|
||||||
};
|
};
|
||||||
|
@ -82,13 +79,9 @@ impl ActivityHandler for UpdateArticle {
|
||||||
|
|
||||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||||
let article_local = {
|
let article_local = {
|
||||||
let edit = DbEdit::from_json(self.result.clone(), data).await?;
|
DbEdit::from_json(self.object.clone(), data).await?;
|
||||||
let mut lock = data.articles.lock().unwrap();
|
let lock = data.articles.lock().unwrap();
|
||||||
let article = lock.get_mut(self.object.inner()).unwrap();
|
let article = lock.get(self.object.object.inner()).unwrap();
|
||||||
article.edits.push(edit);
|
|
||||||
// TODO: probably better to apply patch inside DbEdit::from_json()
|
|
||||||
let patch = Patch::from_str(&self.result.diff)?;
|
|
||||||
article.text = apply(&article.text, &patch)?;
|
|
||||||
article.local
|
article.local
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -101,7 +94,6 @@ impl ActivityHandler for UpdateArticle {
|
||||||
actor: local_instance.ap_id.clone(),
|
actor: local_instance.ap_id.clone(),
|
||||||
to: local_instance.follower_ids(),
|
to: local_instance.follower_ids(),
|
||||||
object: self.object,
|
object: self.object,
|
||||||
result: self.result,
|
|
||||||
kind: Default::default(),
|
kind: Default::default(),
|
||||||
id,
|
id,
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::federation::objects::article::DbArticle;
|
||||||
use activitypub_federation::config::Data;
|
use activitypub_federation::config::Data;
|
||||||
use activitypub_federation::fetch::object_id::ObjectId;
|
use activitypub_federation::fetch::object_id::ObjectId;
|
||||||
use activitypub_federation::traits::Object;
|
use activitypub_federation::traits::Object;
|
||||||
use diffy::create_patch;
|
use diffy::{apply, create_patch, Patch};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use sha2::Sha224;
|
use sha2::Sha224;
|
||||||
|
@ -15,6 +15,7 @@ use url::Url;
|
||||||
pub struct DbEdit {
|
pub struct DbEdit {
|
||||||
pub id: ObjectId<DbEdit>,
|
pub id: ObjectId<DbEdit>,
|
||||||
pub diff: String,
|
pub diff: String,
|
||||||
|
pub article_id: ObjectId<DbArticle>,
|
||||||
pub local: bool,
|
pub local: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ impl DbEdit {
|
||||||
Ok(DbEdit {
|
Ok(DbEdit {
|
||||||
id: edit_id,
|
id: edit_id,
|
||||||
diff: diff.to_string(),
|
diff: diff.to_string(),
|
||||||
|
article_id: original_article.ap_id.clone(),
|
||||||
local: true,
|
local: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -44,7 +46,8 @@ pub struct ApubEdit {
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
kind: EditType,
|
kind: EditType,
|
||||||
id: ObjectId<DbEdit>,
|
id: ObjectId<DbEdit>,
|
||||||
pub(crate) diff: String,
|
pub(crate) content: String,
|
||||||
|
pub object: ObjectId<DbArticle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
@ -64,7 +67,8 @@ impl Object for DbEdit {
|
||||||
Ok(ApubEdit {
|
Ok(ApubEdit {
|
||||||
kind: EditType::Edit,
|
kind: EditType::Edit,
|
||||||
id: self.id,
|
id: self.id,
|
||||||
diff: self.diff,
|
content: self.diff,
|
||||||
|
object: self.article_id,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,14 +80,19 @@ impl Object for DbEdit {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn from_json(
|
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
||||||
json: Self::Kind,
|
let edit = Self {
|
||||||
_data: &Data<Self::DataType>,
|
|
||||||
) -> Result<Self, Self::Error> {
|
|
||||||
Ok(Self {
|
|
||||||
id: json.id,
|
id: json.id,
|
||||||
diff: json.diff,
|
diff: json.content,
|
||||||
|
article_id: json.object,
|
||||||
local: false,
|
local: false,
|
||||||
})
|
};
|
||||||
|
let mut lock = data.articles.lock().unwrap();
|
||||||
|
let article = lock.get_mut(edit.article_id.inner()).unwrap();
|
||||||
|
article.edits.push(edit.clone());
|
||||||
|
let patch = Patch::from_str(&edit.diff)?;
|
||||||
|
article.text = apply(&article.text, &patch)?;
|
||||||
|
|
||||||
|
Ok(edit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,23 +68,11 @@ impl Collection for DbEditCollection {
|
||||||
|
|
||||||
async fn from_json(
|
async fn from_json(
|
||||||
apub: Self::Kind,
|
apub: Self::Kind,
|
||||||
owner: &Self::Owner,
|
_owner: &Self::Owner,
|
||||||
data: &Data<Self::DataType>,
|
data: &Data<Self::DataType>,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
let edits =
|
let edits =
|
||||||
try_join_all(apub.items.into_iter().map(|i| DbEdit::from_json(i, data))).await?;
|
try_join_all(apub.items.into_iter().map(|i| DbEdit::from_json(i, data))).await?;
|
||||||
let mut articles = data.articles.lock().unwrap();
|
|
||||||
let article = articles.get_mut(owner.ap_id.inner()).unwrap();
|
|
||||||
let edit_ids = article
|
|
||||||
.edits
|
|
||||||
.iter()
|
|
||||||
.map(|e| e.id.clone())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
for e in edits.clone() {
|
|
||||||
if !edit_ids.contains(&&e.id) {
|
|
||||||
article.edits.push(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: return value propably not needed
|
// TODO: return value propably not needed
|
||||||
Ok(DbEditCollection(edits))
|
Ok(DbEditCollection(edits))
|
||||||
}
|
}
|
||||||
|
|
|
@ -292,7 +292,9 @@ async fn test_edit_conflict() -> MyResult<()> {
|
||||||
ap_id: create_res.ap_id,
|
ap_id: create_res.ap_id,
|
||||||
new_text: "Ipsum Lorem".to_string(),
|
new_text: "Ipsum Lorem".to_string(),
|
||||||
};
|
};
|
||||||
let edit_res: DbArticle = patch(data.hostname_gamma, "article", &edit_form).await?;
|
dbg!(&edit_form);
|
||||||
|
let edit_res: DbArticle = dbg!(patch(data.hostname_gamma, "article", &edit_form).await)?;
|
||||||
|
dbg!(&edit_res);
|
||||||
assert_eq!(edit_res.text, edit_form.new_text);
|
assert_eq!(edit_res.text, edit_form.new_text);
|
||||||
assert_eq!(edit_res.edits.len(), 1);
|
assert_eq!(edit_res.edits.len(), 1);
|
||||||
assert!(!edit_res.local);
|
assert!(!edit_res.local);
|
||||||
|
|
Loading…
Reference in a new issue