0
0
mirror of https://github.com/signalapp/libsignal.git synced 2024-09-19 19:42:19 +02:00

backup: Sort OutgoingSend records when serializing

This commit is contained in:
Jordan Rose 2024-08-13 17:19:17 -07:00
parent fe864ec139
commit 52912c7c75
2 changed files with 38 additions and 6 deletions

View File

@ -18,7 +18,7 @@ use crate::backup::chat::chat_style::{ChatStyle, ChatStyleError, CustomColorId};
use crate::backup::file::{FilePointerError, MessageAttachmentError};
use crate::backup::frame::RecipientId;
use crate::backup::method::{Contains, Lookup, Method};
use crate::backup::serialize::SerializeOrder;
use crate::backup::serialize::{SerializeOrder, UnorderedList};
use crate::backup::sticker::MessageStickerError;
use crate::backup::time::{Duration, Timestamp};
use crate::backup::{BackupMeta, CallError, ReferencedTypes, TryFromWith, TryIntoWith as _};
@ -250,12 +250,12 @@ pub enum Direction {
read: bool,
sealed_sender: bool,
},
Outgoing(Vec<OutgoingSend>),
Outgoing(UnorderedList<OutgoingSend>),
Directionless,
}
#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(test, derive(PartialEq, Clone))]
pub struct OutgoingSend {
pub recipient: RecipientId,
pub status: DeliveryStatus,
@ -263,7 +263,7 @@ pub struct OutgoingSend {
}
#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(test, derive(PartialEq, Clone))]
pub enum DeliveryFailureReason {
Unknown,
Network,
@ -271,7 +271,7 @@ pub enum DeliveryFailureReason {
}
#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(test, derive(PartialEq, Clone))]
pub enum DeliveryStatus {
Failed(DeliveryFailureReason),
Pending,
@ -960,4 +960,28 @@ mod test {
Err(ChatItemError::ExpirationMismatch)
);
}
#[test]
fn outgoing_sends_are_sorted_when_serialized() {
let send1 = OutgoingSend {
recipient: RecipientId(1),
status: DeliveryStatus::Pending,
last_status_update: Timestamp::test_value(),
};
let send2 = OutgoingSend {
recipient: RecipientId(2),
status: DeliveryStatus::Sent {
sealed_sender: true,
},
last_status_update: Timestamp::test_value(),
};
let message1 = Direction::Outgoing(vec![send1.clone(), send2.clone()].into());
let message2 = Direction::Outgoing(vec![send2, send1].into());
assert_eq!(
serde_json::to_string_pretty(&message1).expect("valid"),
serde_json::to_string_pretty(&message2).expect("valid"),
);
}
}

View File

@ -12,7 +12,7 @@ use uuid::Uuid;
use crate::backup::account_data::AccountData;
use crate::backup::call::AdHocCall;
use crate::backup::chat::text::{TextEffect, TextRange};
use crate::backup::chat::ChatData;
use crate::backup::chat::{ChatData, OutgoingSend};
use crate::backup::frame::RecipientId;
use crate::backup::method::Store;
use crate::backup::recipient::{DistributionListItem, FullRecipientData};
@ -242,6 +242,14 @@ impl SerializeOrder for TextRange {
}
}
impl SerializeOrder for OutgoingSend {
fn serialize_cmp(&self, other: &Self) -> std::cmp::Ordering {
self.recipient
.serialize_cmp(&other.recipient)
.then_with(|| self.last_status_update.cmp(&other.last_status_update))
}
}
impl serde::Serialize for proto::contact_attachment::Name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where