Skip to content

Commit

Permalink
Retrieve metadata from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Dec 18, 2024
1 parent 0ca2d88 commit cbeee6b
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions components/remote_settings/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{client::CollectionMetadata, Attachment, RemoteSettingsRecord, Result};
use crate::{
client::CollectionMetadata, client::CollectionSignature, Attachment, RemoteSettingsRecord,
Result,
};
use camino::Utf8PathBuf;
use rusqlite::{params, Connection, OptionalExtension};
use serde_json;
Expand Down Expand Up @@ -184,6 +187,40 @@ impl Storage {
Ok(())
}

pub fn get_collection_content(
&mut self,
collection_url: &str,
) -> Result<Option<(u64, Vec<RemoteSettingsRecord>, CollectionMetadata)>> {
let result = {
let mut stmt_metadata = self.conn.prepare(
"SELECT last_modified, signature, public_key, x5u FROM collection_metadata WHERE collection_url = ?",
)?;

stmt_metadata
.query_row(params![collection_url], |row| {
Ok((
row.get::<_, u64>(0).unwrap_or_default(),
CollectionMetadata {
signature: CollectionSignature {
signature: row.get(1).unwrap_or_default(),
public_key: row.get(2).unwrap_or_default(),
x5u: row.get(3).unwrap_or_default(),
},
},
))
})
.optional()?
};

if let Some((timestamp, metadata)) = result {
// Call `get_records` after the mutable borrow of `stmt_metadata` has ended
if let Some(records) = self.get_records(collection_url)? {
return Ok(Some((timestamp, records.to_vec(), metadata)));
}
}
Ok(None)
}

/// Set the attachment data stored in the database, clearing out any previously stored data
pub fn set_attachment(
&mut self,
Expand Down Expand Up @@ -227,7 +264,7 @@ impl Storage {
#[cfg(test)]
mod tests {
use super::Storage;
use crate::{client::CollectionMetadata, Attachment, RemoteSettingsRecord, Result};
use crate::{client::CollectionMetadata, client::CollectionSignature, Attachment, RemoteSettingsRecord, Result};
use sha2::{Digest, Sha256};

#[test]
Expand Down Expand Up @@ -659,4 +696,45 @@ mod tests {

Ok(())
}

#[test]
fn test_storage_get_collection_content() -> Result<()> {
let mut storage = Storage::new(":memory:".into())?;

let collection_url = "https://example.com/api";
let initial_records = vec![RemoteSettingsRecord {
id: "2".to_string(),
last_modified: 200,
deleted: false,
attachment: None,
fields: serde_json::json!({"key": "value2"})
.as_object()
.unwrap()
.clone(),
}];

// Set initial records
storage.set_collection_content(
collection_url,
&initial_records,
1337,
CollectionMetadata {
signature: CollectionSignature {
signature: "b64encodedsig".into(),
public_key: "some public key".into(),
x5u: "http://x5u/".into(),
},
}
)?;

let (timestamp, records, metadata) = storage.get_collection_content(collection_url)?.unwrap();

assert_eq!(timestamp, 1337);
assert_eq!(records, initial_records);
assert_eq!(metadata.signature.signature, "b64encodedsig");
assert_eq!(metadata.signature.public_key, "some public key");
assert_eq!(metadata.signature.x5u, "http://x5u/");

Ok(())
}
}

0 comments on commit cbeee6b

Please sign in to comment.