This commit is contained in:
@ -8,6 +8,7 @@ use std::path::PathBuf;
|
||||
pub struct AuthConfig {
|
||||
pub admin: AdminConfig,
|
||||
pub jetstream: JetstreamConfig,
|
||||
pub collections: CollectionConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@ -25,6 +26,12 @@ pub struct JetstreamConfig {
|
||||
pub collections: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CollectionConfig {
|
||||
pub comment: String,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
impl Default for AuthConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@ -39,6 +46,10 @@ impl Default for AuthConfig {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
collections: CollectionConfig {
|
||||
comment: "ai.syui.log".to_string(),
|
||||
user: "ai.syui.log.user".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,6 +116,7 @@ pub async fn init() -> Result<()> {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
collections: generate_collection_config(),
|
||||
};
|
||||
|
||||
// Save config
|
||||
@ -208,7 +220,10 @@ pub fn load_config() -> Result<AuthConfig> {
|
||||
}
|
||||
|
||||
let config_json = fs::read_to_string(&config_path)?;
|
||||
let config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
let mut config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
|
||||
// Update collection configuration
|
||||
update_config_collections(&mut config);
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
@ -233,14 +248,18 @@ pub async fn load_config_with_refresh() -> Result<AuthConfig> {
|
||||
}
|
||||
}
|
||||
|
||||
// Update collection configuration
|
||||
update_config_collections(&mut config);
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn test_api_access_with_auth(config: &AuthConfig) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=1",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=1",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.comment));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
@ -290,4 +309,31 @@ fn save_config(config: &AuthConfig) -> Result<()> {
|
||||
let config_json = serde_json::to_string_pretty(config)?;
|
||||
fs::write(&config_path, config_json)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Generate collection names from admin DID or environment
|
||||
fn generate_collection_config() -> CollectionConfig {
|
||||
// Check environment variables first
|
||||
if let (Ok(comment), Ok(user)) = (
|
||||
std::env::var("AILOG_COLLECTION_COMMENT"),
|
||||
std::env::var("AILOG_COLLECTION_USER")
|
||||
) {
|
||||
return CollectionConfig {
|
||||
comment,
|
||||
user,
|
||||
};
|
||||
}
|
||||
|
||||
// Default collections
|
||||
CollectionConfig {
|
||||
comment: "ai.syui.log".to_string(),
|
||||
user: "ai.syui.log.user".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// Update existing config with collection settings
|
||||
pub fn update_config_collections(config: &mut AuthConfig) {
|
||||
config.collections = generate_collection_config();
|
||||
// Also update jetstream collections to monitor the comment collection
|
||||
config.jetstream.collections = vec![config.collections.comment.clone()];
|
||||
}
|
@ -267,7 +267,7 @@ async fn handle_message(text: &str, config: &mut AuthConfig) -> Result<()> {
|
||||
if let (Some(collection), Some(commit), Some(did)) =
|
||||
(&message.collection, &message.commit, &message.did) {
|
||||
|
||||
if collection == "ai.syui.log" && commit.operation.as_deref() == Some("create") {
|
||||
if collection == &config.collections.comment && commit.operation.as_deref() == Some("create") {
|
||||
let unknown_uri = "unknown".to_string();
|
||||
let uri = commit.uri.as_ref().unwrap_or(&unknown_uri);
|
||||
|
||||
@ -358,9 +358,10 @@ async fn update_user_list(config: &mut AuthConfig, did: &str, handle: &str) -> R
|
||||
|
||||
async fn get_current_user_list(config: &mut AuthConfig) -> Result<Vec<UserRecord>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log.user&limit=10",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=10",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.user));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
@ -416,10 +417,14 @@ async fn post_user_list(config: &mut AuthConfig, users: &[UserRecord], metadata:
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let rkey = now.format("%Y-%m-%dT%H-%M-%S-%3fZ").to_string().replace(".", "-");
|
||||
// Extract short ID from DID (did:plc:xxx -> xxx) for rkey
|
||||
let short_did = config.admin.did
|
||||
.strip_prefix("did:plc:")
|
||||
.unwrap_or(&config.admin.did);
|
||||
let rkey = format!("{}-{}", short_did, now.format("%Y-%m-%dT%H-%M-%S-%3fZ").to_string().replace(".", "-"));
|
||||
|
||||
let record = UserListRecord {
|
||||
record_type: "ai.syui.log.user".to_string(),
|
||||
record_type: config.collections.user.clone(),
|
||||
users: users.to_vec(),
|
||||
created_at: now.to_rfc3339(),
|
||||
updated_by: UserInfo {
|
||||
@ -433,7 +438,7 @@ async fn post_user_list(config: &mut AuthConfig, users: &[UserRecord], metadata:
|
||||
|
||||
let request_body = json!({
|
||||
"repo": config.admin.did,
|
||||
"collection": "ai.syui.log.user",
|
||||
"collection": config.collections.user,
|
||||
"rkey": rkey,
|
||||
"record": record
|
||||
});
|
||||
@ -674,9 +679,10 @@ async fn poll_comments_periodically(mut config: AuthConfig) -> Result<()> {
|
||||
|
||||
async fn get_recent_comments(config: &mut AuthConfig) -> Result<Vec<Value>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=20",
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=20",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
urlencoding::encode(&config.admin.did),
|
||||
urlencoding::encode(&config.collections.comment));
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("🌐 API Request URL: {}", url).yellow());
|
||||
@ -757,7 +763,7 @@ pub async fn test_api() -> Result<()> {
|
||||
println!("{}", format!("✅ Successfully retrieved {} comments", comments.len()).green());
|
||||
|
||||
if comments.is_empty() {
|
||||
println!("{}", "ℹ️ No comments found in ai.syui.log collection".blue());
|
||||
println!("{}", format!("ℹ️ No comments found in {} collection", config.collections.comment).blue());
|
||||
println!("💡 Try posting a comment first using the web interface");
|
||||
} else {
|
||||
println!("{}", "📝 Comment details:".cyan());
|
||||
|
Reference in New Issue
Block a user