fix: refresh the session before the access token expires

jacquard 0.12.2 only refreshes on a 401, but the PDS reports expiry as a 400
ExpiredToken, so DM polling failed after about two hours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
govandClaude Opus 5.5 committed 2026-11-06 15:18:01 -05:00
1 parent edf19507b4
commit 9384e2bcea
1 file changed
+11 -1
+11 -1
View File
@@ -1,4 +1,4 @@
use std::time::Duration; use std::time::{Duration, Instant};
use anyhow::Result; use anyhow::Result;
use jacquard::api::app_bsky::graph::get_relationships::{ use jacquard::api::app_bsky::graph::get_relationships::{
@@ -19,6 +19,9 @@ use crate::cities::Cities;
use crate::labeler::Labeler; use crate::labeler::Labeler;
const POLL_INTERVAL: Duration = Duration::from_secs(3); const POLL_INTERVAL: Duration = Duration::from_secs(3);
// Access tokens last about two hours. jacquard 0.12.2 only refreshes after a
// 401, but the PDS reports expiry as a 400 `ExpiredToken`, so refresh ahead of it.
const REFRESH_INTERVAL: Duration = Duration::from_secs(30 * 60);
const CURSOR_KEY: &str = "chat_cursor"; const CURSOR_KEY: &str = "chat_cursor";
const CHAT_SERVICE: &str = "did:web:api.bsky.chat#bsky_chat"; const CHAT_SERVICE: &str = "did:web:api.bsky.chat#bsky_chat";
@@ -46,7 +49,14 @@ fn chat() -> CallOptions {
impl Dms<'_> { impl Dms<'_> {
pub async fn run(&self) { pub async fn run(&self) {
let mut last_refresh = Instant::now();
loop { loop {
if last_refresh.elapsed() >= REFRESH_INTERVAL {
match self.agent.refresh().await {
Ok(_) => last_refresh = Instant::now(),
Err(err) => eprintln!("session refresh failed: {err:#}"),
}
}
if let Err(err) = self.poll().await { if let Err(err) = self.poll().await {
eprintln!("chat poll failed: {err:#}"); eprintln!("chat poll failed: {err:#}");
} }