Make Comment follow activitystreams 0.7

This commit is contained in:
Kitaiti Makoto 2022-01-12 03:28:53 +09:00
parent ab199e626b
commit 23259498a5

View File

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