mirror of
https://github.com/chatmail/core.git
synced 2026-04-29 03:16:29 +03:00
refactor: Make SyncData::AlterChat an inline struct
This commit is contained in:
14
src/chat.rs
14
src/chat.rs
@@ -1901,7 +1901,7 @@ impl Chat {
|
|||||||
pub(crate) async fn add_sync_item(&self, context: &Context, action: ChatAction) -> Result<()> {
|
pub(crate) async fn add_sync_item(&self, context: &Context, action: ChatAction) -> Result<()> {
|
||||||
if let Some(id) = self.get_sync_id(context).await? {
|
if let Some(id) = self.get_sync_id(context).await? {
|
||||||
context
|
context
|
||||||
.add_sync_item(SyncData::AlterChat(sync::AlterChatData { id, action }))
|
.add_sync_item(SyncData::AlterChat { id, action })
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -4054,8 +4054,12 @@ pub(crate) async fn update_msg_text_and_timestamp(
|
|||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
/// Executes [`SyncData::AlterChat`] item sent by other device.
|
/// Executes [`SyncData::AlterChat`] item sent by other device.
|
||||||
pub(crate) async fn sync_alter_chat(&self, data: &sync::AlterChatData) -> Result<()> {
|
pub(crate) async fn sync_alter_chat(
|
||||||
let chat_id = match &data.id {
|
&self,
|
||||||
|
id: &sync::ChatId,
|
||||||
|
action: &ChatAction,
|
||||||
|
) -> Result<()> {
|
||||||
|
let chat_id = match id {
|
||||||
sync::ChatId::ContactAddr(addr) => {
|
sync::ChatId::ContactAddr(addr) => {
|
||||||
let Some(contact_id) =
|
let Some(contact_id) =
|
||||||
Contact::lookup_id_by_addr_ex(self, addr, Origin::Unknown, None).await?
|
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}'.");
|
warn!(self, "sync_alter_chat: No contact for addr '{addr}'.");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
match &data.action {
|
match action {
|
||||||
ChatAction::Block => {
|
ChatAction::Block => {
|
||||||
return contact::set_blocked(self, Nosync, contact_id, true).await
|
return contact::set_blocked(self, Nosync, contact_id, true).await
|
||||||
}
|
}
|
||||||
@@ -4086,7 +4090,7 @@ impl Context {
|
|||||||
chat_id
|
chat_id
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match &data.action {
|
match action {
|
||||||
ChatAction::Block => chat_id.block_ex(self, Nosync).await,
|
ChatAction::Block => chat_id.block_ex(self, Nosync).await,
|
||||||
ChatAction::Unblock => chat_id.unblock_ex(self, Nosync).await,
|
ChatAction::Unblock => chat_id.unblock_ex(self, Nosync).await,
|
||||||
ChatAction::Accept => chat_id.accept_ex(self, Nosync).await,
|
ChatAction::Accept => chat_id.accept_ex(self, Nosync).await,
|
||||||
|
|||||||
@@ -1446,10 +1446,10 @@ WHERE type=? AND id IN (
|
|||||||
false => sync::ChatAction::Unblock,
|
false => sync::ChatAction::Unblock,
|
||||||
};
|
};
|
||||||
context
|
context
|
||||||
.add_sync_item(SyncData::AlterChat(sync::AlterChatData {
|
.add_sync_item(SyncData::AlterChat {
|
||||||
id: sync::ChatId::ContactAddr(contact.addr.clone()),
|
id: sync::ChatId::ContactAddr(contact.addr.clone()),
|
||||||
action,
|
action,
|
||||||
}))
|
})
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/sync.rs
22
src/sync.rs
@@ -58,17 +58,11 @@ pub(crate) enum ChatAction {
|
|||||||
SetMuted(chat::MuteDuration),
|
SetMuted(chat::MuteDuration),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub(crate) struct AlterChatData {
|
|
||||||
pub(crate) id: ChatId,
|
|
||||||
pub(crate) action: ChatAction,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub(crate) enum SyncData {
|
pub(crate) enum SyncData {
|
||||||
AddQrToken(QrTokenData),
|
AddQrToken(QrTokenData),
|
||||||
DeleteQrToken(QrTokenData),
|
DeleteQrToken(QrTokenData),
|
||||||
AlterChat(AlterChatData),
|
AlterChat { id: ChatId, action: ChatAction },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -283,7 +277,7 @@ impl Context {
|
|||||||
token::delete(self, Namespace::InviteNumber, &token.invitenumber).await?;
|
token::delete(self, Namespace::InviteNumber, &token.invitenumber).await?;
|
||||||
token::delete(self, Namespace::Auth, &token.auth).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(())
|
Ok(())
|
||||||
@@ -322,16 +316,16 @@ mod tests {
|
|||||||
|
|
||||||
assert!(t.build_sync_json().await?.is_none());
|
assert!(t.build_sync_json().await?.is_none());
|
||||||
|
|
||||||
// Having one test on `SyncData::AlterChat` is sufficient here as `AlterChatData` with
|
// Having one test on `SyncData::AlterChat` is sufficient here as `ChatAction::SetMuted`
|
||||||
// `ChatAction::SetMuted` introduces enums inside items and SystemTime. Let's avoid in-depth
|
// introduces enums inside items and `SystemTime`. Let's avoid in-depth testing of the
|
||||||
// testing of the serialiser here which is an external crate.
|
// serialiser here which is an external crate.
|
||||||
t.add_sync_item_with_timestamp(
|
t.add_sync_item_with_timestamp(
|
||||||
SyncData::AlterChat(AlterChatData {
|
SyncData::AlterChat {
|
||||||
id: ChatId::ContactAddr("bob@example.net".to_string()),
|
id: ChatId::ContactAddr("bob@example.net".to_string()),
|
||||||
action: ChatAction::SetMuted(chat::MuteDuration::Until(
|
action: ChatAction::SetMuted(chat::MuteDuration::Until(
|
||||||
SystemTime::UNIX_EPOCH + Duration::from_millis(42999),
|
SystemTime::UNIX_EPOCH + Duration::from_millis(42999),
|
||||||
)),
|
)),
|
||||||
}),
|
},
|
||||||
1631781315,
|
1631781315,
|
||||||
)
|
)
|
||||||
.await?;
|
.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(),
|
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);
|
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");
|
bail!("bad item");
|
||||||
};
|
};
|
||||||
assert_eq!(*id, ChatId::ContactAddr("bob@example.net".to_string()));
|
assert_eq!(*id, ChatId::ContactAddr("bob@example.net".to_string()));
|
||||||
|
|||||||
Reference in New Issue
Block a user