Fix blog title conversion for ActivityPub

This commit is contained in:
Kitaiti Makoto 2023-01-09 07:22:17 +09:00
parent 8afcc1511e
commit f369fa9b25
2 changed files with 22 additions and 17 deletions

View File

@ -24,7 +24,7 @@ use plume_common::{
sign, ActivityStream, ApSignature, CustomGroup, Id, IntoId, PublicKey, Source, sign, ActivityStream, ApSignature, CustomGroup, Id, IntoId, PublicKey, Source,
SourceProperty, ToAsString, ToAsUri, SourceProperty, ToAsString, ToAsUri,
}, },
utils::iri_percent_encode_seg, utils::{iri_percent_encode_seg, make_fqn},
}; };
use webfinger::*; use webfinger::*;
@ -85,14 +85,13 @@ impl Blog {
} }
if inserted.fqn.is_empty() { if inserted.fqn.is_empty() {
// This might not enough for some titles such as all-Japanese title,
// but better than doing nothing.
let fqn = make_fqn(&inserted.title);
if instance.local { if instance.local {
inserted.fqn = iri_percent_encode_seg(&inserted.actor_id); inserted.fqn = fqn;
} else { } else {
inserted.fqn = format!( inserted.fqn = format!("{}@{}", &fqn, instance.public_domain);
"{}@{}",
iri_percent_encode_seg(&inserted.actor_id),
instance.public_domain
);
} }
} }
@ -173,7 +172,7 @@ impl Blog {
pub fn to_activity(&self, conn: &Connection) -> Result<CustomGroup> { pub fn to_activity(&self, conn: &Connection) -> Result<CustomGroup> {
let mut blog = ApActor::new(self.inbox_url.parse()?, Group::new()); let mut blog = ApActor::new(self.inbox_url.parse()?, Group::new());
blog.set_preferred_username(iri_percent_encode_seg(&self.actor_id)); blog.set_preferred_username(&self.fqn);
blog.set_name(self.title.clone()); blog.set_name(self.title.clone());
blog.set_outbox(self.outbox_url.parse()?); blog.set_outbox(self.outbox_url.parse()?);
blog.set_summary(self.summary_html.to_string()); blog.set_summary(self.summary_html.to_string());
@ -399,7 +398,12 @@ impl FromId<DbConn> for Blog {
}; };
let mut new_blog = NewBlog { let mut new_blog = NewBlog {
actor_id: name.to_string(), actor_id: iri_percent_encode_seg(
&acct
.name()
.and_then(|name| name.to_as_string())
.ok_or(Error::MissingApProperty)?,
),
outbox_url, outbox_url,
inbox_url, inbox_url,
public_key: acct.ext_one.public_key.public_key_pem.to_string(), public_key: acct.ext_one.public_key.public_key_pem.to_string(),
@ -566,7 +570,7 @@ pub(crate) mod tests {
conn, conn,
NewBlog::new_local( NewBlog::new_local(
"Blog%20Name".to_owned(), "Blog%20Name".to_owned(),
"Blog name".to_owned(), "Blog Name".to_owned(),
"This is a small blog".to_owned(), "This is a small blog".to_owned(),
Instance::get_local().unwrap().id, Instance::get_local().unwrap().id,
) )
@ -819,7 +823,7 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
assert_eq!(Blog::find_by_fqn(conn, "Some%20Name").unwrap().id, blog.id); assert_eq!(Blog::find_by_fqn(conn, "SomeName").unwrap().id, blog.id);
Ok(()) Ok(())
}) })
} }
@ -842,7 +846,7 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
assert_eq!(blog.fqn, "Some%20Name"); assert_eq!(blog.fqn, "SomeName");
Ok(()) Ok(())
}) })
} }
@ -968,6 +972,7 @@ pub(crate) mod tests {
let _: Blog = blogs[0].save_changes(&**conn).unwrap(); let _: Blog = blogs[0].save_changes(&**conn).unwrap();
let ap_repr = blogs[0].to_activity(conn).unwrap(); let ap_repr = blogs[0].to_activity(conn).unwrap();
blogs[0].delete(conn).unwrap(); blogs[0].delete(conn).unwrap();
eprintln!("{:#?}", &ap_repr);
let blog = Blog::from_activity(conn, ap_repr).unwrap(); let blog = Blog::from_activity(conn, ap_repr).unwrap();
assert_eq!(blog.actor_id, blogs[0].actor_id); assert_eq!(blog.actor_id, blogs[0].actor_id);
@ -1008,9 +1013,9 @@ pub(crate) mod tests {
"url": "https://plu.me/bbb.png" "url": "https://plu.me/bbb.png"
}, },
"inbox": "https://plu.me/~/Blog%20Name/inbox", "inbox": "https://plu.me/~/Blog%20Name/inbox",
"name": "Blog name", "name": "Blog Name",
"outbox": "https://plu.me/~/Blog%20Name/outbox", "outbox": "https://plu.me/~/Blog%20Name/outbox",
"preferredUsername": "Blog%20Name", "preferredUsername": "BlogName",
"publicKey": { "publicKey": {
"id": "https://plu.me/~/Blog%20Name/#main-key", "id": "https://plu.me/~/Blog%20Name/#main-key",
"owner": "https://plu.me/~/Blog%20Name/", "owner": "https://plu.me/~/Blog%20Name/",

View File

@ -9,7 +9,7 @@ use crate::{
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl}; use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use plume_common::utils::{iri_percent_encode_seg, md_to_html}; use plume_common::utils::md_to_html;
use std::sync::RwLock; use std::sync::RwLock;
#[derive(Clone, Identifiable, Queryable)] #[derive(Clone, Identifiable, Queryable)]
@ -173,8 +173,8 @@ impl Instance {
"{instance}/{prefix}/{name}/{box_name}", "{instance}/{prefix}/{name}/{box_name}",
instance = self.public_domain, instance = self.public_domain,
prefix = prefix, prefix = prefix,
name = iri_percent_encode_seg(name), name = name,
box_name = iri_percent_encode_seg(box_name) box_name = box_name
)) ))
} }