use std::collections::BTreeSet;
use std::sync::Arc;
use adk_core::{AdkError, Result};
use adk_harness::website::SiteRuntime;
use adk_harness::{FactFilter, FactScope, FactStatus, FactStore};
use adk_site::content::kind_directory;
use adk_site::gate::GateReport;
use adk_site::pipeline::{Change, EntryPoint};
use awp_types::{
Charter, ContentId, ContentItem, ContentKind, ContentState, FactRef, LanguageTag, Markdown,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
fn site_error(error: adk_site::SiteError) -> AdkError {
AdkError::config(error.to_string())
}
pub const TRUST_GOAL: &str = "trust";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KnownFact {
pub id: String,
pub text: String,
}
pub async fn known_facts(store: &dyn FactStore, scope: &FactScope) -> Result<Vec<KnownFact>> {
Ok(store
.list(FactFilter::recall(scope))
.await?
.into_iter()
.filter(|fact| fact.status == FactStatus::Known)
.map(|fact| KnownFact { id: fact.id, text: fact.text })
.collect())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PageRole {
Home,
About,
Goal,
Legal,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PagePlan {
pub slug: String,
pub title: String,
pub summary: String,
pub goal: String,
pub kpis: Vec<String>,
pub role: PageRole,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub legal: Option<String>,
}
const GOAL_PAGES: &[(&str, &str, &str, &str)] = &[
("audience", "newsletter", "Newsletter", "New writing, by email, when you ask for it."),
("enquiries", "contact", "Contact", "How to reach us."),
("bookings", "book", "Book", "Book a time with us."),
("reputation", "reviews", "Reviews", "What people say, with their permission."),
("sales", "shop", "Shop", "Everything we sell, at its published price."),
("service", "help", "Help", "Orders, delivery and returns."),
("services", "apply", "Apply online", "Apply for services online."),
("accuracy", "announcements", "Announcements", "Official notices in force."),
("donations", "donate", "Donate", "Support our work."),
("volunteers", "volunteer", "Volunteer", "Give your time."),
("events", "whats-on", "What is on", "Events coming up."),
("attendance", "whats-on", "What is on", "Events coming up."),
("admissions", "admissions", "Admissions", "Enquire or book an open day."),
("information", "prospectus", "Prospectus", "Ask for a prospectus."),
("readers", "subscribe", "Subscribe", "Read more of our journalism."),
("corrections", "corrections", "Corrections", "What we corrected, and when."),
("appointments", "appointments", "Appointments", "Book an appointment."),
("registrations", "register", "Register", "Register for the event."),
("attendees", "questions", "Questions", "Answers for attendees."),
];
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SitePlan {
pub pages: Vec<PagePlan>,
}
impl SitePlan {
pub fn new(charter: &Charter, site_name: &str) -> Self {
let kpis_of = |goal: &str| -> Vec<String> {
charter.goals.iter().find(|g| g.id == goal).map(|g| g.kpis.clone()).unwrap_or_default()
};
let trust = charter
.goals
.iter()
.find(|goal| goal.id == TRUST_GOAL)
.or_else(|| charter.goals.last())
.map(|goal| goal.id.clone())
.unwrap_or_default();
let mut pages = Vec::new();
if let Some(first) = charter.goals.first() {
pages.push(PagePlan {
slug: "home".into(),
title: site_name.to_string(),
summary: charter.mission.clone(),
goal: first.id.clone(),
kpis: first.kpis.clone(),
role: PageRole::Home,
legal: None,
});
}
pages.push(PagePlan {
slug: "about".into(),
title: format!("About {site_name}"),
summary: format!("Who is behind {site_name}."),
goal: trust.clone(),
kpis: kpis_of(&trust),
role: PageRole::About,
legal: None,
});
let mut slugs: BTreeSet<String> = ["home".to_string(), "about".to_string()].into();
for goal in charter.goals.iter().filter(|goal| goal.id != trust) {
let (slug, title, summary) = GOAL_PAGES
.iter()
.find(|(id, ..)| *id == goal.id)
.map(|(_, slug, title, summary)| {
((*slug).to_string(), (*title).to_string(), (*summary).to_string())
})
.unwrap_or_else(|| (goal.id.clone(), goal.name.clone(), goal.name.clone()));
if slugs.insert(slug.clone()) {
pages.push(PagePlan {
slug,
title,
summary,
goal: goal.id.clone(),
kpis: goal.kpis.clone(),
role: PageRole::Goal,
legal: None,
});
}
}
let mut legal: Vec<(String, String)> = vec![
("privacy".into(), "Privacy".into()),
("terms".into(), "Terms".into()),
("consent".into(), "Consent".into()),
];
for policy in &charter.public.policies {
if !legal.iter().any(|(name, _)| *name == policy.name) {
legal.push((policy.name.clone(), policy.name.replace('-', " ")));
}
}
for (name, title) in legal {
if slugs.insert(name.clone()) {
pages.push(PagePlan {
summary: format!("{title} at {site_name}."),
slug: name.clone(),
title,
goal: trust.clone(),
kpis: kpis_of(&trust),
role: PageRole::Legal,
legal: Some(name),
});
}
}
Self { pages }
}
pub fn unmapped(&self, charter: &Charter) -> Vec<&PagePlan> {
self.pages.iter().filter(|page| !charter.goals.iter().any(|g| g.id == page.goal)).collect()
}
pub fn items(
&self,
charter: &Charter,
facts: &[KnownFact],
jurisdictions: &[String],
) -> Vec<ContentItem> {
let languages: Vec<LanguageTag> = if charter.voice.languages.is_empty() {
vec![LanguageTag::parse("en").expect("a valid tag")]
} else {
charter.voice.languages.clone()
};
let contact =
charter.public.contact.email.clone().unwrap_or_else(|| "the contact page".into());
let links: Vec<String> = self
.pages
.iter()
.filter(|page| page.role == PageRole::Goal)
.map(|page| format!("- [{}](/{}/)", page.title, page.slug))
.collect();
let mut items = Vec::new();
for (index, language) in languages.iter().enumerate() {
for page in &self.pages {
let (body, sources) = match page.role {
PageRole::Home => {
(format!("{}\n\n{}\n", charter.mission, links.join("\n")), Vec::new())
}
PageRole::About if facts.is_empty() => {
("More about us soon.\n".to_string(), Vec::new())
}
PageRole::About => (
facts.iter().map(|fact| format!("- {}\n", fact.text)).collect(),
facts.iter().map(|fact| FactRef(fact.id.clone())).collect(),
),
PageRole::Goal => (
format!(
"{}\n\nAsk the site's assistant, or use the form on this page.\n",
page.summary
),
Vec::new(),
),
PageRole::Legal => (legal_text(page, charter, &contact), Vec::new()),
};
let mut fields = serde_json::Map::new();
fields.insert("title".into(), json!(page.title));
fields.insert("summary".into(), json!(page.summary));
fields.insert("goal".into(), json!(page.goal));
fields.insert("kpis".into(), json!(page.kpis));
if let Some(legal) = &page.legal {
fields.insert("legal".into(), json!(legal));
if !jurisdictions.is_empty() {
fields.insert("jurisdictions".into(), json!(jurisdictions));
}
}
if index > 0 {
fields.insert("translation".into(), json!("pending"));
}
items.push(ContentItem {
id: ContentId(format!(
"{}/{}/{}",
kind_directory(ContentKind::Page),
page.slug,
language
)),
kind: ContentKind::Page,
slug: page.slug.clone(),
language: language.clone(),
fields: fields.into_iter().collect(),
volatile: Vec::new(),
body: Some(Markdown(body)),
media: Vec::new(),
sources,
schedule: None,
state: ContentState::Draft,
});
}
}
items
}
}
fn legal_text(page: &PagePlan, charter: &Charter, contact: &str) -> String {
let scopes: Vec<&str> = charter.public.consent_scopes.iter().map(|s| s.as_str()).collect();
match page.legal.as_deref() {
Some("privacy") => format!(
"We collect only what you give us, for the purpose you give it for. We use no \
advertising trackers and set no cookies to measure visits. To see, correct or delete \
what we hold about you, write to {contact}.\n"
),
Some("consent") => format!(
"We ask for your consent before we contact you or keep your details, one purpose at a \
time{}. Each consent comes with a receipt, and you can withdraw it at any time; \
withdrawing stops every pending message.\n",
if scopes.is_empty() { String::new() } else { format!(" ({})", scopes.join(", ")) }
),
Some("terms") => {
"Use this site and its services as described on each page. Messages written by the \
site's assistant say so.\n"
.to_string()
}
Some("corrections") => {
"When we get something wrong we correct it in the open: the page shows a correction \
note, and the site's signed log keeps both versions.\n"
.to_string()
}
Some("records-retention") => {
"Superseded notices and records are kept for the period the law requires.\n".to_string()
}
_ => format!("{}\n", page.summary),
}
}
#[derive(Debug, Clone)]
pub struct StagedBuild {
pub written: Vec<ContentId>,
pub held: Vec<ContentId>,
pub gate: GateReport,
}
#[tracing::instrument(skip_all, name = "website.onboarding.build", fields(site.id = %runtime.site().id, items = items.len()))]
pub async fn stage_release_one(
runtime: &SiteRuntime,
items: Vec<ContentItem>,
proposer: &str,
) -> Result<StagedBuild> {
let change = Change {
items,
media: Vec::new(),
entry: EntryPoint::SiteAgent { agent: proposer.to_string() },
proposer: proposer.to_string(),
job: crate::jobs::ONBOARDING.to_string(),
action_class: "publish".to_string(),
};
let draft = runtime.pipeline().draft(change).await.map_err(site_error)?;
let gate = runtime.pipeline().check(&draft).await.map_err(site_error)?;
let held = gate.held_items();
let mut written = Vec::new();
for item in &draft.change.items {
if held.contains(&item.id) {
continue;
}
let mut item = item.clone();
item.state = ContentState::Published;
runtime.store().put(&item).await.map_err(site_error)?;
written.push(item.id);
}
Ok(StagedBuild { written, held: held.into_iter().collect(), gate })
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PreviewSnapshot {
pub facts: usize,
pub digest: String,
pub published: bool,
pub about: Option<String>,
}
pub struct LivePreview {
runtime: Arc<SiteRuntime>,
facts: Arc<dyn FactStore>,
scope: FactScope,
site_name: String,
proposer: String,
}
impl LivePreview {
pub fn new(
runtime: Arc<SiteRuntime>,
facts: Arc<dyn FactStore>,
scope: FactScope,
site_name: impl Into<String>,
proposer: impl Into<String>,
) -> Self {
Self { runtime, facts, scope, site_name: site_name.into(), proposer: proposer.into() }
}
#[tracing::instrument(skip_all, name = "website.onboarding.interview")]
pub async fn refresh(&self, charter: &Charter) -> Result<PreviewSnapshot> {
let facts = known_facts(self.facts.as_ref(), &self.scope).await?;
let plan = SitePlan::new(charter, &self.site_name);
let language = charter.voice.languages.first().map(LanguageTag::as_str).unwrap_or("en");
let items: Vec<ContentItem> = plan
.items(charter, &facts, &[])
.into_iter()
.filter(|item| item.language.as_str() == language && item.slug == "about")
.collect();
let change = Change {
items,
media: Vec::new(),
entry: EntryPoint::SiteAgent { agent: self.proposer.clone() },
proposer: self.proposer.clone(),
job: crate::jobs::ONBOARDING.to_string(),
action_class: "publish".to_string(),
};
let pipeline = self.runtime.pipeline();
let draft = pipeline.draft(change).await.map_err(site_error)?;
let preview = pipeline.preview(&draft).await.map_err(site_error)?;
let about =
tokio::fs::read_to_string(preview.staged.root.join("about/index.md")).await.ok();
let _ = tokio::fs::remove_dir_all(&preview.staged.root).await;
Ok(PreviewSnapshot {
facts: facts.len(),
digest: preview.staged.digest.to_string(),
published: preview.report.is_some(),
about,
})
}
}
pub fn page_goal(item: &ContentItem) -> Option<&str> {
item.fields.get("goal").and_then(Value::as_str)
}