Telegram: factory alert switch and night mode
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
738b39584d
commit
9913a202ec
7 files changed
+169
-7
No files matched your search
+45
-2
@@ -191,6 +191,7 @@ pub fn router(app: AppState) -> Router {
|
||||
.route("/account/memory", get(crate::cli_api::get_memory).put(crate::cli_api::put_memory))
|
||||
.route("/account/modes", get(get_personal_modes).put(put_personal_modes))
|
||||
.route("/account/telegram", get(get_telegram).put(put_telegram).delete(delete_telegram))
|
||||
.route("/account/telegram/prefs", put(put_telegram_prefs))
|
||||
.route("/templates", get(crate::cli_api::get_templates))
|
||||
.route("/templates/{scope}", put(crate::cli_api::put_templates))
|
||||
.route("/cli/config", get(crate::cli_api::config))
|
||||
@@ -435,12 +436,52 @@ async fn get_telegram(Extension(user): Extension<User>, State(app): State<AppSta
|
||||
"configured": !s.token.is_empty(),
|
||||
"bot": s.bot,
|
||||
"paired": s.chat_id.is_some(),
|
||||
"pair_code": if s.chat_id.is_none() { s.pair_code } else { String::new() },
|
||||
"pair_code": if s.chat_id.is_none() { s.pair_code.clone() } else { String::new() },
|
||||
"running": running,
|
||||
"error": error,
|
||||
"factory_alerts": !s.factory_alerts_off,
|
||||
"quiet": s.quiet,
|
||||
"quiet_from": if s.quiet_from.is_empty() { "22:00".to_string() } else { s.quiet_from.clone() },
|
||||
"quiet_to": if s.quiet_to.is_empty() { "06:00".to_string() } else { s.quiet_to.clone() },
|
||||
"quiet_now": s.quiet_now(),
|
||||
"server_time": chrono::Local::now().format("%H:%M").to_string(),
|
||||
})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TelegramPrefs {
|
||||
factory_alerts: Option<bool>,
|
||||
quiet: Option<bool>,
|
||||
quiet_from: Option<String>,
|
||||
quiet_to: Option<String>,
|
||||
}
|
||||
|
||||
/// Notification preferences: factory alerts on/off and the night mode.
|
||||
async fn put_telegram_prefs(Extension(user): Extension<User>, State(app): State<AppState>, Json(b): Json<TelegramPrefs>) -> ApiResult {
|
||||
let mut s = crate::telegram::load(&app, &user.id);
|
||||
for (v, what) in [(&b.quiet_from, "from"), (&b.quiet_to, "to")] {
|
||||
if let Some(t) = v {
|
||||
if crate::telegram::parse_hhmm(t).is_none() {
|
||||
return Err(ApiError(StatusCode::BAD_REQUEST, format!("invalid time ({what}) – use HH:MM")));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(v) = b.factory_alerts {
|
||||
s.factory_alerts_off = !v;
|
||||
}
|
||||
if let Some(v) = b.quiet {
|
||||
s.quiet = v;
|
||||
}
|
||||
if let Some(v) = b.quiet_from {
|
||||
s.quiet_from = v.trim().to_string();
|
||||
}
|
||||
if let Some(v) = b.quiet_to {
|
||||
s.quiet_to = v.trim().to_string();
|
||||
}
|
||||
app.store.set_user_setting(&user.id, "telegram", &s)?;
|
||||
get_telegram(Extension(user), State(app)).await
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TelegramBody {
|
||||
token: String,
|
||||
@@ -451,7 +492,9 @@ async fn put_telegram(Extension(user): Extension<User>, State(app): State<AppSta
|
||||
let token = crate::telegram::extract_token(&b.token)
|
||||
.ok_or_else(|| ApiError(StatusCode::BAD_REQUEST, "that is not a bot token – it looks like 123456789:AAH… (paste only the token from @BotFather)".into()))?;
|
||||
let bot = crate::telegram::check_token(&token).await.map_err(|e| ApiError(StatusCode::BAD_REQUEST, format!("token rejected by Telegram: {e:#}")))?;
|
||||
let s = crate::telegram::Settings { token, bot, pair_code: crate::telegram::new_pair_code(), ..Default::default() };
|
||||
let old = crate::telegram::load(&app, &user.id);
|
||||
// Notification preferences survive a new bot.
|
||||
let s = crate::telegram::Settings { token, bot, pair_code: crate::telegram::new_pair_code(), factory_alerts_off: old.factory_alerts_off, quiet: old.quiet, quiet_from: old.quiet_from, quiet_to: old.quiet_to, ..Default::default() };
|
||||
app.store.set_user_setting(&user.id, "telegram", &s)?;
|
||||
crate::telegram::restart(&app, &user.id);
|
||||
app.audit(Some(&user), "telegram.connect", &s.bot, "");
|
||||
|
||||
Reference in new issue
Block a user