mirror of
https://github.com/Nutomic/ibis.git
synced 2024-12-23 23:51:23 +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::error::{Error, MyResult};
|
||||
use crate::error::{MyResult};
|
||||
use crate::federation::activities::create_article::CreateArticle;
|
||||
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::instance::{ApubInstance, DbInstance};
|
||||
use crate::federation::objects::instance::{DbInstance};
|
||||
use activitypub_federation::config::Data;
|
||||
use activitypub_federation::fetch::object_id::ObjectId;
|
||||
use activitypub_federation::traits::Object;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use axum::extract::Query;
|
||||
use axum::routing::{get, post};
|
||||
use axum::{Form, Json, Router};
|
||||
|
@ -65,7 +65,7 @@ async fn create_article(
|
|||
Ok(Json(article))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct EditArticleData {
|
||||
pub ap_id: ObjectId<DbArticle>,
|
||||
pub new_text: String,
|
||||
|
|
|
@ -11,7 +11,7 @@ use activitypub_federation::{
|
|||
protocol::helpers::deserialize_one_or_many,
|
||||
traits::{ActivityHandler, Object},
|
||||
};
|
||||
use diffy::{apply, Patch};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
|
@ -21,8 +21,7 @@ pub struct UpdateArticle {
|
|||
pub actor: ObjectId<DbInstance>,
|
||||
#[serde(deserialize_with = "deserialize_one_or_many")]
|
||||
pub to: Vec<Url>,
|
||||
pub object: ObjectId<DbArticle>,
|
||||
pub result: ApubEdit,
|
||||
pub object: ApubEdit,
|
||||
#[serde(rename = "type")]
|
||||
pub kind: CreateType,
|
||||
pub id: Url,
|
||||
|
@ -40,8 +39,7 @@ impl UpdateArticle {
|
|||
let update = UpdateArticle {
|
||||
actor: local_instance.ap_id.clone(),
|
||||
to: local_instance.follower_ids(),
|
||||
object: article.ap_id,
|
||||
result: edit.into_json(data).await?,
|
||||
object: edit.into_json(data).await?,
|
||||
kind: Default::default(),
|
||||
id,
|
||||
};
|
||||
|
@ -51,8 +49,7 @@ impl UpdateArticle {
|
|||
let update = UpdateArticle {
|
||||
actor: local_instance.ap_id.clone(),
|
||||
to: vec![article_instance.ap_id.into_inner()],
|
||||
object: article.ap_id,
|
||||
result: edit.into_json(data).await?,
|
||||
object: edit.into_json(data).await?,
|
||||
kind: Default::default(),
|
||||
id,
|
||||
};
|
||||
|
@ -82,13 +79,9 @@ impl ActivityHandler for UpdateArticle {
|
|||
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
let article_local = {
|
||||
let edit = DbEdit::from_json(self.result.clone(), data).await?;
|
||||
let mut lock = data.articles.lock().unwrap();
|
||||
let article = lock.get_mut(self.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)?;
|
||||
DbEdit::from_json(self.object.clone(), data).await?;
|
||||
let lock = data.articles.lock().unwrap();
|
||||
let article = lock.get(self.object.object.inner()).unwrap();
|
||||
article.local
|
||||
};
|
||||
|
||||
|
@ -101,7 +94,6 @@ impl ActivityHandler for UpdateArticle {
|
|||
actor: local_instance.ap_id.clone(),
|
||||
to: local_instance.follower_ids(),
|
||||
object: self.object,
|
||||
result: self.result,
|
||||
kind: Default::default(),
|
||||
id,
|
||||
};
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::federation::objects::article::DbArticle;
|
|||
use activitypub_federation::config::Data;
|
||||
use activitypub_federation::fetch::object_id::ObjectId;
|
||||
use activitypub_federation::traits::Object;
|
||||
use diffy::create_patch;
|
||||
use diffy::{apply, create_patch, Patch};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Digest;
|
||||
use sha2::Sha224;
|
||||
|
@ -15,6 +15,7 @@ use url::Url;
|
|||
pub struct DbEdit {
|
||||
pub id: ObjectId<DbEdit>,
|
||||
pub diff: String,
|
||||
pub article_id: ObjectId<DbArticle>,
|
||||
pub local: bool,
|
||||
}
|
||||
|
||||
|
@ -28,6 +29,7 @@ impl DbEdit {
|
|||
Ok(DbEdit {
|
||||
id: edit_id,
|
||||
diff: diff.to_string(),
|
||||
article_id: original_article.ap_id.clone(),
|
||||
local: true,
|
||||
})
|
||||
}
|
||||
|
@ -44,7 +46,8 @@ pub struct ApubEdit {
|
|||
#[serde(rename = "type")]
|
||||
kind: EditType,
|
||||
id: ObjectId<DbEdit>,
|
||||
pub(crate) diff: String,
|
||||
pub(crate) content: String,
|
||||
pub object: ObjectId<DbArticle>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
|
@ -64,7 +67,8 @@ impl Object for DbEdit {
|
|||
Ok(ApubEdit {
|
||||
kind: EditType::Edit,
|
||||
id: self.id,
|
||||
diff: self.diff,
|
||||
content: self.diff,
|
||||
object: self.article_id,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -76,14 +80,19 @@ impl Object for DbEdit {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn from_json(
|
||||
json: Self::Kind,
|
||||
_data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
||||
let edit = Self {
|
||||
id: json.id,
|
||||
diff: json.diff,
|
||||
diff: json.content,
|
||||
article_id: json.object,
|
||||
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(
|
||||
apub: Self::Kind,
|
||||
owner: &Self::Owner,
|
||||
_owner: &Self::Owner,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let edits =
|
||||
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
|
||||
Ok(DbEditCollection(edits))
|
||||
}
|
||||
|
|
|
@ -292,7 +292,9 @@ async fn test_edit_conflict() -> MyResult<()> {
|
|||
ap_id: create_res.ap_id,
|
||||
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.edits.len(), 1);
|
||||
assert!(!edit_res.local);
|
||||
|
|
Loading…
Reference in a new issue