nostralink 0.2.4

Linked data library for nostr
Documentation
use super::{
    test_env, test_keys_0, test_keys_1, test_keys_2, test_keys_3,
    TestEnv,
};
use crate::niri::FromNamedNode;
use crate::prelude::*;
use nostr::{Filter, Kind, PublicKey};
use rstest::*;
use std::sync::Arc;

fn test_wot_trust_and_wot_trusted_list(
    test_env: TestEnv,
) -> Result<(), RdfStoreError> {
    let keys1 = Keys::generate();
    let keys2 = Keys::generate();

    let public_key1 = keys1.public_key();
    let public_key2 = keys2.public_key();

    // Establish trust between the two public keys
    let trust_result = test_env.events_store.wot_trust(
        &public_key1,
        &public_key2,
        TrustType::Direct,
    );
    assert!(trust_result.is_ok(), "Failed to establish trust");

    // Retrieve the list of trusted keys for public_key1
    let trusted_list = test_env
        .events_store
        .wot_trusted_list(&public_key1)
        .unwrap();

    assert_eq!(trusted_list.len(), 1, "Expected to find one trusted key");
    assert!(
        trusted_list.contains(&public_key2),
        "Trusted list does not contain expected key"
    );

    Ok(())
}

#[derive(Clone, Debug)]
struct TrustQuery;

#[rstest]
#[tokio::test]
async fn test_wot_trust(
    test_env: TestEnv,
    test_keys_0: Keys,
    test_keys_1: Keys,
    test_keys_2: Keys,
    test_keys_3: Keys,
) -> Result<(), RdfStoreError> {
    let public_key0 = test_keys_0.public_key();
    let public_key1 = test_keys_1.public_key();
    let public_key2 = test_keys_2.public_key();

    let pubkeys = vec![test_keys_1.public_key(), test_keys_2.public_key()];
    let evb = EventBuilder::contact_list(
        pubkeys
            .iter()
            .map(|pk| Contact::new(*pk))
            .collect::<Vec<_>>(),
    );

    let cl_event = evb.sign(&test_keys_0).await?;
    test_env.events_store.insert_event(&cl_event)?;

    let pubkeys = vec![test_keys_3.public_key(), test_keys_2.public_key()];
    let evb = EventBuilder::contact_list(
        pubkeys
            .iter()
            .map(|pk| Contact::new(*pk))
            .collect::<Vec<_>>(),
    );

    let cl_event = evb.sign(&test_keys_1).await?;
    test_env.events_store.insert_event(&cl_event)?;

    let trust_result = test_env.events_store.wot_trust(
        &public_key0,
        &public_key1,
        TrustType::Direct,
    );
    assert!(trust_result.is_ok(), "Failed to establish trust");

    test_env.events_store.wot_ring_create(
        &public_key0,
        &public_key1,
        2,
        "#00ff00",
        None,
        Some(0),
    )?;
    let ring_radius = test_env
        .events_store
        .wot_ring_radius(&public_key0, &public_key1)?;
    assert_eq!(ring_radius, 2);

    test_env.events_store.wot_rings_process(&public_key0)?;

    test_env.events_store.wot_ring_create(
        &public_key0,
        &public_key2,
        6,
        "#00ffff",
        None,
        Some(0),
    )?;

    test_env.events_store.wot_rings_process(&public_key0)?;

    let subs = [subnn("truster_nip21", public_key0.named_node()?)?];
    let set: Arc<TRdfResultSet<TrustQuery>> = test_env
        .events_store
        .run_query_typed(&nrq_get("wot_trusts")?, None, subs, None)?;

    let trusted: Vec<PublicKey> = set
        .rows
        .clone()
        .into_iter()
        .map(|row| {
            let tn = row.get_node("trusted_nip21").unwrap();
            tn.from_named_node().unwrap()
        })
        .collect();

    assert!(trusted.contains(&test_keys_3.public_key()));
    assert!(trusted.contains(&test_keys_1.public_key()));

    let _ = test_env.events_store.wot_rings_process(&public_key0)?;

    let trusted = test_env.events_store.wot_ring_members_from_pubk(
        &public_key0,
        &public_key1,
        5,
    )?;
    println!("T: {trusted:?}");

    // downsize the ring
    test_env.events_store.wot_ring_create(
        &public_key0,
        &public_key2,
        3,
        "#00ffff",
        None,
        Some(0),
    )?;

    test_env.dump_ttl("wot_trust.ttl");

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_wot_filters(
    test_env: TestEnv,
    test_keys_0: Keys,
    test_keys_1: Keys,
) -> Result<(), RdfStoreError> {
    let filter = Filter::new()
        .kind(Kind::TextNote)
        .kind(Kind::LongFormTextNote)
        .kind(Kind::Metadata)
        .author(test_keys_1.public_key())
        .author(test_keys_0.public_key());

    test_env.events_store.filter_save(filter, "test")?;

    test_env.dump_ttl("wot_filters.ttl");

    Ok(())
}