From 50f0794c4dbc98fe545efed6e5e32738d72c2304 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 14:28:51 +0100 Subject: [PATCH 1/3] refactor!: the dashboard's forms are ui's, not common's DraftAnnouncement, DraftIntent, AnnouncementAudience, MessageUser and MessageIntent move from sourceorigin_common::admin to sourceorigin-ui, beside the components that render them, and the dashboard's pages convert them into the service's Letter and AnnouncementDraft. ApproveOrigin, whose fields the page's own markup writes, is declared beside that page, as Reservation and ReasonForm are. --- apps/auth-service/src/admin/types/mod.rs | 40 +----- apps/auth-service/src/admin/types/tests.rs | 24 ---- .../src/web/admin/announcements.rs | 40 +++++- apps/auth-service/src/web/admin/origins.rs | 15 +- .../src/web/admin/tests/announcements.rs | 27 ++++ apps/auth-service/src/web/admin/users.rs | 14 +- crates/common/src/admin.rs | 135 +----------------- crates/common/src/tests.rs | 16 --- crates/ui/src/admin/announcements.rs | 117 ++++++++++++++- crates/ui/src/admin/mod.rs | 7 +- crates/ui/src/admin/users.rs | 31 +++- crates/ui/src/lib.rs | 25 ++-- 12 files changed, 249 insertions(+), 242 deletions(-) diff --git a/apps/auth-service/src/admin/types/mod.rs b/apps/auth-service/src/admin/types/mod.rs index 75f19b935..b51b86e2b 100644 --- a/apps/auth-service/src/admin/types/mod.rs +++ b/apps/auth-service/src/admin/types/mod.rs @@ -3,10 +3,7 @@ use super::AdminError; use chrono::Duration; use serde::{Deserialize, Serialize}; -use sourceorigin_common::{ - Handle, HandleError, UserId, - admin::{AnnouncementAudience, DraftAnnouncement, MessageUser}, -}; +use sourceorigin_common::{Handle, HandleError, UserId}; use std::{fmt, str::FromStr}; use thiserror::Error; @@ -270,14 +267,6 @@ impl Letter { } } -impl TryFrom<&MessageUser> for Letter { - type Error = AdminError; - /// Reads the dashboard's message form. - fn try_from(form: &MessageUser) -> Result { - Self::new(&form.subject, &form.body) - } -} - /// An announcement's draft, as an operator writes it: its subject and text, and /// whom it goes to. #[derive(Clone, Debug, PartialEq, Eq)] @@ -327,33 +316,6 @@ impl AnnouncementDraft { } } -impl TryFrom<&DraftAnnouncement> for AnnouncementDraft { - type Error = AdminError; - /// Reads the dashboard's form: one account per line of its list, blank lines - /// skipped. - fn try_from(form: &DraftAnnouncement) -> Result { - let recipients = match form.audience { - AnnouncementAudience::Everyone => AnnouncementRecipients::Everyone, - AnnouncementAudience::Accounts => AnnouncementRecipients::Accounts( - form.accounts - .lines() - .map(str::trim) - .filter(|line| !line.is_empty()) - .map(|line| match line.parse() { - Ok(user) => user, - Err(never) => match never {}, - }) - .collect(), - ), - }; - Self::new( - Letter::new(&form.subject, &form.body)?, - recipients, - form.essential.0, - ) - } -} - /// An account as an operator names it: its ID, a verified email of an active /// account (a value with an `@` other than the leading one of `@handle`), or its /// current handle, with or without that `@`. diff --git a/apps/auth-service/src/admin/types/tests.rs b/apps/auth-service/src/admin/types/tests.rs index b5c6ae4e8..51d0b7df9 100644 --- a/apps/auth-service/src/admin/types/tests.rs +++ b/apps/auth-service/src/admin/types/tests.rs @@ -69,30 +69,6 @@ fn announcements_go_to_bounded_accounts() { assert!(matches!(draft(1_001), Err(AdminError::TooManyRecipients))); } -/// The form lists one account per line, by handle, `@handle` as the dashboard -/// writes it, verified email or ID. -#[test] -fn the_form_names_one_account_per_line() { - let id = UserId::new(); - let form = DraftAnnouncement { - subject: "Incident".into(), - body: "Text".into(), - audience: AnnouncementAudience::Accounts, - accounts: format!("ada\r\n\r\n grace@example.com \n{id}\n@lin\n"), - ..DraftAnnouncement::default() - }; - let draft = AnnouncementDraft::try_from(&form).expect("a draft"); - assert_eq!( - draft.recipients, - AnnouncementRecipients::Accounts(vec![ - UserRef::Handle("ada".into()), - UserRef::Email("grace@example.com".into()), - UserRef::Id(id), - UserRef::Handle("lin".into()), - ]) - ); -} - /// Reasons count Unicode characters and reject blank or control input. #[test] fn reasons_are_bounded_visible_text() { diff --git a/apps/auth-service/src/web/admin/announcements.rs b/apps/auth-service/src/web/admin/announcements.rs index 2acb7b92d..7ae3d9f03 100644 --- a/apps/auth-service/src/web/admin/announcements.rs +++ b/apps/auth-service/src/web/admin/announcements.rs @@ -6,18 +6,19 @@ //! does. use super::AdminApplication as App; -use crate::{AdminError, AnnouncementDraft, admin::SignedInAdmin, deliveries::AnnouncementSender}; +use crate::{ + AdminError, AnnouncementDraft, AnnouncementRecipients, Letter, admin::SignedInAdmin, + deliveries::AnnouncementSender, +}; use chrono::Duration; use serde::de::IgnoredAny; use sourceorigin_common::{ AnnouncementId, - admin::{ - AnnouncementAudience, AnnouncementDetail, DraftAnnouncement, DraftIntent, SendAnnouncement, - }, + admin::{AnnouncementDetail, SendAnnouncement}, }; use sourceorigin_ui::{ - Action, AdminAnnouncementRow, Change, Checkbox, Crumb, DraftActions, FoundAccount, Query, - Target, + Action, AdminAnnouncementRow, AnnouncementAudience, Change, Checkbox, Crumb, DraftActions, + DraftAnnouncement, DraftIntent, FoundAccount, Query, Target, }; use topcoat::{ Error, Result, @@ -36,6 +37,33 @@ use topcoat::{ path_param!(pub announcement: AnnouncementId, error = not_found); +impl TryFrom<&DraftAnnouncement> for AnnouncementDraft { + type Error = AdminError; + /// Reads the dashboard's form: one account per line of its list, blank lines + /// skipped. + fn try_from(form: &DraftAnnouncement) -> Result { + let recipients = match form.audience { + AnnouncementAudience::Everyone => AnnouncementRecipients::Everyone, + AnnouncementAudience::Accounts => AnnouncementRecipients::Accounts( + form.accounts + .lines() + .map(str::trim) + .filter(|line| !line.is_empty()) + .map(|line| match line.parse() { + Ok(user) => user, + Err(never) => match never {}, + }) + .collect(), + ), + }; + Self::new( + Letter::new(&form.subject, &form.body)?, + recipients, + form.essential.0, + ) + } +} + /// `/announcements`: the latest announcements, drafts and sent ones. #[route(GET "/announcements")] pub(super) async fn list(cx: &Cx) -> Result { diff --git a/apps/auth-service/src/web/admin/origins.rs b/apps/auth-service/src/web/admin/origins.rs index 35d838b77..8c82b675a 100644 --- a/apps/auth-service/src/web/admin/origins.rs +++ b/apps/auth-service/src/web/admin/origins.rs @@ -7,8 +7,8 @@ use super::{ clients::{self, Client}, }; use crate::{Reason, clients::MAX_RELATED_ORIGIN_DOMAINS, security::ApiError}; -use serde::Serialize; -use sourceorigin_common::{admin::ApproveOrigin, clients::RelatedOrigin}; +use serde::{Deserialize, Serialize}; +use sourceorigin_common::clients::RelatedOrigin; use sourceorigin_ui::{Change, Query, Target}; use topcoat::{ Result, @@ -19,6 +19,17 @@ use topcoat::{ view::view, }; +/// The form approving an origin for a client's pages: the origin as typed, which +/// the dashboard checks, so a malformed one is explained beside the form. +#[derive(Clone, Deserialize)] +struct ApproveOrigin { + /// The origin, as browsers send it, e.g. `https://notes.example`. + origin: String, + /// Why: 1 to 500 characters, without control characters. + #[serde(default)] + reason: String, +} + /// The query of [`remove`]: the approved origin to remove. #[query_params(error = bad_request)] #[derive(Serialize)] diff --git a/apps/auth-service/src/web/admin/tests/announcements.rs b/apps/auth-service/src/web/admin/tests/announcements.rs index 193f68ea8..bfcfa587a 100644 --- a/apps/auth-service/src/web/admin/tests/announcements.rs +++ b/apps/auth-service/src/web/admin/tests/announcements.rs @@ -1,6 +1,33 @@ //! Announcements: drafting, checking and sending them. use super::*; +use crate::{AnnouncementDraft, AnnouncementRecipients, Letter, UserRef}; +use sourceorigin_ui::{AnnouncementAudience, DraftAnnouncement}; + +/// The form lists one account per line, by handle, `@handle` as the dashboard +/// writes it, verified email or ID. +#[test] +fn the_form_names_one_account_per_line() { + let id = UserId::new(); + let form = DraftAnnouncement { + subject: "Incident".into(), + body: "Text".into(), + audience: AnnouncementAudience::Accounts, + accounts: format!("ada\r\n\r\n grace@example.com \n{id}\n@lin\n"), + ..DraftAnnouncement::default() + }; + let recipients = AnnouncementRecipients::Accounts(vec![ + UserRef::Handle("ada".into()), + UserRef::Email("grace@example.com".into()), + UserRef::Id(id), + UserRef::Handle("lin".into()), + ]); + let letter = Letter::new("Incident", "Text").expect("a letter"); + assert_eq!( + AnnouncementDraft::try_from(&form).ok(), + AnnouncementDraft::new(letter, recipients, false).ok() + ); +} /// The draft form finds accounts to add, saving nothing; each result adds itself /// to the list, which also takes pasted accounts. "Send" saves the draft and diff --git a/apps/auth-service/src/web/admin/users.rs b/apps/auth-service/src/web/admin/users.rs index d307a3b50..1daec2713 100644 --- a/apps/auth-service/src/web/admin/users.rs +++ b/apps/auth-service/src/web/admin/users.rs @@ -9,11 +9,11 @@ use crate::{ }; use sourceorigin_common::{ AccountStatus, PictureId, SessionId, UserId, - admin::{EventSearch, MessageIntent, MessageUser, Recovery, User, UserSearch}, + admin::{EventSearch, Recovery, User, UserSearch}, }; use sourceorigin_ui::{ - AccountPages, Action, Change, Crumb, MessagePreview, Pager, Query, RecoveryKey, RecoveryRow, - SessionRow, Target, UserRow, + AccountPages, Action, Change, Crumb, MessageIntent, MessagePreview, MessageUser, Pager, Query, + RecoveryKey, RecoveryRow, SessionRow, Target, UserRow, }; use topcoat::{ Result, @@ -34,6 +34,14 @@ const ADMINISTRATOR_TARGET: &str = /// How many of an account's events its page shows. const RECENT_EVENTS: usize = 10; +impl TryFrom<&MessageUser> for Letter { + type Error = AdminError; + /// Reads the dashboard's message form. + fn try_from(form: &MessageUser) -> Result { + Self::new(&form.subject, &form.body) + } +} + /// The query of [`search`]. #[query_params(error = bad_request)] struct SearchQuery { diff --git a/crates/common/src/admin.rs b/crates/common/src/admin.rs index c3b584e66..90fae53e8 100644 --- a/crates/common/src/admin.rs +++ b/crates/common/src/admin.rs @@ -1,5 +1,5 @@ -//! The views of administration: what the admin dashboard shows and posts, and -//! what the CLI's `--json` prints. Auth's service returns them, and its +//! The views of administration: what the admin dashboard shows, and what the +//! CLI's `--json` prints. Auth's service returns them, and its //! database crate decodes some straight into them. //! //! Bodies the account settings also return come from [`crate::protocol::api`] @@ -13,7 +13,6 @@ use crate::{ AccountStatus, AnnouncementId, AuthenticationMethod, ClientId, ClientSecretId, DeliveryId, EventId, Handle, PasskeyId, PictureId, RecoveryId, SessionId, TokenId, TraceId, UserId, clients::{ClientKind, ClientStatus, RelatedOrigin}, - form::Checkbox, protocol::{jwt::KeyId, oauth::ClientAuthMethod}, }; use chrono::{DateTime, Utc}; @@ -624,17 +623,6 @@ pub struct ApprovedOrigin { pub approved_at: DateTime, } -/// The form approving an origin for a client's pages: the origin as typed, which -/// the dashboard checks, so a malformed one is explained beside the form. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ApproveOrigin { - /// The origin, as browsers send it, e.g. `https://notes.example`. - pub origin: String, - /// Why: 1 to 500 characters, without control characters. - #[serde(default)] - pub reason: String, -} - /// An administrator account. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Administrator { @@ -773,125 +761,6 @@ pub struct SendAnnouncement { pub revision: u64, } -/// The form sending an account a message, as typed: the dashboard checks it, -/// so a mistake is explained beside the form. -#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct MessageUser { - /// The email's subject: one line, up to 150 characters. - #[serde(default)] - pub subject: String, - /// Its text: up to 20,000 characters, paragraphs separated by blank lines. - #[serde(default)] - pub body: String, - /// What the button pressed asks for. - #[serde(default)] - pub intent: MessageIntent, -} - -/// What a message form's button asks for. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum MessageIntent { - /// Show the email as it will be sent, which needs no confirmation. - #[default] - Preview, - /// Change the text again, keeping what was typed. - Edit, - /// Send it, which needs the dashboard's recent passkey confirmation. - Send, -} - -/// Whom an announcement goes to, as its form chooses. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum AnnouncementAudience { - /// Every account, as of its sending. - #[default] - Everyone, - /// The accounts the form lists. - Accounts, -} - -/// The form drafting an announcement, or changing a draft, as typed: the -/// dashboard checks it, so a mistake is explained beside the form. -#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct DraftAnnouncement { - /// The email's subject: one line, up to 150 characters. - #[serde(default)] - pub subject: String, - /// Its text: up to 20,000 characters, paragraphs separated by blank lines. - #[serde(default)] - pub body: String, - /// Whom it goes to. - #[serde(default)] - pub audience: AnnouncementAudience, - /// For [`AnnouncementAudience::Accounts`], the accounts, one per line, each by - /// handle, verified email or ID. - #[serde(default)] - pub accounts: String, - /// Whether it is emailed to every recipient, whatever their preference. - #[serde(default, skip_serializing_if = "Checkbox::is_off")] - pub essential: Checkbox, - /// What the button pressed asks for. - #[serde(default)] - pub intent: DraftIntent, - /// The accounts search, as typed. - #[serde(default)] - pub find: String, - /// The handle a search result's button adds to the accounts. - #[serde(default)] - pub add: Option, -} - -impl DraftAnnouncement { - /// Whether the form only looks accounts up, or adds one: nothing is saved, - /// and it shows again as typed. - #[must_use] - pub const fn composes(&self) -> bool { - matches!(self.intent, DraftIntent::Find) || self.add.is_some() - } - - /// Whether the accounts list `handle`, with or without its `@`, ignoring - /// ASCII case as [`Handle::parse`] does. - #[must_use] - pub fn lists(&self, handle: &str) -> bool { - self.accounts.lines().map(str::trim).any(|line| { - line.strip_prefix('@') - .unwrap_or(line) - .eq_ignore_ascii_case(handle) - }) - } - - /// Adds the account the pressed button names, by handle, to the accounts, - /// unless they list it already, and sends the announcement to the accounts. - pub fn add_chosen(&mut self) { - let Some(handle) = self.add.take() else { - return; - }; - self.audience = AnnouncementAudience::Accounts; - if self.lists(&handle) { - return; - } - if !self.accounts.trim_end().is_empty() { - self.accounts = format!("{}\n", self.accounts.trim_end()); - } - self.accounts.push_str(&handle); - } -} - -/// What a draft's form asks for, by the button pressed. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum DraftIntent { - /// Save it as a draft. - #[default] - Save, - /// Save it, then send it as saved. - Send, - /// Look accounts up to add, saving nothing. - Find, -} - /// The result of verifying the audit chain, as `auth-service audit verify` prints /// it and the audit log records it, as `DomainEvent::AuditChainVerified`, for /// the dashboard's audit page to show. Verification stops at the first problem. diff --git a/crates/common/src/tests.rs b/crates/common/src/tests.rs index 34ed2da89..6b5d7e3cf 100644 --- a/crates/common/src/tests.rs +++ b/crates/common/src/tests.rs @@ -411,22 +411,6 @@ fn invitations_created_before_their_id_still_decode() { )); } -/// Adding an account from the search lists it once, however its handle was -/// typed: handles match ignoring case, as `Handle::parse` lowercases them. -#[cfg(feature = "admin")] -#[test] -fn announcements_list_an_account_once_whatever_its_case() { - let mut draft = admin::DraftAnnouncement { - accounts: " @Ada \n".into(), - add: Some("ada".into()), - ..admin::DraftAnnouncement::default() - }; - assert!(draft.lists("ada")); - draft.add_chosen(); - assert_eq!(draft.accounts, " @Ada \n"); - assert_eq!(draft.audience, admin::AnnouncementAudience::Accounts); -} - /// An announcement's text splits into paragraphs at blank lines, even ones of /// spaces, and keeps each paragraph's lines, without their trailing spaces. #[cfg(feature = "admin")] diff --git a/crates/ui/src/admin/announcements.rs b/crates/ui/src/admin/announcements.rs index 3411c3554..654a95431 100644 --- a/crates/ui/src/admin/announcements.rs +++ b/crates/ui/src/admin/announcements.rs @@ -2,20 +2,111 @@ //! recipients read it, and the form drafting one. use super::{Action, Change, confirm, data_table, message_fields, moment}; +use crate::Checkbox; use crate::icons::{Icon, icon}; use crate::{ clients::choice, email::{Mail, email_preview}, layout::{alert_notice, page_head}, }; -use sourceorigin_common::admin::{ - Announcement, AnnouncementAudience, AnnouncementDetail, DraftAnnouncement, User, -}; +use serde::{Deserialize, Serialize}; +use sourceorigin_common::admin::{Announcement, AnnouncementDetail, User}; use topcoat::{ Result, view::{View, component, view}, }; +/// Whom an announcement goes to, as its form chooses. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum AnnouncementAudience { + /// Every account, as of its sending. + #[default] + Everyone, + /// The accounts the form lists. + Accounts, +} + +/// The form drafting an announcement, or changing a draft, as typed: the +/// dashboard checks it, so a mistake is explained beside the form. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct DraftAnnouncement { + /// The email's subject: one line, up to 150 characters. + #[serde(default)] + pub subject: String, + /// Its text: up to 20,000 characters, paragraphs separated by blank lines. + #[serde(default)] + pub body: String, + /// Whom it goes to. + #[serde(default)] + pub audience: AnnouncementAudience, + /// For [`AnnouncementAudience::Accounts`], the accounts, one per line, each by + /// handle, verified email or ID. + #[serde(default)] + pub accounts: String, + /// Whether it is emailed to every recipient, whatever their preference. + #[serde(default, skip_serializing_if = "Checkbox::is_off")] + pub essential: Checkbox, + /// What the button pressed asks for. + #[serde(default)] + pub intent: DraftIntent, + /// The accounts search, as typed. + #[serde(default)] + pub find: String, + /// The handle a search result's button adds to the accounts. + #[serde(default)] + pub add: Option, +} + +impl DraftAnnouncement { + /// Whether the form only looks accounts up, or adds one: nothing is saved, + /// and it shows again as typed. + #[must_use] + pub const fn composes(&self) -> bool { + matches!(self.intent, DraftIntent::Find) || self.add.is_some() + } + + /// Whether the accounts list `handle`, with or without its `@`, ignoring + /// ASCII case as [`Handle::parse`](sourceorigin_common::Handle::parse) does. + #[must_use] + pub fn lists(&self, handle: &str) -> bool { + self.accounts.lines().map(str::trim).any(|line| { + line.strip_prefix('@') + .unwrap_or(line) + .eq_ignore_ascii_case(handle) + }) + } + + /// Adds the account the pressed button names, by handle, to the accounts, + /// unless they list it already, and sends the announcement to the accounts. + pub fn add_chosen(&mut self) { + let Some(handle) = self.add.take() else { + return; + }; + self.audience = AnnouncementAudience::Accounts; + if self.lists(&handle) { + return; + } + if !self.accounts.trim_end().is_empty() { + self.accounts = format!("{}\n", self.accounts.trim_end()); + } + self.accounts.push_str(&handle); + } +} + +/// What a draft's form asks for, by the button pressed. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum DraftIntent { + /// Save it as a draft. + #[default] + Save, + /// Save it, then send it as saved. + Send, + /// Look accounts up to add, saving nothing. + Find, +} + /// An announcement in the list, and its page. #[derive(Clone, Debug)] pub struct AdminAnnouncementRow<'a> { @@ -350,3 +441,23 @@ async fn chosen_accounts<'a>( }) } + +#[cfg(test)] +mod tests { + use super::{AnnouncementAudience, DraftAnnouncement}; + + /// Adding an account from the search lists it once, however its handle was + /// typed: handles match ignoring case, as `Handle::parse` lowercases them. + #[test] + fn announcements_list_an_account_once_whatever_its_case() { + let mut draft = DraftAnnouncement { + accounts: " @Ada \n".into(), + add: Some("ada".into()), + ..DraftAnnouncement::default() + }; + assert!(draft.lists("ada")); + draft.add_chosen(); + assert_eq!(draft.accounts, " @Ada \n"); + assert_eq!(draft.audience, AnnouncementAudience::Accounts); + } +} diff --git a/crates/ui/src/admin/mod.rs b/crates/ui/src/admin/mod.rs index 25826d576..09f222cf2 100644 --- a/crates/ui/src/admin/mod.rs +++ b/crates/ui/src/admin/mod.rs @@ -36,8 +36,8 @@ mod users; pub use alerts::{AlertCard, alert_list, alert_update_fields, previous_alerts}; pub use announcements::{ - AdminAnnouncementRow, DraftActions, FoundAccount, announcement_form, announcement_list, - announcement_page, + AdminAnnouncementRow, AnnouncementAudience, DraftActions, DraftAnnouncement, DraftIntent, + FoundAccount, announcement_form, announcement_list, announcement_page, }; pub use audit::audit_page; pub use clients::{ @@ -57,7 +57,8 @@ pub use lists::{ pub use overview::{AdminOverview, Measure, Problem, admin_overview}; pub use recoveries::{RecoveryKey, RecoveryRow, recovery_list}; pub use users::{ - AccountPages, MessagePreview, SessionRow, UserRow, message_page, user_page, user_search, + AccountPages, MessageIntent, MessagePreview, MessageUser, SessionRow, UserRow, message_page, + user_page, user_search, }; use crate::{ diff --git a/crates/ui/src/admin/users.rs b/crates/ui/src/admin/users.rs index 25b9f4008..20c153cb5 100644 --- a/crates/ui/src/admin/users.rs +++ b/crates/ui/src/admin/users.rs @@ -12,15 +12,44 @@ use crate::{ private_address, }, }; +use serde::{Deserialize, Serialize}; use sourceorigin_common::{ AccountStatus, AuthenticationMethod, - admin::{MessageUser, Session, User, UserDetail}, + admin::{Session, User, UserDetail}, }; use topcoat::{ Result, view::{View, component, view}, }; +/// The form sending an account a message, as typed: the dashboard checks it, +/// so a mistake is explained beside the form. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct MessageUser { + /// The email's subject: one line, up to 150 characters. + #[serde(default)] + pub subject: String, + /// Its text: up to 20,000 characters, paragraphs separated by blank lines. + #[serde(default)] + pub body: String, + /// What the button pressed asks for. + #[serde(default)] + pub intent: MessageIntent, +} + +/// What a message form's button asks for. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum MessageIntent { + /// Show the email as it will be sent, which needs no confirmation. + #[default] + Preview, + /// Change the text again, keeping what was typed. + Edit, + /// Send it, which needs the dashboard's recent passkey confirmation. + Send, +} + /// An account found by a search, and the link to its page. #[derive(Clone, Debug)] pub struct UserRow<'a> { diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index 4b41995fc..da047dd53 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -66,18 +66,19 @@ mod settings; pub use admin::{ AccountPages, Action, AdminAnnouncementRow, AdminClientRow, AdminOverview, AdministratorRow, - AlertCard, Change, ClientChanges, ClientPage, Confirmation as DashboardConfirmation, Dashboard, - DeliveryRow, DraftActions, EventRow, FoundAccount, HandlesPage, HandoffProof, HandoffQuery, - InvitationRow, Measure, MessagePreview, NewClientForm, OperatorDraft, Problem, RecoveryKey, - RecoveryRow, Registration, ReleasedRow, ReservationRow, ReservedHolder, SearchAction, - SessionRow, SigningKeyRow, Tab as DashboardTab, Target, UserRow, account_settings, - admin_overview, administrator_list, alert_list, alert_update_fields, announcement_form, - announcement_list, announcement_page, audit_page, client_created, client_list, client_page, - client_registration, client_secrets, dashboard_change, dashboard_failure, dashboard_frame, - dashboard_signed_out, delivery_list, event_list, event_rows, grant_search, handle_list, - handoff_proof, invitation_created, invitation_list, message_page, no_access, previous_alerts, - recovery_list, reserved_handle_choice, signing_key_list, signing_key_rotation, user_page, - user_search, + AlertCard, AnnouncementAudience, Change, ClientChanges, ClientPage, + Confirmation as DashboardConfirmation, Dashboard, DeliveryRow, DraftActions, DraftAnnouncement, + DraftIntent, EventRow, FoundAccount, HandlesPage, HandoffProof, HandoffQuery, InvitationRow, + Measure, MessageIntent, MessagePreview, MessageUser, NewClientForm, OperatorDraft, Problem, + RecoveryKey, RecoveryRow, Registration, ReleasedRow, ReservationRow, ReservedHolder, + SearchAction, SessionRow, SigningKeyRow, Tab as DashboardTab, Target, UserRow, + account_settings, admin_overview, administrator_list, alert_list, alert_update_fields, + announcement_form, announcement_list, announcement_page, audit_page, client_created, + client_list, client_page, client_registration, client_secrets, dashboard_change, + dashboard_failure, dashboard_frame, dashboard_signed_out, delivery_list, event_list, + event_rows, grant_search, handle_list, handoff_proof, invitation_created, invitation_list, + message_page, no_access, previous_alerts, recovery_list, reserved_handle_choice, + signing_key_list, signing_key_rotation, user_page, user_search, }; pub use asset::{Asset, FONTS, STYLESHEET}; pub use clients::{ From ddf60f4c7c6dd6d8f9b7ea0d90c441b5b3acee79 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 14:28:58 +0100 Subject: [PATCH 2/3] docs: where the dashboard's forms are declared --- docs/dev/extending/admin-operation.md | 6 ++++++ docs/dev/rust-conventions.md | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/dev/extending/admin-operation.md b/docs/dev/extending/admin-operation.md index e3f20eefa..ad42583db 100644 --- a/docs/dev/extending/admin-operation.md +++ b/docs/dev/extending/admin-operation.md @@ -176,6 +176,12 @@ pub(super) async fn reserve(cx: &Cx, form: Option>) -> Result< change whose form posts only its reason uses `App::submit_reason` and `ReasonForm`, which parse the reason first. +Declare the form beside the page, as `Reservation` is, when the page's own +markup writes its fields. A form that a `sourceorigin-ui` component renders, +such as `DraftAnnouncement`, is declared in ui beside that component, and the +page converts it into the service's input. `sourceorigin_common::admin` holds +views, never forms. + The page's words are a `Change` variant in `crates/ui/src/admin/mod.rs`: its title, description, button, label, whether it takes something away, and the reason it asks for. Link the page from the list it belongs to, as the handles diff --git a/docs/dev/rust-conventions.md b/docs/dev/rust-conventions.md index 807f32b78..def405476 100644 --- a/docs/dev/rust-conventions.md +++ b/docs/dev/rust-conventions.md @@ -62,7 +62,8 @@ Keep implementations simple. Follow neighbouring modules and share repeated logi reads opens the sidebar as a drawer. `form` holds what every form shares, including the `Checkbox` and `Lines` types for values a form sends as text; `clients` the client views the console shares with the dashboard; `settings`, - `developers` and `admin` each area's pages, an overview first; `icons` the + `developers` and `admin` each area's pages, an overview first, with the forms + they render, such as the dashboard's `DraftAnnouncement`; `icons` the inline glyphs; `asset` the files pages link, embedded in the executable: the stylesheet, `STYLESHEET`, under a path naming its content, and the `FONTS` it links, under paths naming their version, which the admin dashboard serves From 643c9b32e35a7ea321404f5de8381e8238957126 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 15:28:35 +0100 Subject: [PATCH 3/3] refactor!: common::admin holds no dashboard form CreateInvitation, whose fields the invitation page writes, is declared beside that page, and the unused GrantAdministrator goes. The announcement form's test expects a draft on both sides. --- .../auth-service/src/web/admin/invitations.rs | 20 +++++++++++- .../src/web/admin/tests/announcements.rs | 4 +-- crates/common/src/admin.rs | 31 ++++--------------- docs/dev/extending/admin-operation.md | 4 ++- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/apps/auth-service/src/web/admin/invitations.rs b/apps/auth-service/src/web/admin/invitations.rs index 1759e84cc..4ca830edc 100644 --- a/apps/auth-service/src/web/admin/invitations.rs +++ b/apps/auth-service/src/web/admin/invitations.rs @@ -4,9 +4,10 @@ use super::{AdminApplication as App, ReasonForm, ReasonPage}; use crate::{AdminError, InviteExpiry, Reason}; use chrono::Utc; +use serde::Deserialize; use sourceorigin_common::{ TokenId, - admin::{CreateInvitation, CreatedInvitation, InvitationState}, + admin::{CreatedInvitation, InvitationState}, email::NormalizedEmail, }; use sourceorigin_ui::{Action, Change, InvitationRow, Target}; @@ -19,6 +20,23 @@ use topcoat::{ path_param!(pub invitation: TokenId, error = not_found); +/// The form creating an invitation. +#[derive(Clone, Deserialize)] +struct CreateInvitation { + /// The address to mail the link to, which the account created with it starts + /// with, verified. Without one, the link is shown once to pass on. + #[serde(default)] + email: Option, + /// How long the link stays valid: 1 hour to 365 days, a number followed by `m`, + /// `h` or `d`, e.g. `24h`. + /// Seven days when absent. + #[serde(default)] + expires_in: Option, + /// Why: 1 to 500 characters, without control characters. + #[serde(default)] + reason: String, +} + /// `/invitations`: signup invitations, newest first, with the changes each /// open one takes: a mailed one is resent only while email is on. #[route(GET "/invitations")] diff --git a/apps/auth-service/src/web/admin/tests/announcements.rs b/apps/auth-service/src/web/admin/tests/announcements.rs index bfcfa587a..249859266 100644 --- a/apps/auth-service/src/web/admin/tests/announcements.rs +++ b/apps/auth-service/src/web/admin/tests/announcements.rs @@ -24,8 +24,8 @@ fn the_form_names_one_account_per_line() { ]); let letter = Letter::new("Incident", "Text").expect("a letter"); assert_eq!( - AnnouncementDraft::try_from(&form).ok(), - AnnouncementDraft::new(letter, recipients, false).ok() + AnnouncementDraft::try_from(&form).expect("a draft"), + AnnouncementDraft::new(letter, recipients, false).expect("a draft") ); } diff --git a/crates/common/src/admin.rs b/crates/common/src/admin.rs index 90fae53e8..919a863fb 100644 --- a/crates/common/src/admin.rs +++ b/crates/common/src/admin.rs @@ -1,6 +1,11 @@ //! The views of administration: what the admin dashboard shows, and what the //! CLI's `--json` prints. Auth's service returns them, and its -//! database crate decodes some straight into them. +//! database crate decodes some straight into them. It also holds the searches +//! the service takes, such as [`EventSearch`](crate::admin::EventSearch). The +//! dashboard's forms are not here; two of its page queries, +//! [`UserSearch`](crate::admin::UserSearch) and +//! [`SendAnnouncement`](crate::admin::SendAnnouncement), still are, until they +//! move to `sourceorigin-ui` with them. //! //! Bodies the account settings also return come from [`crate::protocol::api`] //! and are re-exported here, as is the @@ -318,23 +323,6 @@ pub enum InvitationState { Open, } -/// The form creating an invitation. -#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct CreateInvitation { - /// The address to mail the link to, which the account created with it starts - /// with, verified. Without one, the link is shown once to pass on. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub email: Option, - /// How long the link stays valid: 1 hour to 365 days, a number followed by `m`, - /// `h` or `d`, e.g. `24h`. - /// Seven days when absent. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub expires_in: Option, - /// Why: 1 to 500 characters, without control characters. - #[serde(default)] - pub reason: String, -} - /// A new invitation, and how its link reaches the person it invites. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct CreatedInvitation { @@ -637,13 +625,6 @@ pub struct Administrator { pub granted_at: DateTime, } -/// The form granting administrator rights. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct GrantAdministrator { - /// The active account to make an administrator. - pub user_id: UserId, -} - /// An announcement to accounts, such as a security incident or a change to the /// privacy policy: a draft until it is sent, and unchanged once it is. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] diff --git a/docs/dev/extending/admin-operation.md b/docs/dev/extending/admin-operation.md index ad42583db..a0e8bc20c 100644 --- a/docs/dev/extending/admin-operation.md +++ b/docs/dev/extending/admin-operation.md @@ -180,7 +180,9 @@ Declare the form beside the page, as `Reservation` is, when the page's own markup writes its fields. A form that a `sourceorigin-ui` component renders, such as `DraftAnnouncement`, is declared in ui beside that component, and the page converts it into the service's input. `sourceorigin_common::admin` holds -views, never forms. +views and the searches the service takes, never forms. Two of the dashboard's +page queries, `UserSearch` and `SendAnnouncement`, are still there until they +move to ui; declare a new page's query in ui. The page's words are a `Change` variant in `crates/ui/src/admin/mod.rs`: its title, description, button, label, whether it takes something away, and the