Skip to main content

gitpoulpi_hosting/trust/
forget.rs

1//! Forgetting an integration: its trust record and everything cached from it.
2
3use gitpoulpi_config::{ConfigPaths, Integration};
4
5use super::store::TrustStore;
6use super::{clear_pending, origin, TrustSubject};
7
8/// What [`forget`] removed.
9#[derive(Debug, Default, Clone, PartialEq, Eq)]
10pub struct ForgetReport {
11    pub trust_record: bool,
12    pub list_cache_files: usize,
13    pub account_cache: bool,
14    pub review_drafts: usize,
15}
16
17/// Removes `integration`'s trust record and the caches filled from its host
18/// (the Clone dialog's saved repository lists, its Radar account answer, the
19/// remote review drafts of its host). Called by logout and remove, so a later
20/// connection to the same id starts from nothing.
21///
22/// Best effort per part: a file that cannot be read or written is left alone,
23/// the rest still goes.
24pub fn forget(paths: &ConfigPaths, integration: &Integration) -> ForgetReport {
25    let mut report = ForgetReport::default();
26    let id = integration.id.as_str();
27
28    let file = TrustStore::file_in(paths);
29    if let Ok(mut store) = TrustStore::load(&file) {
30        let before = store.integration.len();
31        store.integration.retain(|r| r.id != id);
32        if store.integration.len() != before {
33            report.trust_record = store.save(&file).is_ok();
34        }
35    }
36    clear_pending(&TrustSubject::Integration(id.to_string()));
37
38    #[cfg(feature = "hosting")]
39    {
40        report.list_cache_files =
41            crate::list_cache::remove_integration(&paths.cache_dir().join("clone-lists"), id);
42        let path = crate::account::cache_path(paths.config_dir());
43        let mut cache = crate::account::AccountCache::load(&path);
44        if cache.entries.remove(id).is_some() {
45            report.account_cache = cache.save(&path).is_ok();
46        }
47    }
48
49    if let Some(host) = origin::host_of(&integration.url) {
50        report.review_drafts = forget_review_drafts(paths, &host);
51    }
52    report
53}
54
55/// Drops the remote-review drafts and AI review defaults keyed by `host/`.
56fn forget_review_drafts(paths: &ConfigPaths, host: &str) -> usize {
57    let file = paths.config_dir().join("remote-reviews.toml");
58    let Ok(raw) = std::fs::read_to_string(&file) else {
59        return 0;
60    };
61    let Ok(mut table) = toml::from_str::<toml::Table>(&raw) else {
62        return 0;
63    };
64    let prefix = format!("{}/", host.to_ascii_lowercase());
65    let mut removed = 0;
66    for section in ["drafts", "ai_review"] {
67        if let Some(toml::Value::Table(rows)) = table.get_mut(section) {
68            let doomed: Vec<String> = rows
69                .keys()
70                .filter(|k| k.to_ascii_lowercase().starts_with(&prefix))
71                .cloned()
72                .collect();
73            removed += doomed.len();
74            for key in doomed {
75                rows.remove(&key);
76            }
77        }
78    }
79    if removed > 0 {
80        if let Ok(text) = toml::to_string_pretty(&table) {
81            let _ = gitpoulpi_config::secure_fs::write_private(&file, text.as_bytes());
82        }
83    }
84    removed
85}