Add git hook

This commit is contained in:
notplants 2022-01-15 08:44:43 -05:00
parent bacd954fa0
commit a72fbc9c08
2 changed files with 66 additions and 40 deletions

1
git_hooks/pre-commit Executable file
View File

@ -0,0 +1 @@
cargo fmt

View File

@ -1,10 +1,10 @@
//! Sbot type and associated methods.
use std::collections::HashMap;
use async_std::{
net::TcpStream,
stream::{Stream, StreamExt},
};
use futures::pin_mut;
use std::collections::HashMap;
use kuska_handshake::async_std::BoxStream;
use kuska_sodiumoxide::crypto::{auth, sign::ed25519};
@ -39,7 +39,7 @@ pub struct Sbot {
private_key: ed25519::SecretKey,
address: String,
// aka caps key (scuttleverse identifier)
network_id: auth::Key
network_id: auth::Key,
}
impl Sbot {
@ -68,7 +68,7 @@ impl Sbot {
public_key: pk,
private_key: sk,
address,
network_id
network_id,
})
}
@ -136,8 +136,12 @@ impl Sbot {
.client
.getsubset_req_send(query, options)
.await?;
let get_subset_stream =
get_source_stream(sbot_connection.rpc_reader, req_id, utils::ssb_message_res_parse).await;
let get_subset_stream = get_source_stream(
sbot_connection.rpc_reader,
req_id,
utils::ssb_message_res_parse,
)
.await;
Ok(get_subset_stream)
}
@ -181,29 +185,45 @@ impl Sbot {
}
/// Call the `friends follow` RPC method and return a message reference.
pub async fn friends_follow(&mut self, ssb_id: &str) -> Result<String, GolgiError> {
/// if state is true, then sets the following status to true
/// if state is false, then sets the following status to false (unfollow)
pub async fn friends_follow(
&mut self,
ssb_id: &str,
state: bool,
) -> Result<String, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection.client.friends_follow_req_send(ssb_id).await?;
let req_id = sbot_connection
.client
.friends_follow_req_send(ssb_id, state)
.await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
)
.await
}
/// Call the `friends isfollowing` RPC method and return a message reference.
/// returns true if src_id is following dest_id and false otherwise
pub async fn friends_is_following(
&mut self,
src_id: &str,
dest_id: &str,
) -> Result<String, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection
.client
.friends_isfollowing_req_send(src_id, dest_id)
.await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
)
.await
}
/// Call the `friends isfollowing` RPC method and return a message reference.
/// returns true if src_id is following dest_id and false otherwise
pub async fn friends_is_following(&mut self, src_id: &str, dest_id: &str) -> Result<String, GolgiError> {
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection.client.friends_isfollowing_req_send(src_id, dest_id).await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
).await
.await
}
/// Wrapper for publish which constructs and publishes a post message appropriately from a string.
@ -278,9 +298,7 @@ impl Sbot {
// and remove this filter section
// filter down to about messages
let about_message_stream = get_subset_stream.filter(|msg| match msg {
Ok(val) => {
val.is_message_type(SsbMessageContentType::About)
}
Ok(val) => val.is_message_type(SsbMessageContentType::About),
Err(_err) => false,
});
// return about message stream
@ -298,13 +316,14 @@ impl Sbot {
// now we have a stream of about messages with most recent at the front of the vector
pin_mut!(about_message_stream);
// iterate through the vector looking for most recent about message with the given key
let latest_about_message_res: Option<Result<SsbMessageValue, GolgiError>> = about_message_stream
// find the first msg that contains the field `key`
.find(|res| match res {
Ok(msg) => msg.content.get(key).is_some(),
Err(_) => false,
})
.await;
let latest_about_message_res: Option<Result<SsbMessageValue, GolgiError>> =
about_message_stream
// find the first msg that contains the field `key`
.find(|res| match res {
Ok(msg) => msg.content.get(key).is_some(),
Err(_) => false,
})
.await;
// Option<Result<SsbMessageValue, GolgiError>> -> Option<SsbMessageValue>
let latest_about_message = latest_about_message_res.and_then(|msg| msg.ok());
// Option<SsbMessageValue> -> Option<String>
@ -323,14 +342,20 @@ impl Sbot {
}
/// Get HashMap of profile info for given user
pub async fn get_profile_info(&mut self, ssb_id: &str) -> Result<HashMap<String, String>, GolgiError> {
pub async fn get_profile_info(
&mut self,
ssb_id: &str,
) -> Result<HashMap<String, String>, GolgiError> {
let mut keys_to_search_for = vec!["name", "description", "image"];
self.get_about_info(ssb_id, keys_to_search_for).await
}
/// Get HashMap of name and image for given user
/// (this is can be used to display profile images of a list of users)
pub async fn get_name_and_image(&mut self, ssb_id: &str) -> Result<HashMap<String, String>, GolgiError> {
pub async fn get_name_and_image(
&mut self,
ssb_id: &str,
) -> Result<HashMap<String, String>, GolgiError> {
let mut keys_to_search_for = vec!["name", "image"];
self.get_about_info(ssb_id, keys_to_search_for).await
}
@ -349,7 +374,7 @@ impl Sbot {
pub async fn get_about_info(
&mut self,
ssb_id: &str,
mut keys_to_search_for: Vec<&str>
mut keys_to_search_for: Vec<&str>,
) -> Result<HashMap<String, String>, GolgiError> {
// get about_message_stream
let about_message_stream = self.get_about_message_stream(ssb_id).await?;
@ -361,7 +386,7 @@ impl Sbot {
while let Some(res) = about_message_stream.next().await {
// if there are no more keys we are looking for, then we are done
if keys_to_search_for.len() == 0 {
break
break;
}
// if there are still keys we are looking for, then continue searching
match res {
@ -369,7 +394,9 @@ impl Sbot {
// for each key we are searching for, check if this about
// message contains a value for that key
for key in &keys_to_search_for.clone() {
let option_val = msg.content.get(key)
let option_val = msg
.content
.get(key)
.and_then(|val| val.as_str())
.map(|val| val.to_string());
match option_val {
@ -379,15 +406,13 @@ impl Sbot {
// remove this key fom keys_to_search_for, since we are no longer searching for it
keys_to_search_for.retain(|val| val != key)
}
None => {
continue
}
None => continue,
}
}
}
Err(err) => {
// skip errors
continue
continue;
}
}
}