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

feat: Add supporting atproto headers #175

Merged
merged 6 commits into from
May 22, 2024
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
269 changes: 237 additions & 32 deletions atrium-api/src/agent.rs

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions atrium-api/src/agent/bluesky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Bluesky specific constants.

/// DID of the bluesky labeler service.
pub const CHAT_BSKY_DID: &str = "did:web:api.bsky.chat";

/// Supported proxy targets, which includes the bluesky specific services.
pub enum AtprotoServiceType {
AtprotoLabeler,
BskyChat,
}

impl AsRef<str> for AtprotoServiceType {
fn as_ref(&self) -> &str {
match self {
Self::AtprotoLabeler => "atproto_labeler",
Self::BskyChat => "bsky_chat",
}
}
}
106 changes: 91 additions & 15 deletions atrium-api/src/agent/inner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Session, SessionStore};
use crate::did_doc::DidDocument;
use crate::types::string::Did;
use async_trait::async_trait;
use atrium_xrpc::error::{Error, Result, XrpcErrorKind};
use atrium_xrpc::{HttpClient, OutputDataOrBytes, XrpcClient, XrpcRequest};
Expand All @@ -8,16 +9,48 @@ use serde::{de::DeserializeOwned, Serialize};
use std::sync::{Arc, RwLock};
use tokio::sync::{Mutex, Notify};

const REFRESH_SESSION: &str = "com.atproto.server.refreshSession";

struct SessionStoreClient<S, T> {
struct WrapperClient<S, T> {
store: Arc<Store<S>>,
inner: T,
proxy_header: RwLock<Option<String>>,
labelers_header: Arc<RwLock<Option<Vec<String>>>>,
inner: Arc<T>,
}

impl<S, T> WrapperClient<S, T> {
fn configure_proxy_header(&self, value: String) {
self.proxy_header
.write()
.expect("failed to write proxy header")
.replace(value);
}
fn configure_labelers_header(&self, labelers_dids: Option<Vec<Did>>) {
*self
.labelers_header
.write()
.expect("failed to write labelers header") =
labelers_dids.map(|dids| dids.iter().map(|did| did.as_ref().into()).collect())
}
}

impl<S, T> Clone for WrapperClient<S, T> {
fn clone(&self) -> Self {
Self {
store: self.store.clone(),
labelers_header: self.labelers_header.clone(),
proxy_header: RwLock::new(
self.proxy_header
.read()
.expect("failed to read proxy header")
.clone(),
),
inner: self.inner.clone(),
}
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S, T> HttpClient for SessionStoreClient<S, T>
impl<S, T> HttpClient for WrapperClient<S, T>
where
S: Send + Sync,
T: HttpClient + Send + Sync,
Expand All @@ -33,15 +66,15 @@ where

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S, T> XrpcClient for SessionStoreClient<S, T>
impl<S, T> XrpcClient for WrapperClient<S, T>
where
S: SessionStore + Send + Sync,
T: XrpcClient + Send + Sync,
{
fn base_uri(&self) -> String {
self.store.get_endpoint()
}
async fn auth(&self, is_refresh: bool) -> Option<String> {
async fn authentication_token(&self, is_refresh: bool) -> Option<String> {
self.store.get_session().await.map(|session| {
if is_refresh {
session.refresh_jwt
Expand All @@ -50,13 +83,25 @@ where
}
})
}
async fn atproto_proxy_header(&self) -> Option<String> {
self.proxy_header
.read()
.expect("failed to read proxy header")
.clone()
}
async fn atproto_accept_labelers_header(&self) -> Option<Vec<String>> {
self.labelers_header
.read()
.expect("failed to read labelers header")
.clone()
}
}

pub struct Client<S, T> {
store: Arc<Store<S>>,
inner: SessionStoreClient<S, T>,
is_refreshing: Mutex<bool>,
notify: Notify,
inner: WrapperClient<S, T>,
is_refreshing: Arc<Mutex<bool>>,
notify: Arc<Notify>,
}

impl<S, T> Client<S, T>
Expand All @@ -65,17 +110,33 @@ where
T: XrpcClient + Send + Sync,
{
pub(crate) fn new(store: Arc<Store<S>>, xrpc: T) -> Self {
let inner = SessionStoreClient {
let inner = WrapperClient {
store: Arc::clone(&store),
inner: xrpc,
labelers_header: Arc::new(RwLock::new(None)),
proxy_header: RwLock::new(None),
inner: Arc::new(xrpc),
};
Self {
store,
inner,
is_refreshing: Mutex::new(false),
notify: Notify::new(),
is_refreshing: Arc::new(Mutex::new(false)),
notify: Arc::new(Notify::new()),
}
}
pub(crate) fn configure_proxy_header(&self, did: Did, service_type: impl AsRef<str>) {
self.inner
.configure_proxy_header(format!("{}#{}", did.as_ref(), service_type.as_ref()));
}
pub(crate) fn clone_with_proxy(&self, did: Did, service_type: impl AsRef<str>) -> Self {
let cloned = self.clone();
cloned
.inner
.configure_proxy_header(format!("{}#{}", did.as_ref(), service_type.as_ref()));
cloned
}
pub(crate) fn configure_labelers_header(&self, labeler_dids: Option<Vec<Did>>) {
self.inner.configure_labelers_header(labeler_dids);
}
// Internal helper to refresh sessions
// - Wraps the actual implementation to ensure only one refresh is attempted at a time.
async fn refresh_session(&self) {
Expand Down Expand Up @@ -120,7 +181,7 @@ where
.inner
.send_xrpc::<(), (), _, _>(&XrpcRequest {
method: Method::POST,
path: REFRESH_SESSION.into(),
nsid: crate::com::atproto::server::refresh_session::NSID.into(),
parameters: None,
input: None,
encoding: None,
Expand All @@ -147,6 +208,21 @@ where
}
}

impl<S, T> Clone for Client<S, T>
where
S: SessionStore + Send + Sync,
T: XrpcClient + Send + Sync,
{
fn clone(&self) -> Self {
Self {
store: self.store.clone(),
inner: self.inner.clone(),
is_refreshing: self.is_refreshing.clone(),
notify: self.notify.clone(),
}
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S, T> HttpClient for Client<S, T>
Expand Down
1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/get_preferences.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/get_profile.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/get_profiles.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/get_suggestions.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/put_preferences.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/search_actors.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/actor/search_actors_typeahead.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/describe_feed_generator.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_actor_feeds.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_actor_likes.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_author_feed.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_feed.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_feed_generator.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_feed_generators.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_feed_skeleton.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_likes.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_list_feed.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_post_thread.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_posts.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_reposted_by.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_suggested_feeds.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/get_timeline.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/search_posts.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/feed/send_interactions.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/graph/get_blocks.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/graph/get_followers.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/graph/get_follows.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/graph/get_list.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions atrium-api/src/app/bsky/graph/get_list_blocks.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading