@@ -28,8 +28,9 @@ use crate::download::DownloadState;
2828use crate :: events:: { Event , EventEmitter , EventType , Events } ;
2929use crate :: imap:: { FolderMeaning , Imap , ServerMetadata } ;
3030use crate :: key:: { load_self_public_key, load_self_secret_key, DcKey as _} ;
31+ use crate :: log:: LogExt ;
3132use crate :: login_param:: { ConfiguredLoginParam , EnteredLoginParam } ;
32- use crate :: message:: { self , Message , MessageState , MsgId } ;
33+ use crate :: message:: { self , Message , MessageState , MsgId , Viewtype } ;
3334use crate :: param:: { Param , Params } ;
3435use crate :: peer_channels:: Iroh ;
3536use crate :: peerstate:: Peerstate ;
@@ -1032,6 +1033,18 @@ impl Context {
10321033 . await ?
10331034 . to_string ( ) ,
10341035 ) ;
1036+ res. insert (
1037+ "self_reporting" ,
1038+ self . get_config_bool ( Config :: SelfReporting )
1039+ . await ?
1040+ . to_string ( ) ,
1041+ ) ;
1042+ res. insert (
1043+ "last_self_report_sent" ,
1044+ self . get_config_i64 ( Config :: LastSelfReportSent )
1045+ . await ?
1046+ . to_string ( ) ,
1047+ ) ;
10351048
10361049 let elapsed = time_elapsed ( & self . creation_time ) ;
10371050 res. insert ( "uptime" , duration_to_str ( elapsed) ) ;
@@ -1151,7 +1164,8 @@ impl Context {
11511164 Some ( id) => id,
11521165 None => {
11531166 let id = create_id ( ) ;
1154- self . set_config ( Config :: SelfReportingId , Some ( & id) ) . await ?;
1167+ self . set_config_internal ( Config :: SelfReportingId , Some ( & id) )
1168+ . await ?;
11551169 id
11561170 }
11571171 } ;
@@ -1165,7 +1179,15 @@ impl Context {
11651179 ///
11661180 /// On the other end, a bot will receive the message and make it available
11671181 /// to Delta Chat's developers.
1168- pub async fn draft_self_report ( & self ) -> Result < ChatId > {
1182+ pub async fn send_self_report ( & self ) -> Result < ChatId > {
1183+ info ! ( self , "Sending self report." ) ;
1184+ // Setting `Config::LastHousekeeping` at the beginning avoids endless loops when things do not
1185+ // work out for whatever reason or are interrupted by the OS.
1186+ self . set_config_internal ( Config :: LastSelfReportSent , Some ( & time ( ) . to_string ( ) ) )
1187+ . await
1188+ . log_err ( self )
1189+ . ok ( ) ;
1190+
11691191 const SELF_REPORTING_BOT : & str = "self_reporting@testrun.org" ;
11701192
11711193 let contact_id = Contact :: create ( self , "Statistics bot" , SELF_REPORTING_BOT ) . await ?;
@@ -1196,9 +1218,26 @@ impl Context {
11961218 . set_protection ( self , ProtectionStatus :: Protected , time ( ) , Some ( contact_id) )
11971219 . await ?;
11981220
1199- let mut msg = Message :: new_text ( self . get_self_report ( ) . await ?) ;
1221+ let mut msg = Message :: new ( Viewtype :: File ) ;
1222+ msg. set_text (
1223+ "The attachment contains anonymous usage statistics, \
1224+ because you enabled this in the settings. \
1225+ This helps us improve the security of Delta Chat. \
1226+ See TODO[blog post] for more information."
1227+ . to_string ( ) ,
1228+ ) ;
1229+ msg. set_file_from_bytes (
1230+ self ,
1231+ "statistics.txt" ,
1232+ self . get_self_report ( ) . await ?. as_bytes ( ) ,
1233+ Some ( "text/plain" ) ,
1234+ ) ?;
12001235
1201- chat_id. set_draft ( self , Some ( & mut msg) ) . await ?;
1236+ crate :: chat:: send_msg ( self , chat_id, & mut msg)
1237+ . await
1238+ . context ( "Failed to send self_reporting message" )
1239+ . log_err ( self )
1240+ . ok ( ) ;
12021241
12031242 Ok ( chat_id)
12041243 }
0 commit comments