~vpzom/hitide

a7f69a9142e5944aabf8da369772b1666754b8a2 — Colin Reeder 1 year, 1 month ago c27383b
Add UI for editing instance description
2 files changed, 54 insertions(+), 4 deletions(-)

M res/lang/en.ftl
M src/routes/administration.rs
M res/lang/en.ftl => res/lang/en.ftl +3 -0
@@ 42,6 42,9 @@ community_edit = Customize Community
community_edit_link = Customize
community_flags_link = Flags
community_remote_note = This is a remote community, information on this page may be incomplete.
content_format_html = HTML
content_format_markdown = Markdown
content_format_text = Plain Text
create_invitation = Create Invitation
create_invitation_result = Invitation created. Share this link to invite someone (it can only be used once):
delete = delete

M src/routes/administration.rs => src/routes/administration.rs +51 -4
@@ 2,9 2,10 @@ use super::{
    fetch_base_data, for_client, get_cookie_map_for_headers, get_cookie_map_for_req, html_response,
    res_to_error, CookieMap,
};
use crate::components::{HTPage, MaybeFillOption};
use crate::components::{HTPage, MaybeFillOption, MaybeFillTextArea};
use crate::lang;
use crate::resp_types::RespInstanceInfo;
use std::borrow::Cow;
use std::collections::HashMap;
use std::convert::TryInto;
use std::sync::Arc;


@@ 79,7 80,7 @@ async fn page_administration_edit_inner(
    cookies: &CookieMap<'_>,
    ctx: Arc<crate::RouteContext>,
    display_error: Option<String>,
    prev_values: Option<&HashMap<&str, serde_json::Value>>,
    prev_values: Option<&HashMap<Cow<'_, str>, serde_json::Value>>,
) -> Result<hyper::Response<hyper::Body>, crate::Error> {
    let lang = crate::get_lang_for_headers(headers);



@@ 113,6 114,14 @@ async fn page_administration_edit_inner(

    let signup_allowed_value = Some(crate::bool_as_str(api_res.signup_allowed));

    let (description_content, description_format) = match api_res.description.content_markdown {
        Some(content) => (content, "markdown"),
        None => match api_res.description.content_html {
            Some(content) => (content, "html"),
            None => (api_res.description.content_text.unwrap(), "text"),
        },
    };

    Ok(html_response(render::html! {
        <HTPage base_data={&base_data} lang={&lang} title={&title}>
            <h1>{title.as_ref()}</h1>


@@ 136,6 145,24 @@ async fn page_administration_edit_inner(
                    </select>
                </label>
                <br />
                <label>
                    {lang.tr(&lang::description())}
                    <br />
                    <MaybeFillTextArea values={&prev_values} name={"description"} default_value={Some(&description_content)} />
                    <br />
                    <select name={"description_format"}>
                        <MaybeFillOption value={"text"} values={&prev_values} default_value={Some(description_format)} name={"description_format"}>
                            {lang.tr(&lang::content_format_text())}
                        </MaybeFillOption>
                        <MaybeFillOption value={"markdown"} values={&prev_values} default_value={Some(description_format)} name={"description_format"}>
                            {lang.tr(&lang::content_format_markdown())}
                        </MaybeFillOption>
                        <MaybeFillOption value={"html"} values={&prev_values} default_value={Some(description_format)} name={"description_format"}>
                            {lang.tr(&lang::content_format_html())}
                        </MaybeFillOption>
                    </select>
                </label>
                <br />
                <br />
                <button type={"submit"}>{"Save"}</button>
            </form>


@@ 153,10 180,12 @@ async fn handler_administration_edit_submit(
    let cookies = get_cookie_map_for_headers(&req_parts.headers)?;

    let body = hyper::body::to_bytes(body).await?;
    let mut body: HashMap<&str, serde_json::Value> = serde_urlencoded::from_bytes(&body)?;
    let body_original: HashMap<Cow<'_, str>, serde_json::Value> =
        serde_urlencoded::from_bytes(&body)?;
    let mut body = body_original.clone();

    body.insert(
        "signup_allowed",
        "signup_allowed".into(),
        body.get("signup_allowed")
            .and_then(|x| x.as_str())
            .ok_or(crate::Error::InternalStrStatic(


@@ 165,6 194,24 @@ async fn handler_administration_edit_submit(
            .parse()?,
    );

    if let Some(content) = body.remove("description") {
        let content = content.as_str().ok_or(crate::Error::InternalStrStatic(
            "Failed to extract description in administration edit",
        ))?;

        let format = body.remove("description_format");
        let format = match format.as_ref().and_then(|x| x.as_str()) {
            Some(format) => format,
            None => {
                return Err(crate::Error::InternalStrStatic(
                    "Invalid or missing description format",
                ))
            }
        };

        body.insert(format!("description_{}", format).into(), content.into());
    }

    let api_res = res_to_error(
        ctx.http_client
            .request(for_client(