Merge pull request 'Post ap_url as valid IRI' (#947) from post-slug into main

Reviewed-on: https://git.joinplu.me/Plume/Plume/pulls/947
This commit is contained in:
KitaitiMakoto 2021-09-07 05:36:00 +00:00
commit 188d4ac063
2 changed files with 74 additions and 2 deletions

View File

@ -27,6 +27,59 @@ pub fn make_actor_id(name: &str) -> String {
.collect() .collect()
} }
/**
* Percent-encode characters which are not allowed in IRI path segments.
*
* Intended to be used for generating Post ap_url.
*/
pub fn iri_percent_encode_seg(segment: &str) -> String {
segment.chars().map(iri_percent_encode_seg_char).collect()
}
pub fn iri_percent_encode_seg_char(c: char) -> String {
if c.is_alphanumeric() {
c.to_string()
} else {
match c {
'-'
| '.'
| '_'
| '~'
| '\u{A0}'..='\u{D7FF}'
| '\u{20000}'..='\u{2FFFD}'
| '\u{30000}'..='\u{3FFFD}'
| '\u{40000}'..='\u{4FFFD}'
| '\u{50000}'..='\u{5FFFD}'
| '\u{60000}'..='\u{6FFFD}'
| '\u{70000}'..='\u{7FFFD}'
| '\u{80000}'..='\u{8FFFD}'
| '\u{90000}'..='\u{9FFFD}'
| '\u{A0000}'..='\u{AFFFD}'
| '\u{B0000}'..='\u{BFFFD}'
| '\u{C0000}'..='\u{CFFFD}'
| '\u{D0000}'..='\u{DFFFD}'
| '\u{E0000}'..='\u{EFFFD}'
| '!'
| '$'
| '&'
| '\''
| '('
| ')'
| '*'
| '+'
| ','
| ';'
| '='
| ':'
| '@' => c.to_string(),
_ => {
let s = c.to_string();
Uri::percent_encode(&s).to_string()
}
}
}
}
/** /**
* Redirects to the login page with a given message. * Redirects to the login page with a given message.
* *
@ -476,6 +529,20 @@ mod tests {
} }
} }
#[test]
fn test_iri_percent_encode_seg() {
assert_eq!(
&iri_percent_encode_seg("including whitespace"),
"including%20whitespace"
);
assert_eq!(&iri_percent_encode_seg("%20"), "%2520");
assert_eq!(&iri_percent_encode_seg("é"), "é");
assert_eq!(
&iri_percent_encode_seg("空白入り 日本語"),
"空白入り%20日本語"
);
}
#[test] #[test]
fn test_inline() { fn test_inline() {
assert_eq!( assert_eq!(

View File

@ -17,7 +17,7 @@ use plume_common::{
inbox::{AsActor, AsObject, FromId}, inbox::{AsActor, AsObject, FromId},
Hashtag, Id, IntoId, Licensed, Source, PUBLIC_VISIBILITY, Hashtag, Id, IntoId, Licensed, Source, PUBLIC_VISIBILITY,
}, },
utils::md_to_html, utils::{iri_percent_encode_seg, md_to_html},
}; };
use riker::actors::{Publish, Tell}; use riker::actors::{Publish, Tell};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -249,7 +249,12 @@ impl Post {
} }
pub fn ap_url(blog: Blog, slug: &str) -> String { pub fn ap_url(blog: Blog, slug: &str) -> String {
ap_url(&format!("{}/~/{}/{}/", CONFIG.base_url, blog.fqn, slug)) ap_url(&format!(
"{}/~/{}/{}/",
CONFIG.base_url,
blog.fqn,
iri_percent_encode_seg(slug)
))
} }
// It's better to calc slug in insert and update // It's better to calc slug in insert and update