Remove emit_event! macro

This commit is contained in:
link2xt
2021-09-05 17:15:58 +00:00
parent 40fe65716f
commit 02a1abc0d5
8 changed files with 57 additions and 96 deletions

View File

@@ -1099,10 +1099,9 @@ impl Chat {
if self.typ == Chattype::Group if self.typ == Chattype::Group
&& !is_contact_in_chat(context, self.id, DC_CONTACT_ID_SELF).await && !is_contact_in_chat(context, self.id, DC_CONTACT_ID_SELF).await
{ {
emit_event!( context.emit_event(EventType::ErrorSelfNotInGroup(
context, "Cannot send message; self not in group.".into(),
EventType::ErrorSelfNotInGroup("Cannot send message; self not in group.".into()) ));
);
bail!("Cannot set message; self not in group."); bail!("Cannot set message; self not in group.");
} }
@@ -2273,12 +2272,9 @@ pub(crate) async fn add_contact_to_chat_ex(
if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await { if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
/* we should respect this - whatever we send to the group, it gets discarded anyway! */ /* we should respect this - whatever we send to the group, it gets discarded anyway! */
emit_event!( context.emit_event(EventType::ErrorSelfNotInGroup(
context, "Cannot add contact to group; self not in group.".into(),
EventType::ErrorSelfNotInGroup( ));
"Cannot add contact to group; self not in group.".into()
)
);
bail!("can not add contact because our account is not part of it"); bail!("can not add contact because our account is not part of it");
} }
if from_handshake && chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 1 { if from_handshake && chat.param.get_int(Param::Unpromoted).unwrap_or_default() == 1 {
@@ -2485,12 +2481,9 @@ pub async fn remove_contact_from_chat(
if let Ok(chat) = Chat::load_from_db(context, chat_id).await { if let Ok(chat) = Chat::load_from_db(context, chat_id).await {
if chat.typ == Chattype::Group { if chat.typ == Chattype::Group {
if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await { if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
emit_event!( context.emit_event(EventType::ErrorSelfNotInGroup(
context, "Cannot remove contact from chat; self not in group.".into(),
EventType::ErrorSelfNotInGroup( ));
"Cannot remove contact from chat; self not in group.".into()
)
);
} else { } else {
if let Ok(contact) = Contact::get_by_id(context, contact_id).await { if let Ok(contact) = Contact::get_by_id(context, contact_id).await {
if chat.is_promoted() { if chat.is_promoted() {
@@ -2582,10 +2575,9 @@ pub async fn set_chat_name(context: &Context, chat_id: ChatId, new_name: &str) -
if chat.name == new_name { if chat.name == new_name {
success = true; success = true;
} else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await { } else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
emit_event!( context.emit_event(EventType::ErrorSelfNotInGroup(
context, "Cannot set chat name; self not in group".into(),
EventType::ErrorSelfNotInGroup("Cannot set chat name; self not in group".into()) ));
);
} else { } else {
/* we should respect this - whatever we send to the group, it gets discarded anyway! */ /* we should respect this - whatever we send to the group, it gets discarded anyway! */
if context if context
@@ -2649,12 +2641,9 @@ pub async fn set_chat_profile_image(
); );
/* we should respect this - whatever we send to the group, it gets discarded anyway! */ /* we should respect this - whatever we send to the group, it gets discarded anyway! */
if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await { if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
emit_event!( context.emit_event(EventType::ErrorSelfNotInGroup(
context, "Cannot set chat profile image; self not in group.".into(),
EventType::ErrorSelfNotInGroup( ));
"Cannot set chat profile image; self not in group.".into()
)
);
bail!("Failed to set profile image"); bail!("Failed to set profile image");
} }
let mut msg = Message::new(Viewtype::Text); let mut msg = Message::new(Viewtype::Text);
@@ -2682,15 +2671,12 @@ pub async fn set_chat_profile_image(
chat.update_param(context).await?; chat.update_param(context).await?;
if chat.is_promoted() && !chat.is_mailing_list() { if chat.is_promoted() && !chat.is_mailing_list() {
msg.id = send_msg(context, chat_id, &mut msg).await?; msg.id = send_msg(context, chat_id, &mut msg).await?;
emit_event!( context.emit_event(EventType::MsgsChanged {
context, chat_id,
EventType::MsgsChanged { msg_id: msg.id,
chat_id, });
msg_id: msg.id
}
);
} }
emit_event!(context, EventType::ChatModified(chat_id)); context.emit_event(EventType::ChatModified(chat_id));
Ok(()) Ok(())
} }

View File

@@ -416,24 +416,18 @@ pub async fn schedule_ephemeral_task(context: &Context) {
let context1 = context.clone(); let context1 = context.clone();
let ephemeral_task = task::spawn(async move { let ephemeral_task = task::spawn(async move {
async_std::task::sleep(duration).await; async_std::task::sleep(duration).await;
emit_event!( context1.emit_event(EventType::MsgsChanged {
context1, chat_id: ChatId::new(0),
EventType::MsgsChanged { msg_id: MsgId::new(0),
chat_id: ChatId::new(0), });
msg_id: MsgId::new(0)
}
);
}); });
*context.ephemeral_task.write().await = Some(ephemeral_task); *context.ephemeral_task.write().await = Some(ephemeral_task);
} else { } else {
// Emit event immediately // Emit event immediately
emit_event!( context.emit_event(EventType::MsgsChanged {
context, chat_id: ChatId::new(0),
EventType::MsgsChanged { msg_id: MsgId::new(0),
chat_id: ChatId::new(0), });
msg_id: MsgId::new(0)
}
);
} }
} }
} }

View File

@@ -350,10 +350,10 @@ impl Imap {
self.connected = true; self.connected = true;
self.session = Some(session); self.session = Some(session);
self.login_failed_once = false; self.login_failed_once = false;
emit_event!( context.emit_event(EventType::ImapConnected(format!(
context, "IMAP-LOGIN as {}",
EventType::ImapConnected(format!("IMAP-LOGIN as {}", self.config.lp.user)) self.config.lp.user
); )));
Ok(()) Ok(())
} }
@@ -1015,13 +1015,10 @@ impl Imap {
if let Some(ref mut session) = &mut self.session { if let Some(ref mut session) = &mut self.session {
match session.uid_mv(&set, &dest_folder).await { match session.uid_mv(&set, &dest_folder).await {
Ok(_) => { Ok(_) => {
emit_event!( context.emit_event(EventType::ImapMessageMoved(format!(
context, "IMAP Message {} moved to {}",
EventType::ImapMessageMoved(format!( display_folder_id, dest_folder
"IMAP Message {} moved to {}", )));
display_folder_id, dest_folder
))
);
return ImapActionResult::Success; return ImapActionResult::Success;
} }
Err(err) => { Err(err) => {
@@ -1059,23 +1056,17 @@ impl Imap {
if !self.add_flag_finalized(context, uid, "\\Deleted").await { if !self.add_flag_finalized(context, uid, "\\Deleted").await {
warn!(context, "Cannot mark {} as \"Deleted\" after copy.", uid); warn!(context, "Cannot mark {} as \"Deleted\" after copy.", uid);
emit_event!( context.emit_event(EventType::ImapMessageMoved(format!(
context, "IMAP Message {} copied to {} (delete FAILED)",
EventType::ImapMessageMoved(format!( display_folder_id, dest_folder
"IMAP Message {} copied to {} (delete FAILED)", )));
display_folder_id, dest_folder
))
);
ImapActionResult::Failed ImapActionResult::Failed
} else { } else {
self.config.selected_folder_needs_expunge = true; self.config.selected_folder_needs_expunge = true;
emit_event!( context.emit_event(EventType::ImapMessageMoved(format!(
context, "IMAP Message {} copied to {} (delete successfull)",
EventType::ImapMessageMoved(format!( display_folder_id, dest_folder
"IMAP Message {} copied to {} (delete successfull)", )));
display_folder_id, dest_folder
))
);
ImapActionResult::Success ImapActionResult::Success
} }
} }
@@ -1269,13 +1260,10 @@ impl Imap {
); );
ImapActionResult::RetryLater ImapActionResult::RetryLater
} else { } else {
emit_event!( context.emit_event(EventType::ImapMessageDeleted(format!(
context, "IMAP Message {} marked as deleted [{}]",
EventType::ImapMessageDeleted(format!( display_imap_id, message_id
"IMAP Message {} marked as deleted [{}]", )));
display_imap_id, message_id
))
);
self.config.selected_folder_needs_expunge = true; self.config.selected_folder_needs_expunge = true;
ImapActionResult::Success ImapActionResult::Success
} }

View File

@@ -759,7 +759,7 @@ async fn export_backup_inner(context: &Context, temp_path: &PathBuf) -> Result<(
let progress = 1000 * written_files / count; let progress = 1000 * written_files / count;
if progress != last_progress && progress > 10 && progress < 1000 { if progress != last_progress && progress > 10 && progress < 1000 {
// We already emitted ImexProgress(10) above // We already emitted ImexProgress(10) above
emit_event!(context, EventType::ImexProgress(progress)); context.emit_event(EventType::ImexProgress(progress));
last_progress = progress; last_progress = progress;
} }
} }

View File

@@ -13,7 +13,7 @@ macro_rules! info {
file = file!(), file = file!(),
line = line!(), line = line!(),
msg = &formatted); msg = &formatted);
emit_event!($ctx, $crate::EventType::Info(full)); $ctx.emit_event($crate::EventType::Info(full));
}}; }};
} }
@@ -28,7 +28,7 @@ macro_rules! warn {
file = file!(), file = file!(),
line = line!(), line = line!(),
msg = &formatted); msg = &formatted);
emit_event!($ctx, $crate::EventType::Warning(full)); $ctx.emit_event($crate::EventType::Warning(full));
}}; }};
} }
@@ -39,17 +39,10 @@ macro_rules! error {
}; };
($ctx:expr, $msg:expr, $($args:expr),* $(,)?) => {{ ($ctx:expr, $msg:expr, $($args:expr),* $(,)?) => {{
let formatted = format!($msg, $($args),*); let formatted = format!($msg, $($args),*);
emit_event!($ctx, $crate::EventType::Error(formatted)); $ctx.emit_event($crate::EventType::Error(formatted));
}}; }};
} }
#[macro_export]
macro_rules! emit_event {
($ctx:expr, $event:expr) => {
$ctx.emit_event($event);
};
}
pub trait LogExt<T, E> pub trait LogExt<T, E>
where where
Self: std::marker::Sized, Self: std::marker::Sized,
@@ -136,7 +129,7 @@ impl<T, E: std::fmt::Display> LogExt<T, E> for Result<T, E> {
); );
// We can't use the warn!() macro here as the file!() and line!() macros // We can't use the warn!() macro here as the file!() and line!() macros
// don't work with #[track_caller] // don't work with #[track_caller]
emit_event!(context, crate::EventType::Warning(full)); context.emit_event(crate::EventType::Warning(full));
}; };
self self
} }

