help module restructure

This commit is contained in:
Mahesh Bansod 2021-10-30 22:21:59 +05:30
parent 72c5a8976b
commit 09fb0b1798
4 changed files with 102 additions and 47 deletions

View File

@ -0,0 +1,35 @@
// You need to make a file called `test_creds.test` in the root of the project for
// this example to run. Though, it makes more sense to work using a session instead of saving
// the password in plaintext in a file in real applications
// The file is a JSON file with the following format
// {
// "username":"yourusername",
// "password":"yourpassword"
// }
#[tokio::main]
async fn main() {
let creds1: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string("test_creds.test").unwrap())
.expect("Couldn't read the credentials. Check the JSON format or something");
let mut client1 = socialvoid::new_with_defaults().await.unwrap();
client1
.authenticate_user(
creds1["username"].as_str().unwrap().to_string(),
creds1["password"].as_str().unwrap().to_string(),
None,
)
.await
.unwrap();
let handle = tokio::spawn(async move {
let post = client1.compose_post("Yayaya", vec![]).await.unwrap();
println!("Made post!");
if client1.delete_post(post.id).await.unwrap() {
println!("Deleted successfully!");
}
});
handle.await.unwrap();
}

View File

@ -1 +0,0 @@
*.json.test

View File

@ -1,39 +1,41 @@
use serde_json::json;
use socialvoid_rawclient::Error;
use socialvoid_types::{HelpDocument, ServerInformation};
use std::rc::Rc;
pub async fn get_community_guidelines(
client: &socialvoid_rawclient::Client,
) -> Result<HelpDocument, Error> {
client
.send_request("help.get_community_guidelines", json!(null))
.await
pub struct SVHelpMethods {
client: Rc<socialvoid_rawclient::Client>,
}
pub async fn get_privacy_policy(
client: &socialvoid_rawclient::Client,
) -> Result<HelpDocument, Error> {
client
.send_request("help.get_privacy_policy", json!(null))
.await
}
impl SVHelpMethods {
pub fn new(client: Rc<socialvoid_rawclient::Client>) -> SVHelpMethods {
SVHelpMethods { client }
}
pub async fn get_server_information(
client: &socialvoid_rawclient::Client,
) -> Result<ServerInformation, Error> {
client
.send_request("help.get_server_information", json!(null))
.await
}
pub async fn get_community_guidelines(&self) -> Result<HelpDocument, Error> {
self.client
.send_request("help.get_community_guidelines", json!(null))
.await
}
pub async fn get_terms_of_service(
client: &socialvoid_rawclient::Client,
) -> Result<HelpDocument, Error> {
client
.send_request("help.get_terms_of_service", json!(null))
.await
}
pub async fn get_privacy_policy(&self) -> Result<HelpDocument, Error> {
self.client
.send_request("help.get_privacy_policy", json!(null))
.await
}
pub async fn get_server_information(&self) -> Result<ServerInformation, Error> {
self.client
.send_request("help.get_server_information", json!(null))
.await
}
pub async fn get_terms_of_service(&self) -> Result<HelpDocument, Error> {
self.client
.send_request("help.get_terms_of_service", json!(null))
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -41,24 +43,25 @@ mod tests {
pub async fn save_all_documents() {
use std::fs::File;
let client = socialvoid_rawclient::new();
let help = SVHelpMethods::new(Rc::new(client));
serde_json::to_writer(
&File::create("community_guidelines.json.test").unwrap(),
&get_community_guidelines(&client).await.unwrap(),
&help.get_community_guidelines().await.unwrap(),
)
.unwrap();
serde_json::to_writer(
&File::create("privacy_policy.json.test").unwrap(),
&get_privacy_policy(&client).await.unwrap(),
&help.get_privacy_policy().await.unwrap(),
)
.unwrap();
serde_json::to_writer(
&File::create("server_information.json.test").unwrap(),
&get_server_information(&client).await.unwrap(),
&help.get_server_information().await.unwrap(),
)
.unwrap();
serde_json::to_writer(
&File::create("terms_of_service.json.test").unwrap(),
&get_terms_of_service(&client).await.unwrap(),
&help.get_terms_of_service().await.unwrap(),
)
.unwrap();
}

View File

@ -7,6 +7,7 @@ pub mod timeline;
pub use error::ClientError;
pub use error::SocialvoidError;
use help::SVHelpMethods;
use session::ClientInfo;
use session::RegisterRequest;
use session::Session;
@ -16,30 +17,41 @@ use socialvoid_types::HelpDocument;
use socialvoid_types::Peer;
use socialvoid_types::Post;
use socialvoid_types::Profile;
use std::rc::Rc;
/// Create a client and establish a new session
pub async fn new_with_defaults() -> Result<Client, SocialvoidError> {
let rpc_client = socialvoid_rawclient::new();
let cdn_client = make_cdn_client_from(&rpc_client).await?;
let rpc_client = Rc::new(socialvoid_rawclient::new());
let cdn_client = make_cdn_client_from(Rc::clone(&rpc_client)).await?;
let client_info = ClientInfo::generate();
let mut session = SessionHolder::new(client_info.clone());
session.create(&rpc_client).await?;
let sessions = vec![session];
let (help) = init_methods(Rc::clone(&rpc_client));
Ok(Client {
let client = Client {
current_session: Some(0),
sessions,
client_info,
rpc_client,
rpc_client: socialvoid_rawclient::new(), //temporary,.. TODO: remove this
// rpc_client: &rpc_client,
cdn_client,
})
help,
};
Ok(client)
}
pub fn init_methods(client: Rc<socialvoid_rawclient::Client>) -> (SVHelpMethods) {
(SVHelpMethods::new(client))
}
/// Creates the CDN client by resolving the host url from server information
async fn make_cdn_client_from(
rpc_client: &socialvoid_rawclient::Client,
rpc_client: Rc<socialvoid_rawclient::Client>,
) -> Result<socialvoid_rawclient::CdnClient, SocialvoidError> {
let server_info = help::get_server_information(rpc_client).await?;
let server_info = SVHelpMethods::new(Rc::clone(&rpc_client))
.get_server_information()
.await?;
Ok(socialvoid_rawclient::CdnClient::with_cdn_url(
server_info.cdn_server,
@ -53,27 +65,32 @@ pub async fn new(
client_info: ClientInfo,
sessions: Vec<SessionHolder>,
) -> Result<Client, SocialvoidError> {
let rpc_client = socialvoid_rawclient::new();
let cdn_client = make_cdn_client_from(&rpc_client).await?;
let rpc_client = Rc::new(socialvoid_rawclient::new());
let cdn_client = make_cdn_client_from(Rc::clone(&rpc_client)).await?;
let current_session = if sessions.is_empty() { None } else { Some(0) };
let (help) = init_methods(Rc::clone(&rpc_client));
Ok(Client {
current_session,
sessions,
client_info,
rpc_client,
rpc_client: socialvoid_rawclient::new(),
cdn_client,
help,
})
}
/// Create a client with generated client info and zero sessions
/// Note that, cdn client may not be the one taken from server information
pub fn new_empty_client() -> Client {
let rpc_client = Rc::new(socialvoid_rawclient::new());
let (help) = init_methods(Rc::clone(&rpc_client));
Client {
current_session: None,
sessions: Vec::new(),
client_info: ClientInfo::generate(),
rpc_client: socialvoid_rawclient::new(),
cdn_client: socialvoid_rawclient::CdnClient::new(),
help,
}
}
@ -84,14 +101,15 @@ pub struct Client {
client_info: ClientInfo,
rpc_client: socialvoid_rawclient::Client,
cdn_client: socialvoid_rawclient::CdnClient,
pub help: SVHelpMethods,
}
impl Client {
/// Set the CDN server URL from the ServerInfomation
pub async fn reset_cdn_url(&mut self) -> Result<(), SocialvoidError> {
self.cdn_client = make_cdn_client_from(&self.rpc_client).await?;
Ok(())
}
// pub async fn reset_cdn_url(&mut self) -> Result<(), SocialvoidError> {
// self.cdn_client = make_cdn_client_from(&self.rpc_client).await?;
// Ok(())
// }
/// Saves all your sessions to a file
pub fn save_sessions(&self, filename: &str) -> Result<(), std::io::Error> {
@ -169,7 +187,7 @@ impl Client {
/// Get terms of service
pub async fn get_terms_of_service(&self) -> Result<HelpDocument, SocialvoidError> {
Ok(help::get_terms_of_service(&self.rpc_client).await?)
Ok(self.help.get_terms_of_service().await?)
}
/// Accept terms of service for the current session