fix: undo previous, add replace ~ and use uriproxy

This commit is contained in:
əlemi 2024-05-20 06:59:16 +02:00
parent 96a0cf0df3
commit f9e021a37a
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 12 additions and 30 deletions

View file

@ -21,28 +21,23 @@ impl AsRef<str> for UriClass {
/// unpack uri in id if valid, otherwise compose full uri with "{base}/{entity}/{id}" /// unpack uri in id if valid, otherwise compose full uri with "{base}/{entity}/{id}"
pub fn uri(base: &str, entity: UriClass, id: &str) -> String { pub fn uri(base: &str, entity: UriClass, id: &str) -> String {
if let Some(bare_id) = get_nth_uri_element(id) { if id.starts_with('~') { // ready-to-use base64-encoded id
if bare_id.starts_with('~') { if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(id) {
if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(bare_id.replacen('~', "", 1)) { if let Ok(uri) = std::str::from_utf8(&bytes) {
if let Ok(uri) = std::str::from_utf8(&bytes) { return uri.to_string();
return uri.to_string();
}
} }
} }
} }
format!("{}/{}/{}", base, entity.as_ref(), id) format!("{}/{}/{}", base, entity.as_ref(), id)
} }
fn get_nth_uri_element(uri: &str) -> Option<String> {
uri // https://example.org/users/test/followers/page?offset=42
.split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ]
.nth(4) // 'test'
.map(|x| x.to_string())
}
/// decompose local id constructed by uri() fn /// decompose local id constructed by uri() fn
pub fn decompose_id(full_id: &str) -> String { pub fn decompose_id(full_id: &str) -> String {
get_nth_uri_element(full_id).unwrap_or_default() full_id // https://example.org/users/test/followers/page?offset=42
.split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ]
.nth(4) // 'test'
.unwrap_or("")
.to_string()
} }
/// encode with base64 remote url and prefix it with ~ /// encode with base64 remote url and prefix it with ~

View file

@ -10,15 +10,8 @@ use apb::{Base, Object};
pub fn ObjectPage(tl: Timeline) -> impl IntoView { pub fn ObjectPage(tl: Timeline) -> impl IntoView {
let params = use_params_map(); let params = use_params_map();
let auth = use_context::<Auth>().expect("missing auth context"); let auth = use_context::<Auth>().expect("missing auth context");
let mut uid = params.get().get("id") let id = params.get().get("id").cloned().unwrap_or_default();
.cloned() let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::Object, &id);
.unwrap_or_default()
.replace("/web/objects/", "")
.replacen('+', "https://", 1)
.replace('@', "/");
if !uid.starts_with("http") {
uid = format!("{URL_BASE}/web/objects/{uid}");
}
let object = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |oid| { let object = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |oid| {
async move { async move {
match CACHE.get(&Uri::full(U::Object, &oid)) { match CACHE.get(&Uri::full(U::Object, &oid)) {

View file

@ -27,13 +27,7 @@ pub fn UserPage(tl: Timeline) -> impl IntoView {
.get("id") .get("id")
.cloned() .cloned()
.unwrap_or_default(); .unwrap_or_default();
let mut uid = id let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::User, &id);
.replace("/web/objects/", "")
.replacen('+', "https://", 1)
.replace('@', "/");
if !uid.starts_with("http") {
uid = format!("{URL_BASE}/web/objects/{uid}");
}
let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |id| { let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |id| {
async move { async move {
match CACHE.get(&Uri::full(U::User, &id)) { match CACHE.get(&Uri::full(U::User, &id)) {