Refactor and simplify code

This commit is contained in:
Hocuri
2026-04-21 15:35:59 +02:00
parent ceb10869c9
commit 6601495d39

View File

@@ -3282,74 +3282,32 @@ pub async fn marknoticed_all_chats(context: &Context) -> Result<()> {
pub(crate) async fn get_msgs_for_resending( pub(crate) async fn get_msgs_for_resending(
context: &Context, context: &Context,
chat_id: ChatId, chat_id: ChatId,
n_last: usize, n_msgs: usize,
) -> Result<Vec<MsgId>> { ) -> Result<Vec<MsgId>> {
let process_row = |row: &rusqlite::Row| { let items = context
// is_info logic taken from Message::is_info() .sql
let params = row.get::<_, String>("param")?; .query_map_vec(
let (from_id, to_id) = ( &format!(
row.get::<_, ContactId>("from_id")?, "
row.get::<_, ContactId>("to_id")?, SELECT id
); FROM msgs
// TODO probably we don't actually need to check `is_info_msg` here, WHERE chat_id=?
// because the SQL statement does it for us? AND hidden=0
let is_info_msg: bool = from_id == ContactId::INFO
|| to_id == ContactId::INFO
|| match Params::from_str(&params) {
Ok(p) => {
let cmd = p.get_cmd();
cmd != SystemMessage::Unknown && cmd != SystemMessage::AutocryptSetupMessage
}
_ => false,
};
Ok((
row.get::<_, i64>("timestamp")?,
row.get::<_, MsgId>("id")?,
!is_info_msg,
))
};
let process_rows = |rows: rusqlite::AndThenRows<_>| {
let mut filtered_rows = Vec::new();
for row in rows {
let (ts, curr_id, include): (i64, MsgId, bool) = row?;
if include {
filtered_rows.push((ts, curr_id));
}
}
let mut ret = Vec::new();
let mut last_day = 0;
let cnv_to_local = gm2local_offset();
for (ts, curr_id) in filtered_rows.into_iter().rev() {
ret.push(curr_id);
}
Ok(ret)
};
let items =
context
.sql
.query_map(
&format!("
SELECT m.id AS id, m.timestamp AS timestamp, m.param AS param, m.from_id AS from_id, m.to_id AS to_id
FROM msgs m
WHERE m.chat_id=?
AND m.hidden=0
AND NOT ( -- Exclude info and system messages AND NOT ( -- Exclude info and system messages
m.param GLOB '*\nS=*' OR param GLOB 'S=*' param GLOB '*\nS=*' OR param GLOB 'S=*'
OR m.from_id=? OR from_id=?
OR m.to_id=? OR to_id=?
) )
ORDER BY timestamp DESC, id DESC LIMIT ?" ORDER BY timestamp DESC, id DESC LIMIT ?"
), ),
(chat_id, ContactId::INFO, ContactId::INFO, n_last), (chat_id, ContactId::INFO, ContactId::INFO, n_msgs),
process_row, |row: &rusqlite::Row| Ok(row.get::<_, MsgId>(0)?),
process_rows, )
) .await?
.await? .into_iter()
; .rev()
.collect();
Ok(items) Ok(items)
} }