Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions apps/auth-service/src/admin/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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, Self::Error> {
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)]
Expand Down Expand Up @@ -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<Self, Self::Error> {
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 `@`.
Expand Down
24 changes: 0 additions & 24 deletions apps/auth-service/src/admin/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 34 additions & 6 deletions apps/auth-service/src/web/admin/announcements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Self, Self::Error> {
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<Response> {
Expand Down
20 changes: 19 additions & 1 deletion apps/auth-service/src/web/admin/invitations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<String>,
/// 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<String>,
/// 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")]
Expand Down
15 changes: 13 additions & 2 deletions apps/auth-service/src/web/admin/origins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down
27 changes: 27 additions & 0 deletions apps/auth-service/src/web/admin/tests/announcements.rs
Original file line number Diff line number Diff line change
@@ -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).expect("a draft"),
AnnouncementDraft::new(letter, recipients, false).expect("a draft")
);
}

/// 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
Expand Down
14 changes: 11 additions & 3 deletions apps/auth-service/src/web/admin/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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, Self::Error> {
Self::new(&form.subject, &form.body)
}
}

/// The query of [`search`].
#[query_params(error = bad_request)]
struct SearchQuery {
Expand Down
Loading
Loading