Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/unwrap #348

Merged
merged 6 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
601 changes: 290 additions & 311 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ regex = "1.9.6"
xml-builder = "0.5.2"
diesel = { version = "2.1.2", features = ["chrono", "r2d2"] }
r2d2 = "0.8.10"
utoipa = { version = "3.5.0", features = ["actix_extras"] }
utoipa = { version = "4.0.0", features = ["actix_extras"] }
futures = "0.3.28"
utoipa-swagger-ui = {version = "3.1.5", features = ["actix-web"] }
utoipa-swagger-ui = {version = "4.0.0", features = ["actix-web"] }
clokwerk= "0.4.0"
tokio = {version = "1.32.0", features = ["full"]}
serde = "1.0.188"
Expand Down
24 changes: 12 additions & 12 deletions src/auth_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ impl<S, B> Service<ServiceRequest> for AuthFilterMiddleware<S>
forward_ready!(service);

fn call(&self, req: ServiceRequest) -> Self::Future {
return if is_env_var_present_and_true(BASIC_AUTH) {
self.handle_basic_auth(req)
} else if is_env_var_present_and_true(OIDC_AUTH) {
self.handle_oidc_auth(req)
} else {
// It can only be no auth
self.handle_no_auth(req)
}
if is_env_var_present_and_true(BASIC_AUTH) {
self.handle_basic_auth(req)
} else if is_env_var_present_and_true(OIDC_AUTH) {
self.handle_oidc_auth(req)
} else {
// It can only be no auth
self.handle_no_auth(req)
}

}
}

