~vpzom/hitide

7ca10ced7b48ecca3ecd645f7b6512e43d71dc81 — Colin Reeder 8 months ago 50e529d
Handle different actor types for lookup
1 files changed, 27 insertions(+), 7 deletions(-)

M src/routes/mod.rs
M src/routes/mod.rs => src/routes/mod.rs +27 -7
@@ 721,8 721,19 @@ async fn page_lookup(
    let query = query.query;

    #[derive(Deserialize)]
    #[serde(rename_all = "snake_case")]
    enum ActorType {
        Community,
        User,
        #[serde(other)]
        Unknown,
    }

    #[derive(Deserialize)]
    struct LookupResult {
        id: i64,
        #[serde(rename = "type")]
        kind: ActorType,
    }

    let api_res: Option<Result<Vec<LookupResult>, String>> = if let Some(query) = &query {


@@ 755,13 766,22 @@ async fn page_lookup(
    };

    match api_res {
        Some(Ok(items)) if !items.is_empty() => Ok(hyper::Response::builder()
            .status(hyper::StatusCode::FOUND)
            .header(
                hyper::header::LOCATION,
                format!("/communities/{}", items[0].id),
            )
            .body("Redirecting…".into())?),
        Some(Ok(items)) if !items.is_empty() => {
            let item = &items[0];
            let dest = match item.kind {
                ActorType::Community => format!("/communities/{}", item.id),
                ActorType::User => format!("/users/{}", item.id),
                ActorType::Unknown => {
                    return Err(crate::Error::InternalStr(
                        "Unknown actor type received from lookup".to_owned(),
                    ));
                }
            };
            Ok(hyper::Response::builder()
                .status(hyper::StatusCode::FOUND)
                .header(hyper::header::LOCATION, dest)
                .body("Redirecting…".into())?)
        }
        api_res => {
            let title = lang.tr("lookup_title", None);
            Ok(html_response(render::html! {