| 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 |
use quasi_declare::declare; |
| 35 |
use quasi_router::screen::Tag; |
| 36 |
use quasi_router::{Request, Response, RouteError}; |
| 37 |
use quasi_webview::Webview; |
| 38 |
|
| 39 |
use super::Viewer; |
| 40 |
|
| 41 |
|
| 42 |
pub const LIBRARY_SCREEN: &str = "library_communities"; |
| 43 |
|
| 44 |
|
| 45 |
pub const LIBRARY_PATH: &str = "/library/tabs/communities"; |
| 46 |
|
| 47 |
|
| 48 |
pub const LIBRARY_REGION: &str = "library-communities"; |
| 49 |
|
| 50 |
|
| 51 |
pub const SETTINGS_SCREEN: &str = "user_forums"; |
| 52 |
|
| 53 |
|
| 54 |
pub const SETTINGS_PATH: &str = "/dashboard/tabs/forums"; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub const SETTINGS_REGION: &str = "settings-forums"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
const UPSTREAM_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); |
| 66 |
|
| 67 |
|
| 68 |
pub(crate) struct MembershipView { |
| 69 |
community: String, |
| 70 |
profile_url: String, |
| 71 |
role: String, |
| 72 |
posts: String, |
| 73 |
joined: String, |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pub fn library_screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 82 |
let Some(base) = configured_base(viewer) else { |
| 83 |
return Ok(Response::fragment(LIBRARY_REGION, library_pane(&[], ""))); |
| 84 |
}; |
| 85 |
let memberships = fetch(viewer, viewer.reader()?, &base); |
| 86 |
Ok(Response::fragment( |
| 87 |
LIBRARY_REGION, |
| 88 |
library_pane(&memberships, &base), |
| 89 |
)) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub fn settings_screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 99 |
let base = configured_base(viewer) |
| 100 |
.ok_or_else(|| RouteError::not_found("forums are not configured"))?; |
| 101 |
let memberships = fetch(viewer, viewer.reader()?, &base); |
| 102 |
Ok(Response::fragment( |
| 103 |
SETTINGS_REGION, |
| 104 |
settings_pane(&memberships, &base), |
| 105 |
)) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
pub(crate) fn reading(viewer: &Viewer) -> Result<(Vec<MembershipView>, String), RouteError> { |
| 114 |
let Some(base) = configured_base(viewer) else { |
| 115 |
return Ok((Vec::new(), String::new())); |
| 116 |
}; |
| 117 |
let memberships = fetch(viewer, viewer.reader()?, &base); |
| 118 |
Ok((memberships, base)) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pub(crate) fn settings_reading( |
| 124 |
viewer: &Viewer, |
| 125 |
) -> Result<(Vec<MembershipView>, String), RouteError> { |
| 126 |
let base = configured_base(viewer) |
| 127 |
.ok_or_else(|| RouteError::not_found("forums are not configured"))?; |
| 128 |
let memberships = fetch(viewer, viewer.reader()?, &base); |
| 129 |
Ok((memberships, base)) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
fn configured_base(viewer: &Viewer) -> Option<String> { |
| 134 |
viewer.app.config.integrations.mt_base_url.clone() |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
fn fetch(viewer: &Viewer, reader: &crate::auth::SessionUser, base: &str) -> Vec<MembershipView> { |
| 144 |
let url = format!("{base}/api/user/{}/summary", reader.id); |
| 145 |
let username = reader.username.as_ref(); |
| 146 |
|
| 147 |
|
| 148 |
let body = viewer.block_on(async { |
| 149 |
let response = crate::helpers::HTTP_CLIENT |
| 150 |
.get(&url) |
| 151 |
.timeout(UPSTREAM_TIMEOUT) |
| 152 |
.send() |
| 153 |
.await |
| 154 |
.inspect_err(|error| tracing::warn!(?error, "failed to fetch MT user summary")) |
| 155 |
.ok()?; |
| 156 |
if !response.status().is_success() { |
| 157 |
return None; |
| 158 |
} |
| 159 |
response |
| 160 |
.json::<serde_json::Value>() |
| 161 |
.await |
| 162 |
.inspect_err(|error| tracing::warn!(?error, "failed to parse MT summary response")) |
| 163 |
.ok() |
| 164 |
}); |
| 165 |
|
| 166 |
let Some(body) = body else { |
| 167 |
return Vec::new(); |
| 168 |
}; |
| 169 |
body["memberships"] |
| 170 |
.as_array() |
| 171 |
.map(|entries| { |
| 172 |
entries |
| 173 |
.iter() |
| 174 |
.filter_map(|entry| { |
| 175 |
let slug = entry["community_slug"].as_str()?; |
| 176 |
Some(MembershipView { |
| 177 |
community: entry["community_name"].as_str()?.to_owned(), |
| 178 |
profile_url: format!("{base}/p/{slug}/u/{username}"), |
| 179 |
role: entry["role"].as_str()?.to_owned(), |
| 180 |
posts: entry["post_count"].as_i64().unwrap_or(0).to_string(), |
| 181 |
joined: entry["joined_at"] |
| 182 |
.as_str() |
| 183 |
.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) |
| 184 |
.map(|at| at.format("%b %d, %Y").to_string()) |
| 185 |
.unwrap_or_default(), |
| 186 |
}) |
| 187 |
}) |
| 188 |
.collect() |
| 189 |
}) |
| 190 |
.unwrap_or_default() |
| 191 |
} |
| 192 |
|
| 193 |
declare! { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#[staged] |
| 208 |
shape upstream_line(base: &str) -> Node; |
| 209 |
|
| 210 |
rich "Your memberships across [Multithreaded]({base}) forum communities." { |
| 211 |
trust quasi_router::Trust::Trusted; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
declare! { |
| 216 |
|
| 217 |
#[staged] |
| 218 |
pub(crate) shape library_pane(memberships: &[MembershipView], base: &str) -> Node; |
| 219 |
|
| 220 |
region LIBRARY_REGION as Pane { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
empty "You haven't joined any forum communities yet." |
| 225 |
when memberships.is_empty() |
| 226 |
{ |
| 227 |
offering "Browse Communities" to external base unless base.is_empty(); |
| 228 |
} |
| 229 |
|
| 230 |
include upstream_line(base) unless memberships.is_empty(); |
| 231 |
include table(memberships) unless memberships.is_empty(); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
declare! { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
#[staged] |
| 241 |
pub(crate) shape settings_pane(memberships: &[MembershipView], base: &str) -> Node; |
| 242 |
|
| 243 |
region SETTINGS_REGION as Pane { |
| 244 |
section "Forum Communities"; |
| 245 |
include upstream_line(base); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
empty "You haven't joined any forum communities yet." when memberships.is_empty(); |
| 252 |
include table(memberships) unless memberships.is_empty(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
declare! { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
#[staged] |
| 264 |
shape table(memberships: &[MembershipView]) -> Node; |
| 265 |
|
| 266 |
table { |
| 267 |
column "Community" { |
| 268 |
width Fill; |
| 269 |
priority Essential; |
| 270 |
} |
| 271 |
column "Role" { |
| 272 |
width Content; |
| 273 |
} |
| 274 |
column "Posts" { |
| 275 |
width Content; |
| 276 |
} |
| 277 |
column "Joined" { |
| 278 |
width Content; |
| 279 |
priority Optional; |
| 280 |
} |
| 281 |
|
| 282 |
for membership in memberships.iter() { |
| 283 |
cells { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
cell membership.community.clone() { |
| 289 |
activate to external membership.profile_url.clone(); |
| 290 |
} |
| 291 |
cell "" { |
| 292 |
token Tag::badge(membership.role.clone()); |
| 293 |
} |
| 294 |
cell membership.posts.clone(); |
| 295 |
cell membership.joined.clone(); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 303 |
Webview::new().with_shell(viewer.shell()) |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#[cfg(test)] |
| 311 |
pub(crate) fn sample(community: &str, role: &str) -> MembershipView { |
| 312 |
MembershipView { |
| 313 |
community: community.into(), |
| 314 |
profile_url: format!("https://mt.example.com/p/{community}/u/max"), |
| 315 |
role: role.into(), |
| 316 |
posts: "12".into(), |
| 317 |
joined: "Aug 10, 2026".into(), |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
#[cfg(test)] |
| 322 |
mod tests { |
| 323 |
use quasi_axum::Serves; |
| 324 |
use quasi_router::Node; |
| 325 |
|
| 326 |
use super::*; |
| 327 |
|
| 328 |
use super::sample as membership; |
| 329 |
|
| 330 |
fn render(node: &Node) -> String { |
| 331 |
Webview::new().fragment(node) |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn each_screen_matches_the_nav_that_targets_it() { |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
let library = crate::quasi::library_tabs::html("", true, true); |
| 342 |
assert!( |
| 343 |
library.contains(&format!("hx-get=\"{LIBRARY_PATH}\"")), |
| 344 |
"{library}" |
| 345 |
); |
| 346 |
assert!( |
| 347 |
library.contains(&format!("id=\"{LIBRARY_REGION}\"")), |
| 348 |
"{library}" |
| 349 |
); |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
let settings = crate::quasi::settings_tabs::html( |
| 354 |
0, |
| 355 |
"", |
| 356 |
crate::quasi::settings_tabs::Gates { |
| 357 |
has_media: true, |
| 358 |
git_enabled: true, |
| 359 |
has_mt_memberships: true, |
| 360 |
has_sync_apps: true, |
| 361 |
}, |
| 362 |
); |
| 363 |
assert!( |
| 364 |
settings.contains(&format!("hx-get=\"{SETTINGS_PATH}\"")), |
| 365 |
"{settings}" |
| 366 |
); |
| 367 |
assert!( |
| 368 |
settings.contains(&format!("id=\"{SETTINGS_REGION}\"")), |
| 369 |
"{settings}" |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
#[test] |
| 377 |
fn every_column_the_table_had_is_still_named() { |
| 378 |
let html = render(&table(&[membership("rust", "Moderator")])); |
| 379 |
|
| 380 |
for heading in ["Community", "Role", "Posts", "Joined"] { |
| 381 |
assert!(html.contains(heading), "{heading} is gone from {html}"); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn one_table_serves_both_screens() { |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
let rows = [membership("rust", "Moderator")]; |
| 391 |
let library = render(&library_pane(&rows, "https://mt.example.com")); |
| 392 |
let settings = render(&settings_pane(&rows, "https://mt.example.com")); |
| 393 |
|
| 394 |
for fragment in ["rust", "Moderator", "12", "Aug 10, 2026"] { |
| 395 |
assert!( |
| 396 |
library.contains(fragment), |
| 397 |
"{fragment} in library: {library}" |
| 398 |
); |
| 399 |
assert!( |
| 400 |
settings.contains(fragment), |
| 401 |
"{fragment} in settings: {settings}" |
| 402 |
); |
| 403 |
} |
| 404 |
|
| 405 |
assert!(settings.contains("Forum Communities")); |
| 406 |
assert!(!library.contains("Forum Communities")); |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn a_community_name_leaves_for_multithreaded() { |
| 411 |
let html = render(&table(&[membership("rust", "Member")])); |
| 412 |
|
| 413 |
assert!( |
| 414 |
html.contains("href=\"https://mt.example.com/p/rust/u/max\""), |
| 415 |
"{html}" |
| 416 |
); |
| 417 |
|
| 418 |
|
| 419 |
assert!(html.contains("rel=\"noopener noreferrer\""), "{html}"); |
| 420 |
assert!(!html.contains("hx-get"), "nothing swaps: {html}"); |
| 421 |
} |
| 422 |
|
| 423 |
#[test] |
| 424 |
fn the_upstream_line_keeps_its_link() { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
let html = render(&upstream_line("https://mt.example.com")); |
| 429 |
assert!(html.contains("href=\"https://mt.example.com\""), "{html}"); |
| 430 |
assert!(html.contains("Multithreaded"), "{html}"); |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn an_empty_library_offers_a_way_out_only_when_there_is_one() { |
| 435 |
let configured = render(&library_pane(&[], "https://mt.example.com")); |
| 436 |
|
| 437 |
|
| 438 |
assert!(configured.contains("joined any forum communities yet.")); |
| 439 |
assert!(configured.contains("Browse Communities"), "{configured}"); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
assert!(!configured.contains("data-tone"), "{configured}"); |
| 445 |
assert!(!configured.contains("hx-confirm"), "{configured}"); |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
let bare = render(&library_pane(&[], "")); |
| 450 |
assert!(bare.contains("joined any forum communities yet.")); |
| 451 |
assert!(!bare.contains("Browse Communities"), "{bare}"); |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
fn an_empty_settings_section_keeps_its_heading() { |
| 456 |
|
| 457 |
|
| 458 |
let html = render(&settings_pane(&[], "https://mt.example.com")); |
| 459 |
assert!(html.contains("Forum Communities")); |
| 460 |
assert!(html.contains("joined any forum communities yet.")); |
| 461 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn a_community_name_cannot_smuggle_markup() { |
| 466 |
|
| 467 |
|
| 468 |
let html = render(&table(&[membership("<script>x()</script>", "Member")])); |
| 469 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 470 |
} |
| 471 |
} |
| 472 |
|