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