Skip to content

Commit 5f3948b

Browse files
committed
refactor(sql): add query_map_vec()
This also replaces some cases where flatten() was used, effectively ignoring the errors.
1 parent 45a1d81 commit 5f3948b

File tree

16 files changed

+83
-203
lines changed

16 files changed

+83
-203
lines changed

src/chat.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,9 @@ impl ChatId {
925925
/// Chat is considered active if something was posted there within the last 42 days.
926926
pub async fn get_similar_chat_ids(self, context: &Context) -> Result<Vec<(ChatId, f64)>> {
927927
// Count number of common members in this and other chats.
928-
let intersection: Vec<(ChatId, f64)> = context
928+
let intersection = context
929929
.sql
930-
.query_map(
930+
.query_map_vec(
931931
"SELECT y.chat_id, SUM(x.contact_id = y.contact_id)
932932
FROM chats_contacts as x
933933
JOIN chats_contacts as y
@@ -945,10 +945,6 @@ impl ChatId {
945945
let intersection: f64 = row.get(1)?;
946946
Ok((chat_id, intersection))
947947
},
948-
|rows| {
949-
rows.collect::<std::result::Result<Vec<_>, _>>()
950-
.map_err(Into::into)
951-
},
952948
)
953949
.await
954950
.context("failed to calculate member set intersections")?;
@@ -2010,7 +2006,7 @@ impl Chat {
20102006
let self_fp = self_fingerprint(context).await?;
20112007
let fingerprint_addrs = context
20122008
.sql
2013-
.query_map(
2009+
.query_map_vec(
20142010
"SELECT c.id, c.fingerprint, c.addr
20152011
FROM contacts c INNER JOIN chats_contacts cc
20162012
ON c.id=cc.contact_id
@@ -2024,22 +2020,20 @@ impl Chat {
20242020
let addr = row.get(2)?;
20252021
Ok((fingerprint, addr))
20262022
},
2027-
|addrs| addrs.collect::<Result<Vec<_>, _>>().map_err(Into::into),
20282023
)
20292024
.await?;
20302025
self.sync(context, SyncAction::SetPgpContacts(fingerprint_addrs))
20312026
.await?;
20322027
} else {
20332028
let addrs = context
20342029
.sql
2035-
.query_map(
2030+
.query_map_vec(
20362031
"SELECT c.addr \
20372032
FROM contacts c INNER JOIN chats_contacts cc \
20382033
ON c.id=cc.contact_id \
20392034
WHERE cc.chat_id=? AND cc.add_timestamp >= cc.remove_timestamp",
20402035
(self.id,),
20412036
|row| row.get::<_, String>(0),
2042-
|addrs| addrs.collect::<Result<Vec<_>, _>>().map_err(Into::into),
20432037
)
20442038
.await?;
20452039
self.sync(context, SyncAction::SetContacts(addrs)).await?;
@@ -3125,13 +3119,12 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31253119
if chat_id.is_archived_link() {
31263120
let chat_ids_in_archive = context
31273121
.sql
3128-
.query_map(
3122+
.query_map_vec(
31293123
"SELECT DISTINCT(m.chat_id) FROM msgs m
31303124
LEFT JOIN chats c ON m.chat_id=c.id
31313125
WHERE m.state=10 AND m.hidden=0 AND m.chat_id>9 AND c.archived=1",
31323126
(),
31333127
|row| row.get::<_, ChatId>(0),
3134-
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
31353128
)
31363129
.await?;
31373130
if chat_ids_in_archive.is_empty() {
@@ -3175,7 +3168,7 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31753168
// locally (i.e. when the chat was opened locally).
31763169
let hidden_messages = context
31773170
.sql
3178-
.query_map(
3171+
.query_map_vec(
31793172
"SELECT id, rfc724_mid FROM msgs
31803173
WHERE state=?
31813174
AND hidden=1
@@ -3187,10 +3180,6 @@ pub async fn marknoticed_chat(context: &Context, chat_id: ChatId) -> Result<()>
31873180
let rfc724_mid: String = row.get(1)?;
31883181
Ok((msg_id, rfc724_mid))
31893182
},
3190-
|rows| {
3191-
rows.collect::<std::result::Result<Vec<_>, _>>()
3192-
.map_err(Into::into)
3193-
},
31943183
)
31953184
.await?;
31963185
for (msg_id, rfc724_mid) in &hidden_messages {
@@ -3299,7 +3288,7 @@ pub async fn get_chat_media(
32993288
{
33003289
context
33013290
.sql
3302-
.query_map(
3291+
.query_map_vec(
33033292
"SELECT id
33043293
FROM msgs
33053294
WHERE (1=? OR chat_id=?)
@@ -3314,13 +3303,12 @@ pub async fn get_chat_media(
33143303
Viewtype::Webxdc,
33153304
),
33163305
|row| row.get::<_, MsgId>(0),
3317-
|ids| Ok(ids.flatten().collect()),
33183306
)
33193307
.await?
33203308
} else {
33213309
context
33223310
.sql
3323-
.query_map(
3311+
.query_map_vec(
33243312
"SELECT id
33253313
FROM msgs
33263314
WHERE (1=? OR chat_id=?)
@@ -3345,7 +3333,6 @@ pub async fn get_chat_media(
33453333
},
33463334
),
33473335
|row| row.get::<_, MsgId>(0),
3348-
|ids| Ok(ids.flatten().collect()),
33493336
)
33503337
.await?
33513338
};
@@ -3356,10 +3343,9 @@ pub async fn get_chat_media(
33563343
pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec<ContactId>> {
33573344
// Normal chats do not include SELF. Group chats do (as it may happen that one is deleted from a
33583345
// groupchat but the chats stays visible, moreover, this makes displaying lists easier)
3359-
3360-
let list = context
3346+
context
33613347
.sql
3362-
.query_map(
3348+
.query_map_vec(
33633349
"SELECT cc.contact_id
33643350
FROM chats_contacts cc
33653351
LEFT JOIN contacts c
@@ -3368,21 +3354,18 @@ pub async fn get_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec
33683354
ORDER BY c.id=1, c.last_seen DESC, c.id DESC;",
33693355
(chat_id,),
33703356
|row| row.get::<_, ContactId>(0),
3371-
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
33723357
)
3373-
.await?;
3374-
3375-
Ok(list)
3358+
.await
33763359
}
33773360

33783361
/// Returns a vector of contact IDs for given chat ID that are no longer part of the group.
33793362
///
33803363
/// Members that have been removed recently are in the beginning of the list.
33813364
pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Result<Vec<ContactId>> {
33823365
let now = time();
3383-
let list = context
3366+
context
33843367
.sql
3385-
.query_map(
3368+
.query_map_vec(
33863369
"SELECT cc.contact_id
33873370
FROM chats_contacts cc
33883371
LEFT JOIN contacts c
@@ -3393,11 +3376,8 @@ pub async fn get_past_chat_contacts(context: &Context, chat_id: ChatId) -> Resul
33933376
ORDER BY c.id=1, cc.remove_timestamp DESC, c.id DESC",
33943377
(chat_id, now.saturating_sub(60 * 24 * 3600)),
33953378
|row| row.get::<_, ContactId>(0),
3396-
|ids| ids.collect::<Result<Vec<_>, _>>().map_err(Into::into),
33973379
)
3398-
.await?;
3399-
3400-
Ok(list)
3380+
.await
34013381
}
34023382

34033383
/// Creates an encrypted group chat.

src/contact.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,14 +1282,10 @@ impl Contact {
12821282

12831283
let list = context
12841284
.sql
1285-
.query_map(
1285+
.query_map_vec(
12861286
"SELECT id FROM contacts WHERE id>? AND blocked!=0 ORDER BY last_seen DESC, id DESC;",
12871287
(ContactId::LAST_SPECIAL,),
12881288
|row| row.get::<_, ContactId>(0),
1289-
|ids| {
1290-
ids.collect::<std::result::Result<Vec<_>, _>>()
1291-
.map_err(Into::into)
1292-
},
12931289
)
12941290
.await?;
12951291
Ok(list)

src/context.rs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ impl Context {
10951095
pub async fn get_fresh_msgs(&self) -> Result<Vec<MsgId>> {
10961096
let list = self
10971097
.sql
1098-
.query_map(
1098+
.query_map_vec(
10991099
concat!(
11001100
"SELECT m.id",
11011101
" FROM msgs m",
@@ -1113,13 +1113,6 @@ impl Context {
11131113
),
11141114
(MessageState::InFresh, time()),
11151115
|row| row.get::<_, MsgId>(0),
1116-
|rows| {
1117-
let mut list = Vec::new();
1118-
for row in rows {
1119-
list.push(row?);
1120-
}
1121-
Ok(list)
1122-
},
11231116
)
11241117
.await?;
11251118
Ok(list)
@@ -1152,7 +1145,7 @@ impl Context {
11521145

11531146
let list = self
11541147
.sql
1155-
.query_map(
1148+
.query_map_vec(
11561149
"SELECT m.id
11571150
FROM msgs m
11581151
LEFT JOIN contacts ct
@@ -1172,13 +1165,6 @@ impl Context {
11721165
let msg_id: MsgId = row.get(0)?;
11731166
Ok(msg_id)
11741167
},
1175-
|rows| {
1176-
let mut list = Vec::new();
1177-
for row in rows {
1178-
list.push(row?);
1179-
}
1180-
Ok(list)
1181-
},
11821168
)
11831169
.await?;
11841170
Ok(list)
@@ -1219,7 +1205,7 @@ impl Context {
12191205

12201206
let list = if let Some(chat_id) = chat_id {
12211207
self.sql
1222-
.query_map(
1208+
.query_map_vec(
12231209
"SELECT m.id AS id
12241210
FROM msgs m
12251211
LEFT JOIN contacts ct
@@ -1231,13 +1217,6 @@ impl Context {
12311217
ORDER BY m.timestamp,m.id;",
12321218
(chat_id, str_like_in_text),
12331219
|row| row.get::<_, MsgId>("id"),
1234-
|rows| {
1235-
let mut ret = Vec::new();
1236-
for id in rows {
1237-
ret.push(id?);
1238-
}
1239-
Ok(ret)
1240-
},
12411220
)
12421221
.await?
12431222
} else {
@@ -1252,7 +1231,7 @@ impl Context {
12521231
// According to some tests, this limit speeds up eg. 2 character searches by factor 10.
12531232
// The limit is documented and UI may add a hint when getting 1000 results.
12541233
self.sql
1255-
.query_map(
1234+
.query_map_vec(
12561235
"SELECT m.id AS id
12571236
FROM msgs m
12581237
LEFT JOIN contacts ct
@@ -1267,13 +1246,6 @@ impl Context {
12671246
ORDER BY m.id DESC LIMIT 1000",
12681247
(str_like_in_text,),
12691248
|row| row.get::<_, MsgId>("id"),
1270-
|rows| {
1271-
let mut ret = Vec::new();
1272-
for id in rows {
1273-
ret.push(id?);
1274-
}
1275-
Ok(ret)
1276-
},
12771249
)
12781250
.await?
12791251
};

src/ephemeral.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ async fn select_expired_messages(
386386
) -> Result<Vec<(MsgId, ChatId, Viewtype, u32)>> {
387387
let mut rows = context
388388
.sql
389-
.query_map(
389+
.query_map_vec(
390390
r#"
391391
SELECT id, chat_id, type, location_id
392392
FROM msgs
@@ -407,7 +407,6 @@ WHERE
407407
let location_id: u32 = row.get("location_id")?;
408408
Ok((id, chat_id, viewtype, location_id))
409409
},
410-
|rows| rows.collect::<Result<Vec<_>, _>>().map_err(Into::into),
411410
)
412411
.await?;
413412

@@ -425,7 +424,7 @@ WHERE
425424

426425
let rows_expired = context
427426
.sql
428-
.query_map(
427+
.query_map_vec(
429428
r#"
430429
SELECT id, chat_id, type, location_id
431430
FROM msgs
@@ -453,7 +452,6 @@ WHERE
453452
let location_id: u32 = row.get("location_id")?;
454453
Ok((id, chat_id, viewtype, location_id))
455454
},
456-
|rows| rows.collect::<Result<Vec<_>, _>>().map_err(Into::into),
457455
)
458456
.await?;
459457

src/imap.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl Session {
10361036
async fn move_delete_messages(&mut self, context: &Context, folder: &str) -> Result<()> {
10371037
let rows = context
10381038
.sql
1039-
.query_map(
1039+
.query_map_vec(
10401040
"SELECT id, uid, target FROM imap
10411041
WHERE folder = ?
10421042
AND target != folder
@@ -1048,7 +1048,6 @@ impl Session {
10481048
let target: String = row.get(2)?;
10491049
Ok((rowid, uid, target))
10501050
},
1051-
|rows| rows.collect::<Result<Vec<_>, _>>().map_err(Into::into),
10521051
)
10531052
.await?;
10541053

@@ -1139,7 +1138,7 @@ impl Session {
11391138
pub(crate) async fn store_seen_flags_on_imap(&mut self, context: &Context) -> Result<()> {
11401139
let rows = context
11411140
.sql
1142-
.query_map(
1141+
.query_map_vec(
11431142
"SELECT imap.id, uid, folder FROM imap, imap_markseen
11441143
WHERE imap.id = imap_markseen.id AND target = folder
11451144
ORDER BY folder, uid",
@@ -1150,7 +1149,6 @@ impl Session {
11501149
let folder: String = row.get(2)?;
11511150
Ok((rowid, uid, folder))
11521151
},
1153-
|rows| rows.collect::<Result<Vec<_>, _>>().map_err(Into::into),
11541152
)
11551153
.await?;
11561154

src/imex.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ async fn export_self_keys(context: &Context, dir: &Path) -> Result<()> {
648648

649649
let keys = context
650650
.sql
651-
.query_map(
651+
.query_map_vec(
652652
"SELECT id, public_key, private_key, id=(SELECT value FROM config WHERE keyname='key_id') FROM keypairs;",
653653
(),
654654
|row| {
@@ -661,10 +661,6 @@ async fn export_self_keys(context: &Context, dir: &Path) -> Result<()> {
661661

662662
Ok((id, public_key, private_key, is_default))
663663
},
664-
|keys| {
665-
keys.collect::<std::result::Result<Vec<_>, _>>()
666-
.map_err(Into::into)
667-
},
668664
)
669665
.await?;
670666
let self_addr = context.get_primary_self_addr().await?;

0 commit comments

Comments
 (0)