@@ -8,32 +8,33 @@ use std::sync::atomic::{AtomicBool, Ordering};
8
8
use std:: sync:: { Arc , OnceLock } ;
9
9
use std:: time:: Duration ;
10
10
11
- use anyhow:: { Context as _, Result , bail , ensure } ;
11
+ use anyhow:: { bail , ensure , Context as _, Result } ;
12
12
use async_channel:: { self as channel, Receiver , Sender } ;
13
13
use pgp:: types:: PublicKeyTrait ;
14
14
use ratelimit:: Ratelimit ;
15
15
use tokio:: sync:: { Mutex , Notify , RwLock } ;
16
16
17
- use crate :: chat:: { ChatId , ProtectionStatus , get_chat_cnt } ;
17
+ use crate :: chat:: { get_chat_cnt , ChatId , ProtectionStatus } ;
18
18
use crate :: chatlist_events;
19
19
use crate :: config:: Config ;
20
20
use crate :: constants:: {
21
21
self , DC_BACKGROUND_FETCH_QUOTA_CHECK_RATELIMIT , DC_CHAT_ID_TRASH , DC_VERSION_STR ,
22
22
} ;
23
- use crate :: contact:: { Contact , ContactId , import_vcard , mark_contact_id_as_verified } ;
23
+ use crate :: contact:: { import_vcard , mark_contact_id_as_verified , Contact , ContactId } ;
24
24
use crate :: debug_logging:: DebugLogging ;
25
25
use crate :: download:: DownloadState ;
26
26
use crate :: events:: { Event , EventEmitter , EventType , Events } ;
27
27
use crate :: imap:: { FolderMeaning , Imap , ServerMetadata } ;
28
- use crate :: key:: { load_self_secret_key, self_fingerprint} ;
28
+ use crate :: key:: { load_self_public_key, load_self_secret_key, DcKey as _} ;
29
+ use crate :: log:: LogExt ;
29
30
use crate :: log:: { info, warn} ;
30
31
use crate :: login_param:: { ConfiguredLoginParam , EnteredLoginParam } ;
31
- use crate :: message:: { self , Message , MessageState , MsgId } ;
32
+ use crate :: message:: { self , Message , MessageState , MsgId , Viewtype } ;
32
33
use crate :: param:: { Param , Params } ;
33
34
use crate :: peer_channels:: Iroh ;
34
35
use crate :: push:: PushSubscriber ;
35
36
use crate :: quota:: QuotaInfo ;
36
- use crate :: scheduler:: { SchedulerState , convert_folder_meaning } ;
37
+ use crate :: scheduler:: { convert_folder_meaning , SchedulerState } ;
37
38
use crate :: sql:: Sql ;
38
39
use crate :: stock_str:: StockStrings ;
39
40
use crate :: timesmearing:: SmearedTimestamp ;
@@ -1041,6 +1042,18 @@ impl Context {
1041
1042
. await ?
1042
1043
. to_string ( ) ,
1043
1044
) ;
1045
+ res. insert (
1046
+ "self_reporting" ,
1047
+ self . get_config_bool ( Config :: SelfReporting )
1048
+ . await ?
1049
+ . to_string ( ) ,
1050
+ ) ;
1051
+ res. insert (
1052
+ "last_self_report_sent" ,
1053
+ self . get_config_i64 ( Config :: LastSelfReportSent )
1054
+ . await ?
1055
+ . to_string ( ) ,
1056
+ ) ;
1044
1057
1045
1058
let elapsed = time_elapsed ( & self . creation_time ) ;
1046
1059
res. insert ( "uptime" , duration_to_str ( elapsed) ) ;
@@ -1160,7 +1173,8 @@ impl Context {
1160
1173
Some ( id) => id,
1161
1174
None => {
1162
1175
let id = create_id ( ) ;
1163
- self . set_config ( Config :: SelfReportingId , Some ( & id) ) . await ?;
1176
+ self . set_config_internal ( Config :: SelfReportingId , Some ( & id) )
1177
+ . await ?;
1164
1178
id
1165
1179
}
1166
1180
} ;
@@ -1174,7 +1188,15 @@ impl Context {
1174
1188
///
1175
1189
/// On the other end, a bot will receive the message and make it available
1176
1190
/// to Delta Chat's developers.
1177
- pub async fn draft_self_report ( & self ) -> Result < ChatId > {
1191
+ pub async fn send_self_report ( & self ) -> Result < ChatId > {
1192
+ info ! ( self , "Sending self report." ) ;
1193
+ // Setting `Config::LastHousekeeping` at the beginning avoids endless loops when things do not
1194
+ // work out for whatever reason or are interrupted by the OS.
1195
+ self . set_config_internal ( Config :: LastSelfReportSent , Some ( & time ( ) . to_string ( ) ) )
1196
+ . await
1197
+ . log_err ( self )
1198
+ . ok ( ) ;
1199
+
1178
1200
const SELF_REPORTING_BOT_VCARD : & str = include_str ! ( "../assets/self-reporting-bot.vcf" ) ;
1179
1201
let contact_id: ContactId = * import_vcard ( self , SELF_REPORTING_BOT_VCARD )
1180
1202
. await ?
@@ -1187,9 +1209,26 @@ impl Context {
1187
1209
. set_protection ( self , ProtectionStatus :: Protected , time ( ) , Some ( contact_id) )
1188
1210
. await ?;
1189
1211
1190
- let mut msg = Message :: new_text ( self . get_self_report ( ) . await ?) ;
1212
+ let mut msg = Message :: new ( Viewtype :: File ) ;
1213
+ msg. set_text (
1214
+ "The attachment contains anonymous usage statistics, \
1215
+ because you enabled this in the settings. \
1216
+ This helps us improve the security of Delta Chat. \
1217
+ See TODO[blog post] for more information."
1218
+ . to_string ( ) ,
1219
+ ) ;
1220
+ msg. set_file_from_bytes (
1221
+ self ,
1222
+ "statistics.txt" ,
1223
+ self . get_self_report ( ) . await ?. as_bytes ( ) ,
1224
+ Some ( "text/plain" ) ,
1225
+ ) ?;
1191
1226
1192
- chat_id. set_draft ( self , Some ( & mut msg) ) . await ?;
1227
+ crate :: chat:: send_msg ( self , chat_id, & mut msg)
1228
+ . await
1229
+ . context ( "Failed to send self_reporting message" )
1230
+ . log_err ( self )
1231
+ . ok ( ) ;
1193
1232
1194
1233
Ok ( chat_id)
1195
1234
}
0 commit comments