skybase 0.1.5

The Open-Source Backend & Developer Platform for the AT Protocol (Firebase for ATProto, powered by skyauth)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Asynchronous HTTP client for ATProto Chat (`chat.bsky.convo.*`).
//!
//! Provides authenticated XRPC access to the Bluesky chat service, including
//! App-Password login, automatic single-flight session refresh/re-login, and
//! the conversation listing, request acceptance, message send/read, and
//! conversation-lookup operations used by DM bots.

use std::sync::Arc;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderName, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use serde::Deserialize;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

use crate::chat::types::{
    AcceptConvoRequest, AcceptConvoResponse, ConvoView, GetMessagesResponse,
    ListConvoRequestsResponse, ListConvosResponse, MessageView, SendMessagePayload,
    SendMessageRequest, UpdateReadRequest,
};
use crate::error::SkybaseError;

/// Default public Bluesky chat service endpoint.
pub const DEFAULT_CHAT_ENDPOINT: &str = "https://api.bsky.chat";

/// Service DID for Bluesky chat backend proxying through PDS.
pub const ATPROTO_CHAT_PROXY_DID: &str = "did:web:api.bsky.chat#bsky_chat";

#[derive(Debug, Deserialize)]
struct CreateSessionResponse {
    did: String,
    #[serde(rename = "accessJwt")]
    access_jwt: String,
    #[serde(rename = "refreshJwt", default)]
    refresh_jwt: Option<String>,
}

#[derive(Debug, Deserialize)]
struct RefreshSessionResponse {
    #[serde(rename = "accessJwt")]
    access_jwt: String,
    #[serde(rename = "refreshJwt", default)]
    refresh_jwt: Option<String>,
}

#[derive(Debug, Clone)]
struct BotCredentials {
    pds_endpoint: String,
    identifier: String,
    password: String,
}

#[derive(Debug, Clone)]
struct AuthState {
    access_jwt: String,
    refresh_jwt: Option<String>,
    credentials: Option<BotCredentials>,
}

/// Asynchronous HTTP client for interacting with the ATProto Chat service.
#[derive(Clone)]
pub struct ChatClient {
    base_url: String,
    client: reqwest::Client,
    auth_state: Arc<RwLock<AuthState>>,
}

impl ChatClient {
    /// Creates a new [`ChatClient`] targeting the specified base URL and bearer token.
    ///
    /// If `base_url` is a PDS rather than `api.bsky.chat`, the required `atproto-proxy`
    /// header is automatically attached to route chat XRPC requests through the gateway.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Chat`] if HTTP client configuration or headers fail.
    pub fn new(
        base_url: impl Into<String>,
        access_token: impl Into<String>,
    ) -> Result<Self, SkybaseError> {
        let base_url = base_url.into().trim_end_matches('/').to_string();
        let token = access_token.into();

        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        if !base_url.contains("api.bsky.chat") {
            headers.insert(
                HeaderName::from_static("atproto-proxy"),
                HeaderValue::from_static(ATPROTO_CHAT_PROXY_DID),
            );
        }

        let client = reqwest::Client::builder()
            .default_headers(headers)
            .timeout(Duration::from_secs(10))
            .build()
            .map_err(|e| SkybaseError::Chat(format!("Failed to build chat HTTP client: {e}")))?;

        let auth_state = Arc::new(RwLock::new(AuthState {
            access_jwt: token,
            refresh_jwt: None,
            credentials: None,
        }));

        Ok(Self {
            base_url,
            client,
            auth_state,
        })
    }

    /// Attaches a refresh JWT token for automatic session refresh upon token expiry.
    #[must_use]
    pub fn with_refresh_token(self, refresh_jwt: impl Into<String>) -> Self {
        let token = refresh_jwt.into();
        if !token.trim().is_empty() {
            if let Ok(mut state) = self.auth_state.try_write() {
                state.refresh_jwt = Some(token);
            }
        }
        self
    }

