Skip to content

Commit 2637c3b

Browse files
committed
refactor: replace async RwLock with sync RwLock for stock strings
1 parent d1f1633 commit 2637c3b

29 files changed

+309
-412
lines changed

deltachat-ffi/src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,17 @@ pub unsafe extern "C" fn dc_set_stock_translation(
306306
let msg = to_string_lossy(stock_msg);
307307
let ctx = &*context;
308308

309-
block_on(async move {
310-
match StockMessage::from_u32(stock_id)
311-
.with_context(|| format!("Invalid stock message ID {stock_id}"))
309+
match StockMessage::from_u32(stock_id)
310+
.with_context(|| format!("Invalid stock message ID {stock_id}"))
311+
.log_err(ctx)
312+
{
313+
Ok(id) => ctx
314+
.set_stock_translation(id, msg)
315+
.context("set_stock_translation failed")
312316
.log_err(ctx)
313-
{
314-
Ok(id) => ctx
315-
.set_stock_translation(id, msg)
316-
.await
317-
.context("set_stock_translation failed")
318-
.log_err(ctx)
319-
.is_ok() as libc::c_int,
320-
Err(_) => 0,
321-
}
322-
})
317+
.is_ok() as libc::c_int,
318+
Err(_) => 0,
319+
}
323320
}
324321

325322
#[no_mangle]

