Inviting a person to compare charts
CI / test (push) Successful in 1m15s
CI / build (push) Successful in 5m27s
CI / deploy (push) Skipped

The new person page offers 'Adatok megadása' (typed-in data, uses a slot)
or 'Meghívom': a 'how compatible are we?' link with an optional name,
e-mail and message (no links, 300 chars), shareable by copy, the phone's
share menu, WhatsApp or Facebook. With an address we e-mail it too (at most
10 a day per user, opted-out addresses skipped, unsubscribe link included).
The invitee lands on /meghivas/<token>, signs in, gives their own birth data
if needed and gets the comparison; the inviter is told and lists pending
invitations (revocable) on the persons page. No person slot is used.

Also: the accepted e-mail links to /osszevetesek/<id>, and the cross grid no
longer breaks on another user's chart (no highlights there).

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 10:12:51 +00:00
co-authored by Claude Opus 5.5
parent 7b23d71526
commit 4f09aac3f5
16 changed files with 820 additions and 39 deletions
+67 -9
View File
@@ -24,6 +24,8 @@ use crate::error::{ApiError, ApiResult};
use crate::views::{public_chart, rfc3339, rfc3339_opt, synastry_hash};
const INVITE_TTL: time::Duration = time::Duration::days(14);
/// Invitations a user may have us e-mail in a day (the rest go by link).
const EMAILED_PER_DAY: i64 = 10;
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct SynastryRow {
@@ -238,6 +240,10 @@ pub struct CreateInvite {
/// Defaults to the user's primary chart.
chart_id: Option<Uuid>,
message: Option<String>,
/// Who it is for, as the inviter calls them (only the inviter sees it).
label: Option<String>,
/// Have us e-mail the invitation to this address.
email: Option<String>,
}
async fn chart_or_primary(db: &PgPool, chart_id: Option<Uuid>, user: Uuid) -> Result<ChartRow, ApiError> {
@@ -255,13 +261,57 @@ pub async fn create_invite(
WithRejection(Json(req), _): Body<CreateInvite>,
) -> ApiResult<(StatusCode, Json<Value>)> {
let chart = chart_or_primary(&state.db, req.chart_id, auth.id()).await?;
let message = req.message.map(|m| m.trim().chars().take(500).collect::<String>()).filter(|m| !m.is_empty());
// No links: the message may go out in an e-mail from our domain.
let message = super::invitations::clean_message(req.message)?;
let label = req.label.map(|l| l.trim().chars().take(80).collect::<String>()).filter(|l| !l.is_empty());
let email = match req.email.as_deref().map(str::trim).filter(|e| !e.is_empty()) {
Some(raw) => Some(super::auth::normalize_email(raw)?),
None => None,
};
let token = db::new_token();
let id = Uuid::now_v7();
let expires_at = OffsetDateTime::now_utc() + INVITE_TTL;
let url = format!("{}/meghivas/{token}", state.config.public_base_url);
let mut tx = state.db.begin().await?;
let mut emailed = false;
if let Some(to) = &email {
if *to == auth.user.email {
return Err(ApiError::BadRequest("Saját magadat nem hívhatod meg.".into()));
}
let today: i64 = sqlx::query_scalar(
"SELECT count(*) FROM synastry_invites WHERE inviter_id = $1 AND invitee_email IS NOT NULL \
AND created_at > now() - interval '1 day'",
)
.bind(auth.id())
.fetch_one(&mut *tx)
.await?;
if today >= EMAILED_PER_DAY {
return Err(ApiError::conflict(
"invite_mail_limit",
format!(
"Naponta legfeljebb {EMAILED_PER_DAY} meghívót küldünk ki levélben. A linket továbbra is elküldheted."
),
));
}
// Someone who asked for no more invitations gets none; the link still works.
if !super::invitations::opted_out(&mut tx, to).await? {
let (_, unsubscribe_url, unsubscribe_post_url) = super::invitations::links(&state, &token);
let payload = json!({
"url": url,
"inviter_name": auth.user.display_name,
"message": message,
"expires_on": expires_at.date().to_string(),
"unsubscribe_url": unsubscribe_url,
"unsubscribe_post_url": unsubscribe_post_url,
});
super::auth::queue_email_to(&mut tx, None, to, "synastry_invite", &auth.user.locale, payload).await?;
emailed = true;
}
}
sqlx::query(
"INSERT INTO synastry_invites (id, inviter_id, inviter_chart_id, token_hash, message, expires_at) \
VALUES ($1, $2, $3, $4, $5, $6)",
"INSERT INTO synastry_invites (id, inviter_id, inviter_chart_id, token_hash, message, expires_at, label, \
invitee_email) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
)
.bind(id)
.bind(auth.id())
@@ -269,16 +319,22 @@ pub async fn create_invite(
.bind(db::token_hash(&token))
.bind(&message)
.bind(expires_at)
.execute(&state.db)
.bind(&label)
.bind(&email)
.execute(&mut *tx)
.await?;
tx.commit().await?;
Ok((
StatusCode::CREATED,
Json(json!({
"id": id,
"token": token,
"url": format!("{}/invite/{token}", state.config.public_base_url),
"url": url,
"chart_id": chart.id,
"message": message,
"label": label,
"email": email,
"emailed": emailed,
"expires_at": rfc3339(expires_at),
})),
))
@@ -304,10 +360,12 @@ pub async fn list_invites(State(state): St, auth: AuthUser) -> ApiResult<Json<Va
Option<OffsetDateTime>,
Option<Uuid>,
Option<String>,
Option<String>,
Option<String>,
);
let rows: Vec<Row> = sqlx::query_as(
"SELECT i.id, i.inviter_chart_id, i.message, i.created_at, i.expires_at, i.accepted_at, i.synastry_id, \
u.display_name \
u.display_name, i.label, i.invitee_email \
FROM synastry_invites i LEFT JOIN users u ON u.id = i.accepted_by \
WHERE i.inviter_id = $1 ORDER BY i.created_at DESC",
)
@@ -316,9 +374,9 @@ pub async fn list_invites(State(state): St, auth: AuthUser) -> ApiResult<Json<Va
.await?;
let list: Vec<Value> = rows
.into_iter()
.map(|(id, chart_id, message, created, expires, accepted, synastry_id, accepted_by)| {
.map(|(id, chart_id, message, created, expires, accepted, synastry_id, accepted_by, label, email)| {
json!({
"id": id, "chart_id": chart_id, "message": message,
"id": id, "chart_id": chart_id, "message": message, "label": label, "email": email,
"status": invite_status(accepted, expires),
"created_at": rfc3339(created), "expires_at": rfc3339(expires),
"accepted_at": rfc3339_opt(accepted), "accepted_by": accepted_by, "synastry_id": synastry_id,
@@ -425,7 +483,7 @@ pub async fn accept_invite(
.fetch_optional(&mut *tx)
.await?;
if let Some(inviter) = inviter {
let url = format!("{}/synastry/{}", state.config.public_base_url, row.id);
let url = format!("{}/osszevetesek/{}", state.config.public_base_url, row.id);
super::auth::queue_email(
&mut tx,
&inviter,