|
| 1 | +//! Persistence for deployment-level Buzz product feedback. |
| 2 | +//! |
| 3 | +//! Feedback retains its source [`CommunityId`] as provenance, but is not a |
| 4 | +//! community moderation concern and is never inserted into the events table. |
| 5 | +
|
| 6 | +use chrono::{DateTime, Utc}; |
| 7 | +use serde::Serialize; |
| 8 | +use sqlx::{PgPool, Row as _}; |
| 9 | +use uuid::Uuid; |
| 10 | + |
| 11 | +use crate::{error::Result, CommunityId}; |
| 12 | + |
| 13 | +/// Validated fields from an accepted product-feedback event. |
| 14 | +#[derive(Debug, Clone)] |
| 15 | +pub struct NewProductFeedback<'a> { |
| 16 | + /// Signed feedback event id (32 bytes), used for idempotency. |
| 17 | + pub event_id: &'a [u8], |
| 18 | + /// Authenticated submitter's Nostr pubkey (32 bytes). |
| 19 | + pub submitter_pubkey: &'a [u8], |
| 20 | + /// Optional category from the relay-validated vocabulary. |
| 21 | + pub category: Option<&'a str>, |
| 22 | + /// Required free-text feedback body. |
| 23 | + pub body: &'a str, |
| 24 | + /// Full validated event tags (attachments and diagnostics metadata included). |
| 25 | + pub tags: &'a serde_json::Value, |
| 26 | + /// Timestamp signed into the source event. |
| 27 | + pub event_created_at: DateTime<Utc>, |
| 28 | +} |
| 29 | + |
| 30 | +/// Product-feedback row returned to deployment-operator tooling. |
| 31 | +#[derive(Debug, Clone, Serialize)] |
| 32 | +pub struct ProductFeedbackRecord { |
| 33 | + /// Sidecar row id. |
| 34 | + pub id: Uuid, |
| 35 | + /// Source community, retained as provenance only. |
| 36 | + pub community_id: Uuid, |
| 37 | + /// Signed source event id. |
| 38 | + pub event_id: String, |
| 39 | + /// Signed submitter pubkey. |
| 40 | + pub submitter_pubkey: String, |
| 41 | + /// Optional feedback category. |
| 42 | + pub category: Option<String>, |
| 43 | + /// Feedback body. |
| 44 | + pub body: String, |
| 45 | + /// Full source tags, including attachment and diagnostics metadata. |
| 46 | + pub tags: serde_json::Value, |
| 47 | + /// Timestamp signed into the source event. |
| 48 | + pub event_created_at: DateTime<Utc>, |
| 49 | + /// Time accepted by this deployment. |
| 50 | + pub received_at: DateTime<Utc>, |
| 51 | +} |
| 52 | + |
| 53 | +/// Insert product feedback, idempotent deployment-wide by signed event id. |
| 54 | +/// |
| 55 | +/// The first accepted submission owns the provenance row. Replaying the exact |
| 56 | +/// same signed event through another community returns the same row without |
| 57 | +/// changing its source community; callers intentionally receive the same |
| 58 | +/// successful acknowledgment in both cases. |
| 59 | +pub async fn insert( |
| 60 | + pool: &PgPool, |
| 61 | + community: CommunityId, |
| 62 | + feedback: NewProductFeedback<'_>, |
| 63 | +) -> Result<Uuid> { |
| 64 | + let row = sqlx::query( |
| 65 | + r#" |
| 66 | + INSERT INTO product_feedback ( |
| 67 | + community_id, event_id, submitter_pubkey, category, body, tags, |
| 68 | + event_created_at |
| 69 | + ) VALUES ($1, $2, $3, $4, $5, $6, $7) |
| 70 | + ON CONFLICT (event_id) DO UPDATE SET |
| 71 | + event_id = EXCLUDED.event_id |
| 72 | + RETURNING id |
| 73 | + "#, |
| 74 | + ) |
| 75 | + .bind(community.as_uuid()) |
| 76 | + .bind(feedback.event_id) |
| 77 | + .bind(feedback.submitter_pubkey) |
| 78 | + .bind(feedback.category) |
| 79 | + .bind(feedback.body) |
| 80 | + .bind(feedback.tags) |
| 81 | + .bind(feedback.event_created_at) |
| 82 | + .fetch_one(pool) |
| 83 | + .await?; |
| 84 | + |
| 85 | + Ok(row.try_get("id")?) |
| 86 | +} |
| 87 | + |
| 88 | +/// List feedback across all communities, newest received first. |
| 89 | +pub async fn list(pool: &PgPool, limit: i64) -> Result<Vec<ProductFeedbackRecord>> { |
| 90 | + let rows = sqlx::query( |
| 91 | + r#" |
| 92 | + SELECT id, community_id, event_id, submitter_pubkey, category, body, |
| 93 | + tags, event_created_at, received_at |
| 94 | + FROM product_feedback |
| 95 | + ORDER BY received_at DESC, id |
| 96 | + LIMIT $1 |
| 97 | + "#, |
| 98 | + ) |
| 99 | + .bind(limit) |
| 100 | + .fetch_all(pool) |
| 101 | + .await?; |
| 102 | + |
| 103 | + rows.into_iter() |
| 104 | + .map(|row| { |
| 105 | + Ok(ProductFeedbackRecord { |
| 106 | + id: row.try_get("id")?, |
| 107 | + community_id: row.try_get("community_id")?, |
| 108 | + event_id: hex::encode(row.try_get::<Vec<u8>, _>("event_id")?), |
| 109 | + submitter_pubkey: hex::encode(row.try_get::<Vec<u8>, _>("submitter_pubkey")?), |
| 110 | + category: row.try_get("category")?, |
| 111 | + body: row.try_get("body")?, |
| 112 | + tags: row.try_get("tags")?, |
| 113 | + event_created_at: row.try_get("event_created_at")?, |
| 114 | + received_at: row.try_get("received_at")?, |
| 115 | + }) |
| 116 | + }) |
| 117 | + .collect() |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(test)] |
| 121 | +mod tests { |
| 122 | + use super::*; |
| 123 | + |
| 124 | + #[tokio::test] |
| 125 | + #[ignore = "requires migrated Postgres"] |
| 126 | + async fn duplicate_event_keeps_first_community_provenance() { |
| 127 | + let database_url = std::env::var("BUZZ_TEST_DATABASE_URL") |
| 128 | + .or_else(|_| std::env::var("DATABASE_URL")) |
| 129 | + .expect("BUZZ_TEST_DATABASE_URL or DATABASE_URL"); |
| 130 | + let pool = PgPool::connect(&database_url) |
| 131 | + .await |
| 132 | + .expect("connect test DB"); |
| 133 | + let first = Uuid::new_v4(); |
| 134 | + let second = Uuid::new_v4(); |
| 135 | + for (id, host) in [ |
| 136 | + (first, format!("feedback-first-{first}.test")), |
| 137 | + (second, format!("feedback-second-{second}.test")), |
| 138 | + ] { |
| 139 | + sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)") |
| 140 | + .bind(id) |
| 141 | + .bind(host) |
| 142 | + .execute(&pool) |
| 143 | + .await |
| 144 | + .expect("insert community"); |
| 145 | + } |
| 146 | + |
| 147 | + let mut event_id = [0_u8; 32]; |
| 148 | + event_id[..16].copy_from_slice(Uuid::new_v4().as_bytes()); |
| 149 | + event_id[16..].copy_from_slice(Uuid::new_v4().as_bytes()); |
| 150 | + let pubkey = [7_u8; 32]; |
| 151 | + let tags = serde_json::json!([["category", "bug"]]); |
| 152 | + let feedback = || NewProductFeedback { |
| 153 | + event_id: &event_id, |
| 154 | + submitter_pubkey: &pubkey, |
| 155 | + category: Some("bug"), |
| 156 | + body: "same signed feedback", |
| 157 | + tags: &tags, |
| 158 | + event_created_at: Utc::now(), |
| 159 | + }; |
| 160 | + |
| 161 | + let first_row = insert(&pool, CommunityId::from_uuid(first), feedback()) |
| 162 | + .await |
| 163 | + .expect("first insert"); |
| 164 | + let duplicate_row = insert(&pool, CommunityId::from_uuid(second), feedback()) |
| 165 | + .await |
| 166 | + .expect("duplicate insert"); |
| 167 | + assert_eq!(duplicate_row, first_row); |
| 168 | + |
| 169 | + let rows: Vec<Uuid> = |
| 170 | + sqlx::query_scalar("SELECT community_id FROM product_feedback WHERE event_id = $1") |
| 171 | + .bind(event_id.as_slice()) |
| 172 | + .fetch_all(&pool) |
| 173 | + .await |
| 174 | + .expect("read feedback provenance"); |
| 175 | + assert_eq!(rows, vec![first]); |
| 176 | + |
| 177 | + sqlx::query("DELETE FROM product_feedback WHERE event_id = $1") |
| 178 | + .bind(event_id.as_slice()) |
| 179 | + .execute(&pool) |
| 180 | + .await |
| 181 | + .expect("delete feedback"); |
| 182 | + sqlx::query("DELETE FROM communities WHERE id = ANY($1)") |
| 183 | + .bind(vec![first, second]) |
| 184 | + .execute(&pool) |
| 185 | + .await |
| 186 | + .expect("delete communities"); |
| 187 | + } |
| 188 | +} |
0 commit comments