ATrium

repository·main·Indexed 19 days ago

https://github.com/atrium-rs/atrium

A modular ecosystem of Rust libraries for interacting with the AT Protocol and Bluesky. It includes atrium-api for XRPC communication and session management via AtpAgent, atrium-crypto for p256 and k256 cryptographic operations, atrium-identity for DID and handle resolution, atrium-oauth for authentication flows, atrium-repo for user repository and Merkle Search Tree (MST) access, and atrium-xrpc-client providing HTTP backends such as reqwest and isahc with WASM support.

Tokens
31.5K
Snippets
74
Records
133
Agent score
65%

What's inside ATrium

  1. Overview of the ATrium ecosystem

    main

    ATrium is a collection of Rust libraries designed to work with the AT Protocol. It is organized into modular sub-projects to handle different layers of the protocol, from low-level XRPC definitions to high-level Bluesky SDKs.

    Key components include:

    • atrium-api: Models and messaging definitions for XRPC (primarily generated via codegen).
    • atrium-xrpc: Core definitions for XRPC request/response and error handling.
    • atrium-xrpc-client: Implementations of the XrpcClient trait defined in atrium-xrpc.
    • bsky-sdk: A high-level, ATrium-based SDK specifically for interacting with Bluesky.
    • bsky-cli: A command-line interface built using the ATrium API libraries.
  2. Use the Bsky CLI

    main

    The bsky-cli is a command-line application for interacting with Bluesky using the ATrium API. It allows you to manage authentication, retrieve social data (profiles, feeds, timelines, notifications), and perform actions like creating or deleting posts and sending chat messages.

    To use the CLI, you must first authenticate using the login command to create an authentication session.

    Usage: bsky-cli [OPTIONS] <COMMAND>
  3. Use ATrium Identity for atproto decentralized identities

    main
    ATrium Identity is a resolver library designed for handling decentralized identities within the atproto ecosystem. It provides mechanisms to resolve identities using both Decentralized Identifiers (DIDs) and atproto handles.
  4. Use ATrium Repo to access ATProto repositories

    main
    ATrium Repo is a Rust library designed for interacting with ATProto (Bluesky) user repositories. It provides tools to access repository data and includes specialized support for handling the Merkle Search Tree (MST) data structure used by the protocol.
  5. Use ATrium Crypto for AT Protocol cryptographic operations

    main

    ATrium Crypto provides cryptographic helpers specifically designed for the AT Protocol. It implements two elliptic curve systems:

    • p256 (NIST P-256 / secp256r1 / prime256v1)
    • k256 (NIST K-256 / secp256k1)

    The library handles AT Protocol specific requirements such as string encodings, 'low-S' signature validity, byte representation compression, and hashing as defined in the atproto specification.

  6. Implement a custom XrpcClient by providing an HttpClient

    main
    The XrpcClient trait provides the logic for handling ATProto XRPC requests, but it relies on an underlying HttpClient to perform the actual network operations. To create a custom XRPC client, you must implement the HttpClient trait. This allows you to control how asynchronous HTTP requests are sent (e.g., using different HTTP libraries or adding custom middleware) while still leveraging the high-level XRPC request/response definitions and error handling provided by atrium-xrpc.
  7. Manage Bluesky sessions with BskyAgent

    main

    Use BskyAgent to authenticate with a Bluesky server. Most API methods require an active session. You can perform a standard login using credentials, or persist the session to a file using FileStore to avoid re-authenticating in future runs.

    use bsky_sdk::BskyAgent;
    
    #[tokio::main]
    async fn main() -> Result<(), Box<dyn std::error::Error>> {
        let agent = BskyAgent::builder().build().await?;
        let session = agent.login("alice@mail.com", "hunter2").await?;
        Ok(())
    }
  8. Use the Reqwest backend with atrium-xrpc-client

    main

    If you are using tokio as your asynchronous runtime, you can use the reqwest backend. By default, it uses reqwest's default-tls feature.

    To use a custom reqwest::Client (for example, to configure timeouts or use rustls instead of native-tls), use the ReqwestClientBuilder to inject your own client instance.

    use atrium_xrpc_client::reqwest::ReqwestClientBuilder;
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let client = ReqwestClientBuilder::new("https://bsky.social")
            .client(
                reqwest::ClientBuilder::new()
                    .timeout(std::time::Duration::from_millis(1000))
                    .use_rustls_tls()
                    .build()?,
            )
            .build();
        Ok(())
    }
  9. Implement moderation with BskyAgent

    main

    The SDK provides moderation tools similar to the official @atproto/api. To moderate content (like posts in a timeline), first retrieve the user's moderation preferences and label definitions using agent.get_preferences(true). Then, create a moderator instance using agent.moderator(&preferences). You can then use moderator.moderate_post(&post) to evaluate content against the user's settings, using DecisionContext to determine how to filter the output.

    use bsky_sdk::moderation::decision::DecisionContext;
    use bsky_sdk::BskyAgent;
    
    #[tokio::main]
    async fn main() -> Result<(), Box<dyn std::error::Error>> {
        let agent = BskyAgent::builder().build().await?;
        // log in...
    
        let preferences = agent.get_preferences(true).await?;
        let moderator = agent.moderator(&preferences).await?;
    
        let output = agent
            .api
            .app
            .bsky
            .feed
            .get_timeline(
                atrium_api::app::bsky::feed::get_timeline::ParametersData {
                    algorithm: None,
                    cursor: None,
                    limit: None,
                }
                .into(),
            )
            .await?;
    
        for feed_view_post in &output.feed {
            let post_mod = moderator.moderate_post(&feed_view_post.post);
            println!(
                "{:?} (filter: {})",
                feed_view_post.post.cid.as_ref(),
                post_mod.ui(DecisionContext::ContentList).filter()
            );
        }
        Ok(())
    }
  10. Use the Isahc backend with atrium-xrpc-client

    main

    If you are not using the tokio runtime, you can use the isahc backend as an alternative to reqwest.

    You can initialize a basic client with IsahcClient::new(base_url) or use IsahcClientBuilder to provide a custom isahc::HttpClient with specific configurations like timeouts.

    use atrium_xrpc_client::isahc::IsahcClientBuilder;
    use isahc::config::Configurable;
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let client = IsahcClientBuilder::new("https://bsky.social")
            .client(
                isahc::HttpClientBuilder::new()
                    .timeout(std::time::Duration::from_millis(1000))
                    .build()?,
            )
            .build();
        Ok(())
    }