this repo has no description
1use crate::api::read_after_write::{ 2 FeedOutput, FeedViewPost, ProfileRecord, RecordDescript, extract_repo_rev, format_local_post, 3 format_munged_response, get_local_lag, get_records_since_rev, insert_posts_into_feed, 4 proxy_to_appview, 5}; 6use crate::state::AppState; 7use axum::{ 8 Json, 9 extract::{Query, State}, 10 http::StatusCode, 11 response::{IntoResponse, Response}, 12}; 13use serde::Deserialize; 14use std::collections::HashMap; 15use tracing::warn; 16 17#[derive(Deserialize)] 18pub struct GetAuthorFeedParams { 19 pub actor: String, 20 pub limit: Option<u32>, 21 pub cursor: Option<String>, 22 pub filter: Option<String>, 23 #[serde(rename = "includePins")] 24 pub include_pins: Option<bool>, 25} 26 27fn update_author_profile_in_feed( 28 feed: &mut [FeedViewPost], 29 author_did: &str, 30 local_profile: &RecordDescript<ProfileRecord>, 31) { 32 for item in feed.iter_mut() { 33 if item.post.author.did == author_did 34 && let Some(ref display_name) = local_profile.record.display_name { 35 item.post.author.display_name = Some(display_name.clone()); 36 } 37 } 38} 39 40pub async fn get_author_feed( 41 State(state): State<AppState>, 42 headers: axum::http::HeaderMap, 43 Query(params): Query<GetAuthorFeedParams>, 44) -> Response { 45 let auth_header = headers.get("Authorization").and_then(|h| h.to_str().ok()); 46 let auth_user = if let Some(h) = auth_header { 47 if let Some(token) = crate::auth::extract_bearer_token_from_header(Some(h)) { 48 crate::auth::validate_bearer_token(&state.db, &token) 49 .await 50 .ok() 51 } else { 52 None 53 } 54 } else { 55 None 56 }; 57 let auth_did = auth_user.as_ref().map(|u| u.did.clone()); 58 let auth_key_bytes = auth_user.as_ref().and_then(|u| u.key_bytes.clone()); 59 let mut query_params = HashMap::new(); 60 query_params.insert("actor".to_string(), params.actor.clone()); 61 if let Some(limit) = params.limit { 62 query_params.insert("limit".to_string(), limit.to_string()); 63 } 64 if let Some(cursor) = &params.cursor { 65 query_params.insert("cursor".to_string(), cursor.clone()); 66 } 67 if let Some(filter) = &params.filter { 68 query_params.insert("filter".to_string(), filter.clone()); 69 } 70 if let Some(include_pins) = params.include_pins { 71 query_params.insert("includePins".to_string(), include_pins.to_string()); 72 } 73 let proxy_result = match proxy_to_appview( 74 "app.bsky.feed.getAuthorFeed", 75 &query_params, 76 auth_did.as_deref().unwrap_or(""), 77 auth_key_bytes.as_deref(), 78 ) 79 .await 80 { 81 Ok(r) => r, 82 Err(e) => return e, 83 }; 84 if !proxy_result.status.is_success() { 85 return proxy_result.into_response(); 86 } 87 let rev = match extract_repo_rev(&proxy_result.headers) { 88 Some(r) => r, 89 None => return proxy_result.into_response(), 90 }; 91 let mut feed_output: FeedOutput = match serde_json::from_slice(&proxy_result.body) { 92 Ok(f) => f, 93 Err(e) => { 94 warn!("Failed to parse author feed response: {:?}", e); 95 return proxy_result.into_response(); 96 } 97 }; 98 let requester_did = match &auth_did { 99 Some(d) => d.clone(), 100 None => return (StatusCode::OK, Json(feed_output)).into_response(), 101 }; 102 let actor_did = if params.actor.starts_with("did:") { 103 params.actor.clone() 104 } else { 105 let hostname = std::env::var("PDS_HOSTNAME").unwrap_or_else(|_| "localhost".to_string()); 106 let suffix = format!(".{}", hostname); 107 let short_handle = if params.actor.ends_with(&suffix) { 108 params.actor.strip_suffix(&suffix).unwrap_or(&params.actor) 109 } else { 110 &params.actor 111 }; 112 match sqlx::query_scalar!("SELECT did FROM users WHERE handle = $1", short_handle) 113 .fetch_optional(&state.db) 114 .await 115 { 116 Ok(Some(did)) => did, 117 Ok(None) => return (StatusCode::OK, Json(feed_output)).into_response(), 118 Err(e) => { 119 warn!("Database error resolving actor handle: {:?}", e); 120 return proxy_result.into_response(); 121 } 122 } 123 }; 124 if actor_did != requester_did { 125 return (StatusCode::OK, Json(feed_output)).into_response(); 126 } 127 let local_records = match get_records_since_rev(&state, &requester_did, &rev).await { 128 Ok(r) => r, 129 Err(e) => { 130 warn!("Failed to get local records: {}", e); 131 return proxy_result.into_response(); 132 } 133 }; 134 if local_records.count == 0 { 135 return (StatusCode::OK, Json(feed_output)).into_response(); 136 } 137 let handle = match sqlx::query_scalar!("SELECT handle FROM users WHERE did = $1", requester_did) 138 .fetch_optional(&state.db) 139 .await 140 { 141 Ok(Some(h)) => h, 142 Ok(None) => requester_did.clone(), 143 Err(e) => { 144 warn!("Database error fetching handle: {:?}", e); 145 requester_did.clone() 146 } 147 }; 148 if let Some(ref local_profile) = local_records.profile { 149 update_author_profile_in_feed(&mut feed_output.feed, &requester_did, local_profile); 150 } 151 let local_posts: Vec<_> = local_records 152 .posts 153 .iter() 154 .map(|p| format_local_post(p, &requester_did, &handle, local_records.profile.as_ref())) 155 .collect(); 156 insert_posts_into_feed(&mut feed_output.feed, local_posts); 157 let lag = get_local_lag(&local_records); 158 format_munged_response(feed_output, lag) 159}