Skip to content

Commit

Permalink
feat: add sent at timestamp to chat messages (#6314)
Browse files Browse the repository at this point in the history
Description
---
Add sent at timestamp to messages. 

Motivation and Context
---
Because of the lack of a central time server, its hard to get accurate
timestamp, the chat services uses the received at as the timestamp.
But when the receiver gets messages through saf, it can get multiple at
the same time. This fudles up the order completely. So we add sent_at
timestamp to help with ordering of messages.
  • Loading branch information
SWvheerden authored May 6, 2024
1 parent 8cbfeda commit 4adcb26
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 1 deletion.
15 changes: 15 additions & 0 deletions base_layer/chat_ffi/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ unsigned char read_chat_message_direction(struct Message *message, int *error_ou
*/
unsigned long long read_chat_message_stored_at(struct Message *message, int *error_out);

/**
* Returns a c_ulonglong representation of the sent at timestamp as seconds since epoch
*
* ## Arguments
* `message` - A pointer to a Message
* `error_out` - Pointer to an int which will be modified
*
* ## Returns
* `c_ulonglong` - The stored_at timestamp, seconds since epoch. Returns 0 if message is null.
*
* ## Safety
* `message` should be destroyed eventually
*/
unsigned long long read_chat_message_sent_at(struct Message *message, int *error_out);

/**
* Returns a c_ulonglong representation of the delivery confirmation timestamp as seconds since epoch
*
Expand Down
25 changes: 25 additions & 0 deletions base_layer/chat_ffi/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@ pub unsafe extern "C" fn read_chat_message_stored_at(message: *mut Message, erro
(*message).stored_at as c_ulonglong
}

/// Returns a c_ulonglong representation of the sent at timestamp as seconds since epoch
///
/// ## Arguments
/// `message` - A pointer to a Message
/// `error_out` - Pointer to an int which will be modified
///
/// ## Returns
/// `c_ulonglong` - The stored_at timestamp, seconds since epoch. Returns 0 if message is null.
///
/// ## Safety
/// `message` should be destroyed eventually
#[no_mangle]
pub unsafe extern "C" fn read_chat_message_sent_at(message: *mut Message, error_out: *mut c_int) -> c_ulonglong {
let mut error = 0;
ptr::swap(error_out, &mut error as *mut c_int);

if message.is_null() {
error = LibChatError::from(InterfaceError::NullError("message".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return 0;
}

(*message).sent_at as c_ulonglong
}

/// Returns a c_ulonglong representation of the delivery confirmation timestamp as seconds since epoch
///
/// ## Arguments
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE messages drop sent_at;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE messages ADD sent_at TIMESTAMP;
1 change: 1 addition & 0 deletions base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ where T: ContactsBackend + 'static
Ok(result.map(ContactsServiceResponse::Messages)?)
},
ContactsServiceRequest::SendMessage(address, mut message) => {
message.sent_at = Utc::now().naive_utc().timestamp() as u64;
let ob_message = OutboundDomainMessage::from(MessageDispatch::Message(message.clone()));

message.stored_at = Utc::now().naive_utc().timestamp() as u64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct MessagesSqlInsert {
pub body: Vec<u8>,
pub metadata: Vec<u8>,
pub stored_at: NaiveDateTime,
pub sent_at: NaiveDateTime,
pub direction: i32,
}

Expand All @@ -58,6 +59,7 @@ pub struct MessagesSql {
pub body: Vec<u8>,
pub metadata: Vec<u8>,
pub stored_at: NaiveDateTime,
pub sent_at: NaiveDateTime,
pub delivery_confirmation_at: Option<NaiveDateTime>,
pub read_confirmation_at: Option<NaiveDateTime>,
pub direction: i32,
Expand Down Expand Up @@ -145,6 +147,7 @@ impl TryFrom<MessagesSql> for Message {
u8::try_from(o.direction).map_err(|_| ContactsServiceStorageError::ConversionError)?,
)
.ok_or(ContactsServiceStorageError::ConversionError)?,
sent_at: o.sent_at.timestamp() as u64,
stored_at: o.stored_at.timestamp() as u64,
delivery_confirmation_at: Some(o.stored_at.timestamp() as u64),
read_confirmation_at: Some(o.stored_at.timestamp() as u64),
Expand All @@ -168,7 +171,10 @@ impl TryFrom<Message> for MessagesSqlInsert {
message_id: o.message_id,
body: o.body,
metadata: metadata.into_bytes().to_vec(),
stored_at: NaiveDateTime::from_timestamp_opt(o.stored_at as i64, 0).unwrap(),
stored_at: NaiveDateTime::from_timestamp_opt(o.stored_at as i64, 0)
.ok_or(ContactsServiceStorageError::ConversionError)?,
sent_at: NaiveDateTime::from_timestamp_opt(o.sent_at as i64, 0)
.ok_or(ContactsServiceStorageError::ConversionError)?,
direction: i32::from(o.direction.as_byte()),
})
}
Expand Down
1 change: 1 addition & 0 deletions base_layer/contacts/src/contacts_service/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Message {
pub metadata: Vec<MessageMetadata>,
pub address: TariAddress,
pub direction: Direction,
pub sent_at: u64,
pub stored_at: u64,
pub delivery_confirmation_at: Option<u64>,
pub read_confirmation_at: Option<u64>,
Expand Down
1 change: 1 addition & 0 deletions base_layer/contacts/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ diesel::table! {
body -> Binary,
metadata -> Binary,
stored_at -> Timestamp,
sent_at -> Timestamp,
delivery_confirmation_at -> Nullable<Timestamp>,
read_confirmation_at -> Nullable<Timestamp>,
direction -> Integer,
Expand Down
2 changes: 2 additions & 0 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11576,6 +11576,7 @@ mod test {
address: bob_wallet_address.clone(),
direction: Direction::Outbound,
stored_at: u64::from(i),
sent_at: u64::from(i),
delivery_confirmation_at: None,
read_confirmation_at: None,
message_id: vec![i],
Expand All @@ -11592,6 +11593,7 @@ mod test {
address: alice_wallet_address.clone(),
direction: Direction::Outbound,
stored_at: u64::from(i),
sent_at: u64::from(i),
delivery_confirmation_at: None,
read_confirmation_at: None,
message_id: vec![i],
Expand Down

0 comments on commit 4adcb26

Please sign in to comment.