diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index 27ab029f..d39f1ec6 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -11,18 +11,22 @@ use crate::{ users::User, Connection, Error, Result, CONFIG, }; -use activitypub::{ +use activitystreams::{ activity::{Create, Delete}, + base::{AnyBase, Base}, link, - object::{Note, Tombstone}, + object::{Note, ObjectExt}, + prelude::*, + primitives::OneOrMany, + public, uri as as_uri, }; -use chrono::{self, NaiveDateTime, TimeZone, Utc}; +use chrono::{self, NaiveDateTime}; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl}; use plume_common::{ activity_pub::{ inbox::{AsActor, AsObject, FromId}, sign::Signer, - Id, IntoId, PUBLIC_VISIBILITY, + PUBLIC_VISIBILITY, }, utils, }; @@ -115,47 +119,54 @@ impl Comment { Some(Media::get_media_processor(conn, vec![&author])), ); - let mut note = Note::default(); - let to = vec![Id::new(PUBLIC_VISIBILITY.to_string())]; + let mut note = Note::new(); + // let mut note = Note::default(); + // let to = vec![Id::new(PUBLIC_VISIBILITY.to_string())]; - note.object_props - .set_id_string(self.ap_url.clone().unwrap_or_default())?; - note.object_props - .set_summary_string(self.spoiler_text.clone())?; - note.object_props.set_content_string(html)?; - note.object_props - .set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else( - || Ok(Post::get(conn, self.post_id)?.ap_url), - |id| Ok(Comment::get(conn, id)?.ap_url.unwrap_or_default()) as Result, - )?))?; - note.object_props - .set_published_utctime(Utc.from_utc_datetime(&self.creation_date))?; - note.object_props.set_attributed_to_link(author.into_id())?; - note.object_props.set_to_link_vec(to)?; - note.object_props.set_tag_link_vec( - mentions - .into_iter() - .filter_map(|m| Mention::build_activity(conn, &m).ok()) - .collect::>(), - )?; + // note.object_props + // .set_id_string(self.ap_url.clone().unwrap_or_default())?; + // note.object_props + // .set_summary_string(self.spoiler_text.clone())?; + // note.object_props.set_content_string(html)?; + // note.object_props + // .set_in_reply_to_link(Id::new(self.in_response_to_id.map_or_else( + // || Ok(Post::get(conn, self.post_id)?.ap_url), + // |id| Ok(Comment::get(conn, id)?.ap_url.unwrap_or_default()) as Result, + // )?))?; + // note.object_props + // .set_published_utctime(Utc.from_utc_datetime(&self.creation_date))?; + // note.object_props.set_attributed_to_link(author.into_id())?; + // note.object_props.set_to_link_vec(to)?; + // note.object_props.set_tag_link_vec( + // mentions + // .into_iter() + // .filter_map(|m| Mention::build_activity(conn, &m).ok()) + // .collect::>(), + // )?; Ok(note) } pub fn create_activity(&self, conn: &DbConn) -> Result { let author = User::get(conn, self.author_id)?; - let note = self.to_activity(conn)?; - let mut act = Create::default(); - act.create_props.set_actor_link(author.into_id())?; - act.create_props.set_object_object(note.clone())?; - act.object_props.set_id_string(format!( - "{}/activity", - self.ap_url.clone().ok_or(Error::MissingApProperty)?, - ))?; - act.object_props - .set_to_link_vec(note.object_props.to_link_vec::()?)?; - act.object_props - .set_cc_link_vec(vec![Id::new(self.get_author(conn)?.followers_endpoint)])?; + let note = self.to_activity(&*conn)?; + let base = Base::retract(note)?.into_generic()?; + let any_base = AnyBase::from_base(base); + let act = Create::new::>( + as_uri!(author.ap_url), + OneOrMany::from_one(any_base), + ); + // let mut act = Create::default(); + // act.create_props.set_actor_link(author.into_id())?; + // act.create_props.set_object_object(note.clone())?; + // act.object_props.set_id_string(format!( + // "{}/activity", + // self.ap_url.clone().ok_or(Error::MissingApProperty)?, + // ))?; + // act.object_props + // .set_to_link_vec(note.object_props.to_link_vec::()?)?; + // act.object_props + // .set_cc_link_vec(vec![Id::new(self.get_author(conn)?.followers_endpoint)])?; Ok(act) } @@ -179,21 +190,28 @@ impl Comment { Ok(()) } - pub fn build_delete(&self, conn: &Connection) -> Result { - let mut act = Delete::default(); - act.delete_props - .set_actor_link(self.get_author(conn)?.into_id())?; + pub fn build_delete(&self, conn: &DbConn) -> Result { + let comment = self.to_activity(conn)?; + let base = Base::retract(comment)?.into_generic()?; + let any_base = AnyBase::from_base(base); + let act = Delete::new( + as_uri!(User::get(conn, self.author_id)?.ap_url), // FIXME + OneOrMany::from_one(any_base), + ); + // let mut act = Delete::default(); + // act.delete_props + // .set_actor_link(self.get_author(conn)?.into_id())?; - let mut tombstone = Tombstone::default(); - tombstone - .object_props - .set_id_string(self.ap_url.clone().ok_or(Error::MissingApProperty)?)?; - act.delete_props.set_object_object(tombstone)?; + // let mut tombstone = Tombstone::default(); + // tombstone + // .object_props + // .set_id_string(self.ap_url.clone().ok_or(Error::MissingApProperty)?)?; + // act.delete_props.set_object_object(tombstone)?; - act.object_props - .set_id_string(format!("{}#delete", self.ap_url.clone().unwrap()))?; - act.object_props - .set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY)])?; + // act.object_props + // .set_id_string(format!("{}#delete", self.ap_url.clone().unwrap()))?; + // act.object_props + // .set_to_link_vec(vec![Id::new(PUBLIC_VISIBILITY)])?; Ok(act) } @@ -210,12 +228,10 @@ impl FromId for Comment { fn from_activity(conn: &DbConn, note: Note) -> Result { let comm = { let previous_url = note - .object_props - .in_reply_to - .as_ref() + .in_reply_to() .ok_or(Error::MissingApProperty)? - .as_str() - .ok_or(Error::MissingApProperty)?; + .as_single_xsd_string() + .expect("only one"); let previous_comment = Comment::find_by_ap_url(conn, previous_url); let is_public = |v: &Option| match v @@ -230,73 +246,106 @@ impl FromId for Comment { _ => false, }; - let public_visibility = is_public(¬e.object_props.to) - || is_public(¬e.object_props.bto) - || is_public(¬e.object_props.cc) - || is_public(¬e.object_props.bcc); + let is_public = |v: &Option<&OneOrMany>| match v { + Some(base) => match base.as_single_xsd_any_uri() { + Some(uri) => uri == &public(), + None => false, + }, + None => false, + }; + + let public_visibility = is_public(¬e.to()) + || is_public(¬e.bto()) + || is_public(¬e.cc()) + || is_public(¬e.bcc()); let comm = Comment::insert( conn, NewComment { - content: SafeString::new(¬e.object_props.content_string()?), - spoiler_text: note.object_props.summary_string().unwrap_or_default(), - ap_url: note.object_props.id_string().ok(), + content: SafeString::new( + ¬e + .content() + .expect("exists") + .as_single_xsd_string() + .expect("only one"), + ), + spoiler_text: note + .summary() + .map(|summary| { + summary + .as_single_xsd_string() + .expect("only one") + .to_string() + }) + .unwrap_or_default(), + ap_url: note + .id(&Instance::get_local()?.public_domain) + .expect("authorized domain") + .map(|url| url.to_string()), in_response_to_id: previous_comment.iter().map(|c| c.id).next(), post_id: previous_comment.map(|c| c.post_id).or_else(|_| { Ok(Post::find_by_ap_url(conn, previous_url)?.id) as Result })?, author_id: User::from_id( conn, - ¬e.object_props.attributed_to_link::()?, + ¬e + .attributed_to() + .expect("exists") + .as_single_xsd_string() + .expect("only one"), None, CONFIG.proxy(), ) .map_err(|(_, e)| e)? .id, - sensitive: note.object_props.summary_string().is_ok(), + sensitive: note.summary().is_some(), public_visibility, }, )?; // save mentions - if let Some(serde_json::Value::Array(tags)) = note.object_props.tag.clone() { + if let Some(tags) = note.tag() { + let tags = tags.as_many().expect("many"); for tag in tags { - serde_json::from_value::(tag) - .map_err(Error::from) - .and_then(|m| { - let author = &Post::get(conn, comm.post_id)?.get_authors(conn)?[0]; - let not_author = m.link_props.href_string()? != author.ap_url.clone(); - Mention::from_activity(conn, &m, comm.id, false, not_author) - }) - .ok(); + let author = &Post::get(conn, comm.post_id)?.get_authors(conn)?[0]; + match tag.extend::() { + Err(_) => continue, + Ok(mention) => { + if mention.is_none() { + continue; + } + let mention = mention.unwrap(); + let not_author = + mention.href().expect("exists").as_str() != &author.ap_url; + Mention::from_activity(conn, &mention, comm.id, false, not_author); + } + } } } comm }; if !comm.public_visibility { - let receivers_ap_url = |v: Option| { - let filter = |e: serde_json::Value| { - if let serde_json::Value::String(s) = e { - Some(s) + let receivers_ap_url = |v: Option>| { + let filter = |addr: AnyBase| { + if let Some(url) = addr.as_xsd_any_uri() { + Some(url.to_string()) } else { None } }; - match v.unwrap_or(serde_json::Value::Null) { - serde_json::Value::Array(v) => v, - v => vec![v], - } - .into_iter() - .filter_map(filter) + v.map(|one_or_many| one_or_many.many().unwrap_or(vec![])) + .unwrap_or(vec![]) + .into_iter() + .filter_map(filter) }; let mut note = note; - let to = receivers_ap_url(note.object_props.to.take()); - let cc = receivers_ap_url(note.object_props.cc.take()); - let bto = receivers_ap_url(note.object_props.bto.take()); - let bcc = receivers_ap_url(note.object_props.bcc.take()); + let to = receivers_ap_url(note.take_to()); + let cc = receivers_ap_url(note.take_cc()); + let bto = receivers_ap_url(note.take_bto()); + let bcc = receivers_ap_url(note.take_bcc()); let receivers_ap_url = to .chain(cc)