From 7cf7700ef73f592be6deccd34f23f641b66a694b Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 7 Mar 2022 00:15:36 +0900 Subject: [PATCH] Implement Post::to_activity07() --- plume-models/src/posts.rs | 78 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 65c98735..51f27ab4 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -9,6 +9,13 @@ use activitypub::{ object::{Article, Image, Tombstone}, CustomObject, }; +use activitystreams::{ + base::AnyBase, + iri_string::types::IriString, + object::{ApObject, Article as Article07, Image as Image07}, + prelude::*, + time::OffsetDateTime, +}; use chrono::{NaiveDateTime, TimeZone, Utc}; use diesel::{self, BelongingToDsl, ExpressionMethods, QueryDsl, RunQueryDsl}; use once_cell::sync::Lazy; @@ -16,7 +23,8 @@ use plume_common::{ activity_pub::{ inbox::{AsActor, AsObject, FromId}, sign::Signer, - Hashtag, Id, IntoId, Licensed, Source, PUBLIC_VISIBILITY, + Hashtag, Id, IntoId, Licensed, Licensed07, LicensedArticle as LicensedArticle07, Source, + SourceProperty, PUBLIC_VISIBILITY, }, utils::{iri_percent_encode_seg, md_to_html}, }; @@ -409,6 +417,74 @@ impl Post { Ok(LicensedArticle::new(article, license)) } + pub fn to_activity07(&self, conn: &Connection) -> Result { + let cc = self.get_receivers_urls(conn)?; + let to = vec![PUBLIC_VISIBILITY.to_string()]; + + let mut mentions_json = Mention::list_for_post(conn, self.id)? + .into_iter() + .map(|m| json!(m.to_activity07(conn).ok())) + .collect::>(); + let mut tags_json = Tag::for_post(conn, self.id)? + .into_iter() + .map(|t| json!(t.to_activity07().ok())) + .collect::>(); + mentions_json.append(&mut tags_json); + + let mut article = ApObject::new(Article07::new()); + article.set_name(self.title.clone()); + article.set_id(self.ap_url.parse::()?); + + let mut authors = self + .get_authors(conn)? + .into_iter() + .filter_map(|x| x.ap_url.parse::().ok()) + .collect::>(); + authors.push(self.get_blog(conn)?.ap_url.parse::()?); // add the blog URL here too + article.set_many_attributed_tos(authors); + article.set_content(self.content.get().clone()); + let source = SourceProperty { + content: self.source.clone(), + media_type: String::from("text/markdown"), + }; + article.set_published( + OffsetDateTime::from_unix_timestamp_nanos(self.creation_date.timestamp_nanos().into()) + .expect("OffsetDateTime"), + ); + article.set_summary(&*self.subtitle); + article.set_tag(AnyBase::from_arbitrary_json(json!(mentions_json))?); + + if let Some(media_id) = self.cover_id { + let media = Media::get(conn, media_id)?; + let mut cover = Image07::new(); + cover.set_url(media.url()?); + if media.sensitive { + cover.set_summary(media.content_warning.unwrap_or_default()); + } + cover.set_content(media.alt_text); + cover.set_many_attributed_tos(vec![User::get(conn, media.owner_id)? + .ap_url + .parse::()?]); + article.set_icon(cover.into_any_base()?); + } + + article.set_url(self.ap_url.parse::()?); + article.set_many_tos( + to.into_iter() + .filter_map(|to| to.parse::().ok()) + .collect::>(), + ); + article.set_many_ccs( + cc.into_iter() + .filter_map(|cc| cc.parse::().ok()) + .collect::>(), + ); + let license = Licensed07 { + license: self.license.clone(), + }; + Ok(LicensedArticle07::new(article, license, source)) + } + pub fn create_activity(&self, conn: &Connection) -> Result { let article = self.to_activity(conn)?; let mut act = Create::default();