Skip to content

Commit

Permalink
Use RecordKey in Repository::get API
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChat committed Feb 10, 2025
1 parent 66d078a commit 117c047
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions atrium-repo/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,14 @@ impl<R: AsyncBlockStoreRead> Repository<R> {
///
/// If you acknowledge the risks and want to access records via CID anyway, you will have to
/// do so by directly accessing the repository's backing storage.
pub async fn get<C: Collection>(&mut self, rkey: &str) -> Result<Option<C::Record>, Error> {
pub async fn get<C: Collection>(
&mut self,
rkey: RecordKey,
) -> Result<Option<C::Record>, Error> {
let path = C::repo_path(&rkey);
let mut mst = mst::Tree::open(&mut self.db, self.latest_commit.data);

if let Some(cid) = mst.get(rkey).await? {
if let Some(cid) = mst.get(&path).await? {
Ok(Some(read_record::<C>(&mut self.db, cid).await?))
} else {
Ok(None)
Expand Down
9 changes: 7 additions & 2 deletions examples/firehose/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
use atrium_api::types::string::RecordKey;
use chrono::Local;
use firehose::stream::frames::Frame;
use firehose::subscription::{CommitHandler, Subscription};
Expand Down Expand Up @@ -65,19 +66,23 @@ impl CommitHandler for Firehose {
.await?;

for op in &commit.ops {
let collection = op.path.split('/').next().expect("op.path is empty");
let mut s = op.path.split('/');
let collection = s.next().expect("op.path is empty");
let rkey = s.next().expect("no record key");
if op.action != "create" {
continue;
}

let rkey = RecordKey::new(rkey.to_string()).expect("invalid record key");

match collection {
feed::Post::NSID => {
// N.B: We do _NOT_ read out the record using `op.cid` because that is insecure.
// It bypasses the MST, which means that we cannot ensure that the contents are
// signed by the owner of the repository.
// You will always want to read out records using the MST to ensure they haven't been
// tampered with.
if let Some(record) = repo.get::<feed::Post>(&op.path).await? {
if let Some(record) = repo.get::<feed::Post>(rkey).await? {
println!(
"{} - {} - {}",
record.created_at.as_ref().with_timezone(&Local),
Expand Down

0 comments on commit 117c047

Please sign in to comment.