Sharing a chart with its short summary, with link previews
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:
@@ -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!(
|
||||
|
||||
@@ -628,6 +628,30 @@ async fn public_shares(pool: PgPool) {
|
||||
for hidden in ["input", "location", "time", "astronomy", "birth"] {
|
||||
assert!(shown.get(hidden).is_none(), "{hidden} must not be public");
|
||||
}
|
||||
assert!(r.body["summary"].is_null(), "no finished summary yet");
|
||||
|
||||
// the newest finished summary comes with the chart
|
||||
sqlx::query(
|
||||
"INSERT INTO analyses (id, kind, chart_id, requested_by, language, model, prompt_version, cache_key, \
|
||||
credits_charged, status, content, completed_at) \
|
||||
VALUES ($1, 'summary', $2::uuid, (SELECT id FROM users WHERE email = 'anna@example.com'), 'hu', 'm', 'v', \
|
||||
'k', 0, 'done', $3, now())",
|
||||
)
|
||||
.bind(uuid::Uuid::now_v7())
|
||||
.bind(chart_id)
|
||||
.bind(json!({"title": "Csendes erő", "summary": "Rövid **összefoglaló**."}))
|
||||
.execute(&app.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let r = app.get(&format!("/v1/public/shares/{slug}"), None).await;
|
||||
assert_eq!(r.body["summary"]["title"], "Csendes erő");
|
||||
assert_eq!(r.body["summary"]["summary"], "Rövid **összefoglaló**.");
|
||||
|
||||
// sharing the same again gives the open link
|
||||
let r = app
|
||||
.post("/v1/shares", Some(&anna), json!({"kind": "chart", "target_id": chart_id, "title": "A radixom"}))
|
||||
.await;
|
||||
assert_eq!((r.status, r.body["slug"].as_str()), (StatusCode::OK, Some(slug.as_str())));
|
||||
|
||||
let r = app
|
||||
.post("/v1/shares", Some(&anna), json!({"kind": "chart", "target_id": chart_id, "show_birth_data": true}))
|
||||
|
||||
Reference in New Issue
Block a user