Skip to content

Commit

Permalink
run linter
Browse files Browse the repository at this point in the history
  • Loading branch information
nanu-c committed Jun 20, 2023
1 parent 55e3b43 commit 641ffdc
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 74 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
// #[cfg(feature = "tauri")]
// tauri_build::build()
// #[cfg(feature = "tauri")]
// tauri_build::build()
}
3 changes: 1 addition & 2 deletions examples/dump/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
let db_path = format!("{config_path}/textsecure.nanuc");

let thedb = sled::open(db_path).unwrap();

dump_registration(thedb.clone());
dump_sessions(thedb.clone());
dump_groups(thedb.clone());
Expand Down Expand Up @@ -82,4 +82,3 @@ fn dump_contacts(thedb: sled::Db) {
// }
// }
// }

20 changes: 8 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use presage_store_sled::SledStoreError;
const FAILED_TO_LOOK_UP_ADDRESS: &str = "failed to lookup address information";
type PresageError = presage::Error<presage_store_sled::SledStoreError>;



#[derive(Debug)]
pub enum ApplicationError {
ManagerThreadPanic,
Expand All @@ -19,8 +17,7 @@ pub enum ApplicationError {
WebSocketHandleMessageError(String),
RegistrationError(String),
InvalidRequest,
RegistrationSuccesful

RegistrationSuccesful,
}

impl std::fmt::Display for ApplicationError {
Expand Down Expand Up @@ -105,12 +102,13 @@ impl From<PresageError> for ApplicationError {
// p::Error::MessageSenderError(p::libsignal_service::sender::MessageSenderError {
// recipient: _,
// }) => ApplicationError::NoInternet,
p::Error::MessageSenderError(p::libsignal_service::sender::MessageSenderError::ServiceError(
p::libsignal_service::content::ServiceError::SendError { reason: e },
)) if e.contains(FAILED_TO_LOOK_UP_ADDRESS) => ApplicationError::NoInternet,
p::Error::MessageSenderError(
p::libsignal_service::sender::MessageSenderError::ServiceError(
p::libsignal_service::content::ServiceError::SendError { reason: e },
),
) if e.contains(FAILED_TO_LOOK_UP_ADDRESS) => ApplicationError::NoInternet,
p::Error::MessageSenderError(e) => ApplicationError::SendFailed(e),
_ => ApplicationError::Presage(e),

}
}
}
Expand All @@ -119,7 +117,6 @@ impl From<SledStoreError> for ApplicationError {
fn from(e: SledStoreError) -> Self {
match e {
_ => ApplicationError::SledStore(e),

}
}
}
Expand Down Expand Up @@ -154,7 +151,7 @@ impl ApplicationError {
}
ApplicationError::WebSocketHandleMessageError(e) => format!("{:#?}", e),
ApplicationError::RegistrationError(e) => format!("{:#?}", e),
ApplicationError::InvalidRequest=> "Invalid request.".to_string(),
ApplicationError::InvalidRequest => "Invalid request.".to_string(),
ApplicationError::SledStore(e) => format!("{:#?}", e),
ApplicationError::RegistrationSuccesful => "Registration succesful.".to_string(),
}
Expand All @@ -171,10 +168,9 @@ impl ApplicationError {
ApplicationError::WebSocketError => true,
ApplicationError::WebSocketHandleMessageError(_) => true,
ApplicationError::RegistrationError(_) => true,
ApplicationError::InvalidRequest=> false,
ApplicationError::InvalidRequest => false,
ApplicationError::SledStore(_) => true,
ApplicationError::RegistrationSuccesful => false,
}
}
}

