Skip to content

Commit

Permalink
backup: Include the timestamp of a ChatItem on error
Browse files Browse the repository at this point in the history
  • Loading branch information
jrose-signal committed Nov 22, 2024
1 parent 6cce17d commit b52183b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
23 changes: 19 additions & 4 deletions rust/message-backup/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,19 @@ impl<M: Method + ReferencedTypes> PartialBackup<M> {

fn add_chat_item(&mut self, chat_item: proto::ChatItem) -> Result<(), ValidationError> {
let chat_id = ChatId(chat_item.chatId);
let raw_timestamp = chat_item.dateSent;

let chat_item_data = chat_item
.try_into_with(self)
.map_err(|e: ChatItemError| ChatFrameError(chat_id, e.into()))?;
.map_err(|error: ChatItemError| {
ChatFrameError(
chat_id,
ChatError::ChatItem {
raw_timestamp,
error,
},
)
})?;

Ok(self.chats.add_chat_item(chat_id, chat_item_data)?)
}
Expand Down Expand Up @@ -527,9 +536,15 @@ impl<M: Method + ReferencedTypes> ChatsData<M> {
pinned: _,
} = self;

let chat_data = items
.get_mut(&chat_id)
.ok_or(ChatFrameError(chat_id, ChatItemError::NoChatForItem.into()))?;
let chat_data = items.get_mut(&chat_id).ok_or_else(|| {
ChatFrameError(
chat_id,
ChatError::ChatItem {
raw_timestamp: item.sent_at.as_millis(),
error: ChatItemError::NoChatForItem,
},
)
})?;

item.total_chat_item_order_index = *chat_items_count;

Expand Down
7 changes: 5 additions & 2 deletions rust/message-backup/src/backup/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ pub enum ChatError {
InvalidRecipient(RecipientId, DestinationKind),
/// chat with {0:?} has an expirationTimerMs but no expireTimerVersion
MissingExpireTimerVersion(RecipientId),
/// chat item: {0}
ChatItem(#[from] ChatItemError),
/// chat item {raw_timestamp}: {error}
ChatItem {
raw_timestamp: u64,
error: ChatItemError,
},
/// {0:?} already appeared
DuplicatePinnedOrder(PinOrder),
/// style error: {0}
Expand Down
15 changes: 10 additions & 5 deletions rust/message-backup/src/backup/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,23 @@ impl Timestamp {
pub(super) fn into_inner(self) -> SystemTime {
self.0
}

pub fn as_millis(&self) -> u64 {
self.0
.duration_since(UNIX_EPOCH)
.expect("should not be possible to construct a Timestamp older than UNIX_EPOCH")
.as_millis()
.try_into()
.expect("should not be possible to construct a Timestamp that requires u128 millis since UNIX_EPOCH")
}
}

impl serde::Serialize for Timestamp {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let offset = self
.0
.duration_since(UNIX_EPOCH)
.expect("should not be possible to construct a Timestamp older than UNIX_EPOCH");
serde::Serialize::serialize(&Duration(offset), serializer)
self.as_millis().serialize(serializer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"chatItem": {
"chatId": "1",
"authorId": "1",
"dateSent": 12345,
"directionless": {},
"updateMessage": {
"groupChange": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
chat frame ChatId(1) error: chat item: group update: group update: SelfInvitedToGroupUpdate.inviterAci: invalid ACI
chat frame ChatId(1) error: chat item 12345: group update: group update: SelfInvitedToGroupUpdate.inviterAci: invalid ACI
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"chatId": "1",
"authorId": "1",
"directionless": {},
"dateSent": 12345,
"stickerMessage": {
"sticker": {
"packId": "YXNkZgo=",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
chat frame ChatId(1) error: chat item: sticker message: pack ID is invalid
chat frame ChatId(1) error: chat item 12345: sticker message: pack ID is invalid

0 comments on commit b52183b

Please sign in to comment.