Skip to content

Commit

Permalink
update some helper docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sualeh Asif committed Jun 30, 2022
1 parent 9d763ff commit 4ebe93a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions daemon/db/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,13 +1959,20 @@ impl DB {
}
}

/// Returns the acks that we need to send to the server.
///
/// Returns:
///
/// A vector of OutgoingAck structs.
pub fn acks_to_send(&self) -> Result<Vec<ffi::OutgoingAck>, DbError> {
let mut conn = self.connect()?;

self.check_rep(&mut conn);

use crate::schema::friend;
use crate::schema::transmission;
/// Creating a query that will return the uid, received_seqnum, write_key, and ack_index for all friends
/// that are not deleted. It does this by joining the friend table with the transmission table.
let wide_friends =
friend::table.filter(friend::deleted.eq(false)).inner_join(transmission::table);
let q = wide_friends.select((
Expand All @@ -1974,13 +1981,24 @@ impl DB {
transmission::write_key,
transmission::ack_index,
));

/// Loading the results of the query into a vector of OutgoingAck structs.
let r = q.load::<ffi::OutgoingAck>(&mut conn);
match r {
Ok(acks) => Ok(acks),
Err(e) => Err(DbError::Unknown(format!("acks_to_send: {}", e))),
}
}

/// It gets the address of a friend from the database.
///
/// Arguments:
///
/// * `uid`: The user id of the friend.
///
/// Returns:
///
/// A tuple of the friend_uid, read_index, read_key, write_key, and ack_index.
pub fn get_friend_address(&self, uid: i32) -> Result<ffi::Address, DbError> {
let mut conn = self.connect()?;
self.check_rep(&mut conn);
Expand All @@ -2003,6 +2021,15 @@ impl DB {
}
}

/// Get a random friend that is not deleted and not in the excluded list
///
/// Arguments:
///
/// * `uids`: A list of friend uids to exclude from the random selection.
///
/// Returns:
///
/// A random friend address that is not deleted and is not in the excluded list.
pub fn get_random_enabled_friend_address_excluding(
&self,
uids: Vec<i32>,
Expand All @@ -2026,8 +2053,10 @@ impl DB {
));
let addresses = q.load::<ffi::Address>(&mut conn);

/// Getting a random friend address from the database, excluding the ones in the uids list.
match addresses {
Ok(addresses) => {
/// Filtering out the addresses that are in the excluded list.
let rng: usize = rand::random();
let mut filtered_addresses =
addresses.into_iter().filter(|address| !uids.contains(&address.uid)).collect::<Vec<_>>();
Expand Down

0 comments on commit 4ebe93a

Please sign in to comment.