deltachat-jsonrpc/src/api.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,7 @@ impl CommandApi {
473473
let accounts = self.accounts.read().await;
474474
for (stock_id, stock_message) in strings {
475475
if let Some(stock_id) = StockMessage::from_u32(stock_id) {
476-
accounts
477-
.set_stock_translation(stock_id, stock_message)
478-
.await?;
476+
accounts.set_stock_translation(stock_id, stock_message)?;
479477
}
480478
}
481479
Ok(())

src/accounts.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,13 +1249,11 @@ mod tests {
12491249
let account1 = accounts.get_account(1).context("failed to get account 1")?;
12501250
let account2 = accounts.get_account(2).context("failed to get account 2")?;
12511251

1252-
assert_eq!(stock_str::no_messages(&account1).await, "No messages.");
1253-
assert_eq!(stock_str::no_messages(&account2).await, "No messages.");
1254-
account1
1255-
.set_stock_translation(StockMessage::NoMessages, "foobar".to_string())
1256-
.await?;
1257-
assert_eq!(stock_str::no_messages(&account1).await, "foobar");
1258-
assert_eq!(stock_str::no_messages(&account2).await, "foobar");
1252+
assert_eq!(stock_str::no_messages(&account1), "No messages.");
1253+
assert_eq!(stock_str::no_messages(&account2), "No messages.");
1254+
account1.set_stock_translation(StockMessage::NoMessages, "foobar".to_string())?;
1255+
assert_eq!(stock_str::no_messages(&account1), "foobar");
1256+
assert_eq!(stock_str::no_messages(&account2), "foobar");
12591257

12601258
Ok(())
12611259
}

src/calls.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,11 @@ impl CallInfo {
104104
};
105105

106106
if self.is_incoming() {
107-
let incoming_call_str =
108-
stock_str::incoming_call(context, self.has_video_initially()).await;
107+
let incoming_call_str = stock_str::incoming_call(context, self.has_video_initially());
109108
self.update_text(context, &format!("{incoming_call_str}\n{duration}"))
110109
.await?;
111110
} else {
112-
let outgoing_call_str =
113-
stock_str::outgoing_call(context, self.has_video_initially()).await;
111+
let outgoing_call_str = stock_str::outgoing_call(context, self.has_video_initially());
114112
self.update_text(context, &format!("{outgoing_call_str}\n{duration}"))
115113
.await?;
116114
}
@@ -207,7 +205,7 @@ impl Context {
207205
);
208206
ensure!(!chat.is_self_talk(), "Cannot call self");
209207

210-
let outgoing_call_str = stock_str::outgoing_call(self, has_video_initially).await;
208+
let outgoing_call_str = stock_str::outgoing_call(self, has_video_initially);
211209
let mut call = Message {
212210
viewtype: Viewtype::Call,
213211
text: outgoing_call_str,
@@ -286,11 +284,11 @@ impl Context {
286284
if call.is_incoming() {
287285
call.mark_as_ended(self).await?;
288286
markseen_msgs(self, vec![call_id]).await?;
289-
let declined_call_str = stock_str::declined_call(self).await;
287+
let declined_call_str = stock_str::declined_call(self);
290288
call.update_text(self, &declined_call_str).await?;
291289
} else {
292290
call.mark_as_canceled(self).await?;
293-
let canceled_call_str = stock_str::canceled_call(self).await;
291+
let canceled_call_str = stock_str::canceled_call(self);
294292
call.update_text(self, &canceled_call_str).await?;
295293
}
296294
} else {
@@ -333,11 +331,11 @@ impl Context {
333331
if !call.is_accepted() && !call.is_ended() {
334332
if call.is_incoming() {
335333
call.mark_as_canceled(&context).await?;
336-
let missed_call_str = stock_str::missed_call(&context).await;
334+
let missed_call_str = stock_str::missed_call(&context);
337335
call.update_text(&context, &missed_call_str).await?;
338336
} else {
339337
call.mark_as_ended(&context).await?;
340-
let canceled_call_str = stock_str::canceled_call(&context).await;
338+
let canceled_call_str = stock_str::canceled_call(&context);
341339
call.update_text(&context, &canceled_call_str).await?;
342340
}
343341
context.emit_msgs_changed(call.msg.chat_id, call_id);
@@ -363,12 +361,12 @@ impl Context {
363361

364362
if call.is_incoming() {
365363
if call.is_stale() {
366-
let missed_call_str = stock_str::missed_call(self).await;
364+
let missed_call_str = stock_str::missed_call(self);
367365
call.update_text(self, &missed_call_str).await?;
368366
self.emit_incoming_msg(call.msg.chat_id, call_id); // notify missed call
369367
} else {
370368
let incoming_call_str =
371-
stock_str::incoming_call(self, call.has_video_initially()).await;
369+
stock_str::incoming_call(self, call.has_video_initially());
372370
call.update_text(self, &incoming_call_str).await?;
373371
self.emit_msgs_changed(call.msg.chat_id, call_id); // ringing calls are not additionally notified
374372
let can_call_me = match who_can_call_me(self).await? {
@@ -409,8 +407,7 @@ impl Context {
409407
));
410408
}
411409
} else {
412-
let outgoing_call_str =
413-
stock_str::outgoing_call(self, call.has_video_initially()).await;
410+
let outgoing_call_str = stock_str::outgoing_call(self, call.has_video_initially());
414411
call.update_text(self, &outgoing_call_str).await?;
415412
self.emit_msgs_changed(call.msg.chat_id, call_id);
416413
}
@@ -462,22 +459,22 @@ impl Context {
462459
if call.is_incoming() {
463460
if from_id == ContactId::SELF {
464461
call.mark_as_ended(self).await?;
465-
let declined_call_str = stock_str::declined_call(self).await;
462+
let declined_call_str = stock_str::declined_call(self);
466463
call.update_text(self, &declined_call_str).await?;
467464
} else {
468465
call.mark_as_canceled(self).await?;
469-
let missed_call_str = stock_str::missed_call(self).await;
466+
let missed_call_str = stock_str::missed_call(self);
470467
call.update_text(self, &missed_call_str).await?;
471468
}
472469
} else {
473470
// outgoing
474471
if from_id == ContactId::SELF {
475472
call.mark_as_canceled(self).await?;
476-
let canceled_call_str = stock_str::canceled_call(self).await;
473+
let canceled_call_str = stock_str::canceled_call(self);
477474
call.update_text(self, &canceled_call_str).await?;
478475
} else {
479476
call.mark_as_ended(self).await?;
480-
let declined_call_str = stock_str::declined_call(self).await;
477+
let declined_call_str = stock_str::declined_call(self);
481478
call.update_text(self, &declined_call_str).await?;
482479
}
483480
}

src/chat.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl ChatId {
476476

477477
/// Adds message "Messages are end-to-end encrypted".
478478
pub(crate) async fn add_e2ee_notice(self, context: &Context, timestamp: i64) -> Result<()> {
479-
let text = stock_str::messages_e2ee_info_msg(context).await;
479+
let text = stock_str::messages_e2ee_info_msg(context);
480480
add_info_msg_with_cmd(
481481
context,
482482
self,
@@ -669,7 +669,7 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=?
669669
}
670670

671671
if chat.is_self_talk() {
672-
let mut msg = Message::new_text(stock_str::self_deleted_msg_body(context).await);
672+
let mut msg = Message::new_text(stock_str::self_deleted_msg_body(context));
673673
add_device_msg(context, None, Some(&mut msg)).await?;
674674
}
675675
chatlist_events::emit_chatlist_changed(context);
@@ -1155,10 +1155,10 @@ SELECT id, rfc724_mid, pre_rfc724_mid, timestamp, ?, 1 FROM msgs WHERE chat_id=?
11551155
pub async fn get_encryption_info(self, context: &Context) -> Result<String> {
11561156
let chat = Chat::load_from_db(context, self).await?;
11571157
if !chat.is_encrypted(context).await? {
1158-
return Ok(stock_str::encr_none(context).await);
1158+
return Ok(stock_str::encr_none(context));
11591159
}
11601160

1161-
let mut ret = stock_str::messages_are_e2ee(context).await + "\n";
1161+
let mut ret = stock_str::messages_are_e2ee(context) + "\n";
11621162

11631163
for &contact_id in get_chat_contacts(context, self)
11641164
.await?
@@ -1392,7 +1392,7 @@ impl Chat {
13921392
.context(format!("Failed loading chat {chat_id} from database"))?;
13931393

13941394
if chat.id.is_archived_link() {
1395-
chat.name = stock_str::archived_chats(context).await;
1395+
chat.name = stock_str::archived_chats(context);
13961396
} else {
13971397
if chat.typ == Chattype::Single && chat.name.is_empty() {
13981398
// chat.name is set to contact.display_name on changes,
@@ -1416,9 +1416,9 @@ impl Chat {
14161416
chat.name = chat_name;
14171417
}
14181418
if chat.param.exists(Param::Selftalk) {
1419-
chat.name = stock_str::saved_messages(context).await;
1419+
chat.name = stock_str::saved_messages(context);
14201420
} else if chat.param.exists(Param::Devicetalk) {
1421-
chat.name = stock_str::device_messages(context).await;
1421+
chat.name = stock_str::device_messages(context);
14221422
}
14231423
}
14241424

@@ -2306,15 +2306,10 @@ pub(crate) async fn update_special_chat_names(context: &Context) -> Result<()> {
23062306
update_special_chat_name(
23072307
context,
23082308
ContactId::DEVICE,
2309-
stock_str::device_messages(context).await,
2310-
)
2311-
.await?;
2312-
update_special_chat_name(
2313-
context,
2314-
ContactId::SELF,
2315-
stock_str::saved_messages(context).await,
2309+
stock_str::device_messages(context),
23162310
)
23172311
.await?;
2312+
update_special_chat_name(context, ContactId::SELF, stock_str::saved_messages(context)).await?;
23182313
Ok(())
23192314
}
23202315

@@ -3068,7 +3063,7 @@ async fn donation_request_maybe(context: &Context) -> Result<()> {
30683063
let ts = if ts == 0 || msg_cnt.await? < 100 {
30693064
now.saturating_add(secs_between_checks)
30703065
} else {
3071-
let mut msg = Message::new_text(stock_str::donation_request(context).await);
3066+
let mut msg = Message::new_text(stock_str::donation_request(context));
30723067
add_device_msg(context, None, Some(&mut msg)).await?;
30733068
i64::MAX
30743069
};
@@ -3622,10 +3617,10 @@ pub(crate) async fn create_group_ex(
36223617
{
36233618
let text = if !grpid.is_empty() {
36243619
// Add "Others will only see this group after you sent a first message." message.
3625-
stock_str::new_group_send_first_message(context).await
3620+
stock_str::new_group_send_first_message(context)
36263621
} else {
36273622
// Add "Messages in this chat use classic email and are not encrypted." message.
3628-
stock_str::chat_unencrypted_explanation(context).await
3623+
stock_str::chat_unencrypted_explanation(context)
36293624
};
36303625
add_info_msg(context, chat_id, &text).await?;
36313626
}
@@ -4197,7 +4192,7 @@ async fn send_member_removal_msg(
41974192

41984193
if contact_id == ContactId::SELF {
41994194
if chat.typ == Chattype::InBroadcast {
4200-
msg.text = stock_str::msg_you_left_broadcast(context).await;
4195+
msg.text = stock_str::msg_you_left_broadcast(context);
42014196
} else {
42024197
msg.text = stock_str::msg_group_left_local(context, ContactId::SELF).await;
42034198
}
@@ -4358,7 +4353,7 @@ async fn rename_ex(
43584353
{
43594354
msg.viewtype = Viewtype::Text;
43604355
msg.text = if chat.typ == Chattype::OutBroadcast {
4361-
stock_str::msg_broadcast_name_changed(context, &chat.name, &new_name).await
4356+
stock_str::msg_broadcast_name_changed(context, &chat.name, &new_name)
43624357
} else {
43634358
stock_str::msg_grp_name(context, &chat.name, &new_name, ContactId::SELF).await
43644359
};
@@ -4423,7 +4418,7 @@ pub async fn set_chat_profile_image(
44234418
chat.param.remove(Param::ProfileImage);
44244419
msg.param.remove(Param::Arg);
44254420
msg.text = if chat.typ == Chattype::OutBroadcast {
4426-
stock_str::msg_broadcast_img_changed(context).await
4421+
stock_str::msg_broadcast_img_changed(context)
44274422
} else {
44284423
stock_str::msg_grp_img_deleted(context, ContactId::SELF).await
44294424
};
@@ -4437,7 +4432,7 @@ pub async fn set_chat_profile_image(
44374432
chat.param.set(Param::ProfileImage, image_blob.as_name());
44384433
msg.param.set(Param::Arg, image_blob.as_name());
44394434
msg.text = if chat.typ == Chattype::OutBroadcast {
4440-
stock_str::msg_broadcast_img_changed(context).await
4435+
stock_str::msg_broadcast_img_changed(context)
44414436
} else {
44424437
stock_str::msg_grp_img_changed(context, ContactId::SELF).await
44434438
};

src/chat/chat_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ async fn test_self_talk() -> Result<()> {
806806
assert!(chat.visibility == ChatVisibility::Normal);
807807
assert!(!chat.is_device_talk());
808808
assert!(chat.can_send(&t).await?);
809-
assert_eq!(chat.name, stock_str::saved_messages(&t).await);
809+
assert_eq!(chat.name, stock_str::saved_messages(&t));
810810
assert!(chat.get_profile_image(&t).await?.is_some());
811811

812812
let msg_id = send_text_msg(&t, chat.id, "foo self".to_string()).await?;
@@ -911,7 +911,7 @@ async fn test_add_device_msg_labelled() -> Result<()> {
911911
assert!(!chat.can_send(&t).await?);
912912
assert!(chat.why_cant_send(&t).await? == Some(CantSendReason::DeviceChat));
913913

914-
assert_eq!(chat.name, stock_str::device_messages(&t).await);
914+
assert_eq!(chat.name, stock_str::device_messages(&t));
915915
let device_msg_icon = chat.get_profile_image(&t).await?.unwrap();
916916
assert_eq!(
917917
device_msg_icon.metadata()?.len(),
@@ -3797,7 +3797,7 @@ async fn test_leave_broadcast_multidevice() -> Result<()> {
37973797
assert_eq!(rcvd.chat_id, bob1_hello.chat_id);
37983798
assert!(rcvd.is_info());
37993799
assert_eq!(rcvd.get_info_type(), SystemMessage::MemberRemovedFromGroup);
3800-
assert_eq!(rcvd.text, stock_str::msg_you_left_broadcast(bob1).await);
3800+
assert_eq!(rcvd.text, stock_str::msg_you_left_broadcast(bob1));
38013801

38023802
Ok(())
38033803
}

src/chatlist.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl Chatlist {
417417
Summary::new_with_reaction_details(context, &lastmsg, chat, lastcontact.as_ref()).await
418418
} else {
419419
Ok(Summary {
420-
text: stock_str::no_messages(context).await,
420+
text: stock_str::no_messages(context),
421421
..Default::default()
422422
})
423423
}
@@ -648,15 +648,13 @@ mod tests {
648648
assert_eq!(chats.len(), 0);
649649

650650
t.set_stock_translation(StockMessage::SavedMessages, "test-1234-save".to_string())
651-
.await
652651
.unwrap();
653652
let chats = Chatlist::try_load(&t, 0, Some("t-1234-s"), None)
654653
.await
655654
.unwrap();
656655
assert_eq!(chats.len(), 1);
657656

658657
t.set_stock_translation(StockMessage::DeviceMessages, "test-5678-babbel".to_string())
659-
.await
660658
.unwrap();
661659
let chats = Chatlist::try_load(&t, 0, Some("t-5678-b"), None)
662660
.await

src/configure.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Context {
146146
if let Err(err) = res.as_ref() {
147147
// We are using Anyhow's .context() and to show the
148148
// inner error, too, we need the {:#}:
149-
let error_msg = stock_str::configuration_failed(self, &format!("{err:#}")).await;
149+
let error_msg = stock_str::configuration_failed(self, &format!("{err:#}"));
150150
progress!(self, 0, Some(error_msg.clone()));
151151
bail!(error_msg);
152152
} else {
@@ -637,10 +637,7 @@ async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<Option<&'
637637
let imap_session = match imap.connect(ctx, configuring).await {
638638
Ok(imap_session) => imap_session,
639639
Err(err) => {
640-
bail!(
641-
"{}",
642-
nicer_configuration_error(ctx, format!("{err:#}")).await
643-
);
640+
bail!("{}", nicer_configuration_error(ctx, format!("{err:#}")));
644641
}
645642
};
646643

@@ -781,7 +778,7 @@ async fn get_autoconfig(
781778
None
782779
}
783780

784-
async fn nicer_configuration_error(context: &Context, e: String) -> String {
781+
fn nicer_configuration_error(context: &Context, e: String) -> String {
785782
if e.to_lowercase().contains("could not resolve")
786783
|| e.to_lowercase().contains("connection attempts")
787784
|| e.to_lowercase()
@@ -790,7 +787,7 @@ async fn nicer_configuration_error(context: &Context, e: String) -> String {
790787
|| e.to_lowercase()
791788
.contains("failed to lookup address information")
792789
{
793-
return stock_str::error_no_network(context).await;
790+
return stock_str::error_no_network(context);
794791
}
795792

796793
e

0 commit comments

Comments
 (0)