| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
use quasi_declare::declare; |
| 47 |
use quasi_router::screen::Choice; |
| 48 |
use quasi_router::{Action, RouteError}; |
| 49 |
|
| 50 |
use crate::state::AppState; |
| 51 |
use crate::syncstore::sync_state; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub(super) const AUTO_SYNC: &str = "auto_sync_enabled"; |
| 60 |
|
| 61 |
pub(super) const INTERVAL: &str = "sync_interval_minutes"; |
| 62 |
|
| 63 |
|
| 64 |
struct State { |
| 65 |
configured: bool, |
| 66 |
authenticated: bool, |
| 67 |
server_url: Option<String>, |
| 68 |
encryption_ready: bool, |
| 69 |
device_id: Option<String>, |
| 70 |
auto_sync_enabled: bool, |
| 71 |
interval_minutes: u32, |
| 72 |
last_sync_at: Option<String>, |
| 73 |
pending_changes: i64, |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
fn read(state: &AppState) -> State { |
| 82 |
let client = state.read_recovering(); |
| 83 |
let (configured, authenticated, server_url, encryption_ready) = |
| 84 |
client.map_or((false, false, None, false), |client| { |
| 85 |
( |
| 86 |
true, |
| 87 |
client.session_info().is_some(), |
| 88 |
Some(client.config().server_url.clone()), |
| 89 |
client.has_master_key(), |
| 90 |
) |
| 91 |
}); |
| 92 |
|
| 93 |
let states = sync_state::get_sync_states_batch( |
| 94 |
&state.db, |
| 95 |
&[ |
| 96 |
"device_id", |
| 97 |
"auto_sync_enabled", |
| 98 |
"sync_interval_minutes", |
| 99 |
"last_sync_at", |
| 100 |
], |
| 101 |
) |
| 102 |
.unwrap_or_default(); |
| 103 |
|
| 104 |
State { |
| 105 |
configured, |
| 106 |
authenticated, |
| 107 |
server_url, |
| 108 |
encryption_ready, |
| 109 |
device_id: states.get("device_id").filter(|s| !s.is_empty()).cloned(), |
| 110 |
|
| 111 |
|
| 112 |
auto_sync_enabled: states.get("auto_sync_enabled").is_none_or(|v| v == "1"), |
| 113 |
interval_minutes: states |
| 114 |
.get("sync_interval_minutes") |
| 115 |
.and_then(|v| v.parse().ok()) |
| 116 |
.unwrap_or(5), |
| 117 |
last_sync_at: states.get("last_sync_at").cloned(), |
| 118 |
pending_changes: sync_state::count_pending_changes(&state.db).unwrap_or(0), |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
fn auto_choice(state: &State) -> &'static str { |
| 124 |
if state.auto_sync_enabled { |
| 125 |
"enabled" |
| 126 |
} else { |
| 127 |
"disabled" |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
fn server(state: &State) -> Option<&str> { |
| 133 |
state.server_url.as_deref().filter(|url| !url.is_empty()) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
fn device(state: &State) -> Option<&str> { |
| 138 |
state.device_id.as_deref().filter(|id| !id.is_empty()) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
fn status_label(state: &State) -> &'static str { |
| 143 |
if state.authenticated { |
| 144 |
"Signed in" |
| 145 |
} else { |
| 146 |
"Set up, not signed in" |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
fn encryption_label(state: &State) -> &'static str { |
| 152 |
if state.encryption_ready { |
| 153 |
"Ready" |
| 154 |
} else { |
| 155 |
"Not set up" |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
fn last_sync(state: &State) -> String { |
| 161 |
state |
| 162 |
.last_sync_at |
| 163 |
.clone() |
| 164 |
.unwrap_or_else(|| "Never".to_owned()) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn waiting(state: &State) -> String { |
| 172 |
match state.pending_changes { |
| 173 |
0 => "Nothing".to_owned(), |
| 174 |
1 => "1 change".to_owned(), |
| 175 |
n => format!("{n} changes"), |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
declare! { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
pub(super) shape pane(app: &AppState) -> Vec<Node>; |
| 190 |
|
| 191 |
let state = read(app); |
| 192 |
|
| 193 |
section "Cloud Sync"; |
| 194 |
|
| 195 |
empty "Sync is not set up on this device. Setting it up asks a server for an \ |
| 196 |
account, which this screen cannot do yet." |
| 197 |
unless state.configured; |
| 198 |
|
| 199 |
list { |
| 200 |
row "Status" { |
| 201 |
meta status_label(&state); |
| 202 |
} |
| 203 |
for server in server(&state).into_iter() { |
| 204 |
row "Server" { |
| 205 |
meta server; |
| 206 |
} |
| 207 |
} |
| 208 |
row "Encryption" { |
| 209 |
meta encryption_label(&state); |
| 210 |
} |
| 211 |
for device in device(&state).into_iter() { |
| 212 |
row "This device" { |
| 213 |
meta device; |
| 214 |
} |
| 215 |
} |
| 216 |
row "Last sync" { |
| 217 |
meta last_sync(&state); |
| 218 |
} |
| 219 |
row "Waiting to send" { |
| 220 |
meta waiting(&state); |
| 221 |
} |
| 222 |
} when state.configured; |
| 223 |
|
| 224 |
field Select AUTO_SYNC "Sync automatically" when state.configured { |
| 225 |
option Choice::new("enabled", "Enabled (default)"); |
| 226 |
option Choice::new("disabled", "Disabled"); |
| 227 |
value auto_choice(&state); |
| 228 |
hint "When off, nothing is sent or fetched until a sync is started by hand."; |
| 229 |
writes Action::post("/settings/sync/auto"); |
| 230 |
} |
| 231 |
|
| 232 |
field Number INTERVAL "Minutes between syncs" when state.configured { |
| 233 |
within "1" "1440"; |
| 234 |
value state.interval_minutes.to_string(); |
| 235 |
writes Action::post("/settings/sync/interval"); |
| 236 |
} |
| 237 |
|
| 238 |
act "Disconnect" to post "/settings/sync/disconnect" |
| 239 |
when state.configured and state.authenticated { |
| 240 |
tone Danger; |
| 241 |
confirm "Disconnect this device from cloud sync? Your data stays here; \ |
| 242 |
nothing more will be sent or fetched until you sign in again."; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
pub(super) fn set_auto(app: &AppState, on: bool) -> Result<&'static str, RouteError> { |
| 248 |
sync_state::set_sync_state(&app.db, AUTO_SYNC, if on { "1" } else { "0" }) |
| 249 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 250 |
Ok(if on { |
| 251 |
"Syncing automatically." |
| 252 |
} else { |
| 253 |
"Automatic syncing off." |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
pub(super) fn set_interval(app: &AppState, minutes: u32) -> Result<String, RouteError> { |
| 262 |
let minutes = minutes.clamp(1, 1440); |
| 263 |
sync_state::set_sync_state(&app.db, INTERVAL, &minutes.to_string()) |
| 264 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 265 |
Ok(format!("Syncing every {minutes} minutes.")) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
pub(super) fn disconnect(app: &AppState) -> Result<&'static str, RouteError> { |
| 274 |
crate::oauth::credentials::CredentialStore::delete_sync_token() |
| 275 |
.map_err(RouteError::internal)?; |
| 276 |
if let Some(store) = &app.sync_store { |
| 277 |
store.disconnect(); |
| 278 |
} |
| 279 |
Ok("Disconnected. Your data is still here.") |
| 280 |
} |
| 281 |
|
| 282 |
#[cfg(test)] |
| 283 |
mod tests; |
| 284 |
|