Skip to content

Commit 0f69230

Browse files
committed
feat: Donation request device message (#6913)
If there are at least {10,30,70...} outgoing delivered encrypted messages, add a donation request device messaage, but not more than once every two weeks.
1 parent a8a7cec commit 0f69230

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/chat.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,9 @@ async fn prepare_send_msg(
29832983
let row_ids = create_send_msg_jobs(context, msg)
29842984
.await
29852985
.context("Failed to create send jobs")?;
2986+
if !row_ids.is_empty() {
2987+
donation_request_maybe(context).await.log_err(context).ok();
2988+
}
29862989
Ok(row_ids)
29872990
}
29882991

@@ -3227,6 +3230,43 @@ pub async fn send_videochat_invitation(context: &Context, chat_id: ChatId) -> Re
32273230
send_msg(context, chat_id, &mut msg).await
32283231
}
32293232

3233+
async fn donation_request_maybe(context: &Context) -> Result<()> {
3234+
let request_ts = context.get_config_i64(Config::DonationRequestTs).await?;
3235+
let now = time();
3236+
let update_config = if request_ts.saturating_add(14 * 24 * 60 * 60) <= now {
3237+
let msg_cnt_prev = context
3238+
.get_config_parsed(Config::DonationRequestMsgCnt)
3239+
.await?
3240+
.unwrap_or(0);
3241+
let msg_cnt = context
3242+
.sql
3243+
.count(
3244+
"
3245+
SELECT COUNT(*) FROM msgs
3246+
WHERE state>=? AND hidden=0 AND chat_id!=? AND param GLOB \"*c=*\"",
3247+
(MessageState::OutDelivered, DC_CHAT_ID_TRASH),
3248+
)
3249+
.await?;
3250+
// 10, 30, 70...
3251+
if msg_cnt_prev < msg_cnt.saturating_sub(8) / 2 {
3252+
context
3253+
.set_config_internal(Config::DonationRequestMsgCnt, Some(&msg_cnt.to_string()))
3254+
.await?;
3255+
let mut msg = Message::new_text(stock_str::donation_request(context).await);
3256+
add_device_msg(context, None, Some(&mut msg)).await?;
3257+
}
3258+
true
3259+
} else {
3260+
request_ts > now
3261+
};
3262+
if update_config {
3263+
context
3264+
.set_config_internal(Config::DonationRequestTs, Some(&now.to_string()))
3265+
.await?;
3266+
}
3267+
Ok(())
3268+
}
3269+
32303270
/// Chat message list request options.
32313271
#[derive(Debug)]
32323272
pub struct MessageListOptions {

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ pub enum Config {
369369
#[strum(props(default = "0"))]
370370
DisableIdle,
371371

372+
/// Timestamp of the last check for donation request need.
373+
DonationRequestTs,
374+
375+
/// Number of outgoing encrypted messages at the time of the last donation request.
376+
DonationRequestMsgCnt,
377+
372378
/// Defines the max. size (in bytes) of messages downloaded automatically.
373379
/// 0 = no limit.
374380
#[strum(props(default = "0"))]

src/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,18 @@ impl Context {
10321032
.await?
10331033
.to_string(),
10341034
);
1035+
res.insert(
1036+
"donation_request_ts",
1037+
self.get_config_i64(Config::DonationRequestTs)
1038+
.await?
1039+
.to_string(),
1040+
);
1041+
res.insert(
1042+
"donation_request_msg_cnt",
1043+
self.get_config_u64(Config::DonationRequestMsgCnt)
1044+
.await?
1045+
.to_string(),
1046+
);
10351047

10361048
let elapsed = time_elapsed(&self.creation_time);
10371049
res.insert("uptime", duration_to_str(elapsed));

src/stock_str.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ pub enum StockMessage {
433433
fallback = "The contact must be online to proceed.\n\nThis process will continue automatically in background."
434434
))]
435435
SecurejoinTakesLonger = 192,
436+
437+
#[strum(props(fallback = "❤️ Seems you're using Delta Chat!
438+
439+
Please consider donating to help that Delta Chat stays free for everyone.
440+
441+
While Delta Chat is free to use and open source, development costs time and developers need money.
442+
Help keeping us to keep Delta Chat independent and make it more awesome in the future.
443+
444+
https://delta.chat/donate"))]
445+
DonationRequest = 193,
436446
}
437447

438448
impl StockMessage {
@@ -829,6 +839,11 @@ pub(crate) async fn securejoin_takes_longer(context: &Context) -> String {
829839
translated(context, StockMessage::SecurejoinTakesLonger).await
830840
}
831841

842+
/// Stock string: `❤️ Seems you're using Delta Chat!`…
843+
pub(crate) async fn donation_request(context: &Context) -> String {
844+
translated(context, StockMessage::DonationRequest).await
845+
}
846+
832847
/// Stock string: `Scan to chat with %1$s`.
833848
pub(crate) async fn setup_contact_qr_description(
834849
context: &Context,

0 commit comments

Comments
 (0)