Share reward and less robot load on shared links
CI / test (push) Successful in 1m9s
CI / build (push) Successful in 5m9s
CI / deploy (push) Skipped

The first visit of a shared link by someone else earns the sharer one free
natal analysis, once per user. The share page reports visits from the
visitor's browser; robots, the logged-in sharer and the address the link was
made at do not count. The share button is a proper card that says so in small
print; /v1/me tells whether the reward is used up.

Robots: reading a share writes nothing (view_count counts people only), the
frontend keeps shared content for five minutes (cleared when a link is
revoked), and robots.txt turns away the known AI crawlers while leaving the
link preview robots alone.

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 09:26:06 +00:00
co-authored by Claude Opus 5.5
parent c7600608d3
commit 19df36369c
16 changed files with 370 additions and 23 deletions
+68 -1
View File
@@ -163,7 +163,22 @@ impl TestApp {
}
async fn call(&self, method: &str, uri: &str, token: Option<&str>, body: Option<Value>) -> Response {
self.call_with(method, uri, token, body, &[]).await
}
/// A request with extra headers (user agent, forwarded address).
async fn call_with(
&self,
method: &str,
uri: &str,
token: Option<&str>,
body: Option<Value>,
headers: &[(&str, &str)],
) -> Response {
let mut req = Request::builder().method(method).uri(uri);
for (name, value) in headers {
req = req.header(*name, *value);
}
if let Some(t) = token {
req = req.header("authorization", format!("Bearer {t}"));
}
@@ -706,7 +721,7 @@ async fn export_and_delete_account(pool: PgPool) {
/// config and catalog, shared links, invitation pages, the waitlist and
/// unsubscribing. Every other route (charts, calculations, analyses) needs a
/// logged-in user, so other apps cannot use them.
const PUBLIC_ROUTES: [&str; 14] = [
const PUBLIC_ROUTES: [&str; 15] = [
"/healthz",
"/v1/config",
"/v1/packages",
@@ -717,12 +732,64 @@ const PUBLIC_ROUTES: [&str; 14] = [
"/v1/auth/password-reset",
"/v1/auth/password-reset/confirm",
"/v1/public/shares/{slug}",
"/v1/public/shares/{slug}/visit",
"/v1/public/invites/{token}",
"/v1/public/invitations/unsubscribe",
"/v1/public/invitations/{token}",
"/v1/public/waitlist",
];
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn a_visit_by_someone_else_earns_one_analysis_once(pool: PgPool) {
let app = TestApp::new(pool).await;
let anna = app.register("anna@example.com", "Anna").await;
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
let share = json!({"kind": "chart", "target_id": chart["id"]});
let home = ("x-forwarded-for", "203.0.113.7");
let r = app.call_with("POST", "/v1/shares", Some(&anna), Some(share), &[home]).await;
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
let visit = format!("/v1/public/shares/{}/visit", r.body["slug"].as_str().unwrap());
let browser = ("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) Mobile/15E148");
let me = || async { app.get("/v1/me", Some(&anna)).await.body };
let before = me().await["analyses_left"]["natal"].as_i64().unwrap();
// robots, a missing user agent, the sharer's address and the sharer do not count
let elsewhere = ("x-forwarded-for", "198.51.100.1, 172.70.1.1");
for (token, headers) in [
(None, vec![("user-agent", "facebookexternalhit/1.1"), elsewhere]),
(None, vec![elsewhere]),
(None, vec![browser, home]),
(Some(anna.as_str()), vec![browser, elsewhere]),
] {
let r = app.call_with("POST", &visit, token, None, &headers).await;
assert_eq!((r.status, &r.body["counted"]), (StatusCode::OK, &json!(false)), "{headers:?}");
}
let body = me().await;
assert_eq!((body["analyses_left"]["natal"].as_i64(), &body["share_reward_granted"]), (Some(before), &json!(false)));
// reading the share counts nothing either
app.get(&visit.replace("/visit", ""), None).await;
// someone else: counted, and the reward comes once
for _ in 0..2 {
let r = app.call_with("POST", &visit, None, None, &[browser, elsewhere]).await;
assert_eq!(r.body["counted"], true);
}
let body = me().await;
assert_eq!(
(body["analyses_left"]["natal"].as_i64(), &body["share_reward_granted"]),
(Some(before + 1), &json!(true))
);
let r = app.get("/v1/shares", Some(&anna)).await;
assert_eq!(r.body["shares"][0]["view_count"], 2);
// a second link earns nothing more
let r =
app.post("/v1/shares", Some(&anna), json!({"kind": "chart", "target_id": chart["id"], "title": "Új"})).await;
let other = format!("/v1/public/shares/{}/visit", r.body["slug"].as_str().unwrap());
app.call_with("POST", &other, None, None, &[browser, elsewhere]).await;
assert_eq!(me().await["analyses_left"]["natal"].as_i64(), Some(before + 1));
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn everything_else_needs_login(pool: PgPool) {
let app = TestApp::new(pool).await;