From f9e021a37a95ee1d90c660d79d57fb0276ace83b Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 20 May 2024 06:59:16 +0200 Subject: [PATCH] fix: undo previous, add replace ~ and use uriproxy --- uriproxy/src/lib.rs | 23 +++++++++-------------- web/src/page/object.rs | 11 ++--------- web/src/page/user.rs | 8 +------- 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/uriproxy/src/lib.rs b/uriproxy/src/lib.rs index 13f9ae9..41dc53f 100644 --- a/uriproxy/src/lib.rs +++ b/uriproxy/src/lib.rs @@ -21,28 +21,23 @@ impl AsRef for UriClass { /// unpack uri in id if valid, otherwise compose full uri with "{base}/{entity}/{id}" pub fn uri(base: &str, entity: UriClass, id: &str) -> String { - if let Some(bare_id) = get_nth_uri_element(id) { - if bare_id.starts_with('~') { - if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(bare_id.replacen('~', "", 1)) { - if let Ok(uri) = std::str::from_utf8(&bytes) { - return uri.to_string(); - } + if id.starts_with('~') { // ready-to-use base64-encoded id + if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(id) { + if let Ok(uri) = std::str::from_utf8(&bytes) { + return uri.to_string(); } } } format!("{}/{}/{}", base, entity.as_ref(), id) } -fn get_nth_uri_element(uri: &str) -> Option { - 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 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 ~ diff --git a/web/src/page/object.rs b/web/src/page/object.rs index 1ccc1f2..5d7bffc 100644 --- a/web/src/page/object.rs +++ b/web/src/page/object.rs @@ -10,15 +10,8 @@ use apb::{Base, Object}; pub fn ObjectPage(tl: Timeline) -> impl IntoView { let params = use_params_map(); let auth = use_context::().expect("missing auth context"); - let mut uid = params.get().get("id") - .cloned() - .unwrap_or_default() - .replace("/web/objects/", "") - .replacen('+', "https://", 1) - .replace('@', "/"); - if !uid.starts_with("http") { - uid = format!("{URL_BASE}/web/objects/{uid}"); - } + let id = params.get().get("id").cloned().unwrap_or_default(); + let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::Object, &id); let object = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |oid| { async move { match CACHE.get(&Uri::full(U::Object, &oid)) { diff --git a/web/src/page/user.rs b/web/src/page/user.rs index f84460a..9079dff 100644 --- a/web/src/page/user.rs +++ b/web/src/page/user.rs @@ -27,13 +27,7 @@ pub fn UserPage(tl: Timeline) -> impl IntoView { .get("id") .cloned() .unwrap_or_default(); - let mut uid = id - .replace("/web/objects/", "") - .replacen('+', "https://", 1) - .replace('@', "/"); - if !uid.starts_with("http") { - uid = format!("{URL_BASE}/web/objects/{uid}"); - } + let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::User, &id); let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), move |id| { async move { match CACHE.get(&Uri::full(U::User, &id)) {