Share rewards: a question for a visit, an analysis for a new user
A click proves little, so the first visit of a shared link now earns a question (once per user) instead of a natal analysis. A natal analysis is earned by bringing a new user: the share page remembers the link, the Google sign-up sends it, and when that user makes their own chart the sharer gets one, at most three (referrals table, referral_reward). This is off until sign-up opens (REFERRAL_REWARDS, default false). The share button and its small print follow; /v1/me tells what was earned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -9,8 +9,10 @@
|
||||
//! share page reports a visit from the visitor's browser
|
||||
//! (`POST /v1/public/shares/{slug}/visit`): it is counted unless it comes
|
||||
//! from a robot or from the sharer (logged in, or from the address the link
|
||||
//! was made at). The sharer's first such visit earns one free natal analysis,
|
||||
//! once per user.
|
||||
//! was made at). The sharer's first such visit earns one free question, once
|
||||
//! per user: a click proves little. A natal analysis is earned by bringing a
|
||||
//! new user (`REFERRAL_REWARDS`): someone who signs up from a shared link and
|
||||
//! makes their own chart, at most [`REFERRAL_LIMIT`] times.
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::{Path, State};
|
||||
@@ -332,7 +334,7 @@ pub async fn visit(
|
||||
sqlx::query("UPDATE shares SET view_count = view_count + 1 WHERE id = $1").bind(share.id).execute(&mut *tx).await?;
|
||||
let rewarded = sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, note) \
|
||||
VALUES ($1, $2, 'natal', 1, 'share_reward', $3) ON CONFLICT DO NOTHING",
|
||||
VALUES ($1, $2, 'question', 1, 'share_reward', $3) ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(share.owner_id)
|
||||
@@ -348,6 +350,72 @@ pub async fn visit(
|
||||
Ok(Json(json!({ "counted": true })))
|
||||
}
|
||||
|
||||
/// Natal analyses a user can earn by bringing new users through shared links.
|
||||
pub const REFERRAL_LIMIT: i64 = 3;
|
||||
|
||||
/// A new user came from a shared link (`slug`): remembered for the reward,
|
||||
/// unless rewards are off, the link is unknown or revoked, or it is their own.
|
||||
pub(super) async fn record_referral(
|
||||
conn: &mut sqlx::PgConnection,
|
||||
enabled: bool,
|
||||
user_id: Uuid,
|
||||
slug: Option<&str>,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let Some(slug) = slug.map(str::trim).filter(|s| enabled && !s.is_empty()) else {
|
||||
return Ok(());
|
||||
};
|
||||
sqlx::query(
|
||||
"INSERT INTO referrals (user_id, referrer_id, share_id) \
|
||||
SELECT $1, owner_id, id FROM shares WHERE slug = $2 AND revoked_at IS NULL AND owner_id <> $1 \
|
||||
ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(slug)
|
||||
.execute(conn)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The new user made their own chart: the sharer who brought them gets a
|
||||
/// natal analysis, while under the limit. Runs once per new user.
|
||||
pub(super) async fn reward_referral(state: &crate::AppState, user_id: Uuid) -> Result<(), ApiError> {
|
||||
if !state.config.referral_rewards {
|
||||
return Ok(());
|
||||
}
|
||||
let mut tx = state.db.begin().await?;
|
||||
let referrer: Option<Uuid> =
|
||||
sqlx::query_scalar("SELECT referrer_id FROM referrals WHERE user_id = $1 AND rewarded_at IS NULL FOR UPDATE")
|
||||
.bind(user_id)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
let Some(referrer) = referrer else { return Ok(()) };
|
||||
sqlx::query("SELECT id FROM users WHERE id = $1 FOR UPDATE").bind(referrer).execute(&mut *tx).await?;
|
||||
let earned = referral_rewards(&mut tx, referrer).await?;
|
||||
if earned < REFERRAL_LIMIT {
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, note) \
|
||||
VALUES ($1, $2, 'natal', 1, 'referral_reward', $3)",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(referrer)
|
||||
.bind(format!("new user {user_id} from a shared link"))
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
tracing::info!(referrer = %referrer, user = %user_id, "referral reward granted");
|
||||
}
|
||||
sqlx::query("UPDATE referrals SET rewarded_at = now() WHERE user_id = $1").bind(user_id).execute(&mut *tx).await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Natal analyses the user has earned by bringing new users.
|
||||
pub async fn referral_rewards(conn: &mut sqlx::PgConnection, user_id: Uuid) -> Result<i64, sqlx::Error> {
|
||||
sqlx::query_scalar("SELECT count(*) FROM credit_ledger WHERE user_id = $1 AND reason = 'referral_reward'")
|
||||
.bind(user_id)
|
||||
.fetch_one(conn)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Whether the user has already received the reward for sharing.
|
||||
pub async fn reward_granted(db: &PgPool, user_id: Uuid) -> Result<bool, sqlx::Error> {
|
||||
sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM credit_ledger WHERE user_id = $1 AND reason = 'share_reward')")
|
||||
|
||||
Reference in New Issue
Block a user