refactor: Make SyncData::AlterChat an inline struct

This commit is contained in:
iequidoo
2023-10-31 23:03:13 -03:00
committed by iequidoo
parent 6cd7296001
commit cdcb10fb58
3 changed files with 19 additions and 21 deletions

View File

@@ -1901,7 +1901,7 @@ impl Chat {
pub(crate) async fn add_sync_item(&self, context: &Context, action: ChatAction) -> Result<()> {
if let Some(id) = self.get_sync_id(context).await? {
context
.add_sync_item(SyncData::AlterChat(sync::AlterChatData { id, action }))
.add_sync_item(SyncData::AlterChat { id, action })
.await?;
}
Ok(())
@@ -4054,8 +4054,12 @@ pub(crate) async fn update_msg_text_and_timestamp(
impl Context {
/// Executes [`SyncData::AlterChat`] item sent by other device.
pub(crate) async fn sync_alter_chat(&self, data: &sync::AlterChatData) -> Result<()> {
let chat_id = match &data.id {
pub(crate) async fn sync_alter_chat(
&self,
id: &sync::ChatId,
action: &ChatAction,
) -> Result<()> {
let chat_id = match id {
sync::ChatId::ContactAddr(addr) => {
let Some(contact_id) =
Contact::lookup_id_by_addr_ex(self, addr, Origin::Unknown, None).await?
@@ -4063,7 +4067,7 @@ impl Context {
warn!(self, "sync_alter_chat: No contact for addr '{addr}'.");
return Ok(());
};
match &data.action {
match action {
ChatAction::Block => {
return contact::set_blocked(self, Nosync, contact_id, true).await
}
@@ -4086,7 +4090,7 @@ impl Context {
chat_id
}
};
match &data.action {
match action {
ChatAction::Block => chat_id.block_ex(self, Nosync).await,
ChatAction::Unblock => chat_id.unblock_ex(self, Nosync).await,
ChatAction::Accept => chat_id.accept_ex(self, Nosync).await,

View File

@@ -1446,10 +1446,10 @@ WHERE type=? AND id IN (
false => sync::ChatAction::Unblock,
};
context
.add_sync_item(SyncData::AlterChat(sync::AlterChatData {
.add_sync_item(SyncData::AlterChat {
id: sync::ChatId::ContactAddr(contact.addr.clone()),
action,
}))
})
.await?;
}
}

View File

@@ -58,17 +58,11 @@ pub(crate) enum ChatAction {
SetMuted(chat::MuteDuration),
}
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct AlterChatData {
pub(crate) id: ChatId,
pub(crate) action: ChatAction,
}
#[derive(Debug, Serialize, Deserialize)]
pub(crate) enum SyncData {
AddQrToken(QrTokenData),
DeleteQrToken(QrTokenData),
AlterChat(AlterChatData),
AlterChat { id: ChatId, action: ChatAction },
}
#[derive(Debug, Serialize, Deserialize)]
@@ -283,7 +277,7 @@ impl Context {
token::delete(self, Namespace::InviteNumber, &token.invitenumber).await?;
token::delete(self, Namespace::Auth, &token.auth).await?;
}
AlterChat(data) => self.sync_alter_chat(data).await?,
AlterChat { id, action } => self.sync_alter_chat(id, action).await?,
}
}
Ok(())
@@ -322,16 +316,16 @@ mod tests {
assert!(t.build_sync_json().await?.is_none());
// Having one test on `SyncData::AlterChat` is sufficient here as `AlterChatData` with
// `ChatAction::SetMuted` introduces enums inside items and SystemTime. Let's avoid in-depth
// testing of the serialiser here which is an external crate.
// Having one test on `SyncData::AlterChat` is sufficient here as `ChatAction::SetMuted`
// introduces enums inside items and `SystemTime`. Let's avoid in-depth testing of the
// serialiser here which is an external crate.
t.add_sync_item_with_timestamp(
SyncData::AlterChat(AlterChatData {
SyncData::AlterChat {
id: ChatId::ContactAddr("bob@example.net".to_string()),
action: ChatAction::SetMuted(chat::MuteDuration::Until(
SystemTime::UNIX_EPOCH + Duration::from_millis(42999),
)),
}),
},
1631781315,
)
.await?;
@@ -438,7 +432,7 @@ mod tests {
r#"{"items":[{"timestamp":1631781318,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}}]}"#.to_string(),
)?;
assert_eq!(sync_items.items.len(), 1);
let AlterChat(AlterChatData { id, action }) = &sync_items.items.get(0).unwrap().data else {
let AlterChat { id, action } = &sync_items.items.get(0).unwrap().data else {
bail!("bad item");
};
assert_eq!(*id, ChatId::ContactAddr("bob@example.net".to_string()));