    /// Attaches bot credentials for automatic re-authentication if session tokens expire.
    #[must_use]
    pub fn with_credentials(
        self,
        pds_endpoint: impl Into<String>,
        identifier: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        if let Ok(mut state) = self.auth_state.try_write() {
            state.credentials = Some(BotCredentials {
                pds_endpoint: pds_endpoint.into().trim_end_matches('/').to_string(),
                identifier: identifier.into(),
                password: password.into(),
            });
        }
        self
    }

    /// Returns the currently active bearer access JWT.
    pub async fn current_access_token(&self) -> String {
        self.auth_state.read().await.access_jwt.clone()
    }

    /// Authenticates with an ATProto account identifier and App Password via `com.atproto.server.createSession`.
    ///
    /// Returns a configured [`ChatClient`] proxied through the PDS and the resolved account DID.
    /// The client retains credentials and refresh token to automatically recover from session expiry.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if authentication or session parsing fails.
    pub async fn login_with_app_password(
        pds_endpoint: &str,
        chat_endpoint: &str,
        identifier: &str,
        password: &str,
    ) -> Result<(Self, String), SkybaseError> {
        let pds_url = pds_endpoint.trim_end_matches('/');
        let login_url = format!("{pds_url}/xrpc/com.atproto.server.createSession");

        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(15))
            .build()
            .map_err(|e| SkybaseError::Chat(format!("Failed to build login HTTP client: {e}")))?;

        let body = serde_json::json!({
            "identifier": identifier,
            "password": password,
        });

        let resp = http
            .post(&login_url)
            .json(&body)
            .send()
            .await
            .map_err(|e| SkybaseError::Chat(format!("createSession request failed: {e}")))?;
        if !resp.status().is_success() {
            let status = resp.status();
            let err_body = resp.text().await.unwrap_or_default();
            return Err(SkybaseError::Chat(format!(
                "Failed to authenticate bot account ({identifier}) at {pds_url} (HTTP {status}): {err_body}"
            )));
        }

        let session: CreateSessionResponse = resp.json().await.map_err(|e| {
            SkybaseError::Chat(format!("Failed to parse createSession response: {e}"))
        })?;

        let mut chat_client = Self::new(chat_endpoint, &session.access_jwt)?;
        if let Some(ref refresh) = session.refresh_jwt {
            chat_client = chat_client.with_refresh_token(refresh);
        }
        chat_client = chat_client.with_credentials(pds_url, identifier, password);