20 changes: 12 additions & 8 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ impl Handler {
"Got phone number: {}",
self.phone_number.as_ref().unwrap()
);
let (code_tx, code_rx) = mpsc::channel(MESSAGE_BOUND);
let (code_tx, code_rx) =
mpsc::channel(MESSAGE_BOUND);
self.get_phone_pin().await;
match self.get_verification_code(code_rx).await {
Ok(_) => log::debug!(
Expand Down Expand Up @@ -620,7 +621,7 @@ impl Handler {
}
};
log::debug!("check code_rx for code {:?}", code_rx);
let code = match code_rx.recv().await{
let code = match code_rx.recv().await {
Some(c) => c,
None => {
log::error!("No code provided");
Expand Down Expand Up @@ -865,7 +866,7 @@ impl Handler {
let response = serde_json::to_string(&response).unwrap();

let mut ws_sender = sender.lock().await;
match ws_sender.send(Message::text(response)).await{
match ws_sender.send(Message::text(response)).await {
Ok(_) => (),
Err(e) => {
log::error!("Error sending message to client: {}", e);
Expand Down Expand Up @@ -1254,19 +1255,22 @@ impl Handler {
let profile = manager.get_contact_by_id(uuid).await.ok().unwrap();
let mut profile = match profile {
Some(p) => p,
None =>{
None => {
//request contact sync
manager.request_contacts_sync().await.ok().unwrap();
return Err(ApplicationError::InvalidRequest);
}
}
};
if profile.name =="".to_string(){
if profile.name == "".to_string() {
//request contact sync
log::debug!("Updating contact from profile");
manager.update_contact_from_profile(profile.uuid).await.ok().unwrap();
manager
.update_contact_from_profile(profile.uuid)
.await
.ok()
.unwrap();
profile = manager.get_contact_by_id(uuid).await.ok().unwrap().unwrap();
log::debug!("Updated contact {:?}", profile);

}
let response = AxolotlResponse {
response_type: "profile".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod error;
pub mod handlers;
pub mod manager_thread;
pub mod messages;
pub mod requests;
pub mod manager_thread;
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axolotl::handlers::{get_app_dir};
use axolotl::handlers::get_app_dir;
use std::{process::exit, sync::Arc};

use axolotl::handlers::Handler;
Expand Down Expand Up @@ -53,10 +53,15 @@ async fn start_websocket(handler: Arc<Mutex<Handler>>) {
.and_then(handle_ws_client);

// Just serve the attachments/ directory
let axolotl_http_attachments_route = warp::path("attachments")
.and(warp::fs::dir(format!("{}/{}", get_app_dir(), "attachments")));

warp::serve(axolotl_ws_route.or(axolotl_http_attachments_route)).run(([127, 0, 0, 1], 9080)).await;
let axolotl_http_attachments_route = warp::path("attachments").and(warp::fs::dir(format!(
"{}/{}",
get_app_dir(),
"attachments"
)));

warp::serve(axolotl_ws_route.or(axolotl_http_attachments_route))
.run(([127, 0, 0, 1], 9080))
.await;
log::info!("Server stopped");
}

Expand Down
2 changes: 1 addition & 1 deletion src/manager_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ async fn command_loop(
}
}
}
}
}
match m.body.clone() {

Some(_data) => {
Expand Down
67 changes: 31 additions & 36 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::UNIX_EPOCH;

/**
* Send a message to one people or a group.
*
*
* - recipient is a String containing the UUID of the recipient. A contact or a
* group, both are supported.
* - msg is an optional String containing the text body of the message. Most messages
Expand All @@ -28,9 +28,8 @@ pub async fn send_message(
msg: Option<String>,
attachments: Option<Vec<AttachmentPointer>>,
manager: &ManagerThread,
response_type: &str
) -> Result<AxolotlResponse, ApplicationError>
{
response_type: &str,
) -> Result<AxolotlResponse, ApplicationError> {
log::info!("Sending a message.");
let timestamp = std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -55,11 +54,15 @@ pub async fn send_message(
let result = match recipient {
Thread::Contact(uuid) => {
log::debug!("Sending a message to a contact.");
manager.send_message(uuid, data_message.clone(), timestamp).await
manager
.send_message(uuid, data_message.clone(), timestamp)
.await
}
Thread::Group(uuid) => {
log::debug!("Sending a message to a group.");
manager.send_message_to_group(uuid, data_message.clone(), timestamp).await
manager
.send_message_to_group(uuid, data_message.clone(), timestamp)
.await
}
};
let is_failed = result.is_err();
Expand All @@ -78,25 +81,26 @@ pub async fn send_message(
Ok(response)
}

pub async fn send_message_to_group(msg: &str, master_key_str: &str, attachments: Option<Vec<AttachmentPointer>>,config_store: SledStore)
{
pub async fn send_message_to_group(
msg: &str,
master_key_str: &str,
attachments: Option<Vec<AttachmentPointer>>,
config_store: SledStore,
) {
let mut manager = Manager::load_registered(config_store).await.unwrap();
// Send message
let timestamp = std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_millis() as u64;

// Add the attachments to the message, if any
let mut attachments_vec = Vec::new();
if let Some(a) = attachments {
attachments_vec = a;
}

let master_key: [u8; 32] = hex::decode(master_key_str)
.unwrap()
.try_into()
.unwrap();
let master_key: [u8; 32] = hex::decode(master_key_str).unwrap().try_into().unwrap();

let message = DataMessage {
body: Some(msg.to_string()),
Expand All @@ -110,29 +114,20 @@ pub async fn send_message_to_group(msg: &str, master_key_str: &str, attachments:
..Default::default()
};

match manager
.group(&master_key)
{
Ok(group) => {
match group{
Some(_) => {
manager
.send_message_to_group(
&master_key,
message,
timestamp
).await.unwrap();
},
None => {
println!("Group not found");
}
}

},
Err(e) => {
println!("Group not found: {:?}", e);
match manager.group(&master_key) {
Ok(group) => match group {
Some(_) => {
manager
.send_message_to_group(&master_key, message, timestamp)
.await
.unwrap();
}
None => {
println!("Group not found");
}
},
Err(e) => {
println!("Group not found: {:?}", e);
}


}
}
23 changes: 16 additions & 7 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ impl AxolotlMessage {
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id.to_string()));
}
if let Some(AttachmentIdentifier::CdnKey(id)) = attachment.attachment_identifier.clone()
if let Some(AttachmentIdentifier::CdnKey(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id));
Expand All @@ -167,7 +168,8 @@ impl AxolotlMessage {
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id.to_string()));
}
if let Some(AttachmentIdentifier::CdnKey(id)) = attachment.attachment_identifier.clone()
if let Some(AttachmentIdentifier::CdnKey(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id));
Expand Down Expand Up @@ -229,12 +231,14 @@ impl AxolotlMessage {
if let ContentBody::DataMessage(ref data) = body {
if !data.attachments.is_empty() {
for attachment in &data.attachments {
if let Some(AttachmentIdentifier::CdnId(id)) = attachment.attachment_identifier.clone()
if let Some(AttachmentIdentifier::CdnId(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id.to_string()));
}
if let Some(AttachmentIdentifier::CdnKey(id)) = attachment.attachment_identifier.clone()
if let Some(AttachmentIdentifier::CdnKey(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id));
Expand Down Expand Up @@ -275,7 +279,8 @@ impl AxolotlMessage {
attachments
.push(AttachmentMessage::new(content_type, &id.to_string()));
}
if let Some(AttachmentIdentifier::CdnKey(id)) = attachment.attachment_identifier.clone()
if let Some(AttachmentIdentifier::CdnKey(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id));
Expand Down Expand Up @@ -324,11 +329,15 @@ impl AxolotlMessage {
let mut attachments: Vec<AttachmentMessage> = Vec::new();
if !data.attachments.is_empty() {
for attachment in &data.attachments {
if let Some(AttachmentIdentifier::CdnId(id)) = attachment.attachment_identifier.clone() {
if let Some(AttachmentIdentifier::CdnId(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id.to_string()));
}
if let Some(AttachmentIdentifier::CdnKey(id)) = attachment.attachment_identifier.clone() {
if let Some(AttachmentIdentifier::CdnKey(id)) =
attachment.attachment_identifier.clone()
{
let content_type = attachment.content_type();
attachments.push(AttachmentMessage::new(content_type, &id));
}
Expand Down

0 comments on commit 641ffdc

Please sign in to comment.