-
Notifications
You must be signed in to change notification settings - Fork 20
feat(sdk/rust): add ContactsApi for the DM permission graph #208
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
Merged
+96
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //! First-level mutual contact graph (`/contacts`). Mirrors | ||
| //! `sdk/typescript/src/api/contacts.ts`. | ||
| //! | ||
| //! An **accepted** contact relationship is the prerequisite for direct | ||
| //! messaging — the relay refuses a DM between two agents that are not contacts | ||
| //! (`not_a_contact`). Typical bootstrap: | ||
| //! | ||
| //! ```ignore | ||
| //! client.contacts.request("@peer").await?; // initiator | ||
| //! client.contacts.accept("@initiator").await?; // peer (or auto-accept on a | ||
| //! // reverse-pending request) | ||
| //! client.messages.send(envelope).await?; // now permitted | ||
| //! ``` | ||
| //! | ||
| //! All calls are authenticated as the acting agent (`X-Agent-ID` + signature). | ||
| //! Contacts are keyed by cryptoId. Responses are returned as [`serde_json::Value`] | ||
| //! (the backend contact record shape is not needed by current callers). | ||
|
|
||
| use crate::error::Result; | ||
| use crate::http::HttpClient; | ||
| use crate::util::encode; | ||
|
|
||
| /// Manages the mutual first-level contact graph: send/accept/decline requests, | ||
| /// block, and list contacts. | ||
| #[derive(Clone)] | ||
| pub struct ContactsApi { | ||
| http: HttpClient, | ||
| } | ||
|
|
||
| impl ContactsApi { | ||
| pub(crate) fn new(http: HttpClient) -> Self { | ||
| Self { http } | ||
| } | ||
|
|
||
| /// Send a contact request to `agent_id` (idempotent; **auto-accepts** when a | ||
| /// reverse request from `agent_id` is already pending). | ||
| pub async fn request(&self, agent_id: &str) -> Result<serde_json::Value> { | ||
| self.http | ||
| .post_agent_auth::<serde_json::Value, serde_json::Value>( | ||
| &format!("/contacts/{}", encode(agent_id)), | ||
| None, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| /// Accept a pending incoming request from `agent_id`. | ||
| pub async fn accept(&self, agent_id: &str) -> Result<serde_json::Value> { | ||
| self.http | ||
| .post_agent_auth::<serde_json::Value, serde_json::Value>( | ||
| &format!("/contacts/{}/accept", encode(agent_id)), | ||
| None, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| /// Decline an incoming request, cancel an outgoing one, or remove a contact. | ||
| pub async fn remove(&self, agent_id: &str) -> Result<()> { | ||
| self.http | ||
| .delete_agent_auth::<(), serde_json::Value>( | ||
| &format!("/contacts/{}", encode(agent_id)), | ||
| None, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| /// Block `agent_id`, suppressing the relationship and refusing new requests. | ||
| pub async fn block(&self, agent_id: &str) -> Result<serde_json::Value> { | ||
| self.http | ||
| .post_agent_auth::<serde_json::Value, serde_json::Value>( | ||
| &format!("/contacts/{}/block", encode(agent_id)), | ||
| None, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| /// Get the relationship status with `agent_id`. | ||
| pub async fn status(&self, agent_id: &str) -> Result<serde_json::Value> { | ||
| self.http | ||
| .get_agent_auth(&format!("/contacts/{}/status", encode(agent_id)), &[]) | ||
| .await | ||
| } | ||
|
|
||
| /// List the acting agent's accepted contacts. | ||
| pub async fn list(&self) -> Result<serde_json::Value> { | ||
| self.http.get_agent_auth("/contacts", &[]).await | ||
| } | ||
|
|
||
| /// List pending incoming and outgoing requests. | ||
| pub async fn requests(&self) -> Result<serde_json::Value> { | ||
| self.http.get_agent_auth("/contacts/requests", &[]).await | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a Rust client uses this new
block()method, the SDK has no corresponding wrapper forPOST /contacts/{id}/unblock, even though the mirrored TypeScript API exposesunblock()atsdk/typescript/src/api/contacts.ts:71. In the blocked-contact scenario, Rust callers cannot restore the relationship throughclient.contactsand must drop to rawclient.http(), making this newly added contact graph surface one-way for users who need to resume messaging.Useful? React with 👍 / 👎.