| 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 makeover_layout as layout; |
| 35 |
use quasi_router::screen::{Act, Cell, Cells, Column, Tag}; |
| 36 |
use quasi_router::{Action, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 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 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 |
fn configured_base(viewer: &Viewer) -> Option<String> { |
| 110 |
viewer.app.config.integrations.mt_base_url.clone() |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
fn fetch(viewer: &Viewer, reader: &crate::auth::SessionUser, base: &str) -> Vec<MembershipView> { |
| 120 |
let url = format!("{base}/api/user/{}/summary", reader.id); |
| 121 |
let username = reader.username.as_ref(); |
| 122 |
|
| 123 |
|
| 124 |
let body = viewer.block_on(async { |
| 125 |
let response = crate::helpers::HTTP_CLIENT |
| 126 |
.get(&url) |
| 127 |
.timeout(UPSTREAM_TIMEOUT) |
| 128 |
.send() |
| 129 |
.await |
| 130 |
.inspect_err(|error| tracing::warn!(?error, "failed to fetch MT user summary")) |
| 131 |
.ok()?; |
| 132 |
if !response.status().is_success() { |
| 133 |
return None; |
| 134 |
} |
| 135 |
response |
| 136 |
.json::<serde_json::Value>() |
| 137 |
.await |
| 138 |
.inspect_err(|error| tracing::warn!(?error, "failed to parse MT summary response")) |
| 139 |
.ok() |
| 140 |
}); |
| 141 |
|
| 142 |
let Some(body) = body else { |
| 143 |
return Vec::new(); |
| 144 |
}; |
| 145 |
body["memberships"] |
| 146 |
.as_array() |
| 147 |
.map(|entries| { |
| 148 |
entries |
| 149 |
.iter() |
| 150 |
.filter_map(|entry| { |
| 151 |
let slug = entry["community_slug"].as_str()?; |
| 152 |
Some(MembershipView { |
| 153 |
community: entry["community_name"].as_str()?.to_owned(), |
| 154 |
profile_url: format!("{base}/p/{slug}/u/{username}"), |
| 155 |
role: entry["role"].as_str()?.to_owned(), |
| 156 |
posts: entry["post_count"].as_i64().unwrap_or(0).to_string(), |
| 157 |
joined: entry["joined_at"] |
| 158 |
.as_str() |
| 159 |
.and_then(|at| chrono::DateTime::parse_from_rfc3339(at).ok()) |
| 160 |
.map(|at| at.format("%b %d, %Y").to_string()) |
| 161 |
.unwrap_or_default(), |
| 162 |
}) |
| 163 |
}) |
| 164 |
.collect() |
| 165 |
}) |
| 166 |
.unwrap_or_default() |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
fn upstream_line(base: &str) -> Node { |
| 175 |
super::own_prose(format!( |
| 176 |
"Your memberships across [Multithreaded]({base}) forum communities." |
| 177 |
)) |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
fn library_pane(memberships: &[MembershipView], base: &str) -> Node { |
| 182 |
let mut slot = Slot::new(LIBRARY_REGION, RegionKind::Pane); |
| 183 |
|
| 184 |
if memberships.is_empty() { |
| 185 |
let mut nothing = Node::empty("You haven't joined any forum communities yet."); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if !base.is_empty() { |
| 190 |
nothing = nothing.offering(Act::new("Browse Communities", Action::external(base))); |
| 191 |
} |
| 192 |
return Node::Region(slot.with(nothing)); |
| 193 |
} |
| 194 |
|
| 195 |
slot = slot.with(upstream_line(base)).with(table(memberships)); |
| 196 |
Node::Region(slot) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
fn settings_pane(memberships: &[MembershipView], base: &str) -> Node { |
| 201 |
let mut slot = Slot::new(SETTINGS_REGION, RegionKind::Pane) |
| 202 |
.with(Node::section("Forum Communities")) |
| 203 |
.with(upstream_line(base)); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
slot = if memberships.is_empty() { |
| 208 |
slot.with(Node::empty("You haven't joined any forum communities yet.")) |
| 209 |
} else { |
| 210 |
slot.with(table(memberships)) |
| 211 |
}; |
| 212 |
|
| 213 |
Node::Region(slot) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
fn table(memberships: &[MembershipView]) -> Node { |
| 218 |
Node::Table { |
| 219 |
columns: vec![ |
| 220 |
Column::new("Community") |
| 221 |
.width(layout::Width::Fill) |
| 222 |
.priority(layout::Priority::Essential), |
| 223 |
Column::new("Role").width(layout::Width::Content), |
| 224 |
Column::new("Posts").width(layout::Width::Content), |
| 225 |
Column::new("Joined") |
| 226 |
.width(layout::Width::Content) |
| 227 |
.priority(layout::Priority::Optional), |
| 228 |
], |
| 229 |
rows: memberships |
| 230 |
.iter() |
| 231 |
.map(|membership| { |
| 232 |
Cells::new([ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
Cell::new(membership.community.clone()) |
| 238 |
.activate(Action::external(membership.profile_url.clone())), |
| 239 |
Cell::tag(Tag::badge(membership.role.clone())), |
| 240 |
Cell::new(membership.posts.clone()), |
| 241 |
Cell::new(membership.joined.clone()), |
| 242 |
]) |
| 243 |
}) |
| 244 |
.collect(), |
| 245 |
|
| 246 |
|
| 247 |
more: None, |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 253 |
Webview::new().with_shell(viewer.shell()) |
| 254 |
} |
| 255 |
|
| 256 |
#[cfg(test)] |
| 257 |
mod tests { |
| 258 |
use super::*; |
| 259 |
use quasi_axum::Serves; |
| 260 |
|
| 261 |
fn membership(community: &str, role: &str) -> MembershipView { |
| 262 |
MembershipView { |
| 263 |
community: community.into(), |
| 264 |
profile_url: format!("https://mt.example.com/p/{community}/u/max"), |
| 265 |
role: role.into(), |
| 266 |
posts: "12".into(), |
| 267 |
joined: "Aug 10, 2026".into(), |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
fn render(node: &Node) -> String { |
| 272 |
Webview::new().fragment(node) |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn each_screen_matches_the_nav_that_targets_it() { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
let library = crate::quasi::library_tabs::html("", true, true); |
| 283 |
assert!( |
| 284 |
library.contains(&format!("hx-get=\"{LIBRARY_PATH}\"")), |
| 285 |
"{library}" |
| 286 |
); |
| 287 |
assert!( |
| 288 |
library.contains(&format!("id=\"{LIBRARY_REGION}\"")), |
| 289 |
"{library}" |
| 290 |
); |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
let settings = crate::quasi::settings_tabs::html( |
| 295 |
0, |
| 296 |
"", |
| 297 |
crate::quasi::settings_tabs::Gates { |
| 298 |
has_media: true, |
| 299 |
git_enabled: true, |
| 300 |
has_mt_memberships: true, |
| 301 |
has_sync_apps: true, |
| 302 |
}, |
| 303 |
); |
| 304 |
assert!( |
| 305 |
settings.contains(&format!("hx-get=\"{SETTINGS_PATH}\"")), |
| 306 |
"{settings}" |
| 307 |
); |
| 308 |
assert!( |
| 309 |
settings.contains(&format!("id=\"{SETTINGS_REGION}\"")), |
| 310 |
"{settings}" |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn one_table_serves_both_screens() { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
let rows = [membership("rust", "Moderator")]; |
| 320 |
let library = render(&library_pane(&rows, "https://mt.example.com")); |
| 321 |
let settings = render(&settings_pane(&rows, "https://mt.example.com")); |
| 322 |
|
| 323 |
for fragment in ["rust", "Moderator", "12", "Aug 10, 2026"] { |
| 324 |
assert!( |
| 325 |
library.contains(fragment), |
| 326 |
"{fragment} in library: {library}" |
| 327 |
); |
| 328 |
assert!( |
| 329 |
settings.contains(fragment), |
| 330 |
"{fragment} in settings: {settings}" |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
assert!(settings.contains("Forum Communities")); |
| 335 |
assert!(!library.contains("Forum Communities")); |
| 336 |
} |
| 337 |
|
| 338 |
#[test] |
| 339 |
fn a_community_name_leaves_for_multithreaded() { |
| 340 |
let html = render(&table(&[membership("rust", "Member")])); |
| 341 |
|
| 342 |
assert!( |
| 343 |
html.contains("href=\"https://mt.example.com/p/rust/u/max\""), |
| 344 |
"{html}" |
| 345 |
); |
| 346 |
|
| 347 |
|
| 348 |
assert!(html.contains("rel=\"noopener noreferrer\""), "{html}"); |
| 349 |
assert!(!html.contains("hx-get"), "nothing swaps: {html}"); |
| 350 |
} |
| 351 |
|
| 352 |
#[test] |
| 353 |
fn the_upstream_line_keeps_its_link() { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
let html = render(&upstream_line("https://mt.example.com")); |
| 358 |
assert!(html.contains("href=\"https://mt.example.com\""), "{html}"); |
| 359 |
assert!(html.contains("Multithreaded"), "{html}"); |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn an_empty_library_offers_a_way_out_only_when_there_is_one() { |
| 364 |
let configured = render(&library_pane(&[], "https://mt.example.com")); |
| 365 |
|
| 366 |
|
| 367 |
assert!(configured.contains("joined any forum communities yet.")); |
| 368 |
assert!(configured.contains("Browse Communities"), "{configured}"); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
assert!(!configured.contains("data-tone"), "{configured}"); |
| 374 |
assert!(!configured.contains("hx-confirm"), "{configured}"); |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
let bare = render(&library_pane(&[], "")); |
| 379 |
assert!(bare.contains("joined any forum communities yet.")); |
| 380 |
assert!(!bare.contains("Browse Communities"), "{bare}"); |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn an_empty_settings_section_keeps_its_heading() { |
| 385 |
|
| 386 |
|
| 387 |
let html = render(&settings_pane(&[], "https://mt.example.com")); |
| 388 |
assert!(html.contains("Forum Communities")); |
| 389 |
assert!(html.contains("joined any forum communities yet.")); |
| 390 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn a_community_name_cannot_smuggle_markup() { |
| 395 |
|
| 396 |
|
| 397 |
let html = render(&table(&[membership("<script>x()</script>", "Member")])); |
| 398 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 399 |
} |
| 400 |
} |
| 401 |
|