|
| 1 | +use crate::{types::ServiceUpdater, ChangesSummary}; |
| 2 | +use anyhow::anyhow; |
| 3 | +use async_trait::async_trait; |
| 4 | +use fastside_shared::serde_types::Instance; |
| 5 | +use serde::Deserialize; |
| 6 | +use url::Url; |
| 7 | + |
| 8 | +pub struct SopranoUpdater { |
| 9 | + pub instances_urls: [String; 2], |
| 10 | +} |
| 11 | + |
| 12 | +impl SopranoUpdater { |
| 13 | + pub fn new() -> Self { |
| 14 | + Self { |
| 15 | + instances_urls: [ |
| 16 | + "https://git.vern.cc/cobra/Soprano/raw/branch/main/instances.json".to_string(), |
| 17 | + "https://codeberg.org/cobra/Soprano/raw/branch/main/instances.json".to_string(), |
| 18 | + ], |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for SopranoUpdater { |
| 24 | + fn default() -> Self { |
| 25 | + Self::new() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Deserialize)] |
| 30 | +struct SopranoInstance { |
| 31 | + #[serde(default)] |
| 32 | + clearnet: String, |
| 33 | + #[serde(default)] |
| 34 | + tor: String, |
| 35 | + #[serde(default)] |
| 36 | + i2p: String, |
| 37 | +} |
| 38 | + |
| 39 | +impl SopranoInstance { |
| 40 | + fn into_urls(self) -> Vec<Url> { |
| 41 | + [self.clearnet, self.tor, self.i2p] |
| 42 | + .into_iter() |
| 43 | + .filter(|s| !s.is_empty()) |
| 44 | + .filter_map(|s| Url::parse(&s).ok()) |
| 45 | + .collect() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[async_trait] |
| 50 | +impl ServiceUpdater for SopranoUpdater { |
| 51 | + async fn update( |
| 52 | + &self, |
| 53 | + client: reqwest::Client, |
| 54 | + current_instances: &[Instance], |
| 55 | + changes_summary: ChangesSummary, |
| 56 | + ) -> anyhow::Result<Vec<Instance>> { |
| 57 | + let mut parsed = None; |
| 58 | + let mut last_error = None; |
| 59 | + |
| 60 | + for instances_url in &self.instances_urls { |
| 61 | + let response = match client.get(instances_url).send().await { |
| 62 | + Ok(response) => response, |
| 63 | + Err(error) => { |
| 64 | + last_error = Some(anyhow!("failed to fetch {}: {}", instances_url, error)); |
| 65 | + continue; |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + if !response.status().is_success() { |
| 70 | + last_error = Some(anyhow!( |
| 71 | + "failed to fetch {}: HTTP {}", |
| 72 | + instances_url, |
| 73 | + response.status() |
| 74 | + )); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + let response_text = match response.text().await { |
| 79 | + Ok(response_text) => response_text, |
| 80 | + Err(error) => { |
| 81 | + last_error = Some(anyhow!( |
| 82 | + "failed to read response body from {}: {}", |
| 83 | + instances_url, |
| 84 | + error |
| 85 | + )); |
| 86 | + continue; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + match serde_json::from_str::<Vec<SopranoInstance>>(&response_text) { |
| 91 | + Ok(instances) => { |
| 92 | + parsed = Some(instances); |
| 93 | + break; |
| 94 | + } |
| 95 | + Err(error) => { |
| 96 | + last_error = Some(anyhow!( |
| 97 | + "failed to parse JSON from {}: {}", |
| 98 | + instances_url, |
| 99 | + error |
| 100 | + )); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + let parsed = parsed.ok_or_else(|| { |
| 106 | + last_error |
| 107 | + .unwrap_or_else(|| anyhow!("failed to load soprano instances from all sources")) |
| 108 | + })?; |
| 109 | + |
| 110 | + let mut instances = current_instances.to_vec(); |
| 111 | + let mut new_instances = Vec::new(); |
| 112 | + |
| 113 | + for soprano_instance in parsed { |
| 114 | + for url in soprano_instance.into_urls() { |
| 115 | + if current_instances.iter().any(|instance| instance.url == url) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + new_instances.push(Instance::from(url)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + changes_summary |
| 123 | + .set_new_instances_added( |
| 124 | + "soprano", |
| 125 | + new_instances |
| 126 | + .iter() |
| 127 | + .map(|instance| instance.url.clone()) |
| 128 | + .collect(), |
| 129 | + ) |
| 130 | + .await; |
| 131 | + |
| 132 | + instances.extend(new_instances); |
| 133 | + |
| 134 | + Ok(instances) |
| 135 | + } |
| 136 | +} |
0 commit comments