Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221//! Hosted Space metadata, membership, writer announcements, and notification subscriptions.
use crate::space::{NotifiedWrite, NotifyRegistrationInput, SpacePageQuery, UpdateSpaceInput};use rusqlite::params;
use crate::{ database::{Database, constraint_conflict, now, sql_row}, error::{Error, Result}, space::domain::{Space, SpaceWriter},};
impl Database { pub(crate) fn space_is_deleted(&self, uri: &str) -> Result<Option<bool>> { self.query_optional( "SELECT deleted_at IS NOT NULL FROM spaces WHERE uri = ?1", [uri], |row| Ok(row.get(0)?), ) }
pub(crate) fn create_space(&self, space: &Space) -> Result<()> { let policy = serde_json::to_string(&space.policy)?; let app_access = serde_json::to_string(&space.app_access)?; self.transaction(|transaction| { let changed = transaction .execute( "INSERT INTO spaces (uri, authority, space_type, skey, policy_json, app_access_json, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?7) ON CONFLICT(uri) DO UPDATE SET policy_json = excluded.policy_json, app_access_json = excluded.app_access_json, deleted_at = NULL, created_at = excluded.created_at, updated_at = excluded.updated_at WHERE spaces.deleted_at IS NOT NULL", params![ space.uri, space.authority, space.kind, space.skey, policy, app_access, now(), ], ) .map_err(|error| constraint_conflict(error, "space already exists"))?; if changed == 0 { return Err(Error::Conflict("space already exists".to_owned())); } transaction.execute( "DELETE FROM space_members WHERE space_uri = ?1", [&space.uri], )?; transaction.execute( "INSERT INTO space_members (space_uri, did, created_at) VALUES (?1, ?2, ?3)", params![space.uri, space.authority, now()], )?; Ok(()) }) }
pub(crate) fn space(&self, uri: &str) -> Result<Space> { self.query_optional( "SELECT authority, space_type, skey, policy_json, app_access_json FROM spaces WHERE uri = ?1 AND deleted_at IS NULL", [uri], |row| { let (authority, kind, skey, policy, app_access) = <(_, _, _, String, String)>::try_from(row)?; Ok(Space { uri: uri.to_owned(), authority, kind, skey, policy: serde_json::from_str(&policy)?, app_access: serde_json::from_str(&app_access)?, }) }, )? .ok_or(Error::NotFound) }
pub(crate) fn update_space(&self, input: &UpdateSpaceInput) -> Result<()> { let policy = input .policy .as_ref() .map(serde_json::to_string) .transpose()?; let app_access = input .app_access .as_ref() .map(serde_json::to_string) .transpose()?; let changed = self.connection()?.execute( "UPDATE spaces SET policy_json = COALESCE(?2, policy_json), app_access_json = COALESCE(?3, app_access_json), updated_at = ?4 WHERE uri = ?1 AND deleted_at IS NULL", params![input.space, policy, app_access, now()], )?; if changed == 0 { return Err(Error::NotFound); } Ok(()) }
pub(crate) fn delete_space(&self, uri: &str, authority: &str) -> Result<()> { self.transaction(|transaction| { transaction.execute( "UPDATE spaces SET deleted_at = COALESCE(deleted_at, ?3), updated_at = ?3 WHERE uri = ?1 AND authority = ?2", params![uri, authority, now()], )?; transaction.execute( "DELETE FROM space_records WHERE space_uri = ?1 AND did = ?2", params![uri, authority], )?; transaction.execute( "DELETE FROM space_repo_ops WHERE space_uri = ?1 AND did = ?2", params![uri, authority], )?; Ok(()) }) }
pub(crate) fn add_space_member(&self, uri: &str, did: &str) -> Result<()> { self.connection()?.execute( "INSERT OR IGNORE INTO space_members (space_uri, did, created_at) VALUES (?1, ?2, ?3)", params![uri, did, now()], )?; Ok(()) }
pub(crate) fn remove_space_member(&self, uri: &str, did: &str) -> Result<()> { self.connection()?.execute( "DELETE FROM space_members WHERE space_uri = ?1 AND did = ?2", params![uri, did], )?; Ok(()) }
pub(crate) fn list_space_members(&self, query: &SpacePageQuery) -> Result<Vec<String>> { let limit = i64::try_from(query.limit.unwrap_or(100).clamp(1, 1000)).unwrap_or(i64::MAX); self.query( "SELECT did FROM space_members WHERE space_uri = ?1 AND (?2 IS NULL OR did > ?2) ORDER BY did LIMIT ?3", params![query.space, query.cursor, limit], |row| Ok(row.get(0)?), ) }
pub(crate) fn is_space_member(&self, uri: &str, did: &str) -> Result<bool> { self.connection()? .prepare("SELECT 1 FROM space_members WHERE space_uri = ?1 AND did = ?2")? .exists(params![uri, did]) .map_err(Into::into) }
pub(crate) fn record_space_writer(&self, input: &NotifiedWrite<'_>) -> Result<()> { self.connection()?.execute( "INSERT INTO space_writers (space_uri, did, rev, hash, updated_at) VALUES (?1, ?2, ?3, ?4, ?5) ON CONFLICT(space_uri, did) DO UPDATE SET rev = excluded.rev, hash = excluded.hash, updated_at = excluded.updated_at", params![input.space, input.repo, input.rev, input.hash, now()], )?; Ok(()) }
pub(crate) fn list_space_writers(&self, query: &SpacePageQuery) -> Result<Vec<SpaceWriter>> { let limit = i64::try_from(query.limit.unwrap_or(100).clamp(1, 1000)).unwrap_or(i64::MAX); self.query( "SELECT did, rev, hash FROM space_writers WHERE space_uri = ?1 AND (?2 IS NULL OR did > ?2) ORDER BY did LIMIT ?3", params![query.space, query.cursor, limit], |row| Ok(sql_row!(row => SpaceWriter { did: 0, rev: 1, hash: 2 })), ) }
pub(crate) fn register_space_notify( &self, input: &NotifyRegistrationInput, endpoint: &str, expires_at: i64, ) -> Result<()> { self.connection()?.execute( "INSERT INTO space_notify_registrations (space_uri, service, endpoint, expires_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5) ON CONFLICT(space_uri, service) DO UPDATE SET endpoint = excluded.endpoint, expires_at = excluded.expires_at, updated_at = excluded.updated_at", params![input.space, input.service, endpoint, expires_at, now()], )?; Ok(()) }
pub(crate) fn unregister_space_notify(&self, input: &NotifyRegistrationInput) -> Result<()> { self.connection()?.execute( "DELETE FROM space_notify_registrations WHERE space_uri = ?1 AND service = ?2", params![input.space, input.service], )?; Ok(()) }
pub(crate) fn space_notify_registrations(&self, space: &str) -> Result<Vec<(String, String)>> { self.query( "SELECT service, endpoint FROM space_notify_registrations WHERE space_uri = ?1 AND expires_at > ?2 ORDER BY service", params![space, now()], |row| Ok(row.try_into()?), ) }}