| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
use makeover_layout as layout; |
| 51 |
use quasi_declare::declare; |
| 52 |
use quasi_router::{Document, Request, Response, RouteError}; |
| 53 |
use quasi_webview::Webview; |
| 54 |
|
| 55 |
|
| 56 |
pub const PATH: &str = "/dashboard/export"; |
| 57 |
|
| 58 |
|
| 59 |
pub const PAGE_REGION: &str = "export"; |
| 60 |
|
| 61 |
|
| 62 |
const CARDS_REGION: &str = "export-cards"; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub const CONTENT_STATUS: &str = "content-status"; |
| 69 |
|
| 70 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 71 |
|
| 72 |
|
| 73 |
pub struct Page { |
| 74 |
|
| 75 |
pub has_content: bool, |
| 76 |
|
| 77 |
pub content_size: String, |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
pub async fn load(db: &sqlx::PgPool, user: crate::db::UserId) -> crate::error::Result<Page> { |
| 83 |
let items = crate::db::items::get_items_by_user(db, user).await?; |
| 84 |
let has_item_content = items.iter().any(crate::db::DbItem::has_s3_content); |
| 85 |
|
| 86 |
let known_size = crate::db::creator_tiers::get_user_content_size(db, user).await?; |
| 87 |
let has_content = has_item_content || known_size > 0; |
| 88 |
|
| 89 |
let content_size = if !has_content { |
| 90 |
"No files".to_string() |
| 91 |
} else if has_item_content && known_size > 0 { |
| 92 |
format!( |
| 93 |
"{} + audio/cover files", |
| 94 |
crate::helpers::format_file_size(known_size) |
| 95 |
) |
| 96 |
} else if known_size > 0 { |
| 97 |
crate::helpers::format_file_size(known_size) |
| 98 |
} else { |
| 99 |
"Audio/cover files".to_string() |
| 100 |
}; |
| 101 |
|
| 102 |
Ok(Page { |
| 103 |
has_content, |
| 104 |
content_size, |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
pub(crate) fn reading(viewer: &super::Viewer) -> Result<Page, RouteError> { |
| 110 |
viewer |
| 111 |
.block_on(load(&viewer.app.db, viewer.reader()?.id)) |
| 112 |
.map_err(|_| RouteError::internal("your exports could not be read")) |
| 113 |
} |
| 114 |
|
| 115 |
pub fn screen(viewer: &super::Viewer, _request: Request) -> Result<Response, RouteError> { |
| 116 |
Ok(page_screen(&reading(viewer)?).into()) |
| 117 |
} |
| 118 |
|
| 119 |
declare! { |
| 120 |
|
| 121 |
pub(crate) shape page_screen(page: &Page) -> Screen; |
| 122 |
|
| 123 |
screen single "Export Your Data - Makenotwork" { |
| 124 |
measured MEASURE; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
documented Document::default().classed(crate::shell::body_class(MEASURE, &["export-page"])); |
| 130 |
summarised "Download your content, projects, and transaction history."; |
| 131 |
|
| 132 |
include page_region(page); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
declare! { |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
#[staged] |
| 142 |
pub(crate) shape page_region(page: &Page) -> Slot; |
| 143 |
|
| 144 |
region PAGE_REGION as Pane { |
| 145 |
link "Back to Dashboard" to get "/dashboard" navigating; |
| 146 |
page "Export Your Data"; |
| 147 |
text "Download your content, projects, and transaction history."; |
| 148 |
include cards(page); |
| 149 |
section "About Your Data"; |
| 150 |
text "Your data belongs to you. These exports contain everything we store \ |
| 151 |
about your account and content. If you're planning to delete your \ |
| 152 |
account, we recommend downloading your data first."; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
declare! { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#[staged] |
| 165 |
shape cards(page: &Page) -> Slot; |
| 166 |
|
| 167 |
region CARDS_REGION as Group { |
| 168 |
list { |
| 169 |
for export in copy "content/export-portal.toml" as direct { |
| 170 |
row export.title { |
| 171 |
secondary export.description; |
| 172 |
meta export.meta; |
| 173 |
include super::export_act::control("Download", export.route, export.filename); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
include content_row(&page.content_size) when page.has_content; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
region CONTENT_STATUS as Group when page.has_content {} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
declare! { |
| 187 |
|
| 188 |
#[staged] |
| 189 |
shape content_row(size: &str) -> Row; |
| 190 |
|
| 191 |
row "Content Files" { |
| 192 |
secondary "All your uploaded audio files, cover images, version downloads, and dynamic clips."; |
| 193 |
meta "ZIP archive ({size})"; |
| 194 |
act "Download" to post "/api/export/content" replacing CONTENT_STATUS awaiting; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#[must_use] |
| 199 |
pub fn renderer(viewer: &super::Viewer) -> Webview { |
| 200 |
Webview::new().with_shell(viewer.document_shell().with_body_first(format!( |
| 201 |
"{}{}", |
| 202 |
crate::shell::skip_link(PAGE_REGION), |
| 203 |
crate::shell::site_header(viewer.user.as_ref()), |
| 204 |
))) |
| 205 |
} |
| 206 |
|
| 207 |
#[cfg(test)] |
| 208 |
mod tests { |
| 209 |
use super::*; |
| 210 |
|
| 211 |
fn page(has_content: bool) -> Page { |
| 212 |
Page { |
| 213 |
has_content, |
| 214 |
content_size: "12.3 MB".to_string(), |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
fn html(has_content: bool) -> String { |
| 219 |
use quasi_axum::Serves as _; |
| 220 |
|
| 221 |
Webview::new().screen(&page_screen(&page(has_content))) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
#[test] |
| 227 |
fn the_document_carries_the_class_the_template_carried() { |
| 228 |
let screen = page_screen(&page(true)); |
| 229 |
|
| 230 |
assert_eq!( |
| 231 |
screen.document.body_class.as_deref(), |
| 232 |
Some("padded-page export-page") |
| 233 |
); |
| 234 |
assert!( |
| 235 |
html(true).contains("padded-page export-page"), |
| 236 |
"{}", |
| 237 |
html(true) |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
fn direct() -> Vec<toml::Table> { |
| 248 |
let copy: toml::Table = include_str!("../../content/export-portal.toml") |
| 249 |
.parse() |
| 250 |
.expect("the export copy is TOML"); |
| 251 |
|
| 252 |
copy["direct"] |
| 253 |
.as_array() |
| 254 |
.expect("a list of direct exports") |
| 255 |
.iter() |
| 256 |
.map(|export| export.as_table().expect("a table").clone()) |
| 257 |
.collect() |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
fn says(export: &toml::Table, key: &str) -> String { |
| 262 |
export[key].as_str().expect("a string").to_owned() |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#[test] |
| 268 |
fn every_direct_export_names_its_route_and_its_filename() { |
| 269 |
let html = html(true); |
| 270 |
|
| 271 |
assert_eq!(direct().len(), 5, "five exports answer with a file"); |
| 272 |
for export in direct() { |
| 273 |
let route = says(&export, "route"); |
| 274 |
let filename = says(&export, "filename"); |
| 275 |
|
| 276 |
assert!( |
| 277 |
html.contains(&format!(r#"hx-post="{route}""#)), |
| 278 |
"{route} missing from {html}" |
| 279 |
); |
| 280 |
assert!( |
| 281 |
html.contains(&format!(r#"data-saves="{filename}""#)), |
| 282 |
"{filename} missing from {html}" |
| 283 |
); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#[test] |
| 290 |
fn the_saved_filenames_are_the_ones_the_endpoints_set() { |
| 291 |
let api = include_str!("../routes/api/exports/mod.rs"); |
| 292 |
|
| 293 |
for export in direct() { |
| 294 |
let filename = says(&export, "filename"); |
| 295 |
assert!( |
| 296 |
api.contains(&filename), |
| 297 |
"{filename} is not a filename `routes::api::exports` sets" |
| 298 |
); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#[test] |
| 305 |
fn the_content_export_fills_a_region_rather_than_saving_a_file() { |
| 306 |
let html = html(true); |
| 307 |
|
| 308 |
assert!(html.contains(r#"hx-post="/api/export/content""#), "{html}"); |
| 309 |
assert!(html.contains(CONTENT_STATUS), "{html}"); |
| 310 |
assert!( |
| 311 |
!html.contains(r#"data-saves="makenot-work-content"#), |
| 312 |
"the content export is queued and mailed, so it saves nothing: {html}" |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
#[test] |
| 319 |
fn a_reader_with_no_files_is_offered_no_content_export() { |
| 320 |
let html = html(false); |
| 321 |
|
| 322 |
assert!(!html.contains("/api/export/content"), "{html}"); |
| 323 |
assert!(!html.contains("Content Files"), "{html}"); |
| 324 |
assert!(!html.contains(CONTENT_STATUS), "{html}"); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
#[test] |
| 330 |
fn no_card_spells_a_spinner() { |
| 331 |
let html = html(true); |
| 332 |
|
| 333 |
assert!(html.contains("data-awaiting="), "{html}"); |
| 334 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 335 |
assert!(!html.contains(spelling), "{spelling} survives in {html}"); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|