        Ok((chat_client, session.did))
    }

    /// Checks if token refresh or re-authentication is possible.
    async fn can_refresh(&self) -> bool {
        let state = self.auth_state.read().await;
        state.refresh_jwt.is_some() || state.credentials.is_some()
    }

    /// Attempts to refresh the session using `refreshJwt` or re-login with stored credentials.
    ///
    /// If the token was already refreshed by another concurrent request, this is a no-op.
    async fn refresh_session_or_relogin(&self, failed_token: &str) -> Result<(), SkybaseError> {
        let mut state = self.auth_state.write().await;
        // If another task already refreshed the token, do nothing
        if state.access_jwt != failed_token {
            debug!("ATProto chat session token was already refreshed by another task");
            return Ok(());
        }

        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(15))
            .build()
            .map_err(|e| SkybaseError::Chat(format!("Failed to build refresh HTTP client: {e}")))?;

        // Strategy 1: Attempt refreshSession via refreshJwt if available
        if let (Some(ref refresh_jwt), Some(ref creds)) = (&state.refresh_jwt, &state.credentials) {
            let refresh_url = format!(
                "{}/xrpc/com.atproto.server.refreshSession",
                creds.pds_endpoint.trim_end_matches('/')
            );
            match http
                .post(&refresh_url)
                .header(AUTHORIZATION, format!("Bearer {refresh_jwt}"))
                .send()
                .await
            {
                Ok(resp) if resp.status().is_success() => {
                    match resp.json::<RefreshSessionResponse>().await {
                        Ok(refreshed) => {
                            info!("Successfully refreshed ATProto chat session token via refreshSession");
                            state.access_jwt = refreshed.access_jwt;
                            if let Some(new_refresh) = refreshed.refresh_jwt {
                                if !new_refresh.trim().is_empty() {
                                    state.refresh_jwt = Some(new_refresh);
                                }
                            }
                            return Ok(());
                        }
                        Err(e) => {
                            warn!(
                                error = %e,
                                "Failed to parse refreshSession response; falling back to full re-login"
                            );
                        }
                    }
                }
                Ok(resp) => {
                    let status = resp.status();
                    let body = resp.text().await.unwrap_or_default();
                    warn!(
                        status = %status,
                        body = %body,
                        "refreshSession failed; falling back to full re-login"
                    );
                }
                Err(e) => {
                    warn!(
                        error = %e,
                        "Network error during refreshSession; falling back to full re-login"
                    );
                }
            }
        }

        // Strategy 2: Full re-login using stored App Password credentials
        if let Some(ref creds) = state.credentials {
            info!(
                identifier = %creds.identifier,
                "Re-authenticating ATProto chat session via App Password..."
            );
            let login_url = format!(
                "{}/xrpc/com.atproto.server.createSession",
                creds.pds_endpoint.trim_end_matches('/')
            );
            let body = serde_json::json!({
                "identifier": creds.identifier,
                "password": creds.password,
            });

            let resp = http
                .post(&login_url)
                .json(&body)
                .send()
                .await
                .map_err(|e| SkybaseError::Chat(format!("Re-login request failed: {e}")))?;
            if !resp.status().is_success() {
                let status = resp.status();
                let err_body = resp.text().await.unwrap_or_default();
                return Err(SkybaseError::Chat(format!(
                    "Failed to re-authenticate bot session at {login_url} (HTTP {status}): {err_body}"
                )));
            }

            let session: CreateSessionResponse = resp.json().await.map_err(|e| {
                SkybaseError::Chat(format!("Failed to parse createSession response: {e}"))
            })?;

            info!("Successfully re-authenticated ATProto chat session via App Password");
            state.access_jwt = session.access_jwt;
            if let Some(new_refresh) = session.refresh_jwt {
                if !new_refresh.trim().is_empty() {
                    state.refresh_jwt = Some(new_refresh);
                }
            }
            return Ok(());
        }

        Err(SkybaseError::Chat(
            "Session token expired, but no refresh token or credentials configured to refresh"
                .to_string(),
        ))
    }

    /// Internal request helper with automatic single-flight token refresh retry.
    async fn send_request(
        &self,
        op_name: &str,
        method: reqwest::Method,
        url: &str,
        body: Option<serde_json::Value>,
    ) -> Result<reqwest::Response, SkybaseError> {
        let current_token = self.auth_state.read().await.access_jwt.clone();
        let req = self.build_request(method.clone(), url, &current_token, body.as_ref());

        let resp = req
            .send()
            .await
            .map_err(|e| SkybaseError::Chat(format!("{op_name} request failed: {e}")))?;
        let status = resp.status();

        if status.is_success() {
            return Ok(resp);
        }

        let body_text = resp.text().await.unwrap_or_default();
        let is_token_expired = status == reqwest::StatusCode::UNAUTHORIZED
            || (status == reqwest::StatusCode::BAD_REQUEST && body_text.contains("ExpiredToken"));

        if is_token_expired && self.can_refresh().await {
            warn!(
                op = op_name,
                status = %status,
                "ATProto chat session token expired; refreshing and retrying..."
            );
            if let Err(e) = self.refresh_session_or_relogin(&current_token).await {
                error!(error = %e, "Failed to refresh ATProto chat session");
                return Err(SkybaseError::Chat(format!(
                    "{op_name} failed with status {status}: {body_text} (refresh failed: {e})"
                )));
            }

            let new_token = self.auth_state.read().await.access_jwt.clone();
            let retry_req = self.build_request(method, url, &new_token, body.as_ref());

            let retry_resp = retry_req
                .send()
                .await
                .map_err(|e| SkybaseError::Chat(format!("{op_name} retry failed: {e}")))?;
            if !retry_resp.status().is_success() {
                let retry_status = retry_resp.status();
                let retry_body = retry_resp.text().await.unwrap_or_default();
                return Err(SkybaseError::Chat(format!(
                    "{op_name} failed with status {retry_status}: {retry_body}"
                )));
            }

            return Ok(retry_resp);
        }

        Err(SkybaseError::Chat(format!(
            "{op_name} failed with status {status}: {body_text}"
        )))
    }

    /// Builds a bearer-authenticated XRPC request, optionally attaching a JSON body.
    fn build_request(
        &self,
        method: reqwest::Method,
        url: &str,
        token: &str,
        body: Option<&serde_json::Value>,
    ) -> reqwest::RequestBuilder {
        let mut req = self
            .client
            .request(method, url)
            .header(AUTHORIZATION, format!("Bearer {token}"));
        if let Some(b) = body {
            req = req.json(b);
        }
        req
    }

    /// Lists active conversations for the authenticated user.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn list_convos(
        &self,
        limit: Option<usize>,
        cursor: Option<&str>,
    ) -> Result<ListConvosResponse, SkybaseError> {
        let mut url = format!("{}/xrpc/chat.bsky.convo.listConvos", self.base_url);
        let mut query_params = Vec::new();

        if let Some(limit) = limit {
            query_params.push(format!("limit={limit}"));
        }
        if let Some(cursor) = cursor {
            query_params.push(format!("cursor={cursor}"));
        }
        if !query_params.is_empty() {
            url.push('?');
            url.push_str(&query_params.join("&"));
        }

        let resp = self
            .send_request("listConvos", reqwest::Method::GET, &url, None)
            .await?;
        let result = resp
            .json::<ListConvosResponse>()
            .await
            .map_err(|e| SkybaseError::Chat(format!("Failed to parse listConvos response: {e}")))?;
        debug!(count = result.convos.len(), "Fetched chat conversations");
        Ok(result)
    }

    /// Lists incoming conversation requests for the authenticated user via `chat.bsky.convo.listConvoRequests`.
    ///
    /// Non-followed accounts initiating conversations land in the requests state until accepted.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn list_convo_requests(
        &self,
        limit: Option<usize>,
        cursor: Option<&str>,
    ) -> Result<ListConvoRequestsResponse, SkybaseError> {
        let mut url = format!("{}/xrpc/chat.bsky.convo.listConvoRequests", self.base_url);
        let mut query_params = Vec::new();

        if let Some(limit) = limit {
            query_params.push(format!("limit={limit}"));
        }
        if let Some(cursor) = cursor {
            query_params.push(format!("cursor={cursor}"));
        }
        if !query_params.is_empty() {
            url.push('?');
            url.push_str(&query_params.join("&"));
        }

        let resp = self
            .send_request("listConvoRequests", reqwest::Method::GET, &url, None)
            .await?;
        let result = resp
            .json::<ListConvoRequestsResponse>()
            .await
            .map_err(|e| {
                SkybaseError::Chat(format!("Failed to parse listConvoRequests response: {e}"))
            })?;
        debug!(
            count = result.requests.len(),
            "Fetched chat conversation requests"
        );
        Ok(result)
    }

    /// Accepts an incoming conversation request via `chat.bsky.convo.acceptConvo`.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn accept_convo(&self, convo_id: &str) -> Result<AcceptConvoResponse, SkybaseError> {
        let url = format!("{}/xrpc/chat.bsky.convo.acceptConvo", self.base_url);
        let req = AcceptConvoRequest {
            convo_id: convo_id.to_string(),
        };
        let body = serde_json::to_value(&req)
            .map_err(|e| SkybaseError::Chat(format!("Failed to serialize acceptConvo: {e}")))?;

        let resp = self
            .send_request("acceptConvo", reqwest::Method::POST, &url, Some(body))
            .await?;
        let result = resp.json::<AcceptConvoResponse>().await.map_err(|e| {
            SkybaseError::Chat(format!("Failed to parse acceptConvo response: {e}"))
        })?;
        debug!(
            convo_id = %convo_id,
            rev = ?result.rev,
            "Accepted chat conversation request"
        );
        Ok(result)
    }

    /// Fetches messages for a specific conversation.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn get_messages(
        &self,
        convo_id: &str,
        limit: Option<usize>,
        cursor: Option<&str>,
    ) -> Result<GetMessagesResponse, SkybaseError> {
        let mut url = format!(
            "{}/xrpc/chat.bsky.convo.getMessages?convoId={convo_id}",
            self.base_url
        );
        if let Some(limit) = limit {
            url.push_str(&format!("&limit={limit}"));
        }
        if let Some(cursor) = cursor {
            url.push_str(&format!("&cursor={cursor}"));
        }

        let resp = self
            .send_request("getMessages", reqwest::Method::GET, &url, None)
            .await?;
        let result = resp.json::<GetMessagesResponse>().await.map_err(|e| {
            SkybaseError::Chat(format!("Failed to parse getMessages response: {e}"))
        })?;
        Ok(result)
    }

    /// Sends a text message to the specified conversation, automatically extracting and attaching link facets.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn send_message(
        &self,
        convo_id: &str,
        text: &str,
    ) -> Result<MessageView, SkybaseError> {
        let payload = SendMessagePayload::new(text);
        self.send_message_payload(convo_id, payload).await
    }

    /// Sends a structured message payload via `chat.bsky.convo.sendMessage`.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn send_message_payload(
        &self,
        convo_id: &str,
        payload: SendMessagePayload,
    ) -> Result<MessageView, SkybaseError> {
        let url = format!("{}/xrpc/chat.bsky.convo.sendMessage", self.base_url);
        let req = SendMessageRequest {
            convo_id: convo_id.to_string(),
            message: payload,
        };
        let body = serde_json::to_value(&req)
            .map_err(|e| SkybaseError::Chat(format!("Failed to serialize sendMessage: {e}")))?;

        let resp = self
            .send_request("sendMessage", reqwest::Method::POST, &url, Some(body))
            .await?;
        let result = resp.json::<MessageView>().await.map_err(|e| {
            SkybaseError::Chat(format!("Failed to parse sendMessage response: {e}"))
        })?;
        Ok(result)
    }

    /// Marks messages up to `message_id` as read in the conversation.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request fails.
    pub async fn update_read(&self, convo_id: &str, message_id: &str) -> Result<(), SkybaseError> {
        let url = format!("{}/xrpc/chat.bsky.convo.updateRead", self.base_url);
        let req = UpdateReadRequest {
            convo_id: convo_id.to_string(),
            message_id: message_id.to_string(),
        };
        let body = serde_json::to_value(&req)
            .map_err(|e| SkybaseError::Chat(format!("Failed to serialize updateRead: {e}")))?;

        self.send_request("updateRead", reqwest::Method::POST, &url, Some(body))
            .await?;
        Ok(())
    }

    /// Finds or initializes a conversation for the specified member DIDs via `chat.bsky.convo.getConvoForMembers`.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if the HTTP request or JSON deserialization fails.
    pub async fn get_convo_for_members(&self, members: &[&str]) -> Result<ConvoView, SkybaseError> {
        let mut url = format!("{}/xrpc/chat.bsky.convo.getConvoForMembers?", self.base_url);
        let params: Vec<String> = members.iter().map(|m| format!("members={m}")).collect();
        url.push_str(&params.join("&"));

        let resp = self
            .send_request("getConvoForMembers", reqwest::Method::GET, &url, None)
            .await?;

        #[derive(Deserialize)]
        struct GetConvoResponse {
            convo: ConvoView,
        }

        let result = resp.json::<GetConvoResponse>().await.map_err(|e| {
            SkybaseError::Chat(format!("Failed to parse getConvoForMembers response: {e}"))
        })?;
        Ok(result.convo)
    }

    /// Attempts to find an existing conversation containing the specified DID or initialize one.
    ///
    /// # Errors
    /// Returns [`SkybaseError`] if conversation lookup fails.
    pub async fn find_or_create_convo_for_did(&self, did: &str) -> Result<String, SkybaseError> {
        // Try get_convo_for_members first
        if let Ok(convo) = self.get_convo_for_members(&[did]).await {
            return Ok(convo.id);
        }

        // Fallback: search existing conversations from listConvos
        let resp = self.list_convos(Some(50), None).await?;
        for convo in resp.convos {
            if convo.members.iter().any(|m| m.did == did) {
                return Ok(convo.id);
            }
        }

        Err(SkybaseError::Chat(format!(
            "Could not find or establish conversation with {did}"
        )))
    }
}