View File

@@ -278,7 +278,7 @@ impl Peerstate {
let msg = stock_str::contact_setup_changed(context, self.addr.clone()).await; let msg = stock_str::contact_setup_changed(context, self.addr.clone()).await;
chat::add_info_msg(context, chat_id, msg, timestamp).await; chat::add_info_msg(context, chat_id, msg, timestamp).await;
emit_event!(context, EventType::ChatModified(chat_id)); context.emit_event(EventType::ChatModified(chat_id));
} else { } else {
bail!("contact with peerstate.addr {:?} not found", &self.addr); bail!("contact with peerstate.addr {:?} not found", &self.addr);
} }

View File

@@ -638,7 +638,7 @@ pub(crate) async fn handle_securejoin_handshake(
Contact::scaleup_origin_by_id(context, contact_id, Origin::SecurejoinInvited).await?; Contact::scaleup_origin_by_id(context, contact_id, Origin::SecurejoinInvited).await?;
info!(context, "Auth verified.",); info!(context, "Auth verified.",);
secure_connection_established(context, contact_chat_id).await?; secure_connection_established(context, contact_chat_id).await?;
emit_event!(context, EventType::ContactsChanged(Some(contact_id))); context.emit_event(EventType::ContactsChanged(Some(contact_id)));
inviter_progress!(context, contact_id, 600); inviter_progress!(context, contact_id, 600);
if join_vg { if join_vg {
// the vg-member-added message is special: // the vg-member-added message is special:
@@ -861,7 +861,7 @@ async fn secure_connection_established(
}; };
let msg = stock_str::contact_verified(context, addr).await; let msg = stock_str::contact_verified(context, addr).await;
chat::add_info_msg(context, contact_chat_id, msg, time()).await; chat::add_info_msg(context, contact_chat_id, msg, time()).await;
emit_event!(context, EventType::ChatModified(contact_chat_id)); context.emit_event(EventType::ChatModified(contact_chat_id));
info!(context, "StockMessage::ContactVerified posted to 1:1 chat"); info!(context, "StockMessage::ContactVerified posted to 1:1 chat");
Ok(()) Ok(())

View File

@@ -358,7 +358,7 @@ impl BobState {
mark_peer_as_verified(context, self.invite.fingerprint()).await?; mark_peer_as_verified(context, self.invite.fingerprint()).await?;
Contact::scaleup_origin_by_id(context, self.invite.contact_id(), Origin::SecurejoinJoined) Contact::scaleup_origin_by_id(context, self.invite.contact_id(), Origin::SecurejoinJoined)
.await?; .await?;
emit_event!(context, EventType::ContactsChanged(None)); context.emit_event(EventType::ContactsChanged(None));
if let QrInvite::Group { .. } = self.invite { if let QrInvite::Group { .. } = self.invite {
let member_added = mime_message let member_added = mime_message