Addressing PR comments.

This commit is contained in:
Dessalines 2024-06-01 08:18:21 -04:00
parent d6376aee1e
commit 9a34d751f8

View file

@ -33,7 +33,6 @@ use lemmy_utils::error::LemmyResult;
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use std::time::Duration; use std::time::Duration;
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use url::Url;
/// Schedules various cleanup tasks for lemmy in a background thread /// Schedules various cleanup tasks for lemmy in a background thread
pub async fn setup(context: LemmyContext) -> LemmyResult<()> { pub async fn setup(context: LemmyContext) -> LemmyResult<()> {
@ -466,11 +465,6 @@ async fn update_instance_software(
info!("Updating instances software and versions..."); info!("Updating instances software and versions...");
let conn = get_conn(pool).await; let conn = get_conn(pool).await;
let allowed_rels = [
Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.1")?,
Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
];
match conn { match conn {
Ok(mut conn) => { Ok(mut conn) => {
let instances = instance::table.get_results::<Instance>(&mut conn).await?; let instances = instance::table.get_results::<Instance>(&mut conn).await?;
@ -481,7 +475,7 @@ async fn update_instance_software(
// not every Fediverse instance has a valid Nodeinfo endpoint (its not required for // not every Fediverse instance has a valid Nodeinfo endpoint (its not required for
// Activitypub). That's why we always need to mark instances as updated if they are // Activitypub). That's why we always need to mark instances as updated if they are
// alive. // alive.
let default_form = InstanceForm::builder() let mut instance_form = InstanceForm::builder()
.domain(instance.domain.clone()) .domain(instance.domain.clone())
.updated(Some(naive_now())) .updated(Some(naive_now()))
.build(); .build();
@ -492,16 +486,17 @@ async fn update_instance_software(
let form = match client.get(&well_known_url).send().await { let form = match client.get(&well_known_url).send().await {
Ok(res) if res.status().is_client_error() => { Ok(res) if res.status().is_client_error() => {
// Instance doesn't have well-known but sent a response, consider it alive // Instance doesn't have well-known but sent a response, consider it alive
Some(default_form) Some(instance_form)
} }
Ok(res) => match res.json::<NodeInfoWellKnown>().await { Ok(res) => match res.json::<NodeInfoWellKnown>().await {
Ok(well_known) => { Ok(well_known) => {
// Find the first link where the rel contains the allowed rels above // Find the first link where the rel contains the allowed rels above
match well_known match well_known.links.into_iter().find(|links| {
.links links
.into_iter() .rel
.find(|links| allowed_rels.contains(&links.rel)) .as_str()
{ .starts_with("http://nodeinfo.diaspora.software/ns/schema/2.")
}) {
Some(well_known_link) => { Some(well_known_link) => {
let node_info_url = well_known_link.href; let node_info_url = well_known_link.href;
@ -510,28 +505,25 @@ async fn update_instance_software(
Ok(node_info_res) => match node_info_res.json::<NodeInfo>().await { Ok(node_info_res) => match node_info_res.json::<NodeInfo>().await {
Ok(node_info) => { Ok(node_info) => {
// Instance sent valid nodeinfo, write it to db // Instance sent valid nodeinfo, write it to db
let software = node_info.software.as_ref(); // Set the instance form fields.
Some( if let Some(software) = node_info.software.as_ref() {
InstanceForm::builder() instance_form.software = software.name.clone();
.domain(instance.domain) instance_form.version = software.version.clone();
.updated(Some(naive_now()))
.software(software.and_then(|s| s.name.clone()))
.version(software.and_then(|s| s.version.clone()))
.build(),
)
} }
Err(_) => Some(default_form), Some(instance_form)
}
Err(_) => Some(instance_form),
}, },
Err(_) => Some(default_form), Err(_) => Some(instance_form),
} }
} }
// If none is found, use the default form above // If none is found, use the default form above
None => Some(default_form), None => Some(instance_form),
} }
} }
Err(_) => { Err(_) => {
// No valid nodeinfo but valid HTTP response, consider instance alive // No valid nodeinfo but valid HTTP response, consider instance alive
Some(default_form) Some(instance_form)
} }
}, },
Err(_) => { Err(_) => {