[Fix] UserBlobs

This commit is contained in:
Alex Emmet 2026-09-07 20:26:02 +02:00
commit fa359bc2c8
No known key found for this signature in database
2 changed files with 54 additions and 42 deletions

View file

@ -1,4 +1,4 @@
/* Opaque client-owned data is stored without interpreting its encrypted bytes. */
/* Opaque client-owned bytes. Confidentiality is provided by the client-side blob format. */
use crate::storage_error::StorageError;
use crate::util::{db, sync};
use rusqlite::{Connection, OptionalExtension, Row, Transaction, params};
@ -17,6 +17,13 @@ pub struct UserBlob {
pub updated_at: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserBlobMetadata {
pub blob_id: String,
pub revision: i64,
pub updated_at: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeletedUserBlob {
pub id: i64,
@ -51,6 +58,14 @@ fn from_row(row: &Row<'_>) -> rusqlite::Result<UserBlob> {
})
}
fn metadata_from_row(row: &Row<'_>) -> rusqlite::Result<UserBlobMetadata> {
Ok(UserBlobMetadata {
blob_id: row.get(0)?,
revision: row.get(1)?,
updated_at: row.get(2)?,
})
}
fn load(tx: &Transaction<'_>, id: i64) -> Result<UserBlob, StorageError> {
Ok(tx.query_row("SELECT id, user_id, blob_id, blob, revision, updated_at FROM user_blobs WHERE id = ?1 AND deleted = 0", [id], from_row)?)
}
@ -143,6 +158,20 @@ pub fn list(user_id: i64) -> Result<Vec<UserBlob>, StorageError> {
&[],
)
}
pub fn list_metadata(user_id: i64) -> Result<Vec<UserBlobMetadata>, StorageError> {
if user_id <= 0 {
return Err(StorageError::Other("invalid blob owner".into()));
}
db::with_db(|conn| {
let mut statement = conn.prepare(
"SELECT blob_id, revision, updated_at FROM user_blobs WHERE user_id = ?1 AND deleted = 0",
)?;
let rows = statement.query_map([user_id], metadata_from_row)?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(StorageError::from)
})
}
pub fn list_by_ids(user_id: i64, ids: &[i64]) -> Result<Vec<UserBlob>, StorageError> {
if user_id <= 0 {
return Err(StorageError::Other("invalid blob owner".into()));