v3 : socle Rust (moteur pur, accès trustinWallet, serveur) ; maquette Nuxt rangée dans maquette/
- crates/ld-engine : formule inertielle (parité avec la maquette, garde C), coquille / changement de fond, assistant d'édition à règles, composition d'un document sous vote permanent (adoptions, aperçus, rivales). - crates/trustin-access : qui voit, qui ouvre, qui gère les accès. - crates/ld-server : API axum + SQLite, connexion Ğ1 (client OAuth2 de connect.monnaie-libre.fr), invitations, entrée en vigueur automatique, signalements de contradiction, traductions éditoriales, publication git, taille de la toile lue sur la chaîne. - seeds/acte-engagement-certification.json : 33 articles, fr + en + es ; en vigueur : préambule, E1, E2 (seed a minima). - maquette/ : frontend, backend et docker v2 déplacés tels quels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
//! Publication of the texts in force to the git repository that client
|
||||
//! applications read to update themselves (Yvv, 2026-10-21: « Le tout est
|
||||
//! automatique poussé sur le dépôt git exploité par les applis clientes »).
|
||||
//!
|
||||
//! Every adoption, typo fix or translation of a text in force queues an
|
||||
//! outbox row. The worker renders the document — one Markdown file per
|
||||
//! language plus a JSON file — commits it in the server's own clone and pushes
|
||||
//! with a deploy key. A failure is retried with backoff; the vote never waits
|
||||
//! for the network.
|
||||
|
||||
use crate::AppState;
|
||||
use crate::docs::{self, DocState};
|
||||
use crate::util;
|
||||
use serde_json::json;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use tokio::process::Command;
|
||||
|
||||
const IDLE: Duration = Duration::from_secs(60);
|
||||
const MAX_BACKOFF_SECS: i64 = 6 * 3600;
|
||||
|
||||
pub async fn worker(state: AppState) {
|
||||
loop {
|
||||
tokio::select! {
|
||||
() = state.publish_wake.notified() => {}
|
||||
() = tokio::time::sleep(IDLE) => {}
|
||||
}
|
||||
if state.cfg.publish.is_none() {
|
||||
continue;
|
||||
}
|
||||
if let Err(e) = pass(&state).await {
|
||||
tracing::warn!(error = %e, "publication pass");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct Due {
|
||||
id: i64,
|
||||
doc_id: String,
|
||||
attempts: i64,
|
||||
}
|
||||
|
||||
async fn pass(state: &AppState) -> anyhow::Result<()> {
|
||||
let due = sqlx::query_as::<_, Due>(
|
||||
"SELECT id, doc_id, attempts FROM outbox WHERE done_at IS NULL AND next_at <= ? ORDER BY id",
|
||||
)
|
||||
.bind(util::now())
|
||||
.fetch_all(&state.db)
|
||||
.await?;
|
||||
let mut docs_due: Vec<String> = due.iter().map(|d| d.doc_id.clone()).collect();
|
||||
docs_due.dedup();
|
||||
for doc_id in docs_due {
|
||||
let rows: Vec<&Due> = due.iter().filter(|d| d.doc_id == doc_id).collect();
|
||||
let last = rows.iter().map(|d| d.id).max().unwrap_or(0);
|
||||
let attempts = rows.iter().map(|d| d.attempts).max().unwrap_or(0);
|
||||
match publish_doc(state, &doc_id).await {
|
||||
Ok(sha) => {
|
||||
sqlx::query("UPDATE outbox SET done_at = ?, commit_sha = ?, last_error = NULL WHERE doc_id = ? AND done_at IS NULL AND id <= ?")
|
||||
.bind(util::now())
|
||||
.bind(&sha)
|
||||
.bind(&doc_id)
|
||||
.bind(last)
|
||||
.execute(&state.db)
|
||||
.await?;
|
||||
tracing::info!(doc = %doc_id, commit = sha.as_deref().unwrap_or("unchanged"), "published");
|
||||
}
|
||||
Err(e) => {
|
||||
let wait = (60_i64 << attempts.min(12)).min(MAX_BACKOFF_SECS);
|
||||
sqlx::query("UPDATE outbox SET attempts = attempts + 1, last_error = ?, next_at = ? WHERE doc_id = ? AND done_at IS NULL")
|
||||
.bind(format!("{e:#}").chars().take(500).collect::<String>())
|
||||
.bind(util::in_seconds(wait))
|
||||
.bind(&doc_id)
|
||||
.execute(&state.db)
|
||||
.await?;
|
||||
tracing::warn!(doc = %doc_id, error = %e, retry_in = wait, "publication failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn git(dir: &Path, state: &AppState, args: &[&str]) -> anyhow::Result<String> {
|
||||
let publish = state.cfg.publish.as_ref().ok_or_else(|| anyhow::anyhow!("publication not configured"))?;
|
||||
let known_hosts = state.cfg.data_dir.join("keys/known_hosts");
|
||||
let ssh = format!(
|
||||
"ssh -i {} -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile={}",
|
||||
publish.key.display(),
|
||||
known_hosts.display()
|
||||
);
|
||||
let out = Command::new("git")
|
||||
.args([
|
||||
"-c",
|
||||
&format!("user.name={}", publish.author_name),
|
||||
"-c",
|
||||
&format!("user.email={}", publish.author_email),
|
||||
])
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.env("GIT_SSH_COMMAND", ssh)
|
||||
.env("GIT_TERMINAL_PROMPT", "0")
|
||||
.output()
|
||||
.await?;
|
||||
if !out.status.success() {
|
||||
anyhow::bail!("git {}: {}", args.first().unwrap_or(&""), String::from_utf8_lossy(&out.stderr).trim());
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&out.stdout).trim().to_owned())
|
||||
}
|
||||
|
||||
/// The server's own clone: a cache of the remote, realigned on it before each
|
||||
/// commit so a push never needs a merge.
|
||||
async fn checkout(state: &AppState) -> anyhow::Result<PathBuf> {
|
||||
let publish = state.cfg.publish.as_ref().ok_or_else(|| anyhow::anyhow!("publication not configured"))?;
|
||||
let dir = state.cfg.data_dir.join("textes");
|
||||
if !dir.join(".git").exists() {
|
||||
tokio::fs::create_dir_all(&dir).await?;
|
||||
git(&dir, state, &["init", "-q"]).await?;
|
||||
git(&dir, state, &["remote", "add", "origin", &publish.remote]).await?;
|
||||
}
|
||||
git(&dir, state, &["remote", "set-url", "origin", &publish.remote]).await?;
|
||||
let has_remote_branch = git(&dir, state, &["ls-remote", "--heads", "origin", &publish.branch]).await?;
|
||||
if has_remote_branch.is_empty() {
|
||||
git(&dir, state, &["checkout", "-q", "-f", "-B", &publish.branch]).await?;
|
||||
} else {
|
||||
// This clone is a cache: whatever a failed pass left is re-rendered from the database.
|
||||
git(&dir, state, &["fetch", "-q", "origin", &publish.branch]).await?;
|
||||
git(&dir, state, &["checkout", "-q", "-f", "-B", &publish.branch, &format!("origin/{}", publish.branch)])
|
||||
.await?;
|
||||
}
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
async fn publish_doc(state: &AppState, doc_id: &str) -> anyhow::Result<Option<String>> {
|
||||
let st = docs::load_state(state, doc_id).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let Some(path) = st.doc.publish_path.clone() else { return Ok(None) };
|
||||
let rendered = render(state, &st).await?;
|
||||
let dir = checkout(state).await?;
|
||||
let target = dir.join(&path);
|
||||
tokio::fs::create_dir_all(&target).await?;
|
||||
for (name, content) in &rendered {
|
||||
tokio::fs::write(target.join(name), content).await?;
|
||||
}
|
||||
let readme = dir.join("README.md");
|
||||
if !readme.exists() {
|
||||
tokio::fs::write(&readme, README).await?;
|
||||
}
|
||||
git(&dir, state, &["add", "-A", "--", &path, "README.md"]).await?;
|
||||
let staged = git(&dir, state, &["diff", "--cached", "--name-only"]).await?;
|
||||
if staged.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let reasons: Vec<String> =
|
||||
sqlx::query_scalar("SELECT reason FROM outbox WHERE doc_id = ? AND done_at IS NULL ORDER BY id")
|
||||
.bind(doc_id)
|
||||
.fetch_all(&state.db)
|
||||
.await?;
|
||||
let title = util::parse_lang(&st.doc.title).get(&st.doc.source_lang).cloned().unwrap_or_default();
|
||||
let mut message = format!("{title} — {}", reasons.last().cloned().unwrap_or_else(|| "mise à jour".into()));
|
||||
if reasons.len() > 1 {
|
||||
message.push_str("\n\n");
|
||||
for r in &reasons {
|
||||
message.push_str(&format!("- {r}\n"));
|
||||
}
|
||||
}
|
||||
git(&dir, state, &["commit", "-q", "-m", &message]).await?;
|
||||
let branch = state.cfg.publish.as_ref().map(|p| p.branch.clone()).unwrap_or_else(|| "main".into());
|
||||
git(&dir, state, &["push", "-q", "origin", &format!("HEAD:{branch}")]).await?;
|
||||
Ok(Some(git(&dir, state, &["rev-parse", "HEAD"]).await?))
|
||||
}
|
||||
|
||||
const README: &str = "# Textes en vigueur — libreDecision\n\n\
|
||||
Ce dépôt est écrit par libreDecision (https://decision.librodrome.org) : chaque fois qu'un article \
|
||||
entre en vigueur par le vote, qu'une coquille est corrigée ou qu'une traduction change, le texte en \
|
||||
vigueur est republié ici. Les applications clientes le lisent pour se mettre à jour.\n\n\
|
||||
- `<groupe>/<document>/fr.md` : le texte qui fait foi (langue source) ;\n\
|
||||
- `en.md`, `es.md` : traductions éditoriales, sans vote ;\n\
|
||||
- `document.json` : le même contenu, structuré, avec l'état du vote de chaque article.\n\n\
|
||||
Ne pas modifier à la main : le prochain passage de libreDecision réécrit ces fichiers.\n";
|
||||
|
||||
/// Markdown per language + JSON. Only the units in force, in order.
|
||||
pub async fn render(state: &AppState, st: &DocState) -> anyhow::Result<Vec<(String, String)>> {
|
||||
let titles = util::parse_lang(&st.doc.title);
|
||||
let src = st.doc.source_lang.clone();
|
||||
let in_force: Vec<_> = st
|
||||
.units
|
||||
.iter()
|
||||
.filter_map(|u| {
|
||||
let fid = u.in_force_id.as_ref()?;
|
||||
st.proposals.iter().find(|p| &p.id == fid).map(|p| (u, p))
|
||||
})
|
||||
.collect();
|
||||
let refs: Vec<&docs::ProposalRow> = in_force.iter().map(|(_, p)| *p).collect();
|
||||
let tr = docs::translations_for(state, &refs).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let updated = util::now();
|
||||
let source_url = format!("{}/g/{}/docs/{}", state.cfg.public_url, st.group.slug, st.doc.slug);
|
||||
let mut files = Vec::new();
|
||||
for lang in util::LANGS {
|
||||
let title = titles.get(lang).or_else(|| titles.get(&src)).cloned().unwrap_or_default();
|
||||
let mut md = format!(
|
||||
"---\ndocument: {}\nlangue: {lang}\nsource: {source_url}\nmise_a_jour: {updated}\n{}---\n\n# {title}\n",
|
||||
st.doc.slug,
|
||||
if lang == src { "fait_foi: oui\n".to_owned() } else { "traduction: editoriale\n".to_owned() },
|
||||
);
|
||||
for (_, p) in &in_force {
|
||||
let t = tr.get(&p.id).and_then(|m| m.get(lang));
|
||||
let (ut, ub, fallback) = match t {
|
||||
Some(t) if lang != src => (
|
||||
t["title"].as_str().unwrap_or(&p.title).to_owned(),
|
||||
t["body"].as_str().unwrap_or(&p.body).to_owned(),
|
||||
t["stale"].as_bool().unwrap_or(false),
|
||||
),
|
||||
_ => (p.title.clone(), p.body.clone(), lang != src),
|
||||
};
|
||||
md.push_str(&format!("\n## {ut}\n\n{ub}\n"));
|
||||
if fallback {
|
||||
md.push_str(&format!(
|
||||
"\n<!-- {lang} : traduction à revoir ou absente, texte source affiché si absente -->\n"
|
||||
));
|
||||
}
|
||||
}
|
||||
files.push((format!("{lang}.md"), md));
|
||||
}
|
||||
let units: Vec<_> = in_force
|
||||
.iter()
|
||||
.map(|(u, p)| {
|
||||
let mut title = serde_json::Map::new();
|
||||
let mut body = serde_json::Map::new();
|
||||
title.insert(src.clone(), json!(p.title));
|
||||
body.insert(src.clone(), json!(p.body));
|
||||
if let Some(m) = tr.get(&p.id) {
|
||||
for (lang, t) in m {
|
||||
title.insert(lang.clone(), t["title"].clone());
|
||||
body.insert(lang.clone(), t["body"].clone());
|
||||
}
|
||||
}
|
||||
let tally = st.tally(&p.id);
|
||||
json!({
|
||||
"code": u.code, "title": title, "body": body, "inertia": u.inertia,
|
||||
"status": if p.entered_at.is_some() { "adopted" } else if p.confirmed_at.is_some() { "confirmed" } else { "seed-minimal" },
|
||||
"enteredAt": p.entered_at, "votes": { "for": tally.for_votes, "against": tally.against },
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let json = json!({
|
||||
"document": st.doc.slug, "group": st.group.slug, "title": titles, "sourceLang": src,
|
||||
"source": source_url, "updatedAt": updated, "electorate": st.electorate,
|
||||
"voteFloor": st.doc.vote_floor, "units": units,
|
||||
});
|
||||
files.push(("document.json".into(), format!("{}\n", serde_json::to_string_pretty(&json)?)));
|
||||
Ok(files)
|
||||
}
|
||||
Reference in New Issue
Block a user