Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sink): Bug in some event handling #11

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{Args, Parser, Subcommand};
use futures::{stream, StreamExt, TryStreamExt};
use ipfs::IpfsClient;
use sdk::mapping::{Entity, Named};
use sdk::{ids, pb::grc20};
use sdk::{ids, pb};
use sink::bootstrap::constants;
use sink::kg;
use tracing_subscriber::layer::SubscriberExt;
Expand Down Expand Up @@ -37,8 +37,8 @@ async fn main() -> anyhow::Result<()> {

// for (op_type, triple) in ops {
// match op_type {
// grc20::OpType::SetTriple => println!("SetTriple: {:?}", triple),
// grc20::OpType::DeleteTriple => println!("DeleteTriple: {:?}", triple),
// ipfs::OpType::SetTriple => println!("SetTriple: {:?}", triple),
// ipfs::OpType::DeleteTriple => println!("DeleteTriple: {:?}", triple),
// _ => (),
// }
// }
Expand Down Expand Up @@ -178,46 +178,49 @@ fn set_log_level() {
}

pub fn find_triples(
ops: impl IntoIterator<Item = grc20::Op>,
ops: impl IntoIterator<Item = pb::ipfs::Op>,
entity_id: &str,
) -> Vec<(grc20::OpType, grc20::Triple)> {
) -> Vec<(pb::ipfs::OpType, pb::ipfs::Triple)> {
ops.into_iter()
.filter_map(|op| match (op.r#type(), &op.triple) {
(
grc20::OpType::SetTriple,
Some(grc20::Triple {
pb::ipfs::OpType::SetTriple,
Some(pb::ipfs::Triple {
entity,
attribute,
value: Some(grc20::Value { value, .. }),
value: Some(pb::ipfs::Value { value, .. }),
}),
) if *entity == entity_id || *attribute == entity_id || *value == entity_id => Some((
grc20::OpType::SetTriple,
pb::ipfs::OpType::SetTriple,
op.triple.expect("Triple should be some"),
)),

(
grc20::OpType::DeleteTriple,
Some(grc20::Triple {
pb::ipfs::OpType::DeleteTriple,
Some(pb::ipfs::Triple {
entity,
attribute,
value: Some(grc20::Value { value, .. }),
value: Some(pb::ipfs::Value { value, .. }),
}),
) if *entity == entity_id || *attribute == entity_id || *value == entity_id => Some((
grc20::OpType::DeleteTriple,
pb::ipfs::OpType::DeleteTriple,
op.triple.expect("Triple should be some"),
)),
_ => None,
})
.collect()
}

async fn import_space(ipfs_client: &IpfsClient, ipfs_hash: &str) -> anyhow::Result<Vec<grc20::Op>> {
let import = ipfs_client.get::<grc20::Import>(ipfs_hash, true).await?;
async fn import_space(
ipfs_client: &IpfsClient,
ipfs_hash: &str,
) -> anyhow::Result<Vec<pb::ipfs::Op>> {
let import = ipfs_client.get::<pb::ipfs::Import>(ipfs_hash, true).await?;

Ok(stream::iter(import.edits)
.then(|edit_hash| async move {
let edit = ipfs_client
.get::<grc20::ImportEdit>(&edit_hash, true)
.get::<pb::ipfs::ImportEdit>(&edit_hash, true)
.await?;
anyhow::Ok(edit.ops)
})
Expand Down
110 changes: 0 additions & 110 deletions sdk/proto/grc20.proto

This file was deleted.

24 changes: 12 additions & 12 deletions sdk/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ids::create_geo_id, pb::grc20, relation::create_relationship, system_ids};
use crate::{ids::create_geo_id, pb::ipfs, relation::create_relationship, system_ids};

pub struct DataBlock;

Expand All @@ -8,7 +8,7 @@ impl DataBlock {
source_type: DataBlockType,
position: Option<&str>,
name: Option<&str>,
) -> impl Iterator<Item = grc20::Triple> {
) -> impl Iterator<Item = ipfs::Triple> {
let new_block_id = create_geo_id();

std::iter::empty()
Expand All @@ -34,11 +34,11 @@ impl DataBlock {
position,
))
// Set attribute: NewBlock.Name
.chain(name.map(|name| grc20::Triple {
.chain(name.map(|name| ipfs::Triple {
entity: new_block_id,
attribute: system_ids::NAME.to_string(),
value: Some(grc20::Value {
r#type: grc20::ValueType::Text.into(),
value: Some(ipfs::Value {
r#type: ipfs::ValueType::Text.into(),
value: name.to_string(),
}),
}))
Expand Down Expand Up @@ -68,7 +68,7 @@ impl TextBlock {
from_id: &str,
text: &str,
position: Option<&str>,
) -> impl Iterator<Item = grc20::Triple> {
) -> impl Iterator<Item = ipfs::Triple> {
let new_block_id = create_geo_id();

std::iter::empty()
Expand All @@ -87,11 +87,11 @@ impl TextBlock {
position,
))
// Set attribute: NewBlock.MarkdownContent
.chain(std::iter::once(grc20::Triple {
.chain(std::iter::once(ipfs::Triple {
entity: new_block_id,
attribute: system_ids::MARKDOWN_CONTENT.to_string(),
value: Some(grc20::Value {
r#type: grc20::ValueType::Text.into(),
value: Some(ipfs::Value {
r#type: ipfs::ValueType::Text.into(),
value: text.to_string(),
}),
}))
Expand All @@ -101,7 +101,7 @@ impl TextBlock {
// pub struct ImageBlock;

// impl ImageBlock {
// pub fn new(from_id: &str, url: &str, position: Option<&str>) -> impl Iterator<Item = grc20::Triple> {
// pub fn new(from_id: &str, url: &str, position: Option<&str>) -> impl Iterator<Item = ipfs::Triple> {
// let new_block_id = create_geo_id();

// std::iter::empty()
Expand All @@ -110,10 +110,10 @@ impl TextBlock {
// // Create relation: Entity > BLOCKS > NewBlock
// .chain(create_relationship(from_id, &new_block_id, system_ids::BLOCKS, position))
// // Set attribute: NewBlock.Url
// .chain(std::iter::once(grc20::Triple {
// .chain(std::iter::once(ipfs::Triple {
// entity: new_block_id,
// attribute: system_ids::URL.to_string(),
// value: Some(grc20::Value { r#type: grc20::ValueType::Text.into(), value: url.to_string() }),
// value: Some(ipfs::Value { r#type: ipfs::ValueType::Text.into(), value: url.to_string() }),
// }))
// }
// }
11 changes: 5 additions & 6 deletions sdk/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ids::Grc20Id, pb::grc20};
use crate::{ids::Grc20Id, pb::ipfs};

/// A trait for converting a type to a sequence of triples.
pub trait ToTriples {
fn to_triples(&self) -> impl Iterator<Item = grc20::Triple>;
fn to_triples(&self) -> impl Iterator<Item = ipfs::Triple>;
}

/// A trait for creating a type from a sequence of triples.
Expand All @@ -11,17 +11,16 @@ pub trait FromTriples: Sized {

fn from_triples(
id: Grc20Id,
triples: impl IntoIterator<Item = grc20::Triple>,
triples: impl IntoIterator<Item = ipfs::Triple>,
) -> Result<Self, Self::Error>;
}

pub trait ToOps {
fn to_ops(&self) -> impl Iterator<Item = grc20::Op>;
fn to_ops(&self) -> impl Iterator<Item = ipfs::Op>;
}

pub trait FromOps: Sized {
type Error;

fn from_ops(id: Grc20Id, ops: impl IntoIterator<Item = grc20::Op>)
-> Result<Self, Self::Error>;
fn from_ops(id: Grc20Id, ops: impl IntoIterator<Item = ipfs::Op>) -> Result<Self, Self::Error>;
}
10 changes: 5 additions & 5 deletions sdk/src/mapping/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ impl<T> Entity<T> {
space_id: &str,
entity_id: &str,
attribute_id: &str,
value: &pb::grc20::Value,
value: &pb::ipfs::Value,
) -> Result<(), DatabaseError> {
match (attribute_id, value.r#type(), value.value.as_str()) {
// Set the type of the entity
(system_ids::TYPES, pb::grc20::ValueType::Url, value) => {
(system_ids::TYPES, pb::ipfs::ValueType::Url, value) => {
const SET_TYPE_QUERY: &str = const_format::formatcp!(
r#"
MERGE (n {{ id: $id, space_id: $space_id }})
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<T> Entity<T> {
// Set the FROM_ENTITY or TO_ENTITY on a relation entity
(
system_ids::RELATION_FROM_ATTRIBUTE | system_ids::RELATION_TO_ATTRIBUTE,
pb::grc20::ValueType::Url,
pb::ipfs::ValueType::Url,
value,
) => {
let query = format!(
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<T> Entity<T> {
}

// Set the RELATION_TYPE on a relation entity
(system_ids::RELATION_TYPE_ATTRIBUTE, pb::grc20::ValueType::Url, value) => {
(system_ids::RELATION_TYPE_ATTRIBUTE, pb::ipfs::ValueType::Url, value) => {
const QUERY: &str = const_format::formatcp!(
r#"
MERGE (r {{ id: $id, space_id: $space_id }})
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<T> Entity<T> {
neo4j: &neo4rs::Graph,
block: &BlockMetadata,
space_id: &str,
triple: pb::grc20::Triple,
triple: pb::ipfs::Triple,
) -> Result<(), DatabaseError> {
let delete_triple_query = format!(
r#"
Expand Down
18 changes: 9 additions & 9 deletions sdk/src/mapping/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,18 @@ impl Display for ValueType {
}
}

impl TryFrom<pb::grc20::ValueType> for ValueType {
impl TryFrom<pb::ipfs::ValueType> for ValueType {
type Error = String;

fn try_from(value: pb::grc20::ValueType) -> Result<Self, Self::Error> {
fn try_from(value: pb::ipfs::ValueType) -> Result<Self, Self::Error> {
match value {
pb::grc20::ValueType::Text => Ok(ValueType::Text),
pb::grc20::ValueType::Number => Ok(ValueType::Number),
pb::grc20::ValueType::Checkbox => Ok(ValueType::Checkbox),
pb::grc20::ValueType::Url => Ok(ValueType::Url),
pb::grc20::ValueType::Time => Ok(ValueType::Time),
pb::grc20::ValueType::Point => Ok(ValueType::Point),
pb::grc20::ValueType::Unknown => Err("Unknown ValueType".to_string()),
pb::ipfs::ValueType::Text => Ok(ValueType::Text),
pb::ipfs::ValueType::Number => Ok(ValueType::Number),
pb::ipfs::ValueType::Checkbox => Ok(ValueType::Checkbox),
pb::ipfs::ValueType::Url => Ok(ValueType::Url),
pb::ipfs::ValueType::Time => Ok(ValueType::Time),
pb::ipfs::ValueType::Point => Ok(ValueType::Point),
pb::ipfs::ValueType::Unknown => Err("Unknown ValueType".to_string()),
}
}
}
Expand Down
Loading
Loading