Skip to content

Commit 781de46

Browse files
committed
WIP: api!: add LIMIT arg to get_chat_media
In Delta Chat desktop we show the 3 recently used WebXDC apps, which relies on `get_chat_media`, which is quite expensive. Hopefully adding `LIMIT 3` makes it faster. Marking this as a breaking change because it's breaking TypeScript-wise, but shouldn't be breaking behavior-wise, because not providing the argument in JSON-RPC should be equivalent to providing `null` (which gets converted to `None`). TODO: - [ ] Add to CFFI? - [ ] Docs. Both the core fn and the JSON-RPC.
1 parent fc81cef commit 781de46

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ pub unsafe extern "C" fn dc_get_chat_media(
15321532
msg_type: libc::c_int,
15331533
or_msg_type2: libc::c_int,
15341534
or_msg_type3: libc::c_int,
1535+
// limit: u32,
15351536
) -> *mut dc_array::dc_array_t {
15361537
if context.is_null() {
15371538
eprintln!("ignoring careless call to dc_get_chat_media()");
@@ -1551,7 +1552,7 @@ pub unsafe extern "C" fn dc_get_chat_media(
15511552

15521553
block_on(async move {
15531554
Box::into_raw(Box::new(
1554-
chat::get_chat_media(ctx, chat_id, msg_type, or_msg_type2, or_msg_type3)
1555+
chat::get_chat_media(ctx, chat_id, msg_type, or_msg_type2, or_msg_type3, None)
15551556
.await
15561557
.unwrap_or_log_default(ctx, "Failed get_chat_media")
15571558
.into(),

deltachat-jsonrpc/src/api.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,7 @@ impl CommandApi {
17341734
message_type: MessageViewtype,
17351735
or_message_type2: Option<MessageViewtype>,
17361736
or_message_type3: Option<MessageViewtype>,
1737+
limit: Option<u32>,
17371738
) -> Result<Vec<u32>> {
17381739
let ctx = self.get_context(account_id).await?;
17391740

@@ -1745,7 +1746,8 @@ impl CommandApi {
17451746
let or_msg_type2 = or_message_type2.map_or(Viewtype::Unknown, |v| v.into());
17461747
let or_msg_type3 = or_message_type3.map_or(Viewtype::Unknown, |v| v.into());
17471748

1748-
let media = get_chat_media(&ctx, chat_id, msg_type, or_msg_type2, or_msg_type3).await?;
1749+
let media =
1750+
get_chat_media(&ctx, chat_id, msg_type, or_msg_type2, or_msg_type3, limit).await?;
17491751
Ok(media.iter().map(|msg_id| msg_id.to_u32()).collect())
17501752
}
17511753

deltachat-repl/src/cmdline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
10161016
Viewtype::Image,
10171017
Viewtype::Gif,
10181018
Viewtype::Video,
1019+
None,
10191020
)
10201021
.await?;
10211022
println!("{} images or videos: ", images.len());

src/chat.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,7 @@ pub async fn get_chat_media(
32923292
msg_type: Viewtype,
32933293
msg_type2: Viewtype,
32943294
msg_type3: Viewtype,
3295+
limit: Option<u32>,
32953296
) -> Result<Vec<MsgId>> {
32963297
let list = if msg_type == Viewtype::Webxdc
32973298
&& msg_type2 == Viewtype::Unknown
@@ -3306,12 +3307,14 @@ pub async fn get_chat_media(
33063307
AND chat_id != ?
33073308
AND type = ?
33083309
AND hidden=0
3309-
ORDER BY max(timestamp, timestamp_rcvd), id;",
3310+
ORDER BY max(timestamp, timestamp_rcvd), id
3311+
LIMIT ?;",
33103312
(
33113313
chat_id.is_none(),
33123314
chat_id.unwrap_or_else(|| ChatId::new(0)),
33133315
DC_CHAT_ID_TRASH,
33143316
Viewtype::Webxdc,
3317+
limit.map(|l| l.to_string()).unwrap_or("ALL".to_string()),
33153318
),
33163319
|row| row.get::<_, MsgId>(0),
33173320
|ids| Ok(ids.flatten().collect()),
@@ -3327,7 +3330,8 @@ pub async fn get_chat_media(
33273330
AND chat_id != ?
33283331
AND type IN (?, ?, ?)
33293332
AND hidden=0
3330-
ORDER BY timestamp, id;",
3333+
ORDER BY timestamp, id
3334+
LIMIT ?;",
33313335
(
33323336
chat_id.is_none(),
33333337
chat_id.unwrap_or_else(|| ChatId::new(0)),
@@ -3343,6 +3347,7 @@ pub async fn get_chat_media(
33433347
} else {
33443348
msg_type
33453349
},
3350+
limit.map(|l| l.to_string()).unwrap_or("ALL".to_string()),
33463351
),
33473352
|row| row.get::<_, MsgId>(0),
33483353
|ids| Ok(ids.flatten().collect()),

src/chat/chat_tests.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3189,7 +3189,8 @@ async fn test_get_chat_media() -> Result<()> {
31893189
Some(chat_id1),
31903190
Viewtype::Image,
31913191
Viewtype::Sticker,
3192-
Viewtype::Unknown
3192+
Viewtype::Unknown,
3193+
None,
31933194
)
31943195
.await?
31953196
.len(),
@@ -3250,6 +3251,7 @@ async fn test_get_chat_media() -> Result<()> {
32503251
Viewtype::Image,
32513252
Viewtype::Unknown,
32523253
Viewtype::Unknown,
3254+
None,
32533255
)
32543256
.await?
32553257
.len(),
@@ -3262,6 +3264,7 @@ async fn test_get_chat_media() -> Result<()> {
32623264
Viewtype::Sticker,
32633265
Viewtype::Unknown,
32643266
Viewtype::Unknown,
3267+
None,
32653268
)
32663269
.await?
32673270
.len(),
@@ -3274,18 +3277,33 @@ async fn test_get_chat_media() -> Result<()> {
32743277
Viewtype::Sticker,
32753278
Viewtype::Image,
32763279
Viewtype::Unknown,
3280+
None,
32773281
)
32783282
.await?
32793283
.len(),
32803284
2
32813285
);
3286+
assert_eq!(
3287+
get_chat_media(
3288+
&t,
3289+
Some(chat_id1),
3290+
Viewtype::Sticker,
3291+
Viewtype::Image,
3292+
Viewtype::Unknown,
3293+
Some(1),
3294+
)
3295+
.await?
3296+
.len(),
3297+
1
3298+
);
32823299
assert_eq!(
32833300
get_chat_media(
32843301
&t,
32853302
Some(chat_id2),
32863303
Viewtype::Webxdc,
32873304
Viewtype::Unknown,
32883305
Viewtype::Unknown,
3306+
None,
32893307
)
32903308
.await?
32913309
.len(),
@@ -3298,6 +3316,7 @@ async fn test_get_chat_media() -> Result<()> {
32983316
Viewtype::Image,
32993317
Viewtype::Unknown,
33003318
Viewtype::Unknown,
3319+
None,
33013320
)
33023321
.await?
33033322
.len(),
@@ -3310,6 +3329,33 @@ async fn test_get_chat_media() -> Result<()> {
33103329
Viewtype::Image,
33113330
Viewtype::Sticker,
33123331
Viewtype::Unknown,
3332+
None,
3333+
)
3334+
.await?
3335+
.len(),
3336+
3
3337+
);
3338+
assert_eq!(
3339+
get_chat_media(
3340+
&t,
3341+
None,
3342+
Viewtype::Image,
3343+
Viewtype::Sticker,
3344+
Viewtype::Webxdc,
3345+
None,
3346+
)
3347+
.await?
3348+
.len(),
3349+
4
3350+
);
3351+
assert_eq!(
3352+
get_chat_media(
3353+
&t,
3354+
None,
3355+
Viewtype::Image,
3356+
Viewtype::Sticker,
3357+
Viewtype::Webxdc,
3358+
Some(3),
33133359
)
33143360
.await?
33153361
.len(),
@@ -3322,6 +3368,7 @@ async fn test_get_chat_media() -> Result<()> {
33223368
Viewtype::Image,
33233369
Viewtype::Sticker,
33243370
Viewtype::Webxdc,
3371+
Some(99),
33253372
)
33263373
.await?
33273374
.len(),
@@ -3337,6 +3384,7 @@ async fn test_get_chat_media() -> Result<()> {
33373384
Viewtype::Image,
33383385
Viewtype::Sticker,
33393386
Viewtype::Webxdc,
3387+
None,
33403388
)
33413389
.await?
33423390
.len(),
@@ -3378,6 +3426,7 @@ async fn test_get_chat_media_webxdc_order() -> Result<()> {
33783426
Viewtype::Webxdc,
33793427
Viewtype::Unknown,
33803428
Viewtype::Unknown,
3429+
None,
33813430
)
33823431
.await?;
33833432
assert_eq!(media.first().unwrap(), &instance1_id);
@@ -3393,6 +3442,7 @@ async fn test_get_chat_media_webxdc_order() -> Result<()> {
33933442
Viewtype::Webxdc,
33943443
Viewtype::Unknown,
33953444
Viewtype::Unknown,
3445+
None,
33963446
)
33973447
.await?;
33983448
assert_eq!(media.first().unwrap(), &instance2_id);

0 commit comments

Comments
 (0)