Expand All @@ -94,8 +95,7 @@ impl<S, B> AuthFilterMiddleware<S> where B: 'static + MessageBody, S: 'static +
if opt_auth_header.is_none() {
return Box::pin(ok(req.error_response(ErrorUnauthorized("Unauthorized")).map_into_right_body()));
}
let authorization = opt_auth_header.unwrap().to_str();
return match authorization {
return match opt_auth_header.unwrap().to_str() {
Ok(auth) => {
let (username, password) = AuthFilter::extract_basic_auth(auth);
let res = req.app_data::<web::Data<DbPool>>().unwrap();
Expand All @@ -107,7 +107,7 @@ impl<S, B> AuthFilterMiddleware<S> where B: 'static + MessageBody, S: 'static +
}
let unwrapped_user = found_user.unwrap();

if unwrapped_user.clone().username == var(USERNAME).unwrap(){
if unwrapped_user.username.clone() == var(USERNAME).unwrap(){
return match password == var(PASSWORD).unwrap() {
true => {
req.extensions_mut().insert(unwrapped_user);
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<S, B> AuthFilterMiddleware<S> where B: 'static + MessageBody, S: 'static +
let binding = req.app_data::<web::Data<Mutex<JWKService>>>().cloned().unwrap();
let mut jwk_service = binding.lock()
.ignore_poison();
match jwk_service.clone().jwk {
match jwk_service.jwk.clone() {
Some(jwk)=>{
if since_the_epoch-jwk_service.timestamp>3600{
//refetch and update timestamp
Expand Down
32 changes: 18 additions & 14 deletions src/command_line_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::utils::error::CustomError;

pub fn start_command_line(mut args: Args) {
println!("Starting from command line");
match args.nth(1).unwrap().as_str() {
match args.next().unwrap().as_str() {
"help" | "--help" => {
println!(r" The following commands are available:
users => Handles user management
Expand All @@ -36,23 +36,27 @@ pub fn start_command_line(mut args: Args) {
match args.next().unwrap().as_str() {
"refresh" => {
let podcast_rss_feed = args.next();
if podcast_rss_feed.is_none() {
println!("Please provide a podcast rss feed url");
exit(1);
}
let rss_feed = podcast_rss_feed.clone().unwrap();
let mut podcast_service = PodcastService::new();
let conn = &mut establish_connection();

match podcast_rss_feed {
Some(feed)=>{
let mut podcast_service = PodcastService::new();
let conn = &mut establish_connection();


let replaced_feed = rss_feed.replace(['\'', ' '], "");
println!("Refreshing podcast {}", replaced_feed);
let replaced_feed = feed.replace(['\'', ' '], "");
println!("Refreshing podcast {}", replaced_feed);

let podcast = Podcast::get_podcast_by_rss_feed(replaced_feed, conn).expect("Error getting podcast");
let podcast = Podcast::get_podcast_by_rss_feed(replaced_feed, conn).expect("Error getting podcast");

let mut podcast_episode_service = PodcastEpisodeService::new();
podcast_episode_service.insert_podcast_episodes(conn, podcast.clone()).unwrap();
podcast_service.schedule_episode_download(podcast, None, conn).unwrap();
let mut podcast_episode_service = PodcastEpisodeService::new();
podcast_episode_service.insert_podcast_episodes(conn, podcast.clone()).unwrap();
podcast_service.schedule_episode_download(podcast, None, conn).unwrap();
}
None=>{
println!("Please provide a podcast rss feed url");
exit(1);
}
}
}
"refresh-all" => {
let conn = &mut establish_connection();
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/api_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use crate::models::podcast_episode::PodcastEpisode;
use models::podcasts::Podcast;
use std::future;
use std::future::Ready;
use utoipa::openapi::security::{ApiKey, ApiKeyValue, SecurityScheme};
use utoipa::{Modify, OpenApi};
use utoipa::{
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
Modify, OpenApi,
};
use crate::models::dto_models::PodcastFavorUpdateModel;
use crate::controllers::podcast_episode_controller::__path_find_all_podcast_episodes_of_podcast;
use crate::controllers::watch_time_controller::*;
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/notification_controller.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::ops::DerefMut;
use actix_web::web::Data;
use actix_web::{get, put, web, HttpResponse};
use std::sync::Mutex;
use crate::{DbPool};

use crate::mutex::LockResultExt;
use crate::service::notification_service::NotificationService;
use crate::utils::error::CustomError;
use crate::utils::error::{CustomError, map_r2d2_error};

#[utoipa::path(
context_path="/api/v1",
Expand All @@ -18,7 +19,7 @@ pub async fn get_unread_notifications(notification_service: Data<Mutex<Notificat
Result<HttpResponse, CustomError> {
let notifications = notification_service
.lock().ignore_poison()
.get_unread_notifications(&mut conn.get().unwrap())?;
.get_unread_notifications(conn.get().map_err(map_r2d2_error)?.deref_mut())?;
Ok(HttpResponse::Ok().json(notifications))
}

Expand All @@ -41,6 +42,6 @@ pub async fn dismiss_notifications(
notification_service.lock()
.ignore_poison()
.update_status_of_notification(id.id, "dismissed",
&mut conn.get().unwrap())?;
conn.get().map_err(map_r2d2_error)?.deref_mut())?;
Ok(HttpResponse::Ok().body(""))
}
20 changes: 10 additions & 10 deletions src/controllers/playlist_controller.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::ops::DerefMut;
use actix_web::{delete, get, HttpResponse, post, put, web};
use actix_web::web::Data;
use crate::controllers::podcast_episode_controller::PodcastEpisodeWithHistory;
use crate::DbPool;
use crate::models::playlist::Playlist;
use crate::models::user::User;
use crate::utils::error::CustomError;
use crate::utils::error::{CustomError, map_r2d2_error};

#[derive(Serialize, Deserialize, Clone)]
pub struct PlaylistDtoPost {
Expand Down Expand Up @@ -37,7 +38,7 @@ pub async fn add_playlist(requester: Option<web::ReqData<User>>, conn: Data<DbPo
let user = requester.unwrap().into_inner();
let playlist = playlist.into_inner();

let res = Playlist::create_new_playlist(&mut conn.get().unwrap(),
let res = Playlist::create_new_playlist(conn.get().map_err(map_r2d2_error)?.deref_mut(),
playlist, user)?;


Expand All @@ -57,7 +58,7 @@ pub async fn update_playlist(requester: Option<web::ReqData<User>>, conn: Data<D
let user = requester.unwrap().into_inner();
let playlist = playlist.into_inner();

let res = Playlist::update_playlist(&mut conn.get().unwrap(),
let res = Playlist::update_playlist(conn.get().map_err(map_r2d2_error)?.deref_mut(),
playlist, playlist_id.clone(),user)?;


Expand All @@ -67,7 +68,7 @@ pub async fn update_playlist(requester: Option<web::ReqData<User>>, conn: Data<D
#[get("/playlist")]
pub async fn get_all_playlists(requester: Option<web::ReqData<User>>, conn: Data<DbPool>) -> Result<HttpResponse,
CustomError>{
Playlist::get_playlists(&mut conn.get().unwrap(), requester.unwrap().into_inner().id)
Playlist::get_playlists(conn.get().map_err(map_r2d2_error)?.deref_mut(), requester.unwrap().into_inner().id)
.map(|playlists| HttpResponse::Ok().json(playlists))
}

Expand All @@ -76,8 +77,8 @@ pub async fn get_playlist_by_id(requester: Option<web::ReqData<User>>, conn: Dat
playlist_id: web::Path<String>) -> Result<HttpResponse, CustomError>{
let user_id = requester.clone().unwrap();
let playlist = Playlist::get_playlist_by_user_and_id(playlist_id.clone(), user_id.clone().into_inner(),
&mut conn.get().unwrap())?;
let playlist = Playlist::get_playlist_dto(playlist_id.clone(), &mut conn.get().unwrap(),
conn.get().map_err(map_r2d2_error)?.deref_mut())?;
let playlist = Playlist::get_playlist_dto(playlist_id.clone(), conn.get().map_err(map_r2d2_error)?.deref_mut(),
playlist, user_id.clone().into_inner())?;
Ok(HttpResponse::Ok().json(playlist))
}
Expand All @@ -86,7 +87,7 @@ pub async fn get_playlist_by_id(requester: Option<web::ReqData<User>>, conn: Dat
pub async fn delete_playlist_by_id(requester: Option<web::ReqData<User>>, conn: Data<DbPool>,
playlist_id: web::Path<String>) -> Result<HttpResponse, CustomError>{
let user_id = requester.clone().unwrap().id;
Playlist::delete_playlist_by_id(playlist_id.clone(), &mut conn.get().unwrap(),user_id)?;
Playlist::delete_playlist_by_id(playlist_id.clone(), conn.get().map_err(map_r2d2_error)?.deref_mut(),user_id)?;
Ok(HttpResponse::Ok().json(()))
}

Expand All @@ -96,8 +97,7 @@ pub async fn delete_playlist_item(requester: Option<web::ReqData<User>>, conn: D
-> Result<HttpResponse, CustomError>{
let user_id = requester.clone().unwrap().id;
let unwrapped_path = path.into_inner();
Playlist::delete_playlist_item(unwrapped_path.0, unwrapped_path.1, &mut conn
.get()
.unwrap(),user_id).await?;
Playlist::delete_playlist_item(unwrapped_path.0, unwrapped_path.1,
conn.get().map_err(map_r2d2_error)?.deref_mut(),user_id).await?;
Ok(HttpResponse::Ok().json(()))
}
Loading
Loading