@@ -10,35 +10,30 @@ use std::time::Duration;
1010
1111use anyhow:: { Context as _, Result , bail, ensure} ;
1212use async_channel:: { self as channel, Receiver , Sender } ;
13- use pgp:: types:: PublicKeyTrait ;
1413use ratelimit:: Ratelimit ;
1514use tokio:: sync:: { Mutex , Notify , RwLock } ;
1615
17- use crate :: chat:: { ChatId , ProtectionStatus , get_chat_cnt} ;
16+ use crate :: chat:: { ChatId , get_chat_cnt} ;
1817use crate :: chatlist_events;
1918use crate :: config:: Config ;
20- use crate :: constants:: {
21- self , DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT , DC_CHAT_ID_TRASH , DC_VERSION_STR ,
22- } ;
23- use crate :: contact:: { Contact , ContactId , import_vcard, mark_contact_id_as_verified} ;
19+ use crate :: constants:: { self , DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT , DC_VERSION_STR } ;
20+ use crate :: contact:: { Contact , ContactId } ;
2421use crate :: debug_logging:: DebugLogging ;
25- use crate :: download:: DownloadState ;
2622use crate :: events:: { Event , EventEmitter , EventType , Events } ;
2723use crate :: imap:: { FolderMeaning , Imap , ServerMetadata } ;
28- use crate :: key:: { load_self_secret_key , self_fingerprint} ;
24+ use crate :: key:: self_fingerprint;
2925use crate :: log:: { info, warn} ;
3026use crate :: logged_debug_assert;
3127use crate :: login_param:: { ConfiguredLoginParam , EnteredLoginParam } ;
32- use crate :: message:: { self , Message , MessageState , MsgId } ;
33- use crate :: param:: { Param , Params } ;
28+ use crate :: message:: { self , MessageState , MsgId } ;
3429use crate :: peer_channels:: Iroh ;
3530use crate :: push:: PushSubscriber ;
3631use crate :: quota:: QuotaInfo ;
3732use crate :: scheduler:: { SchedulerState , convert_folder_meaning} ;
3833use crate :: sql:: Sql ;
3934use crate :: stock_str:: StockStrings ;
4035use crate :: timesmearing:: SmearedTimestamp ;
41- use crate :: tools:: { self , create_id , duration_to_str, time, time_elapsed} ;
36+ use crate :: tools:: { self , duration_to_str, time, time_elapsed} ;
4237
4338/// Builder for the [`Context`].
4439///
@@ -1076,154 +1071,31 @@ impl Context {
10761071 . await ?
10771072 . unwrap_or_default ( ) ,
10781073 ) ;
1074+ res. insert (
1075+ "statistics_id" ,
1076+ self . get_config_bool ( Config :: StatisticsId )
1077+ . await ?
1078+ . to_string ( ) ,
1079+ ) ;
1080+ res. insert (
1081+ "send_statistics" ,
1082+ self . get_config_bool ( Config :: SendStatistics )
1083+ . await ?
1084+ . to_string ( ) ,
1085+ ) ;
1086+ res. insert (
1087+ "last_statistics_sent" ,
1088+ self . get_config_i64 ( Config :: LastStatisticsSent )
1089+ . await ?
1090+ . to_string ( ) ,
1091+ ) ;
10791092
10801093 let elapsed = time_elapsed ( & self . creation_time ) ;
10811094 res. insert ( "uptime" , duration_to_str ( elapsed) ) ;
10821095
10831096 Ok ( res)
10841097 }
10851098
1086- async fn get_self_report ( & self ) -> Result < String > {
1087- #[ derive( Default ) ]
1088- struct ChatNumbers {
1089- protected : u32 ,
1090- opportunistic_dc : u32 ,
1091- opportunistic_mua : u32 ,
1092- unencrypted_dc : u32 ,
1093- unencrypted_mua : u32 ,
1094- }
1095-
1096- let mut res = String :: new ( ) ;
1097- res += & format ! ( "core_version {}\n " , get_version_str( ) ) ;
1098-
1099- let num_msgs: u32 = self
1100- . sql
1101- . query_get_value (
1102- "SELECT COUNT(*) FROM msgs WHERE hidden=0 AND chat_id!=?" ,
1103- ( DC_CHAT_ID_TRASH , ) ,
1104- )
1105- . await ?
1106- . unwrap_or_default ( ) ;
1107- res += & format ! ( "num_msgs {num_msgs}\n " ) ;
1108-
1109- let num_chats: u32 = self
1110- . sql
1111- . query_get_value ( "SELECT COUNT(*) FROM chats WHERE id>9 AND blocked!=1" , ( ) )
1112- . await ?
1113- . unwrap_or_default ( ) ;
1114- res += & format ! ( "num_chats {num_chats}\n " ) ;
1115-
1116- let db_size = tokio:: fs:: metadata ( & self . sql . dbfile ) . await ?. len ( ) ;
1117- res += & format ! ( "db_size_bytes {db_size}\n " ) ;
1118-
1119- let secret_key = & load_self_secret_key ( self ) . await ?. primary_key ;
1120- let key_created = secret_key. public_key ( ) . created_at ( ) . timestamp ( ) ;
1121- res += & format ! ( "key_created {key_created}\n " ) ;
1122-
1123- // how many of the chats active in the last months are:
1124- // - protected
1125- // - opportunistic-encrypted and the contact uses Delta Chat
1126- // - opportunistic-encrypted and the contact uses a classical MUA
1127- // - unencrypted and the contact uses Delta Chat
1128- // - unencrypted and the contact uses a classical MUA
1129- let three_months_ago = time ( ) . saturating_sub ( 3600 * 24 * 30 * 3 ) ;
1130- let chats = self
1131- . sql
1132- . query_map (
1133- "SELECT c.protected, m.param, m.msgrmsg
1134- FROM chats c
1135- JOIN msgs m
1136- ON c.id=m.chat_id
1137- AND m.id=(
1138- SELECT id
1139- FROM msgs
1140- WHERE chat_id=c.id
1141- AND hidden=0
1142- AND download_state=?
1143- AND to_id!=?
1144- ORDER BY timestamp DESC, id DESC LIMIT 1)
1145- WHERE c.id>9
1146- AND (c.blocked=0 OR c.blocked=2)
1147- AND IFNULL(m.timestamp,c.created_timestamp) > ?
1148- GROUP BY c.id" ,
1149- ( DownloadState :: Done , ContactId :: INFO , three_months_ago) ,
1150- |row| {
1151- let protected: ProtectionStatus = row. get ( 0 ) ?;
1152- let message_param: Params =
1153- row. get :: < _ , String > ( 1 ) ?. parse ( ) . unwrap_or_default ( ) ;
1154- let is_dc_message: bool = row. get ( 2 ) ?;
1155- Ok ( ( protected, message_param, is_dc_message) )
1156- } ,
1157- |rows| {
1158- let mut chats = ChatNumbers :: default ( ) ;
1159- for row in rows {
1160- let ( protected, message_param, is_dc_message) = row?;
1161- let encrypted = message_param
1162- . get_bool ( Param :: GuaranteeE2ee )
1163- . unwrap_or ( false ) ;
1164-
1165- if protected == ProtectionStatus :: Protected {
1166- chats. protected += 1 ;
1167- } else if encrypted {
1168- if is_dc_message {
1169- chats. opportunistic_dc += 1 ;
1170- } else {
1171- chats. opportunistic_mua += 1 ;
1172- }
1173- } else if is_dc_message {
1174- chats. unencrypted_dc += 1 ;
1175- } else {
1176- chats. unencrypted_mua += 1 ;
1177- }
1178- }
1179- Ok ( chats)
1180- } ,
1181- )
1182- . await ?;
1183- res += & format ! ( "chats_protected {}\n " , chats. protected) ;
1184- res += & format ! ( "chats_opportunistic_dc {}\n " , chats. opportunistic_dc) ;
1185- res += & format ! ( "chats_opportunistic_mua {}\n " , chats. opportunistic_mua) ;
1186- res += & format ! ( "chats_unencrypted_dc {}\n " , chats. unencrypted_dc) ;
1187- res += & format ! ( "chats_unencrypted_mua {}\n " , chats. unencrypted_mua) ;
1188-
1189- let self_reporting_id = match self . get_config ( Config :: SelfReportingId ) . await ? {
1190- Some ( id) => id,
1191- None => {
1192- let id = create_id ( ) ;
1193- self . set_config ( Config :: SelfReportingId , Some ( & id) ) . await ?;
1194- id
1195- }
1196- } ;
1197- res += & format ! ( "self_reporting_id {self_reporting_id}" ) ;
1198-
1199- Ok ( res)
1200- }
1201-
1202- /// Drafts a message with statistics about the usage of Delta Chat.
1203- /// The user can inspect the message if they want, and then hit "Send".
1204- ///
1205- /// On the other end, a bot will receive the message and make it available
1206- /// to Delta Chat's developers.
1207- pub async fn draft_self_report ( & self ) -> Result < ChatId > {
1208- const SELF_REPORTING_BOT_VCARD : & str = include_str ! ( "../assets/self-reporting-bot.vcf" ) ;
1209- let contact_id: ContactId = * import_vcard ( self , SELF_REPORTING_BOT_VCARD )
1210- . await ?
1211- . first ( )
1212- . context ( "Self reporting bot vCard does not contain a contact" ) ?;
1213- mark_contact_id_as_verified ( self , contact_id, ContactId :: SELF ) . await ?;
1214-
1215- let chat_id = ChatId :: create_for_contact ( self , contact_id) . await ?;
1216- chat_id
1217- . set_protection ( self , ProtectionStatus :: Protected , time ( ) , Some ( contact_id) )
1218- . await ?;
1219-
1220- let mut msg = Message :: new_text ( self . get_self_report ( ) . await ?) ;
1221-
1222- chat_id. set_draft ( self , Some ( & mut msg) ) . await ?;
1223-
1224- Ok ( chat_id)
1225- }
1226-
12271099 /// Get a list of fresh, unmuted messages in unblocked chats.
12281100 ///
12291101 /// The list starts with the most recent message
0 commit comments