mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 06:01:09 +00:00
wip: implemenent edits
This commit is contained in:
parent
12754a53bf
commit
c48f26c908
14 changed files with 250 additions and 34 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -4,12 +4,10 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "activitypub_federation"
|
||||
version = "0.5.0-beta.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a122cf2c2adf45b164134946bc069659cd93083fab294839a3f1d794b707c17"
|
||||
version = "0.5.0-beta.5"
|
||||
source = "git+https://github.com/LemmyNet/activitypub-federation-rust.git?branch=parse-impl#b80408d80619ac014a5cedf5079967c20058532d"
|
||||
dependencies = [
|
||||
"activitystreams-kinds",
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"axum",
|
||||
"base64 0.21.5",
|
||||
|
@ -488,6 +486,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
|
@ -608,6 +615,7 @@ dependencies = [
|
|||
"axum",
|
||||
"axum-macros",
|
||||
"chrono",
|
||||
"diffy",
|
||||
"enum_delegate",
|
||||
"env_logger",
|
||||
"futures",
|
||||
|
@ -1210,6 +1218,16 @@ dependencies = [
|
|||
"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]]
|
||||
name = "num-traits"
|
||||
version = "0.2.17"
|
||||
|
@ -1288,6 +1306,12 @@ dependencies = [
|
|||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking"
|
||||
version = "2.2.0"
|
||||
|
|
|
@ -4,12 +4,13 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[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"
|
||||
async-trait = "0.1.74"
|
||||
axum = "0.6.20"
|
||||
axum-macros = "0.3.8"
|
||||
chrono = { version = "0.4.31", features = ["serde"] }
|
||||
diffy = "0.3.0"
|
||||
enum_delegate = "0.2.0"
|
||||
env_logger = { version = "0.10.1", default-features = false }
|
||||
futures = "0.3.29"
|
||||
|
|
|
@ -34,6 +34,7 @@ pub struct CreateArticle {
|
|||
pub text: String,
|
||||
}
|
||||
|
||||
// TODO: new article should be created with empty content
|
||||
#[debug_handler]
|
||||
async fn create_article(
|
||||
data: Data<DatabaseHandle>,
|
||||
|
@ -45,6 +46,7 @@ async fn create_article(
|
|||
title: create_article.title,
|
||||
text: create_article.text,
|
||||
ap_id,
|
||||
edits: vec![],
|
||||
instance: local_instance_id,
|
||||
local: true,
|
||||
};
|
||||
|
@ -69,6 +71,7 @@ pub struct EditArticle {
|
|||
pub new_text: String,
|
||||
}
|
||||
|
||||
// TODO: this should create an edit object
|
||||
#[debug_handler]
|
||||
async fn edit_article(
|
||||
data: Data<DatabaseHandle>,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::database::DatabaseHandle;
|
||||
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::utils::generate_object_id;
|
||||
use activitypub_federation::{
|
||||
|
@ -25,7 +25,7 @@ pub struct CreateOrUpdateArticle {
|
|||
pub actor: ObjectId<DbInstance>,
|
||||
#[serde(deserialize_with = "deserialize_one_or_many")]
|
||||
pub to: Vec<Url>,
|
||||
pub object: Article,
|
||||
pub object: ApubArticle,
|
||||
#[serde(rename = "type")]
|
||||
pub kind: CreateOrUpdateType,
|
||||
pub id: Url,
|
||||
|
|
10
src/federation/diff.rs
Normal file
10
src/federation/diff.rs
Normal 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());
|
||||
}
|
|
@ -9,6 +9,7 @@ use std::sync::{Arc, Mutex};
|
|||
use url::Url;
|
||||
|
||||
pub mod activities;
|
||||
mod diff;
|
||||
pub mod objects;
|
||||
pub mod routes;
|
||||
|
||||
|
|
|
@ -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::{database::DatabaseHandle, error::Error};
|
||||
use activitypub_federation::fetch::collection_id::CollectionId;
|
||||
use activitypub_federation::kinds::object::ArticleType;
|
||||
use activitypub_federation::{
|
||||
config::Data,
|
||||
|
@ -17,18 +21,29 @@ pub struct DbArticle {
|
|||
pub text: String,
|
||||
pub ap_id: ObjectId<DbArticle>,
|
||||
pub instance: ObjectId<DbInstance>,
|
||||
/// List of all edits which make up this article, oldest first.
|
||||
pub edits: Vec<DbEdit>,
|
||||
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)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Article {
|
||||
pub struct ApubArticle {
|
||||
#[serde(rename = "type")]
|
||||
kind: ArticleType,
|
||||
id: ObjectId<DbArticle>,
|
||||
pub(crate) attributed_to: ObjectId<DbInstance>,
|
||||
#[serde(deserialize_with = "deserialize_one_or_many")]
|
||||
pub(crate) to: Vec<Url>,
|
||||
edits: CollectionId<DbEditCollection>,
|
||||
content: String,
|
||||
name: String,
|
||||
}
|
||||
|
@ -36,7 +51,7 @@ pub struct Article {
|
|||
#[async_trait::async_trait]
|
||||
impl Object for DbArticle {
|
||||
type DataType = DatabaseHandle;
|
||||
type Kind = Article;
|
||||
type Kind = ApubArticle;
|
||||
type Error = Error;
|
||||
|
||||
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> {
|
||||
let instance = self.instance.dereference_local(data).await?;
|
||||
Ok(Article {
|
||||
Ok(ApubArticle {
|
||||
kind: Default::default(),
|
||||
id: self.ap_id,
|
||||
attributed_to: self.instance,
|
||||
id: self.ap_id.clone(),
|
||||
attributed_to: self.instance.clone(),
|
||||
to: vec![public(), instance.followers_url()?],
|
||||
edits: self.edits_id()?,
|
||||
content: self.text,
|
||||
name: self.title,
|
||||
})
|
||||
|
@ -79,11 +95,18 @@ impl Object for DbArticle {
|
|||
text: json.content,
|
||||
ap_id: json.id,
|
||||
instance: json.attributed_to,
|
||||
// TODO: shouldnt overwrite existing edits
|
||||
edits: vec![],
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::database::DatabaseHandle;
|
||||
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::utils::generate_object_id;
|
||||
use activitypub_federation::kinds::collection::CollectionType;
|
||||
|
@ -19,7 +19,7 @@ pub struct ArticleCollection {
|
|||
pub(crate) r#type: CollectionType,
|
||||
pub(crate) id: Url,
|
||||
pub(crate) total_items: i32,
|
||||
pub(crate) items: Vec<Article>,
|
||||
pub(crate) items: Vec<ApubArticle>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -53,10 +53,9 @@ impl Collection for DbArticleCollection {
|
|||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
let ap_id = generate_object_id(data.local_instance().ap_id.inner())?;
|
||||
let collection = ArticleCollection {
|
||||
r#type: Default::default(),
|
||||
id: ap_id,
|
||||
id: data.local_instance().articles_id.into(),
|
||||
total_items: articles.len() as i32,
|
||||
items: articles,
|
||||
};
|
||||
|
@ -75,10 +74,7 @@ impl Collection for DbArticleCollection {
|
|||
apub: Self::Kind,
|
||||
_owner: &Self::Owner,
|
||||
data: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
) -> Result<Self, Self::Error> {
|
||||
let articles = try_join_all(
|
||||
apub.items
|
||||
.into_iter()
|
||||
|
|
61
src/federation/objects/edit.rs
Normal file
61
src/federation/objects/edit.rs
Normal 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!()
|
||||
}
|
||||
}
|
86
src/federation/objects/edits_collection.rs
Normal file
86
src/federation/objects/edits_collection.rs
Normal 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))
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use crate::error::Error;
|
||||
use crate::error::{Error, MyResult};
|
||||
use crate::federation::objects::articles_collection::DbArticleCollection;
|
||||
use crate::{database::DatabaseHandle, federation::activities::follow::Follow};
|
||||
use activitypub_federation::activity_sending::SendActivityTask;
|
||||
|
@ -63,7 +63,7 @@ impl DbInstance {
|
|||
) -> Result<(), <Activity as ActivityHandler>::Error>
|
||||
where
|
||||
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 sends = SendActivityTask::prepare(&activity, self, recipients, data).await?;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
pub mod article;
|
||||
pub mod articles_collection;
|
||||
pub mod edit;
|
||||
pub mod edits_collection;
|
||||
pub mod instance;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use fediwiki::api::{FollowInstance, ResolveObject};
|
||||
use fediwiki::error::MyResult;
|
||||
use fediwiki::federation::objects::instance::DbInstance;
|
||||
use fediwiki::start;
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::Client;
|
||||
use serde::de::Deserialize;
|
||||
|
@ -9,13 +10,12 @@ use std::sync::Once;
|
|||
use tokio::task::JoinHandle;
|
||||
use tracing::log::LevelFilter;
|
||||
use url::Url;
|
||||
use fediwiki::start;
|
||||
|
||||
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
|
||||
|
||||
pub struct TestData {
|
||||
pub hostname_alpha: &'static str,
|
||||
pub hostname_beta:&'static str,
|
||||
pub hostname_beta: &'static str,
|
||||
handle_alpha: 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_beta.abort();
|
||||
Ok(())
|
||||
|
|
|
@ -20,9 +20,12 @@ async fn test_create_and_read_article() -> MyResult<()> {
|
|||
let get_article = GetArticle {
|
||||
title: "Manu_Chao".to_string(),
|
||||
};
|
||||
let not_found =
|
||||
get_query::<DbArticle, _>(data.hostname_alpha, &"article".to_string(), Some(get_article.clone()))
|
||||
.await;
|
||||
let not_found = get_query::<DbArticle, _>(
|
||||
data.hostname_alpha,
|
||||
&"article".to_string(),
|
||||
Some(get_article.clone()),
|
||||
)
|
||||
.await;
|
||||
assert!(not_found.is_err());
|
||||
|
||||
// create article
|
||||
|
@ -35,8 +38,12 @@ async fn test_create_and_read_article() -> MyResult<()> {
|
|||
assert!(create_res.local);
|
||||
|
||||
// now article can be read
|
||||
let get_res: DbArticle =
|
||||
get_query(data.hostname_alpha, &"article".to_string(), Some(get_article.clone())).await?;
|
||||
let get_res: DbArticle = get_query(
|
||||
data.hostname_alpha,
|
||||
&"article".to_string(),
|
||||
Some(get_article.clone()),
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(create_article.title, get_res.title);
|
||||
assert_eq!(create_article.text, get_res.text);
|
||||
assert!(get_res.local);
|
||||
|
@ -134,7 +141,8 @@ async fn test_federate_article_changes() -> MyResult<()> {
|
|||
title: create_res.title.clone(),
|
||||
};
|
||||
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.text, get_res.text);
|
||||
|
||||
|
@ -151,7 +159,8 @@ async fn test_federate_article_changes() -> MyResult<()> {
|
|||
title: edit_res.title.clone(),
|
||||
};
|
||||
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.text, get_res.text);
|
||||
|
||||
|
|
Loading…
Reference in a new issue