tilo-action-policy 0.5.2

Who may invoke which action: grants over the published tree and the one function that decides them.
Documentation
//! A workspace's entry: who it lets in, and where it is listed.
//!
//! The entry is one of the workspace's settings, not a host's: a server serves a
//! public workspace, and a person's own machine holds the same workspace, so
//! both read the same resource and decide alike. Whoever may change the
//! workspace's settings ([`crate::roles::governance::SETTINGS`]) makes the
//! change once; every host reading the workspace then answers its entry the
//! same way, including after a restart.

use serde::{Deserialize, Serialize};

use crate::{ActionKind, Answer, Grant, GrantId, Grantee, KindSet, Lifetime, Role, TreePath};

/// The app that keeps what visitors leave at a workspace's entry: one resource per address left
/// there by someone asking to be let in.
pub const VISITOR_ENTRY_APP: &str = "so.tilo.lang.entry";

/// What a visitor may do at the entry, and nothing else of it: leave an address, confirm it from
/// the link mailed there, and take it off again.
pub const VISITOR_ENTRY_ACTIONS: [&str; 3] = ["join", "confirm", "leave"];

/// The longest description a listing carries, in characters: what a
/// directory shows of a workspace, in a sentence or two.
pub const MAX_DESCRIPTION_CHARS: usize = 280;

/// A workspace as a directory lists it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ListingPolicy {
    /// What the workspace is, in a sentence or two.
    pub description: String,
    /// The role everyone who joins through the entry holds to begin with.
    pub default_role: Role,
}

/// Why an entry's setting cannot stand.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EntryError {
    /// The description is longer than [`MAX_DESCRIPTION_CHARS`] or holds a
    /// control character other than a line break.
    Description,
    /// Moderators are chosen, never handed out at the entry.
    Role,
    /// A wait list's contact is too long or holds a control character.
    Contact,
    /// A wait list's mail names no sender, or its sender holds a control
    /// character.
    Mail,
}

impl std::fmt::Display for EntryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Description => write!(
                f,
                "a description is at most {MAX_DESCRIPTION_CHARS} characters of text"
            ),
            Self::Role => {
                f.write_str("people who join on their own become viewers, commenters or editors")
            }
            Self::Contact => f.write_str(
                "a wait list's contact is at most 280 characters, without control characters",
            ),
            Self::Mail => f.write_str(
                "a wait list mails from a sender (`hello@example.com`) without control characters",
            ),
        }
    }
}

impl std::error::Error for EntryError {}

/// What a workspace's entry decides.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct EntryPolicy {
    /// Listed in a directory, where anyone who asks gets in at
    /// [`ListingPolicy::default_role`]. Unlisted, the entry admits only the
    /// people the workspace has in it, invited, or pre-admitted.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub listing: Option<ListingPolicy>,
    /// The entry admits only people it invited or pre-admitted: a listed
    /// workspace takes nobody who simply asks.
    #[serde(default, skip_serializing_if = "is_false")]
    pub invitation_only: bool,
    /// How many requests left at the entry it keeps for whoever may invite.
    /// `0`: the entry keeps none. Kept while the entry takes everyone who asks
    /// too, dormant: only an entry that takes people by invitation keeps a
    /// wait list ([`Self::keeps_requests`]), so opening and closing it again
    /// leaves the settings in force.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub wait_list_max: usize,
    /// Where someone asks to be taken off the wait list, as the entry shows
    /// it beside a refusal and on its wait-list page. Kept while the entry
    /// takes everyone who asks, dormant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contact: Option<String>,
    /// Where the wait list mails an address, when it mails one at all.
    /// `None` lists an address at once. Kept while the entry takes everyone
    /// who asks, dormant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mail: Option<EntryMail>,
}

/// Where a workspace's wait-list mail is sent from, and where its words are
/// read from.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EntryMail {
    /// The sender an address hears from (`hello@example.com`).
    pub from: String,
    /// The directory the mails' words are read from. `None`: the words built
    /// into the host.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub templates: Option<String>,
}

