Sign GET request when fetching AP object

This commit is contained in:
silverpill 2021-11-18 14:37:48 +00:00
parent ac1027ac2a
commit 3fc0acd2d1
3 changed files with 11 additions and 12 deletions

View file

@ -163,13 +163,10 @@ pub async fn fetch_attachment(
} }
pub async fn fetch_object( pub async fn fetch_object(
instance: &Instance,
object_url: &str, object_url: &str,
) -> Result<Object, FetchError> { ) -> Result<Object, FetchError> {
let client = reqwest::Client::new(); let object_json = send_request(instance, object_url, &[]).await?;
let object_json = client.get(object_url)
.header(reqwest::header::ACCEPT, ACTIVITY_CONTENT_TYPE)
.send().await?
.text().await?;
let object_value: Value = serde_json::from_str(&object_json)?; let object_value: Value = serde_json::from_str(&object_json)?;
let object: Object = serde_json::from_value(object_value)?; let object: Object = serde_json::from_value(object_value)?;
Ok(object) Ok(object)

View file

@ -113,6 +113,7 @@ pub async fn process_note(
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error.into()),
}; };
let instance = config.instance();
let initial_object_id = object.id.clone(); let initial_object_id = object.id.clone();
let mut maybe_parent_object_id = object.in_reply_to.clone(); let mut maybe_parent_object_id = object.in_reply_to.clone();
let mut objects = vec![object]; let mut objects = vec![object];
@ -124,7 +125,7 @@ pub async fn process_note(
loop { loop {
let object_id = match maybe_parent_object_id { let object_id = match maybe_parent_object_id {
Some(parent_object_id) => { Some(parent_object_id) => {
if parse_object_id(&config.instance_url(), &parent_object_id).is_ok() { if parse_object_id(&instance.url(), &parent_object_id).is_ok() {
// Parent object is a local post // Parent object is a local post
break; break;
} }
@ -143,7 +144,7 @@ pub async fn process_note(
break; break;
}, },
}; };
let object = fetch_object(&object_id).await let object = fetch_object(&instance, &object_id).await
.map_err(|_| ValidationError("failed to fetch object"))?; .map_err(|_| ValidationError("failed to fetch object"))?;
maybe_parent_object_id = object.in_reply_to.clone(); maybe_parent_object_id = object.in_reply_to.clone();
objects.push(object); objects.push(object);
@ -157,7 +158,7 @@ pub async fn process_note(
.ok_or(ValidationError("unattributed note"))?; .ok_or(ValidationError("unattributed note"))?;
let author = get_or_fetch_profile_by_actor_id( let author = get_or_fetch_profile_by_actor_id(
db_client, db_client,
&config.instance(), &instance,
&attributed_to, &attributed_to,
&config.media_dir(), &config.media_dir(),
).await?; ).await?;
@ -189,7 +190,7 @@ pub async fn process_note(
if tag.tag_type == MENTION { if tag.tag_type == MENTION {
let profile = get_or_fetch_profile_by_actor_id( let profile = get_or_fetch_profile_by_actor_id(
db_client, db_client,
&config.instance(), &instance,
&tag.href, &tag.href,
&config.media_dir(), &config.media_dir(),
).await?; ).await?;
@ -199,7 +200,7 @@ pub async fn process_note(
}; };
let in_reply_to_id = match object.in_reply_to { let in_reply_to_id = match object.in_reply_to {
Some(object_id) => { Some(object_id) => {
match parse_object_id(&config.instance_url(), &object_id) { match parse_object_id(&instance.url(), &object_id) {
Ok(post_id) => { Ok(post_id) => {
// Local post // Local post
let post = get_post_by_id(db_client, &post_id).await?; let post = get_post_by_id(db_client, &post_id).await?;

View file

@ -80,8 +80,9 @@ async fn search_note(
if url::Url::parse(search_query).is_err() { if url::Url::parse(search_query).is_err() {
// Not a valid URL // Not a valid URL
return Ok(None); return Ok(None);
} };
let maybe_post = if let Ok(object) = fetch_object(search_query).await { let instance = config.instance();
let maybe_post = if let Ok(object) = fetch_object(&instance, search_query).await {
let post = process_note(config, db_client, object).await?; let post = process_note(config, db_client, object).await?;
Some(post) Some(post)
} else { } else {