Skip to content

Commit 6395dba

Browse files
committed
fix: Make it compile & the tests pass
1 parent 6615c55 commit 6395dba

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/self_reporting.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ pub async fn maybe_send_self_report(context: &Context) -> Result<Option<ChatId>>
117117

118118
async fn send_self_report(context: &Context) -> Result<ChatId> {
119119
info!(context, "Sending self report.");
120+
121+
let last_selfreport_time = context.get_config_i64(Config::LastSelfReportSent).await?;
122+
120123
// Setting this config at the beginning avoids endless loops when things do not
121124
// work out for whatever reason.
122125
context
@@ -135,10 +138,13 @@ This helps us improve the security of Delta Chat. \
135138
See TODO[blog post] for more information."
136139
.to_string(),
137140
);
141+
142+
let self_report = get_self_report(context, last_selfreport_time).await?;
143+
138144
msg.set_file_from_bytes(
139145
context,
140146
"statistics.txt",
141-
get_self_report(context).await?.as_bytes(),
147+
self_report.as_bytes(),
142148
Some("text/plain"),
143149
)?;
144150

@@ -151,7 +157,7 @@ See TODO[blog post] for more information."
151157
Ok(chat_id)
152158
}
153159

154-
async fn get_self_report(context: &Context) -> Result<String> {
160+
async fn get_self_report(context: &Context, last_selfreport_time: i64) -> Result<String> {
155161
let num_msgs: u32 = context
156162
.sql
157163
.query_get_value(
@@ -257,7 +263,7 @@ async fn get_self_report(context: &Context) -> Result<String> {
257263
chat_numbers,
258264
self_reporting_id,
259265
contact_stats: get_contact_stats(context).await?,
260-
message_stats: get_message_stats(context).await?,
266+
message_stats: get_message_stats(context, last_selfreport_time).await?,
261267
securejoin_source_stats: get_securejoin_source_stats(context).await?,
262268
};
263269

@@ -394,11 +400,17 @@ async fn get_contact_stats(context: &Context) -> Result<Vec<ContactStat>> {
394400
Ok(contacts)
395401
}
396402

397-
async fn get_message_stats(context: &Context) -> Result<MessageStats> {
403+
async fn get_message_stats(
404+
context: &Context,
405+
mut last_selfreport_time: i64,
406+
) -> Result<MessageStats> {
398407
let enabled_ts: i64 = context
399408
.get_config_i64(Config::SelfReportingEnabledTimestamp)
400409
.await?;
401-
ensure!(enabled_ts > 0, "Enabled Timestamp missing");
410+
if last_selfreport_time == 0 {
411+
last_selfreport_time = enabled_ts;
412+
}
413+
ensure!(last_selfreport_time > 0, "Enabled Timestamp missing");
402414

403415
let selfreporting_bot_chat_id = get_selfreporting_bot(context).await?;
404416

@@ -422,7 +434,7 @@ async fn get_message_stats(context: &Context) -> Result<MessageStats> {
422434
"SELECT COUNT(*) FROM msgs
423435
WHERE chat_id IN temp.verified_chats
424436
AND chat_id<>? AND id>9 AND timestamp_sent>?",
425-
(selfreporting_bot_chat_id, enabled_ts),
437+
(selfreporting_bot_chat_id, last_selfreport_time),
426438
|row| row.get(0),
427439
)?;
428440

@@ -431,7 +443,7 @@ async fn get_message_stats(context: &Context) -> Result<MessageStats> {
431443
WHERE chat_id not IN temp.verified_chats
432444
AND (param GLOB '*\nc=1*' OR param GLOB 'c=1*')
433445
AND chat_id<>? AND id>9 AND timestamp_sent>?",
434-
(selfreporting_bot_chat_id, enabled_ts),
446+
(selfreporting_bot_chat_id, last_selfreport_time),
435447
|row| row.get(0),
436448
)?;
437449

@@ -440,7 +452,7 @@ async fn get_message_stats(context: &Context) -> Result<MessageStats> {
440452
WHERE chat_id not IN temp.verified_chats
441453
AND NOT (param GLOB '*\nc=1*' OR param GLOB 'c=1*')
442454
AND chat_id<>? AND id>9 AND timestamp_sent>=?",
443-
(selfreporting_bot_chat_id, enabled_ts),
455+
(selfreporting_bot_chat_id, last_selfreport_time),
444456
|row| row.get(0),
445457
)?;
446458

src/self_reporting/self_reporting_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ async fn test_self_report_one_contact() -> Result<()> {
4545
let bob = &tcm.bob().await;
4646
alice.set_config_bool(Config::SelfReporting, true).await?;
4747

48-
let report = get_self_report(alice).await?;
48+
let report = get_self_report(alice, 0).await?;
4949
let r: serde_json::Value = serde_json::from_str(&report)?;
5050

5151
tcm.send_recv_accept(bob, alice, "Hi!").await;
5252

53-
let report = get_self_report(alice).await?;
53+
let report = get_self_report(alice, 0).await?;
5454
println!("\nWith Bob:\n{report}\n");
5555
let r2: serde_json::Value = serde_json::from_str(&report)?;
5656

@@ -142,7 +142,7 @@ async fn test_self_report_securejoin_source_stats() -> Result<()> {
142142
}
143143

144144
async fn check_securejoin_report(context: &TestContext, expected: &SecurejoinSourceStats) {
145-
let report = get_self_report(context).await.unwrap();
145+
let report = get_self_report(context, 0).await.unwrap();
146146
let actual: serde_json::Value = serde_json::from_str(&report).unwrap();
147147
let actual = &actual["securejoin_source_stats"];
148148

0 commit comments

Comments
 (0)