impl EntryPolicy {
    /// Whether the workspace is public: a directory lists it.
    #[must_use]
    pub const fn is_listed(&self) -> bool {
        self.listing.is_some()
    }

    /// Whether the entry keeps the addresses people leave when it turns them
    /// away.
    #[must_use]
    pub const fn keeps_requests(&self) -> bool {
        self.invitation_only && self.wait_list_max > 0
    }

    /// The role the entry lets anyone who asks come in at: its listing's
    /// default role on an entry that takes people who ask. `None` on an entry
    /// that takes people by invitation only, and on one no directory lists:
    /// neither lets a stranger in.
    #[must_use]
    pub const fn open_role(&self) -> Option<Role> {
        match &self.listing {
            Some(listing) if !self.invitation_only => Some(listing.default_role),
            _ => None,
        }
    }

    /// What the entry opens to any key ([`Grantee::AnyKey`]) in `workspace`: on an entry that
    /// keeps requests, leaving an address there, confirming it and taking it off. Nothing on any other
    /// entry. Every device derives these from the entry alike, so a visitor's call is admitted,
    /// and its change taken in, by the same grants everywhere.
    #[must_use]
    pub fn visitor_grants(&self, workspace: &str) -> Vec<Grant> {
        if !self.keeps_requests() {
            return Vec::new();
        }
        VISITOR_ENTRY_ACTIONS
            .into_iter()
            .map(|action| Grant {
                id: GrantId::new(format!("entry:visitor:{action}")),
                grantee: Grantee::AnyKey,
                node: TreePath::workspace(workspace)
                    .app(VISITOR_ENTRY_APP)
                    .rest()
                    .action(action),
                kinds: KindSet::of(ActionKind::Read).with(ActionKind::Undoable),
                answer: Answer::Allow,
                lifetime: Lifetime::UntilRevoked,
                granted_by: Grantee::role(Role::Owner),
            })
            .collect()
    }

    /// The entry with its listing's description trimmed, when it may stand.
    ///
    /// # Errors
    ///
    /// [`EntryError`]: what a directory cannot show, a role the entry may not
    /// hand out, or a contact or sender the wait list cannot use.
    pub fn checked(self) -> Result<Self, EntryError> {
        let listing = match self.listing {
            Some(listing) => {
                let description = listing.description.trim().to_owned();
                if description.chars().count() > MAX_DESCRIPTION_CHARS
                    || description.chars().any(|c| c.is_control() && c != '\n')
                {
                    return Err(EntryError::Description);
                }
                if !Role::Editor.includes(listing.default_role) {
                    return Err(EntryError::Role);
                }
                Some(ListingPolicy {
                    description,
                    default_role: listing.default_role,
                })
            }
            None => None,
        };
        let contact = self
            .contact
            .map(|contact| contact.trim().to_owned())
            .filter(|contact| !contact.is_empty());
        if let Some(contact) = &contact
            && (contact.chars().count() > MAX_DESCRIPTION_CHARS
                || contact.chars().any(char::is_control))
        {
            return Err(EntryError::Contact);
        }
        let mail = match self.mail {
            None => None,
            Some(mail) => {
                let from = mail.from.trim().to_owned();
                if from.is_empty() || !from.contains('@') || from.chars().any(char::is_control) {
                    return Err(EntryError::Mail);
                }
                let templates = mail
                    .templates
                    .map(|templates| templates.trim().to_owned())
                    .filter(|templates| !templates.is_empty());
                Some(EntryMail { from, templates })
            }
        };
        Ok(Self {
            listing,
            contact,
            mail,
            ..self
        })
    }
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde's skip_serializing_if takes the field by reference"
)]
const fn is_false(value: &bool) -> bool {
    !*value
}

#[expect(
    clippy::trivially_copy_pass_by_ref,
    reason = "serde's skip_serializing_if takes the field by reference"
)]
const fn is_zero(value: &usize) -> bool {
    *value == 0
}