From 811c20c8fb05fe2d3bdb8e9c5346fe92dab5cbdf Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 2 May 2022 23:07:05 +0900 Subject: [PATCH] Use Blog::to_activity07() instead of to_activity() --- plume-models/src/blogs.rs | 112 +------------------------------------- src/routes/blogs.rs | 4 +- 2 files changed, 4 insertions(+), 112 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 7c7ee559..e9f9fd1a 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -2,7 +2,7 @@ use crate::{ db_conn::DbConn, instance::*, medias::Media, posts::Post, safe_string::SafeString, schema::blogs, users::User, Connection, Error, PlumeRocket, Result, CONFIG, ITEMS_PER_PAGE, }; -use activitypub::{actor::Group, object::Image, CustomObject}; +use activitypub::{actor::Group, CustomObject}; use activitystreams::{ actor::{ApActor, ApActorExt, AsApActor, Group as Group07}, base::AnyBase, @@ -22,7 +22,7 @@ use openssl::{ use plume_common::activity_pub::{ inbox::{AsActor, FromId}, sign, ActivityStream, ApSignature, ApSignature07, CustomGroup as CustomGroup07, Id, IntoId, - PublicKey, PublicKey07, Source, SourceProperty, ToAsString, ToAsUri, + PublicKey07, Source, SourceProperty, ToAsString, ToAsUri, }; use webfinger::*; @@ -163,68 +163,6 @@ impl Blog { }) } - pub fn to_activity(&self, conn: &Connection) -> Result { - let mut blog = Group::default(); - blog.ap_actor_props - .set_preferred_username_string(self.actor_id.clone())?; - blog.object_props.set_name_string(self.title.clone())?; - blog.ap_actor_props - .set_outbox_string(self.outbox_url.clone())?; - blog.ap_actor_props - .set_inbox_string(self.inbox_url.clone())?; - blog.object_props - .set_summary_string(self.summary_html.to_string())?; - blog.ap_object_props.set_source_object(Source { - content: self.summary.clone(), - media_type: String::from("text/markdown"), - })?; - - let mut icon = Image::default(); - icon.object_props.set_url_string( - self.icon_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) - .unwrap_or_default(), - )?; - icon.object_props.set_attributed_to_link( - self.icon_id - .and_then(|id| { - Media::get(conn, id) - .and_then(|m| Ok(User::get(conn, m.owner_id)?.into_id())) - .ok() - }) - .unwrap_or_else(|| Id::new(String::new())), - )?; - blog.object_props.set_icon_object(icon)?; - - let mut banner = Image::default(); - banner.object_props.set_url_string( - self.banner_id - .and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok()) - .unwrap_or_default(), - )?; - banner.object_props.set_attributed_to_link( - self.banner_id - .and_then(|id| { - Media::get(conn, id) - .and_then(|m| Ok(User::get(conn, m.owner_id)?.into_id())) - .ok() - }) - .unwrap_or_else(|| Id::new(String::new())), - )?; - blog.object_props.set_image_object(banner)?; - - blog.object_props.set_id_string(self.ap_url.clone())?; - - let mut public_key = PublicKey::default(); - public_key.set_id_string(format!("{}#main-key", self.ap_url))?; - public_key.set_owner_string(self.ap_url.clone())?; - public_key.set_public_key_pem_string(self.public_key.clone())?; - let mut ap_signature = ApSignature::default(); - ap_signature.set_public_key_publickey(public_key)?; - - Ok(CustomGroup::new(blog, ap_signature)) - } - pub fn to_activity07(&self, conn: &Connection) -> Result { let mut blog = ApActor::new(self.inbox_url.parse()?, Group07::new()); blog.set_preferred_username(self.actor_id.clone()); @@ -1040,52 +978,6 @@ pub(crate) mod tests { }) } - #[test] - fn to_activity() { - let conn = &db(); - conn.test_transaction::<_, Error, _>(|| { - let (_users, blogs) = fill_database(&conn); - let blog = &blogs[0]; - let act = blog.to_activity(conn)?; - - let expected = json!({ - "followers": null, - "following": null, - "icon": { - "attributedTo": "https://plu.me/@/admin/", - "type": "Image", - "url": "https://plu.me/aaa.png" - }, - "id": "https://plu.me/~/BlogName/", - "image": { - "attributedTo": "https://plu.me/@/admin/", - "type": "Image", - "url": "https://plu.me/bbb.png" - }, - "inbox": "https://plu.me/~/BlogName/inbox", - "liked": null, - "name": "Blog name", - "outbox": "https://plu.me/~/BlogName/outbox", - "preferredUsername": "BlogName", - "publicKey": { - "id": "https://plu.me/~/BlogName/#main-key", - "owner": "https://plu.me/~/BlogName/", - "publicKeyPem": blog.public_key - }, - "source": { - "content": "This is a small blog", - "mediaType": "text/markdown" - }, - "summary": "", - "type": "Group" - }); - - assert_json_eq!(to_value(act)?, expected); - - Ok(()) - }); - } - #[test] fn to_activity07() { let conn = &db(); diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 85071313..1135d9ed 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -12,7 +12,7 @@ use validator::{Validate, ValidationError, ValidationErrors}; use crate::routes::{errors::ErrorPage, Page, RespondOrRedirect}; use crate::template_utils::{IntoContext, Ructe}; use crate::utils::requires_login; -use plume_common::activity_pub::{ActivityStream, ApRequest}; +use plume_common::activity_pub::{ActivityStream, ApRequest, CustomGroup}; use plume_common::utils; use plume_models::{ blog_authors::*, blogs::*, db_conn::DbConn, instance::Instance, medias::*, posts::Post, @@ -49,7 +49,7 @@ pub fn activity_details( _ap: ApRequest, ) -> Option> { let blog = Blog::find_by_fqn(&conn, &name).ok()?; - Some(ActivityStream::new(blog.to_activity(&conn).ok()?)) + Some(ActivityStream::new(blog.to_activity07(&conn).ok()?)) } #[get("/blogs/new")]