forked from
smokesignal.events/smokesignal
i18n+filtering fork - fluent-templates v2
1use anyhow::Result;
2use axum::{
3 extract::{Path, Query},
4 response::{IntoResponse, Redirect},
5};
6use axum_htmx::{HxRedirect, HxRequest};
7use http::StatusCode;
8use minijinja::context as template_context;
9
10use crate::{
11 contextual_error, create_renderer,
12 http::{
13 context::AdminRequestContext,
14 errors::{WebError, CommonError},
15 pagination::{Pagination, PaginationView},
16 },
17 storage::handle::{handle_list, handle_nuke},
18};
19
20pub async fn handle_admin_handles(
21 admin_ctx: AdminRequestContext,
22 pagination: Query<Pagination>,
23) -> Result<impl IntoResponse, WebError> {
24 let canonical_url = format!(
25 "https://{}/admin/handles",
26 admin_ctx.web_context.config.external_base
27 );
28
29 // Create the template renderer with enhanced context
30 let renderer = create_renderer!(admin_ctx.web_context.clone(), admin_ctx.language, false, false);
31
32 let (page, page_size) = pagination.admin_clamped();
33
34 let handles = handle_list(&admin_ctx.web_context.pool, page, page_size).await;
35 if let Err(err) = handles {
36 return contextual_error!(renderer: renderer, err, template_context!{});
37 }
38 let (total_count, mut handles) = handles.unwrap();
39
40 let params: Vec<(&str, &str)> = vec![];
41
42 let pagination_view = PaginationView::new(page_size, handles.len() as i64, page, params);
43
44 if handles.len() > page_size as usize {
45 handles.truncate(page_size as usize);
46 }
47
48 Ok(renderer.render_template(
49 "admin_handles",
50 template_context! {
51 handles,
52 total_count,
53 pagination => pagination_view,
54 },
55 Some(&admin_ctx.admin_handle),
56 &canonical_url,
57 ))
58}
59
60pub async fn handle_admin_nuke_identity(
61 admin_ctx: AdminRequestContext,
62 HxRequest(hx_request): HxRequest,
63 Path(did): Path<String>,
64) -> Result<impl IntoResponse, WebError> {
65 let renderer = create_renderer!(admin_ctx.web_context.clone(), admin_ctx.language, false, false);
66
67 if did == admin_ctx.admin_handle.did {
68 return contextual_error!(
69 renderer: renderer,
70 CommonError::NotAuthorized,
71 template_context!{}
72 );
73 }
74 if let Err(err) = handle_nuke(
75 &admin_ctx.web_context.pool,
76 &did,
77 &admin_ctx.admin_handle.did,
78 )
79 .await
80 {
81 return contextual_error!(
82 renderer: renderer,
83 err,
84 template_context! {}
85 );
86 }
87
88 if hx_request {
89 let hx_redirect = HxRedirect::try_from("/admin/handles");
90 if let Err(err) = hx_redirect {
91 return contextual_error!(
92 renderer: renderer,
93 err,
94 template_context! {}
95 );
96 }
97 let hx_redirect = hx_redirect.unwrap();
98 Ok((StatusCode::OK, hx_redirect, "").into_response())
99 } else {
100 Ok(Redirect::to("/admin/handles").into_response())
101 }
102}