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 methods to retrieve AtpAgent info #178

Merged
merged 1 commit 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
49 changes: 39 additions & 10 deletions atrium-api/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ where
) -> Service<inner::Client<S, T>> {
Service::new(Arc::new(self.inner.clone_with_proxy(did, service_type)))
}
/// Get the current session.
pub async fn get_session(&self) -> Option<Session> {
self.store.get_session().await
}
/// Get the current endpoint.
pub async fn get_endpoint(&self) -> String {
self.store.get_endpoint()
}
/// Get the current labelers header.
pub async fn get_labelers_header(&self) -> Option<Vec<String>> {
self.inner.get_labelers_header().await
}
/// Get the current proxy header.
pub async fn get_proxy_header(&self) -> Option<String> {
self.inner.get_proxy_header().await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -261,7 +277,7 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn test_new() {
let agent = AtpAgent::new(MockClient::default(), MemorySessionStore::default());
assert_eq!(agent.store.get_session().await, None);
assert_eq!(agent.get_session().await, None);
}

#[tokio::test]
Expand All @@ -284,7 +300,7 @@ mod tests {
.login("test", "pass")
.await
.expect("login should be succeeded");
assert_eq!(agent.store.get_session().await, Some(session));
assert_eq!(agent.get_session().await, Some(session));
}
// failure with `createSession` error
{
Expand All @@ -299,7 +315,7 @@ mod tests {
.login("test", "bad")
.await
.expect_err("login should be failed");
assert_eq!(agent.store.get_session().await, None);
assert_eq!(agent.get_session().await, None);
}
}

Expand Down Expand Up @@ -447,15 +463,15 @@ mod tests {
..Default::default()
};
let agent = AtpAgent::new(client, MemorySessionStore::default());
assert_eq!(agent.store.get_session().await, None);
assert_eq!(agent.get_session().await, None);
agent
.resume_session(Session {
email: Some(String::from("test@example.com")),
..session.clone()
})
.await
.expect("resume_session should be succeeded");
assert_eq!(agent.store.get_session().await, Some(session.clone()));
assert_eq!(agent.get_session().await, Some(session.clone()));
}
// failure with `getSession` error
{
Expand All @@ -466,12 +482,12 @@ mod tests {
..Default::default()
};
let agent = AtpAgent::new(client, MemorySessionStore::default());
assert_eq!(agent.store.get_session().await, None);
assert_eq!(agent.get_session().await, None);
agent
.resume_session(session)
.await
.expect_err("resume_session should be failed");
assert_eq!(agent.store.get_session().await, None);
assert_eq!(agent.get_session().await, None);
}
}

Expand Down Expand Up @@ -501,7 +517,7 @@ mod tests {
})
.await
.expect("resume_session should be succeeded");
assert_eq!(agent.store.get_session().await, Some(session));
assert_eq!(agent.get_session().await, Some(session));
}

#[tokio::test]
Expand Down Expand Up @@ -542,7 +558,7 @@ mod tests {
.login("test", "pass")
.await
.expect("login should be succeeded");
assert_eq!(agent.store.get_endpoint(), "https://bsky.social");
assert_eq!(agent.get_endpoint().await, "https://bsky.social");
assert_eq!(
agent.api.com.atproto.server.xrpc.base_uri(),
"https://bsky.social"
Expand Down Expand Up @@ -580,7 +596,7 @@ mod tests {
.await
.expect("login should be succeeded");
// not updated
assert_eq!(agent.store.get_endpoint(), "http://localhost:8080");
assert_eq!(agent.get_endpoint().await, "http://localhost:8080");
assert_eq!(
agent.api.com.atproto.server.xrpc.base_uri(),
"http://localhost:8080"
Expand Down Expand Up @@ -643,6 +659,14 @@ mod tests {
HeaderValue::from_static("did:plc:test1, did:plc:test2"),
)]))
);

assert_eq!(
agent.get_labelers_header().await,
Some(vec![
String::from("did:plc:test1"),
String::from("did:plc:test2")
])
);
}

#[tokio::test]
Expand Down Expand Up @@ -736,5 +760,10 @@ mod tests {
HeaderValue::from_static("did:plc:test1#atproto_labeler"),
),]))
);

assert_eq!(
agent.get_proxy_header().await,
Some(String::from("did:plc:test1#atproto_labeler"))
);
}
}
4 changes: 3 additions & 1 deletion atrium-api/src/agent/bluesky.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Bluesky specific constants.

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

/// Supported proxy targets, which includes the bluesky specific services.
pub enum AtprotoServiceType {
Expand Down
14 changes: 10 additions & 4 deletions atrium-api/src/agent/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
S: SessionStore + Send + Sync,
T: XrpcClient + Send + Sync,
{
pub(crate) fn new(store: Arc<Store<S>>, xrpc: T) -> Self {
pub fn new(store: Arc<Store<S>>, xrpc: T) -> Self {
let inner = WrapperClient {
store: Arc::clone(&store),
labelers_header: Arc::new(RwLock::new(None)),
Expand All @@ -123,20 +123,26 @@ where
notify: Arc::new(Notify::new()),
}
}
pub(crate) fn configure_proxy_header(&self, did: Did, service_type: impl AsRef<str>) {
pub 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 {
pub 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>>) {
pub fn configure_labelers_header(&self, labeler_dids: Option<Vec<Did>>) {
self.inner.configure_labelers_header(labeler_dids);
}
pub async fn get_labelers_header(&self) -> Option<Vec<String>> {
self.inner.atproto_accept_labelers_header().await
}
pub async fn get_proxy_header(&self) -> Option<String> {
self.inner.atproto_proxy_header().await
}
// 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
Loading