1use crate::manifest::{UpdateKind, UpdateManifest};
9use crate::{Error, Result, Timestamp};
10use serde::{Deserialize, Serialize};
11use std::collections::BTreeSet;
12
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "kebab-case")]
16pub enum ConsentMode {
17 #[default]
19 Ask,
20 Auto,
22 NotifyOnly,
24 Off,
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "kebab-case")]
31pub enum Flavor {
32 Desktop,
34 Sideload,
36 Play,
38 AppStore,
40 MacAppStore,
42 FDroid,
44}
45
46impl Flavor {
47 pub fn default_mode(self) -> ConsentMode {
49 match self {
50 Self::FDroid => ConsentMode::Off,
51 _ => ConsentMode::Ask,
52 }
53 }
54
55 pub fn allows(self, kind: &UpdateKind, dart_patches: bool) -> bool {
58 match kind {
59 UpdateKind::StoreNotice { .. } => true,
60 UpdateKind::Assets | UpdateKind::Wasm { .. } => !matches!(self, Self::MacAppStore),
61 UpdateKind::Full(_) => matches!(self, Self::Desktop | Self::Sideload),
62 UpdateKind::DartPatch(_) => dart_patches && matches!(self, Self::Desktop | Self::Sideload | Self::Play),
63 }
64 }
65}
66
67#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
69pub struct Policy {
70 pub flavor: Flavor,
72 #[serde(default)]
74 pub dart_patches: bool,
75}
76
77#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(rename_all = "kebab-case")]
80pub enum Choice {
81 Accepted,
83 Declined,
85 Mode(ConsentMode),
87 FDroidBypassAcknowledged,
89}
90
91#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
93pub struct ConsentRecord {
94 pub update_id: Option<String>,
96 pub choice: Choice,
98 pub at: Timestamp,
100}
101
102#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
104pub struct ConsentLog {
105 pub records: Vec<ConsentRecord>,
107}
108
109impl ConsentLog {
110 pub fn decide(&mut self, update_id: &str, accepted: bool, at: Timestamp) {
112 let choice = if accepted { Choice::Accepted } else { Choice::Declined };
113 self.records.push(ConsentRecord { update_id: Some(update_id.to_owned()), choice, at });
114 }
115
116 pub fn set_mode(&mut self, flavor: Flavor, mode: ConsentMode, at: Timestamp) -> Result<()> {
119 if flavor == Flavor::FDroid && mode != ConsentMode::Off && !self.fdroid_acknowledged() {
120 return Err(Error::invalid("consent", "F-Droid builds need the bypass acknowledgement before updates are enabled"));
121 }
122 self.records.push(ConsentRecord { update_id: None, choice: Choice::Mode(mode), at });
123 Ok(())
124 }
125
126 pub fn acknowledge_fdroid(&mut self, at: Timestamp) {
128 self.records.push(ConsentRecord { update_id: None, choice: Choice::FDroidBypassAcknowledged, at });
129 }
130
131 fn fdroid_acknowledged(&self) -> bool {
132 self.records.iter().any(|r| r.choice == Choice::FDroidBypassAcknowledged)
133 }
134
135 pub fn mode(&self, flavor: Flavor) -> ConsentMode {
137 self.records
138 .iter()
139 .rev()
140 .find_map(|r| match r.choice {
141 Choice::Mode(m) => Some(m),
142 _ => None,
143 })
144 .unwrap_or(flavor.default_mode())
145 }
146
147 pub fn choice_for(&self, update_id: &str) -> Option<bool> {
149 self.records.iter().rev().find_map(|r| match (&r.update_id, &r.choice) {
150 (Some(id), Choice::Accepted) if id == update_id => Some(true),
151 (Some(id), Choice::Declined) if id == update_id => Some(false),
152 _ => None,
153 })
154 }
155}
156
157#[derive(Clone, Debug, PartialEq, Eq)]
159pub enum AskReason {
160 PerUpdate,
162 NewCapabilities,
164}
165
166#[derive(Clone, Copy, Debug, PartialEq, Eq)]
168pub enum SkipReason {
169 Off,
171 Declined,
173 KindNotAllowed,
175}
176
177#[derive(Clone, Debug, PartialEq, Eq)]
179pub enum Decision {
180 Install,
182 Ask {
184 reason: AskReason,
186 new_capabilities: Vec<String>,
188 security: bool,
190 },
191 Notify {
193 security: bool,
195 },
196 Skip(SkipReason),
198}
199
200pub fn decide(policy: &Policy, log: &ConsentLog, update: &UpdateManifest, granted: &BTreeSet<String>) -> Decision {
202 if !policy.flavor.allows(&update.kind, policy.dart_patches) {
203 return Decision::Skip(SkipReason::KindNotAllowed);
204 }
205 let mode = log.mode(policy.flavor);
206 let security = update.labels.security.is_some();
207 if mode == ConsentMode::Off {
208 return Decision::Skip(SkipReason::Off);
209 }
210 if matches!(update.kind, UpdateKind::StoreNotice { .. }) {
211 return Decision::Notify { security };
212 }
213 match log.choice_for(&update.update_id) {
214 Some(false) => return Decision::Skip(SkipReason::Declined),
215 Some(true) => return Decision::Install,
216 None => {}
217 }
218 let new_capabilities: Vec<String> = update.capabilities.iter().filter(|c| !granted.contains(*c)).cloned().collect();
219 match mode {
220 ConsentMode::NotifyOnly => Decision::Notify { security },
221 ConsentMode::Ask => Decision::Ask { reason: AskReason::PerUpdate, new_capabilities, security },
222 ConsentMode::Auto if new_capabilities.is_empty() => Decision::Install,
223 ConsentMode::Auto => Decision::Ask { reason: AskReason::NewCapabilities, new_capabilities, security },
224 ConsentMode::Off => Decision::Skip(SkipReason::Off),
225 }
226}