From 06dc2b734974b23f028416d58ff43a77346d71e0 Mon Sep 17 00:00:00 2001 From: brianp Date: Sun, 1 Oct 2023 21:19:08 +0200 Subject: [PATCH] Add a client function for read receipt sending --- .../examples/chat_client/src/client.rs | 10 +++++++ .../contacts/src/contacts_service/handle.rs | 27 +++++++++++++++++-- .../contacts/src/contacts_service/service.rs | 10 +++++-- integration_tests/src/chat_ffi.rs | 4 +++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/base_layer/contacts/examples/chat_client/src/client.rs b/base_layer/contacts/examples/chat_client/src/client.rs index 9fe6b475e1..3ebfcf66d5 100644 --- a/base_layer/contacts/examples/chat_client/src/client.rs +++ b/base_layer/contacts/examples/chat_client/src/client.rs @@ -49,6 +49,7 @@ pub trait ChatClient { fn create_message(&self, receiver: &TariAddress, message: String) -> Message; async fn get_messages(&self, sender: &TariAddress, limit: u64, page: u64) -> Vec; async fn send_message(&self, message: Message); + async fn send_read_receipt(&self, address: &TariAddress, message_id: Vec); fn identity(&self) -> &NodeIdentity; fn shutdown(&mut self); } @@ -171,6 +172,15 @@ impl ChatClient for Client { messages } + async fn send_read_receipt(&self, address: &TariAddress, message_id: Vec) { + if let Some(mut contacts_service) = self.contacts.clone() { + contacts_service + .send_read_confirmation(address.clone(), message_id) + .await + .expect("Read receipt not sent"); + } + } + fn create_message(&self, receiver: &TariAddress, message: String) -> Message { MessageBuilder::new().address(receiver.clone()).message(message).build() } diff --git a/base_layer/contacts/src/contacts_service/handle.rs b/base_layer/contacts/src/contacts_service/handle.rs index 6640b3a40d..822f5c122a 100644 --- a/base_layer/contacts/src/contacts_service/handle.rs +++ b/base_layer/contacts/src/contacts_service/handle.rs @@ -30,13 +30,14 @@ use chrono::{DateTime, Local, NaiveDateTime}; use tari_common_types::tari_address::TariAddress; use tari_comms::peer_manager::NodeId; use tari_service_framework::reply_channel::SenderService; +use tari_utilities::epoch_time::EpochTime; use tokio::sync::broadcast; use tower::Service; use crate::contacts_service::{ error::ContactsServiceError, service::{ContactMessageType, ContactOnlineStatus}, - types::{Contact, Message}, + types::{Confirmation, Contact, Message}, }; pub static DEFAULT_MESSAGE_LIMIT: u64 = 35; @@ -137,7 +138,7 @@ pub enum ContactsServiceRequest { GetContactOnlineStatus(Contact), SendMessage(TariAddress, Message), GetMessages(TariAddress, i64, i64), - SendDeliveryConfirmation(TariAddress, Vec), + SendReadConfirmation(TariAddress, Confirmation), } #[derive(Debug)] @@ -149,6 +150,7 @@ pub enum ContactsServiceResponse { OnlineStatus(ContactOnlineStatus), Messages(Vec), MessageSent, + ReadConfirmationSent, } #[derive(Clone)] @@ -283,4 +285,25 @@ impl ContactsServiceHandle { _ => Err(ContactsServiceError::UnexpectedApiResponse), } } + + pub async fn send_read_confirmation( + &mut self, + address: TariAddress, + message_id: Vec, + ) -> Result<(), ContactsServiceError> { + match self + .request_response_service + .call(ContactsServiceRequest::SendReadConfirmation( + address.clone(), + Confirmation { + message_id, + timestamp: EpochTime::now().as_u64(), + }, + )) + .await?? + { + ContactsServiceResponse::ReadConfirmationSent => Ok(()), + _ => Err(ContactsServiceError::UnexpectedApiResponse), + } + } } diff --git a/base_layer/contacts/src/contacts_service/service.rs b/base_layer/contacts/src/contacts_service/service.rs index eb4550ae5a..fb5e7fa310 100644 --- a/base_layer/contacts/src/contacts_service/service.rs +++ b/base_layer/contacts/src/contacts_service/service.rs @@ -307,8 +307,14 @@ where T: ContactsBackend + 'static Ok(ContactsServiceResponse::MessageSent) }, - ContactsServiceRequest::SendDeliveryConfirmation(address, message_id) => { - Ok(ContactsServiceResponse::MessageSent) + ContactsServiceRequest::SendReadConfirmation(address, confirmation) => { + let msg = OutboundDomainMessage::from(MessageDispatch::DeliveryConfirmation(confirmation.clone())); + self.deliver_message(address, msg).await?; + + self.db + .confirm_message(confirmation.message_id.clone(), None, Some(confirmation.timestamp))?; + + Ok(ContactsServiceResponse::ReadConfirmationSent) }, } } diff --git a/integration_tests/src/chat_ffi.rs b/integration_tests/src/chat_ffi.rs index c8a229d86c..b730ff2c2e 100644 --- a/integration_tests/src/chat_ffi.rs +++ b/integration_tests/src/chat_ffi.rs @@ -188,6 +188,10 @@ impl ChatClient for ChatFFI { } } + async fn send_read_receipt(&self, _address: &TariAddress, _message_id: Vec) { + todo!(); + } + fn identity(&self) -> &NodeIdentity { &self.identity }