nostralink 0.2.4

Linked data library for nostr
Documentation
use super::{test_env, TestEnv};
use crate::prelude::*;
use nostr::Tag;
use oxigraph::io::RdfFormat;
use rstest::*;
use std::path::PathBuf;

#[rstest]
#[tokio::test]
async fn test_custom_feeds_config(
    test_env: TestEnv,
) -> Result<(), RdfStoreError> {
    let node = test_env.events_store.feed_setup(
        &test_env.pubkey(),
        "my-feed",
        "my feed",
        ":heart:",
    )?;

    test_env
        .events_store
        .feed_follow_pubkey(node.as_ref(), &test_env.pubkey())?;

    let followees = test_env.events_store.feed_followees(node.as_ref())?;
    assert_eq!(followees.len(), 1);

    let feeds = test_env.events_store.feeds_list(Some(&test_env.pubkey()))?;

    test_env.events_store.dump_graph_to_file(
        PathBuf::from("feeds_custom0.ttl"),
        Some(RdfFormat::Turtle),
    )?;

    assert_eq!(feeds.len(), 1);
    assert_eq!(feeds[0].1, "my feed");

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_custom_feeds_tagged(
    test_env: TestEnv,
) -> Result<(), RdfStoreError> {
    let node = test_env.events_store.feed_setup(
        &test_env.pubkey(),
        "my-feed",
        "my feed",
        ":heart:",
    )?;

    test_env
        .events_store
        .feed_follow_hashtag(node.as_ref(), "askstr")?;

    let note_event = EventBuilder::text_note("test")
        .tag(Tag::hashtag("askstr"))
        .sign(&test_env.keys)
        .await
        .unwrap();

    test_env.events_store.insert_event(&note_event)?;

    test_env.events_store.dump_graph_to_file(
        PathBuf::from("feeds_store_tagged.ttl"),
        Some(RdfFormat::Turtle),
    )?;

    Ok(())
}

#[rstest]
#[tokio::test]
async fn test_custom_feeds(test_env: TestEnv) -> Result<(), RdfStoreError> {
    let node = test_env.events_store.feed_node(&test_env.pubkey(), None);

    let subs = [
        subl(
            "created_at_since",
            Literal::from((Timestamp::now() - 3600).as_secs()),
        )?,
        subl("only_followees", Literal::from(1))?,
        subl("filter_e_tag", Literal::from(0))?,
        subl("lang_tag_filter", Literal::from("en-GB"))?,
        subnn("feed", node.clone())?,
    ];

    test_env
        .events_store
        .feed_follow_hashtag(node.as_ref(), "askstr")?;

    test_env
        .events_store
        .feed_follow_hashtag(node.as_ref(), "nostralink")?;

    let tags = test_env
        .events_store
        .feed_followed_hashtags(node.as_ref())?;

    assert!(tags.contains(&"askstr".to_owned()), "Tag not in list");
    assert!(tags.contains(&"nostralink".to_owned()), "Tag not in list");

    let note_event = EventBuilder::text_note("test")
        .tag(Tag::hashtag("askstr"))
        .sign(&test_env.keys)
        .await
        .unwrap();

    test_env.events_store.insert_event(&note_event)?;

    let note2_event = EventBuilder::text_note("Hello world")
        .sign(&test_env.keys)
        .await
        .unwrap();

    test_env.events_store.insert_event(&note2_event)?;

    test_env.events_store.dump_graph_to_file(
        PathBuf::from("feeds_store.ttl"),
        Some(RdfFormat::Turtle),
    )?;

    let set = test_env.events_store.run_query(
        &nrq_get("content_union_nostr")?,
        subs,
        None,
    )?;

    assert_eq!(set.count(), 1);

    Ok(())
}