Skip to content

Commit

Permalink
Add a client function for read receipt sending
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Oct 1, 2023
1 parent c6f7805 commit 06dc2b7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 10 additions & 0 deletions base_layer/contacts/examples/chat_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message>;
async fn send_message(&self, message: Message);
async fn send_read_receipt(&self, address: &TariAddress, message_id: Vec<u8>);
fn identity(&self) -> &NodeIdentity;
fn shutdown(&mut self);
}
Expand Down Expand Up @@ -171,6 +172,15 @@ impl ChatClient for Client {
messages
}

async fn send_read_receipt(&self, address: &TariAddress, message_id: Vec<u8>) {
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()
}
Expand Down
27 changes: 25 additions & 2 deletions base_layer/contacts/src/contacts_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +138,7 @@ pub enum ContactsServiceRequest {
GetContactOnlineStatus(Contact),
SendMessage(TariAddress, Message),
GetMessages(TariAddress, i64, i64),
SendDeliveryConfirmation(TariAddress, Vec<u8>),
SendReadConfirmation(TariAddress, Confirmation),
}

#[derive(Debug)]
Expand All @@ -149,6 +150,7 @@ pub enum ContactsServiceResponse {
OnlineStatus(ContactOnlineStatus),
Messages(Vec<Message>),
MessageSent,
ReadConfirmationSent,
}

#[derive(Clone)]
Expand Down Expand Up @@ -283,4 +285,25 @@ impl ContactsServiceHandle {
_ => Err(ContactsServiceError::UnexpectedApiResponse),
}
}

pub async fn send_read_confirmation(
&mut self,
address: TariAddress,
message_id: Vec<u8>,
) -> 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),
}
}
}
10 changes: 8 additions & 2 deletions base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
}
Expand Down
4 changes: 4 additions & 0 deletions integration_tests/src/chat_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ impl ChatClient for ChatFFI {
}
}

async fn send_read_receipt(&self, _address: &TariAddress, _message_id: Vec<u8>) {
todo!();
}

fn identity(&self) -> &NodeIdentity {
&self.identity
}
Expand Down

0 comments on commit 06dc2b7

Please sign in to comment.