Sharing a chart with its short summary, with link previews
CI / test (push) Successful in 1m10s
CI / build (push) Successful in 4m33s
CI / deploy (push) Skipped

The summary card gets a share button: a public link (/s/<slug>) with the
wheel and the newest short summary, no name or birth data. The page carries
og:/twitter: tags, and /s/<slug>/kep/<version>.png renders a 1200x630 preview
(wheel, title, text, big three) with next/og and bundled fonts. The version
changes with the summary, so Facebook fetches a rewritten one again.

Backend: a shared chart comes with its newest finished summary, and sharing
the same thing again returns the open link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-19 07:37:06 +00:00
co-authored by Claude Opus 5.5
parent da533e8fa9
commit c7600608d3
21 changed files with 1001 additions and 0 deletions
+44
View File
@@ -1,6 +1,9 @@
//! Public sharing links. Creating and revoking a link needs login; opening
//! one (`GET /v1/public/shares/{slug}`) does not. The public view never
//! contains birth data unless the owner allowed it for their own chart.
//! A shared chart comes with its newest short summary (the share page and
//! its preview image show both). Sharing the same thing again returns the
//! link that is already open.
use axum::Json;
use axum::extract::{Path, State};
@@ -109,6 +112,24 @@ pub async fn create(
}
};
let title = req.title.map(|t| t.trim().chars().take(120).collect::<String>()).filter(|t| !t.is_empty());
let open: Option<ShareRow> = sqlx::query_as(&format!(
"SELECT {SHARE_COLUMNS} FROM shares WHERE owner_id = $1 AND kind = $2 AND revoked_at IS NULL \
AND chart_id IS NOT DISTINCT FROM $3 AND synastry_id IS NOT DISTINCT FROM $4 \
AND analysis_id IS NOT DISTINCT FROM $5 AND title IS NOT DISTINCT FROM $6 AND show_birth_data = $7 \
ORDER BY created_at DESC LIMIT 1"
))
.bind(auth.id())
.bind(kind)
.bind(chart_id)
.bind(synastry_id)
.bind(analysis_id)
.bind(&title)
.bind(req.show_birth_data)
.fetch_optional(&state.db)
.await?;
if let Some(row) = open {
return Ok((StatusCode::OK, Json(row.view(&state.config.public_base_url))));
}
let row: ShareRow = sqlx::query_as(&format!(
"INSERT INTO shares (id, owner_id, slug, kind, chart_id, synastry_id, analysis_id, title, show_birth_data) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING {SHARE_COLUMNS}"
@@ -175,6 +196,7 @@ async fn public_body(db: &PgPool, share: &ShareRow) -> Result<Value, ApiError> {
if let Some(chart_id) = share.chart_id {
let chart = find_chart(db, chart_id).await?.ok_or(ApiError::NotFound("share"))?;
body["chart"] = public_chart_block(&chart, share);
body["summary"] = latest_summary(db, chart_id).await?;
} else if let Some(synastry_id) = share.synastry_id {
let s = load_synastry(db, synastry_id).await?.ok_or(ApiError::NotFound("share"))?;
body["synastry"] = public_synastry_block(&s, share);
@@ -198,6 +220,28 @@ async fn public_body(db: &PgPool, share: &ShareRow) -> Result<Value, ApiError> {
Ok(body)
}
/// The newest finished short summary of a chart (`null` if none).
async fn latest_summary(db: &PgPool, chart_id: Uuid) -> Result<Value, sqlx::Error> {
type Row = (Uuid, String, Option<Value>, Option<OffsetDateTime>);
let row: Option<Row> = sqlx::query_as(
"SELECT id, language, content, completed_at FROM analyses \
WHERE chart_id = $1 AND kind = 'summary' AND status = 'done' ORDER BY completed_at DESC LIMIT 1",
)
.bind(chart_id)
.fetch_optional(db)
.await?;
Ok(match row {
Some((id, language, content, completed_at)) => json!({
"id": id,
"language": language,
"title": content.as_ref().and_then(|c| c["title"].as_str()),
"summary": content.as_ref().and_then(|c| c["summary"].as_str()),
"completed_at": rfc3339_opt(completed_at),
}),
None => Value::Null,
})
}
/// Public: the shared content for the share page and its OG image.
pub async fn public_view(State(state): St, Path(slug): Path<String>) -> ApiResult<Json<Value>> {
let share: ShareRow = sqlx::query_as(&format!(