|
@@ -3,6 +3,7 @@ use crate::config::Config;
|
|
|
use crate::indexes::{Indexes, SerializableMessage, SerializableThread};
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
use std::fs::{File, metadata};
|
|
|
+use std::future::Future;
|
|
|
use std::io::{BufReader, Read};
|
|
|
use std::net::{IpAddr, SocketAddr};
|
|
|
use std::path::PathBuf;
|
|
@@ -10,8 +11,9 @@ use std::str::FromStr;
|
|
|
use chrono::{DateTime, Utc};
|
|
|
use warp::Filter;
|
|
|
use warp::fs::file;
|
|
|
-use warp::http::StatusCode;
|
|
|
+use warp::http::{response, StatusCode};
|
|
|
use crate::{create_folder_lar, delete_folder_lar, rename_folder_lar};
|
|
|
+use crate::imap::connect_to_imap;
|
|
|
use crate::models::MailAddress;
|
|
|
use crate::sonic::SearchSonic;
|
|
|
use crate::util::read_and_decompress_file;
|
|
@@ -74,6 +76,13 @@ pub async fn run_api() {
|
|
|
.and(warp::post())
|
|
|
.and(warp::body::json())
|
|
|
.and_then(delete_folder_handle))
|
|
|
+ .or(warp::path("is_logged_in")
|
|
|
+ .and(warp::get())
|
|
|
+ .and_then(is_logged_in_handle))
|
|
|
+ .or(warp::path("log_in")
|
|
|
+ .and(warp::post())
|
|
|
+ .and(warp::body::json())
|
|
|
+ .and_then(log_in_handle))
|
|
|
.with(cors);
|
|
|
|
|
|
let ip: IpAddr = IpAddr::from_str(&*Config::global().api_addr.clone()).expect("Invalid API IP address");
|
|
@@ -271,6 +280,34 @@ pub async fn get_sorted_threads_by_date(folder: String, limit: usize, offset: us
|
|
|
get_threads(folder, limit, offset, "date.json".to_string()).await
|
|
|
}
|
|
|
|
|
|
+pub async fn is_logged_in() -> String {
|
|
|
+ let response = if (Config::global().username == "" || Config::global().password == "") {
|
|
|
+ serde_json::to_string(&IsLoggedInResponse { is_logged_in: false })
|
|
|
+ }else{
|
|
|
+ serde_json::to_string(&IsLoggedInResponse { is_logged_in: true })
|
|
|
+ };
|
|
|
+
|
|
|
+ response.unwrap_or_else(|_| serde_json::to_string(&IsLoggedInResponse { is_logged_in: true }).unwrap())
|
|
|
+}
|
|
|
+
|
|
|
+pub async fn log_in(email: String, password: String) -> bool {
|
|
|
+ Config::global().set_username(email.clone());
|
|
|
+ Config::global().set_password(password.clone());
|
|
|
+
|
|
|
+ match connect_to_imap().await {
|
|
|
+ Ok(_) => {
|
|
|
+ match Config::global().update_credentials(&email, &password){
|
|
|
+ Ok(_) => {
|
|
|
+ // TODO start downloading emails
|
|
|
+ true
|
|
|
+ },
|
|
|
+ Err(_) => false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Err(_) => false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// Handlers
|
|
|
#[derive(Deserialize)]
|
|
|
struct GetAttachmentQuery {
|
|
@@ -443,4 +480,27 @@ async fn search_handle(query: SearchQuery) -> Result<impl warp::Reply, warp::Rej
|
|
|
Ok(result) => Ok(warp::reply::json(&result)),
|
|
|
Err(_) => Ok(warp::reply::json(&Vec::<String>::new()))
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Deserialize, Serialize)]
|
|
|
+struct IsLoggedInResponse{
|
|
|
+ is_logged_in: bool
|
|
|
+}
|
|
|
+
|
|
|
+async fn is_logged_in_handle() -> Result<impl warp::Reply, warp::Rejection>{
|
|
|
+ Ok(warp::reply::json(&is_logged_in().await))
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Deserialize)]
|
|
|
+struct LogInRequest {
|
|
|
+ email: String,
|
|
|
+ password: String
|
|
|
+}
|
|
|
+
|
|
|
+async fn log_in_handle(query: LogInRequest) -> Result<impl warp::Reply, warp::Rejection>{
|
|
|
+ if log_in(query.email, query.password).await {
|
|
|
+ Ok(warp::reply::with_status("Successful log in", StatusCode::OK))
|
|
|
+ }else{
|
|
|
+ Ok(warp::reply::with_status("Unable to log in", StatusCode::BAD_REQUEST))
|
|
|
+ }
|
|
|
}
|