Skip to content

Commit 3267126

Browse files
committed
feat: Preserve minimum info for trashed messages
+ Make `MsgId::trash()` `pub(crate)`, not public. + In `delete_expired_messages()`, prepare SQL statements to be executed in a loop.
1 parent 2ee3675 commit 3267126

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

src/ephemeral.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -466,23 +466,18 @@ pub(crate) async fn delete_expired_messages(context: &Context, now: i64) -> Resu
466466
.transaction(|transaction| {
467467
let mut msgs_changed = Vec::with_capacity(rows.len());
468468
let mut webxdc_deleted = Vec::new();
469-
470-
// If you change which information is removed here, also change MsgId::trash() and
471-
// which information receive_imf::add_parts() still adds to the db if the chat_id is TRASH
469+
// If you change which information is preserved here, also change `MsgId::trash()`
470+
// and other places it references.
471+
let mut del_msg_stmt = transaction.prepare(
472+
"INSERT OR REPLACE INTO msgs (id, rfc724_mid, timestamp, chat_id)
473+
SELECT ?1, rfc724_mid, timestamp, ? FROM msgs WHERE id=?1",
474+
)?;
475+
let mut del_location_stmt =
476+
transaction.prepare("DELETE FROM locations WHERE independent=1 AND id=?")?;
472477
for (msg_id, chat_id, viewtype, location_id) in rows {
473-
transaction.execute(
474-
"UPDATE msgs
475-
SET chat_id=?, txt='', txt_normalized=NULL, subject='', txt_raw='',
476-
mime_headers='', from_id=0, to_id=0, param=''
477-
WHERE id=?",
478-
(DC_CHAT_ID_TRASH, msg_id),
479-
)?;
480-
478+
del_msg_stmt.execute((msg_id, DC_CHAT_ID_TRASH))?;
481479
if location_id > 0 {
482-
transaction.execute(
483-
"DELETE FROM locations WHERE independent=1 AND id=?",
484-
(location_id,),
485-
)?;
480+
del_location_stmt.execute((location_id,))?;
486481
}
487482

488483
msgs_changed.push((chat_id, msg_id));

src/message.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,16 @@ impl MsgId {
125125
/// if all parts of the message are trashed with this flag. `true` if the user explicitly
126126
/// deletes the message. As for trashing a partially downloaded message when replacing it with
127127
/// a fully downloaded one, see `receive_imf::add_parts()`.
128-
pub async fn trash(self, context: &Context, on_server: bool) -> Result<()> {
129-
let chat_id = DC_CHAT_ID_TRASH;
130-
let deleted_subst = match on_server {
131-
true => ", deleted=1",
132-
false => "",
133-
};
128+
pub(crate) async fn trash(self, context: &Context, on_server: bool) -> Result<()> {
134129
context
135130
.sql
136131
.execute(
137-
// If you change which information is removed here, also change delete_expired_messages() and
138-
// which information receive_imf::add_parts() still adds to the db if the chat_id is TRASH
139-
&format!(
140-
"UPDATE msgs SET \
141-
chat_id=?, txt='', txt_normalized=NULL, \
142-
subject='', txt_raw='', \
143-
mime_headers='', \
144-
from_id=0, to_id=0, \
145-
param=''{deleted_subst} \
146-
WHERE id=?"
147-
),
148-
(chat_id, self),
132+
// If you change which information is preserved here, also change
133+
// `delete_expired_messages()` and which information `receive_imf::add_parts()`
134+
// still adds to the db if chat_id is TRASH.
135+
"INSERT OR REPLACE INTO msgs (id, rfc724_mid, timestamp, chat_id, deleted)
136+
SELECT ?1, rfc724_mid, timestamp, ?, ? FROM msgs WHERE id=?1",
137+
(self, DC_CHAT_ID_TRASH, on_server),
149138
)
150139
.await?;
151140

src/receive_imf.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,11 +2055,11 @@ RETURNING id
20552055
if trash { ContactId::UNDEFINED } else { from_id },
20562056
if trash { ContactId::UNDEFINED } else { to_id },
20572057
sort_timestamp,
2058-
mime_parser.timestamp_sent,
2059-
mime_parser.timestamp_rcvd,
2060-
typ,
2061-
state,
2062-
is_dc_message,
2058+
if trash { 0 } else { mime_parser.timestamp_sent },
2059+
if trash { 0 } else { mime_parser.timestamp_rcvd },
2060+
if trash { Viewtype::Unknown } else { typ },
2061+
if trash { MessageState::Undefined } else { state },
2062+
if trash { MessengerMessage::No } else { is_dc_message },
20632063
if trash || hidden { "" } else { msg },
20642064
if trash || hidden { None } else { message::normalize_text(msg) },
20652065
if trash || hidden { "" } else { &subject },
@@ -2068,27 +2068,29 @@ RETURNING id
20682068
} else {
20692069
param.to_string()
20702070
},
2071-
hidden,
2072-
part.bytes as isize,
2071+
!trash && hidden,
2072+
if trash { 0 } else { part.bytes as isize },
20732073
if save_mime_modified && !(trash || hidden) {
20742074
mime_headers.clone()
20752075
} else {
20762076
Vec::new()
20772077
},
2078-
mime_in_reply_to,
2079-
mime_references,
2080-
save_mime_modified,
2081-
part.error.as_deref().unwrap_or_default(),
2082-
ephemeral_timer,
2083-
ephemeral_timestamp,
2084-
if is_partial_download.is_some() {
2078+
if trash { "" } else { mime_in_reply_to },
2079+
if trash { "" } else { mime_references },
2080+
!trash && save_mime_modified,
2081+
if trash { "" } else { part.error.as_deref().unwrap_or_default() },
2082+
if trash { 0 } else { ephemeral_timer.to_u32() },
2083+
if trash { 0 } else { ephemeral_timestamp },
2084+
if trash {
2085+
DownloadState::Done
2086+
} else if is_partial_download.is_some() {
20852087
DownloadState::Available
20862088
} else if mime_parser.decrypting_failed {
20872089
DownloadState::Undecipherable
20882090
} else {
20892091
DownloadState::Done
20902092
},
2091-
mime_parser.hop_info
2093+
if trash { "" } else { &mime_parser.hop_info },
20922094
],
20932095
|row| {
20942096
let msg_id: MsgId = row.get(0)?;

0 commit comments

Comments
 (0)