mirror of
https://github.com/chatmail/core.git
synced 2026-04-29 11:26:29 +03:00
maybe_add_time_based_warnings(): Use release date instead of the provider DB update one (#4213)
bjoern <r10s@b44t.com> wrote: > maybe_add_time_based_warnings() requires some date guaranteed to be in the near past. based on this known date we check if the system clock is wrong (if earlier than known date) and if the used Delta Chat version may be outdated (1 year passed since known date). while this does not catch all situations, it catches quite some errors with comparable few effort. > > figuring out the date guaranteed to be in the near past is a bit tricky. that time, we added get_provider_update_timestamp() for exactly that purpose - it is checked manually by some dev and updated from time to time, usually before a release. > > however, meanwhile, the provider-db gets updated less frequently - things might be settled a bit more - and, get_provider_update_timestamp() was also changed to return the date of the last commit, instead of last run. while that seem to be more on-purpose, we cannot even do an “empty” database update to update the known date. > > as get_provider_update_timestamp() is not used for anything else, maybe we should completely remove that function and replace it by get_last_release_timestamp that is then updated by ./scripts/set_core_version.py - the result of that is reviewed manually anyway, so that seems to be a good place (i prefer manual review here and mistrust further automation as also dev or ci clocks may be wrong :)
This commit is contained in:
@@ -71,6 +71,7 @@ pub mod ephemeral;
|
||||
mod http;
|
||||
mod imap;
|
||||
pub mod imex;
|
||||
pub mod release;
|
||||
mod scheduler;
|
||||
#[macro_use]
|
||||
mod job;
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
mod data;
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::{NaiveDateTime, NaiveTime};
|
||||
use trust_dns_resolver::{config, AsyncResolver, TokioAsyncResolver};
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::context::Context;
|
||||
use crate::provider::data::{PROVIDER_DATA, PROVIDER_IDS, PROVIDER_UPDATED};
|
||||
use crate::provider::data::{PROVIDER_DATA, PROVIDER_IDS};
|
||||
|
||||
/// Provider status according to manual testing.
|
||||
#[derive(Debug, Display, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
|
||||
@@ -258,22 +257,12 @@ pub fn get_provider_by_id(id: &str) -> Option<&'static Provider> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns update timestamp as a unix timestamp compatible for comparison with time() and database times.
|
||||
pub fn get_provider_update_timestamp() -> i64 {
|
||||
NaiveDateTime::new(*PROVIDER_UPDATED, NaiveTime::from_hms_opt(0, 0, 0).unwrap())
|
||||
.timestamp_millis()
|
||||
/ 1_000
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::indexing_slicing)]
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use super::*;
|
||||
use crate::test_utils::TestContext;
|
||||
use crate::tools::time;
|
||||
|
||||
#[test]
|
||||
fn test_get_provider_by_domain_unexistant() {
|
||||
@@ -336,18 +325,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_provider_update_timestamp() {
|
||||
let timestamp_past = NaiveDateTime::new(
|
||||
NaiveDate::from_ymd_opt(2020, 9, 9).unwrap(),
|
||||
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
)
|
||||
.timestamp_millis()
|
||||
/ 1_000;
|
||||
assert!(get_provider_update_timestamp() <= time());
|
||||
assert!(get_provider_update_timestamp() > timestamp_past);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_resolver() -> Result<()> {
|
||||
assert!(get_resolver().is_ok());
|
||||
|
||||
@@ -1945,5 +1945,5 @@ pub(crate) static PROVIDER_IDS: Lazy<HashMap<&'static str, &'static Provider>> =
|
||||
])
|
||||
});
|
||||
|
||||
pub static PROVIDER_UPDATED: Lazy<chrono::NaiveDate> =
|
||||
pub static _PROVIDER_UPDATED: Lazy<chrono::NaiveDate> =
|
||||
Lazy::new(|| chrono::NaiveDate::from_ymd_opt(2023, 2, 20).unwrap());
|
||||
|
||||
10
src/release.rs
Normal file
10
src/release.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
//! DC release info.
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
const DATE_STR: &str = include_str!("../release-date.in");
|
||||
|
||||
/// Last release date.
|
||||
pub static DATE: Lazy<NaiveDate> =
|
||||
Lazy::new(|| NaiveDate::parse_from_str(DATE_STR, "%Y-%m-%d").unwrap());
|
||||
62
src/tools.rs
62
src/tools.rs
@@ -13,7 +13,7 @@ use std::time::{Duration, SystemTime};
|
||||
|
||||
use anyhow::{bail, Context as _, Result};
|
||||
use base64::Engine as _;
|
||||
use chrono::{Local, TimeZone};
|
||||
use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
|
||||
use futures::{StreamExt, TryStreamExt};
|
||||
use mailparse::dateparse;
|
||||
use mailparse::headers::Headers;
|
||||
@@ -26,7 +26,6 @@ use crate::constants::{DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS};
|
||||
use crate::context::Context;
|
||||
use crate::events::EventType;
|
||||
use crate::message::{Message, Viewtype};
|
||||
use crate::provider::get_provider_update_timestamp;
|
||||
use crate::stock_str;
|
||||
|
||||
/// Shortens a string to a specified length and adds "[...]" to the
|
||||
@@ -163,12 +162,23 @@ pub(crate) fn create_smeared_timestamps(context: &Context, count: usize) -> i64
|
||||
context.smeared_timestamp.create_n(now, count as i64)
|
||||
}
|
||||
|
||||
/// Returns the last release timestamp as a unix timestamp compatible for comparison with time() and
|
||||
/// database times.
|
||||
pub fn get_release_timestamp() -> i64 {
|
||||
NaiveDateTime::new(
|
||||
*crate::release::DATE,
|
||||
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
)
|
||||
.timestamp_millis()
|
||||
/ 1_000
|
||||
}
|
||||
|
||||
// if the system time is not plausible, once a day, add a device message.
|
||||
// for testing we're using time() as that is also used for message timestamps.
|
||||
// moreover, add a warning if the app is outdated.
|
||||
pub(crate) async fn maybe_add_time_based_warnings(context: &Context) {
|
||||
if !maybe_warn_on_bad_time(context, time(), get_provider_update_timestamp()).await {
|
||||
maybe_warn_on_outdated(context, time(), get_provider_update_timestamp()).await;
|
||||
if !maybe_warn_on_bad_time(context, time(), get_release_timestamp()).await {
|
||||
maybe_warn_on_outdated(context, time(), get_release_timestamp()).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1140,17 +1150,17 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
/ 1_000;
|
||||
|
||||
// a correct time must not add a device message
|
||||
maybe_warn_on_bad_time(&t, timestamp_now, get_provider_update_timestamp()).await;
|
||||
maybe_warn_on_bad_time(&t, timestamp_now, get_release_timestamp()).await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
assert_eq!(chats.len(), 0);
|
||||
|
||||
// we cannot find out if a date in the future is wrong - a device message is not added
|
||||
maybe_warn_on_bad_time(&t, timestamp_future, get_provider_update_timestamp()).await;
|
||||
maybe_warn_on_bad_time(&t, timestamp_future, get_release_timestamp()).await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
assert_eq!(chats.len(), 0);
|
||||
|
||||
// a date in the past must add a device message
|
||||
maybe_warn_on_bad_time(&t, timestamp_past, get_provider_update_timestamp()).await;
|
||||
maybe_warn_on_bad_time(&t, timestamp_past, get_release_timestamp()).await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
assert_eq!(chats.len(), 1);
|
||||
let device_chat_id = chats.get_chat_id(0).unwrap();
|
||||
@@ -1158,31 +1168,21 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
assert_eq!(msgs.len(), 1);
|
||||
|
||||
// the message should be added only once a day - test that an hour later and nearly a day later
|
||||
maybe_warn_on_bad_time(
|
||||
&t,
|
||||
timestamp_past + 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
)
|
||||
.await;
|
||||
maybe_warn_on_bad_time(&t, timestamp_past + 60 * 60, get_release_timestamp()).await;
|
||||
let msgs = chat::get_chat_msgs(&t, device_chat_id).await.unwrap();
|
||||
assert_eq!(msgs.len(), 1);
|
||||
|
||||
maybe_warn_on_bad_time(
|
||||
&t,
|
||||
timestamp_past + 60 * 60 * 24 - 1,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
let msgs = chat::get_chat_msgs(&t, device_chat_id).await.unwrap();
|
||||
assert_eq!(msgs.len(), 1);
|
||||
|
||||
// next day, there should be another device message
|
||||
maybe_warn_on_bad_time(
|
||||
&t,
|
||||
timestamp_past + 60 * 60 * 24,
|
||||
get_provider_update_timestamp(),
|
||||
)
|
||||
.await;
|
||||
maybe_warn_on_bad_time(&t, timestamp_past + 60 * 60 * 24, get_release_timestamp()).await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
assert_eq!(chats.len(), 1);
|
||||
assert_eq!(device_chat_id, chats.get_chat_id(0).unwrap());
|
||||
@@ -1200,7 +1200,7 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
maybe_warn_on_outdated(
|
||||
&t,
|
||||
timestamp_now + 180 * 24 * 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
@@ -1210,7 +1210,7 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
maybe_warn_on_outdated(
|
||||
&t,
|
||||
timestamp_now + 365 * 24 * 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
@@ -1224,13 +1224,13 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
maybe_warn_on_outdated(
|
||||
&t,
|
||||
timestamp_now + (365 + 1) * 24 * 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
maybe_warn_on_outdated(
|
||||
&t,
|
||||
timestamp_now + (365 + 2) * 24 * 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
@@ -1245,7 +1245,7 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
maybe_warn_on_outdated(
|
||||
&t,
|
||||
timestamp_now + (365 + 33) * 24 * 60 * 60,
|
||||
get_provider_update_timestamp(),
|
||||
get_release_timestamp(),
|
||||
)
|
||||
.await;
|
||||
let chats = Chatlist::try_load(&t, 0, None, None).await.unwrap();
|
||||
@@ -1255,6 +1255,18 @@ DKIM Results: Passed=true, Works=true, Allow_Keychange=true";
|
||||
assert_eq!(msgs.len(), test_len + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_release_timestamp() {
|
||||
let timestamp_past = NaiveDateTime::new(
|
||||
NaiveDate::from_ymd_opt(2020, 9, 9).unwrap(),
|
||||
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
|
||||
)
|
||||
.timestamp_millis()
|
||||
/ 1_000;
|
||||
assert!(get_release_timestamp() <= time());
|
||||
assert!(get_release_timestamp() > timestamp_past);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_subject_prefix() {
|
||||
assert_eq!(remove_subject_prefix("Subject"), "Subject");
|
||||
|
||||
Reference in New Issue
Block a user