1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 18:11:09 +00:00

wip: implemenent edits

This commit is contained in:
Felix Ableitner 2023-11-20 16:48:29 +01:00
parent 12754a53bf
commit c48f26c908
14 changed files with 250 additions and 34 deletions

32
Cargo.lock generated
View file

@ -4,12 +4,10 @@ version = 3
[[package]] [[package]]
name = "activitypub_federation" name = "activitypub_federation"
version = "0.5.0-beta.4" version = "0.5.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/LemmyNet/activitypub-federation-rust.git?branch=parse-impl#b80408d80619ac014a5cedf5079967c20058532d"
checksum = "9a122cf2c2adf45b164134946bc069659cd93083fab294839a3f1d794b707c17"
dependencies = [ dependencies = [
"activitystreams-kinds", "activitystreams-kinds",
"anyhow",
"async-trait", "async-trait",
"axum", "axum",
"base64 0.21.5", "base64 0.21.5",
@ -488,6 +486,15 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "diffy"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e616e59155c92257e84970156f506287853355f58cd4a6eb167385722c32b790"
dependencies = [
"nu-ansi-term",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -608,6 +615,7 @@ dependencies = [
"axum", "axum",
"axum-macros", "axum-macros",
"chrono", "chrono",
"diffy",
"enum_delegate", "enum_delegate",
"env_logger", "env_logger",
"futures", "futures",
@ -1210,6 +1218,16 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.17" version = "0.2.17"
@ -1288,6 +1306,12 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "parking" name = "parking"
version = "2.2.0" version = "2.2.0"

View file

@ -4,12 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
activitypub_federation = { version = "0.5.0-beta.4", features = ["axum"], default-features = false } activitypub_federation = { git = "https://github.com/LemmyNet/activitypub-federation-rust.git", branch = "parse-impl", features = ["axum"], default-features = false }
anyhow = "1.0.75" anyhow = "1.0.75"
async-trait = "0.1.74" async-trait = "0.1.74"
axum = "0.6.20" axum = "0.6.20"
axum-macros = "0.3.8" axum-macros = "0.3.8"
chrono = { version = "0.4.31", features = ["serde"] } chrono = { version = "0.4.31", features = ["serde"] }
diffy = "0.3.0"
enum_delegate = "0.2.0" enum_delegate = "0.2.0"
env_logger = { version = "0.10.1", default-features = false } env_logger = { version = "0.10.1", default-features = false }
futures = "0.3.29" futures = "0.3.29"

View file

@ -34,6 +34,7 @@ pub struct CreateArticle {
pub text: String, pub text: String,
} }
// TODO: new article should be created with empty content
#[debug_handler] #[debug_handler]
async fn create_article( async fn create_article(
data: Data<DatabaseHandle>, data: Data<DatabaseHandle>,
@ -45,6 +46,7 @@ async fn create_article(
title: create_article.title, title: create_article.title,
text: create_article.text, text: create_article.text,
ap_id, ap_id,
edits: vec![],
instance: local_instance_id, instance: local_instance_id,
local: true, local: true,
}; };
@ -69,6 +71,7 @@ pub struct EditArticle {
pub new_text: String, pub new_text: String,
} }
// TODO: this should create an edit object
#[debug_handler] #[debug_handler]
async fn edit_article( async fn edit_article(
data: Data<DatabaseHandle>, data: Data<DatabaseHandle>,

View file

@ -1,6 +1,6 @@
use crate::database::DatabaseHandle; use crate::database::DatabaseHandle;
use crate::error::MyResult; use crate::error::MyResult;
use crate::federation::objects::article::{Article, DbArticle}; use crate::federation::objects::article::{ApubArticle, DbArticle};
use crate::federation::objects::instance::DbInstance; use crate::federation::objects::instance::DbInstance;
use crate::utils::generate_object_id; use crate::utils::generate_object_id;
use activitypub_federation::{ use activitypub_federation::{
@ -25,7 +25,7 @@ pub struct CreateOrUpdateArticle {
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: Article, pub object: ApubArticle,
#[serde(rename = "type")] #[serde(rename = "type")]
pub kind: CreateOrUpdateType, pub kind: CreateOrUpdateType,
pub id: Url, pub id: Url,

10
src/federation/diff.rs Normal file
View file

@ -0,0 +1,10 @@
#[test]
fn test_diff() {
use diffy::create_patch;
let original = "The Way of Kings\nWords of Radiance\n";
let modified = "The Way of Kings\nWords of Radiance\nOathbringer\n";
let patch = create_patch(original, modified);
assert_eq!("--- original\n+++ modified\n@@ -1,2 +1,3 @@\n The Way of Kings\n Words of Radiance\n+Oathbringer\n", patch.to_string());
}

View file

@ -9,6 +9,7 @@ use std::sync::{Arc, Mutex};
use url::Url; use url::Url;
pub mod activities; pub mod activities;
mod diff;
pub mod objects; pub mod objects;
pub mod routes; pub mod routes;

View file

@ -1,5 +1,9 @@
use crate::error::MyResult;
use crate::federation::objects::edit::DbEdit;
use crate::federation::objects::edits_collection::{ApubEditCollection, DbEditCollection};
use crate::federation::objects::instance::DbInstance; use crate::federation::objects::instance::DbInstance;
use crate::{database::DatabaseHandle, error::Error}; use crate::{database::DatabaseHandle, error::Error};
use activitypub_federation::fetch::collection_id::CollectionId;
use activitypub_federation::kinds::object::ArticleType; use activitypub_federation::kinds::object::ArticleType;
use activitypub_federation::{ use activitypub_federation::{
config::Data, config::Data,
@ -17,18 +21,29 @@ pub struct DbArticle {
pub text: String, pub text: String,
pub ap_id: ObjectId<DbArticle>, pub ap_id: ObjectId<DbArticle>,
pub instance: ObjectId<DbInstance>, pub instance: ObjectId<DbInstance>,
/// List of all edits which make up this article, oldest first.
pub edits: Vec<DbEdit>,
pub local: bool, pub local: bool,
} }
impl DbArticle {
fn edits_id(&self) -> MyResult<CollectionId<DbEditCollection>> {
Ok(CollectionId::parse(&format!("{}/edits", self.ap_id))
.unwrap()
.into())
}
}
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Article { pub struct ApubArticle {
#[serde(rename = "type")] #[serde(rename = "type")]
kind: ArticleType, kind: ArticleType,
id: ObjectId<DbArticle>, id: ObjectId<DbArticle>,
pub(crate) attributed_to: ObjectId<DbInstance>, pub(crate) attributed_to: ObjectId<DbInstance>,
#[serde(deserialize_with = "deserialize_one_or_many")] #[serde(deserialize_with = "deserialize_one_or_many")]
pub(crate) to: Vec<Url>, pub(crate) to: Vec<Url>,
edits: CollectionId<DbEditCollection>,
content: String, content: String,
name: String, name: String,
} }
@ -36,7 +51,7 @@ pub struct Article {
#[async_trait::async_trait] #[async_trait::async_trait]
impl Object for DbArticle { impl Object for DbArticle {
type DataType = DatabaseHandle; type DataType = DatabaseHandle;
type Kind = Article; type Kind = ApubArticle;
type Error = Error; type Error = Error;
async fn read_from_id( async fn read_from_id(
@ -54,11 +69,12 @@ 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 = self.instance.dereference_local(data).await?; let instance = self.instance.dereference_local(data).await?;
Ok(Article { Ok(ApubArticle {
kind: Default::default(), kind: Default::default(),
id: self.ap_id, id: self.ap_id.clone(),
attributed_to: self.instance, attributed_to: self.instance.clone(),
to: vec![public(), instance.followers_url()?], to: vec![public(), instance.followers_url()?],
edits: self.edits_id()?,
content: self.text, content: self.text,
name: self.title, name: self.title,
}) })
@ -79,11 +95,18 @@ impl Object for DbArticle {
text: json.content, text: json.content,
ap_id: json.id, ap_id: json.id,
instance: json.attributed_to, instance: json.attributed_to,
// TODO: shouldnt overwrite existing edits
edits: vec![],
local: false, local: false,
}; };
let mut lock = data.articles.lock().unwrap(); {
lock.insert(article.ap_id.inner().clone(), article.clone()); let mut lock = data.articles.lock().unwrap();
lock.insert(article.ap_id.inner().clone(), article.clone());
}
json.edits.dereference(&article, &data).await?;
Ok(article) Ok(article)
} }
} }

View file

@ -1,6 +1,6 @@
use crate::database::DatabaseHandle; use crate::database::DatabaseHandle;
use crate::error::Error; use crate::error::Error;
use crate::federation::objects::article::{Article, DbArticle}; use crate::federation::objects::article::{ApubArticle, DbArticle};
use crate::federation::objects::instance::DbInstance; use crate::federation::objects::instance::DbInstance;
use crate::utils::generate_object_id; use crate::utils::generate_object_id;
use activitypub_federation::kinds::collection::CollectionType; use activitypub_federation::kinds::collection::CollectionType;
@ -19,7 +19,7 @@ pub struct ArticleCollection {
pub(crate) r#type: CollectionType, pub(crate) r#type: CollectionType,
pub(crate) id: Url, pub(crate) id: Url,
pub(crate) total_items: i32, pub(crate) total_items: i32,
pub(crate) items: Vec<Article>, pub(crate) items: Vec<ApubArticle>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -53,10 +53,9 @@ impl Collection for DbArticleCollection {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
.await?; .await?;
let ap_id = generate_object_id(data.local_instance().ap_id.inner())?;
let collection = ArticleCollection { let collection = ArticleCollection {
r#type: Default::default(), r#type: Default::default(),
id: ap_id, id: data.local_instance().articles_id.into(),
total_items: articles.len() as i32, total_items: articles.len() as i32,
items: articles, items: articles,
}; };
@ -75,10 +74,7 @@ impl Collection for DbArticleCollection {
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> {
where
Self: Sized,
{
let articles = try_join_all( let articles = try_join_all(
apub.items apub.items
.into_iter() .into_iter()

View file

@ -0,0 +1,61 @@
use crate::database::DatabaseHandle;
use crate::error::Error;
use crate::federation::objects::article::DbArticle;
use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId;
use activitypub_federation::traits::Object;
use serde::{Deserialize, Serialize};
use url::Url;
/// Represents a single change to the article.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DbEdit {
pub id: ObjectId<DbEdit>,
pub diff: String,
pub local: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EditType {
Edit,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApubEdit {
#[serde(rename = "type")]
kind: EditType,
id: ObjectId<DbEdit>,
article_id: ObjectId<DbArticle>,
diff: String,
}
#[async_trait::async_trait]
impl Object for DbEdit {
type DataType = DatabaseHandle;
type Kind = ApubEdit;
type Error = Error;
async fn read_from_id(
object_id: Url,
data: &Data<Self::DataType>,
) -> Result<Option<Self>, Self::Error> {
todo!()
}
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
todo!()
}
async fn verify(
json: &Self::Kind,
expected_domain: &Url,
data: &Data<Self::DataType>,
) -> Result<(), Self::Error> {
todo!()
}
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
todo!()
}
}

View file

@ -0,0 +1,86 @@
use crate::database::DatabaseHandle;
use crate::error::Error;
use crate::federation::objects::article::{ApubArticle, DbArticle};
use crate::federation::objects::edit::{ApubEdit, DbEdit};
use crate::federation::objects::instance::DbInstance;
use crate::utils::generate_object_id;
use activitypub_federation::kinds::collection::{CollectionType, OrderedCollectionType};
use activitypub_federation::{
config::Data,
traits::{Collection, Object},
};
use futures::future;
use futures::future::try_join_all;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ApubEditCollection {
pub(crate) r#type: OrderedCollectionType,
pub(crate) id: Url,
pub(crate) total_items: i32,
pub(crate) items: Vec<ApubEdit>,
}
#[derive(Clone, Debug)]
pub struct DbEditCollection(Vec<DbEdit>);
#[async_trait::async_trait]
impl Collection for DbEditCollection {
type Owner = DbArticle;
type DataType = DatabaseHandle;
type Kind = ApubEditCollection;
type Error = Error;
async fn read_local(
owner: &Self::Owner,
data: &Data<Self::DataType>,
) -> Result<Self::Kind, Self::Error> {
let edits = {
let lock = data.articles.lock().unwrap();
DbEditCollection(lock.get(owner.ap_id.inner()).unwrap().edits.clone())
};
let edits = future::try_join_all(
edits
.0
.into_iter()
.map(|a| a.into_json(data))
.collect::<Vec<_>>(),
)
.await?;
let collection = ApubEditCollection {
r#type: Default::default(),
id: Url::from(data.local_instance().articles_id),
total_items: edits.len() as i32,
items: edits,
};
Ok(collection)
}
async fn verify(
_apub: &Self::Kind,
_expected_domain: &Url,
_data: &Data<Self::DataType>,
) -> Result<(), Self::Error> {
Ok(())
}
async fn from_json(
apub: Self::Kind,
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 mut article = articles.get_mut(owner.ap_id.inner()).unwrap();
for e in edits.clone() {
// TODO: edits need a unique id to avoid pushing duplicates
article.edits.push(e);
}
// TODO: return value propably not needed
Ok(DbEditCollection(edits))
}
}

View file

@ -1,4 +1,4 @@
use crate::error::Error; use crate::error::{Error, MyResult};
use crate::federation::objects::articles_collection::DbArticleCollection; use crate::federation::objects::articles_collection::DbArticleCollection;
use crate::{database::DatabaseHandle, federation::activities::follow::Follow}; use crate::{database::DatabaseHandle, federation::activities::follow::Follow};
use activitypub_federation::activity_sending::SendActivityTask; use activitypub_federation::activity_sending::SendActivityTask;
@ -63,7 +63,7 @@ impl DbInstance {
) -> Result<(), <Activity as ActivityHandler>::Error> ) -> Result<(), <Activity as ActivityHandler>::Error>
where where
Activity: ActivityHandler + Serialize + Debug + Send + Sync, Activity: ActivityHandler + Serialize + Debug + Send + Sync,
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>, <Activity as ActivityHandler>::Error: From<activitypub_federation::error::Error>,
{ {
let activity = WithContext::new_default(activity); let activity = WithContext::new_default(activity);
let sends = SendActivityTask::prepare(&activity, self, recipients, data).await?; let sends = SendActivityTask::prepare(&activity, self, recipients, data).await?;

View file

@ -1,3 +1,5 @@
pub mod article; pub mod article;
pub mod articles_collection; pub mod articles_collection;
pub mod edit;
pub mod edits_collection;
pub mod instance; pub mod instance;

View file

@ -1,6 +1,7 @@
use fediwiki::api::{FollowInstance, ResolveObject}; use fediwiki::api::{FollowInstance, ResolveObject};
use fediwiki::error::MyResult; use fediwiki::error::MyResult;
use fediwiki::federation::objects::instance::DbInstance; use fediwiki::federation::objects::instance::DbInstance;
use fediwiki::start;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use reqwest::Client; use reqwest::Client;
use serde::de::Deserialize; use serde::de::Deserialize;
@ -9,13 +10,12 @@ use std::sync::Once;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing::log::LevelFilter; use tracing::log::LevelFilter;
use url::Url; use url::Url;
use fediwiki::start;
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new); pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
pub struct TestData { pub struct TestData {
pub hostname_alpha: &'static str, pub hostname_alpha: &'static str,
pub hostname_beta:&'static str, pub hostname_beta: &'static str,
handle_alpha: JoinHandle<()>, handle_alpha: JoinHandle<()>,
handle_beta: JoinHandle<()>, handle_beta: JoinHandle<()>,
} }
@ -47,7 +47,7 @@ impl TestData {
} }
} }
pub fn stop(self) -> MyResult<()>{ pub fn stop(self) -> MyResult<()> {
self.handle_alpha.abort(); self.handle_alpha.abort();
self.handle_beta.abort(); self.handle_beta.abort();
Ok(()) Ok(())

View file

@ -20,9 +20,12 @@ async fn test_create_and_read_article() -> MyResult<()> {
let get_article = GetArticle { let get_article = GetArticle {
title: "Manu_Chao".to_string(), title: "Manu_Chao".to_string(),
}; };
let not_found = let not_found = get_query::<DbArticle, _>(
get_query::<DbArticle, _>(data.hostname_alpha, &"article".to_string(), Some(get_article.clone())) data.hostname_alpha,
.await; &"article".to_string(),
Some(get_article.clone()),
)
.await;
assert!(not_found.is_err()); assert!(not_found.is_err());
// create article // create article
@ -35,8 +38,12 @@ async fn test_create_and_read_article() -> MyResult<()> {
assert!(create_res.local); assert!(create_res.local);
// now article can be read // now article can be read
let get_res: DbArticle = let get_res: DbArticle = get_query(
get_query(data.hostname_alpha, &"article".to_string(), Some(get_article.clone())).await?; data.hostname_alpha,
&"article".to_string(),
Some(get_article.clone()),
)
.await?;
assert_eq!(create_article.title, get_res.title); assert_eq!(create_article.title, get_res.title);
assert_eq!(create_article.text, get_res.text); assert_eq!(create_article.text, get_res.text);
assert!(get_res.local); assert!(get_res.local);
@ -134,7 +141,8 @@ async fn test_federate_article_changes() -> MyResult<()> {
title: create_res.title.clone(), title: create_res.title.clone(),
}; };
let get_res = let get_res =
get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone())).await?; get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone()))
.await?;
assert_eq!(create_res.title, get_res.title); assert_eq!(create_res.title, get_res.title);
assert_eq!(create_res.text, get_res.text); assert_eq!(create_res.text, get_res.text);
@ -151,7 +159,8 @@ async fn test_federate_article_changes() -> MyResult<()> {
title: edit_res.title.clone(), title: edit_res.title.clone(),
}; };
let get_res = let get_res =
get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone())).await?; get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone()))
.await?;
assert_eq!(edit_res.title, get_res.title); assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.text, get_res.text); assert_eq!(edit_res.text, get_res.text);