diff options
Diffstat (limited to 'src/db.rs')
| -rw-r--r-- | src/db.rs | 68 |
1 files changed, 68 insertions, 0 deletions
| @@ -84,6 +84,24 @@ impl MirrorDb { | |||
| 84 | CREATE INDEX IF NOT EXISTS idx_repos_pubkey ON repos(pubkey); | 84 | CREATE INDEX IF NOT EXISTS idx_repos_pubkey ON repos(pubkey); |
| 85 | CREATE INDEX IF NOT EXISTS idx_server_syncs_repo ON server_syncs(repo_id); | 85 | CREATE INDEX IF NOT EXISTS idx_server_syncs_repo ON server_syncs(repo_id); |
| 86 | CREATE INDEX IF NOT EXISTS idx_seen_events_id ON seen_events(event_id); | 86 | CREATE INDEX IF NOT EXISTS idx_seen_events_id ON seen_events(event_id); |
| 87 | |||
| 88 | CREATE TABLE IF NOT EXISTS nip46_sessions ( | ||
| 89 | npub TEXT PRIMARY KEY, | ||
| 90 | client_secret TEXT NOT NULL, | ||
| 91 | signer_pubkey TEXT, | ||
| 92 | connected INTEGER NOT NULL DEFAULT 0 | ||
| 93 | ); | ||
| 94 | |||
| 95 | CREATE TABLE IF NOT EXISTS signing_queue ( | ||
| 96 | id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| 97 | npub TEXT NOT NULL, | ||
| 98 | repo_identifier TEXT NOT NULL, | ||
| 99 | state_event_json TEXT NOT NULL, | ||
| 100 | status TEXT NOT NULL DEFAULT 'pending', | ||
| 101 | created_at INTEGER NOT NULL, | ||
| 102 | signed_at INTEGER, | ||
| 103 | error TEXT | ||
| 104 | ); | ||
| 87 | "#, | 105 | "#, |
| 88 | ) | 106 | ) |
| 89 | .execute(&self.pool) | 107 | .execute(&self.pool) |
| @@ -258,6 +276,56 @@ impl MirrorDb { | |||
| 258 | .await?; | 276 | .await?; |
| 259 | Ok(records) | 277 | Ok(records) |
| 260 | } | 278 | } |
| 279 | |||
| 280 | pub async fn get_nip46_session(&self, npub: &str) -> Result<Option<Nip46SessionRecord>> { | ||
| 281 | let result = sqlx::query_as::<_, Nip46SessionRecord>( | ||
| 282 | "SELECT * FROM nip46_sessions WHERE npub = ?", | ||
| 283 | ) | ||
| 284 | .bind(npub) | ||
| 285 | .fetch_optional(&self.pool) | ||
| 286 | .await?; | ||
| 287 | Ok(result) | ||
| 288 | } | ||
| 289 | |||
| 290 | pub async fn upsert_nip46_session( | ||
| 291 | &self, | ||
| 292 | npub: &str, | ||
| 293 | client_secret: &str, | ||
| 294 | signer_pubkey: Option<&str>, | ||
| 295 | connected: bool, | ||
| 296 | ) -> Result<()> { | ||
| 297 | sqlx::query( | ||
| 298 | r#"INSERT INTO nip46_sessions (npub, client_secret, signer_pubkey, connected) | ||
| 299 | VALUES (?, ?, ?, ?) | ||
| 300 | ON CONFLICT(npub) DO UPDATE SET client_secret = ?, signer_pubkey = ?, connected = ?"#, | ||
| 301 | ) | ||
| 302 | .bind(npub) | ||
| 303 | .bind(client_secret) | ||
| 304 | .bind(signer_pubkey) | ||
| 305 | .bind(connected as i32) | ||
| 306 | .bind(client_secret) | ||
| 307 | .bind(signer_pubkey) | ||
| 308 | .bind(connected as i32) | ||
| 309 | .execute(&self.pool) | ||
| 310 | .await?; | ||
| 311 | Ok(()) | ||
| 312 | } | ||
| 313 | |||
| 314 | pub async fn get_all_nip46_sessions(&self) -> Result<Vec<Nip46SessionRecord>> { | ||
| 315 | let records = | ||
| 316 | sqlx::query_as::<_, Nip46SessionRecord>("SELECT * FROM nip46_sessions") | ||
| 317 | .fetch_all(&self.pool) | ||
| 318 | .await?; | ||
| 319 | Ok(records) | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | #[derive(Debug, sqlx::FromRow)] | ||
| 324 | pub struct Nip46SessionRecord { | ||
| 325 | pub npub: String, | ||
| 326 | pub client_secret: String, | ||
| 327 | pub signer_pubkey: Option<String>, | ||
| 328 | pub connected: bool, | ||
| 261 | } | 329 | } |
| 262 | 330 | ||
| 263 | fn chrono_now_secs() -> i64 { | 331 | fn chrono_now_secs() -> i64 { |