Skip to content

Commit

Permalink
feat: Limit the size of aggregated WebXDC update to 100 KiB (#4825)
Browse files Browse the repository at this point in the history
Before, update sending might be delayed due to rate limits and later merged into large
messages. This is undesirable for apps that want to send large files over WebXDC updates because the
message with aggregated update may be too large for actual sending and hit the provider limit or
require multiple attempts on a flaky SMTP connection.

So, don't aggregate updates if the size of an aggregated update will exceed the limit of 100
KiB. This is a soft limit, so it may be exceeded if a single update is larger and it limits only the
update JSON size, so the message with all envelopes still may be larger. Also don't send any updates
together with the WebXDC instance to not complicate the code, the only downside is sending one
message more when resending or forwarding WebXDC instances.
  • Loading branch information
iequidoo committed Apr 24, 2024
1 parent aff6bf9 commit 0df1218
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 80 deletions.
34 changes: 32 additions & 2 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::tools::{
create_smeared_timestamps, get_abs_path, gm2local_offset, improve_single_line_input,
smeared_time, time, IsNoneOrEmpty, SystemTime,
};
use crate::webxdc::WEBXDC_SUFFIX;
use crate::webxdc::{StatusUpdateSerial, WEBXDC_SUFFIX};

/// An chat item, such as a message or a marker.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -2894,7 +2894,37 @@ pub(crate) async fn create_send_msg_jobs(context: &Context, msg: &mut Message) -
}
Ok(row_ids)
};
context.sql.transaction(trans_fn).await
let job_ids = context.sql.transaction(trans_fn).await?;

if msg.viewtype == Viewtype::Webxdc {
let conn_fn = |conn: &mut rusqlite::Connection| {
let range = conn.query_row(
"SELECT IFNULL(min(id), 1), IFNULL(max(id), 0) \
FROM msgs_status_updates WHERE msg_id=?",
(msg.id,),
|row| {
let min_id: StatusUpdateSerial = row.get(0)?;
let max_id: StatusUpdateSerial = row.get(1)?;
Ok((min_id, max_id))
},
)?;
if range.0 > range.1 {
return Ok(());
};
// `first_serial` must be decreased to make parallel running
// `Context::flush_status_updates()` send the updates again.
conn.execute(
"INSERT INTO smtp_status_updates (msg_id, first_serial, last_serial, descr) \
VALUES(?, ?, ?, '') \
ON CONFLICT(msg_id) \
DO UPDATE SET first_serial=min(first_serial - 1, excluded.first_serial)",
(msg.id, range.0, range.1),
)?;
Ok(())
};
context.sql.call_write(conn_fn).await?;
}
Ok(job_ids)
}

/// Sends a text message to the given chat.
Expand Down
7 changes: 0 additions & 7 deletions src/mimefactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,13 +1302,6 @@ impl<'a> MimeFactory<'a> {
} else if command == SystemMessage::WebxdcStatusUpdate {
let json = self.msg.param.get(Param::Arg).unwrap_or_default();
parts.push(context.build_status_update_part(json));
} else if self.msg.viewtype == Viewtype::Webxdc {
if let Some(json) = context
.render_webxdc_status_update_object(self.msg.id, None)
.await?
{
parts.push(context.build_status_update_part(&json));
}
}

if self.attach_selfavatar {
Expand Down

0 comments on commit 0df1218

Please sign in to comment.