|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use async_graphql::{Context, Object}; |
| 4 | +use uuid::Uuid; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + ErrorWrapper, |
| 8 | + upload::InternUploadSessionCommitData, |
| 9 | + utils::traits_utils::{MangadexAsyncGraphQLContextExt, MangadexTauriManagerExt}, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct InternalSessionsMutations; |
| 13 | + |
| 14 | +#[Object] |
| 15 | +impl InternalSessionsMutations { |
| 16 | + pub async fn session(&self, id: Uuid) -> InternalSessionMutation { |
| 17 | + InternalSessionMutation(id) |
| 18 | + } |
| 19 | + /// Returns the internal session id |
| 20 | + pub async fn create_session( |
| 21 | + &self, |
| 22 | + ctx: &Context<'_>, |
| 23 | + manga_id: Uuid, |
| 24 | + groups: Option<Vec<Uuid>>, |
| 25 | + ) -> Result<Uuid, ErrorWrapper> { |
| 26 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 27 | + let manager = app_handle.upload_manager(); |
| 28 | + Ok(manager.create_session(manga_id, groups).await?) |
| 29 | + } |
| 30 | + pub async fn start_queue_runner( |
| 31 | + &self, |
| 32 | + ctx: &Context<'_>, |
| 33 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 34 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 35 | + let manager = app_handle.upload_manager(); |
| 36 | + manager.start_queue_runner().await; |
| 37 | + Ok(None) |
| 38 | + } |
| 39 | + pub async fn swap_queue_order( |
| 40 | + &self, |
| 41 | + ctx: &Context<'_>, |
| 42 | + a: Uuid, |
| 43 | + b: Uuid, |
| 44 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 45 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 46 | + let manager = app_handle.upload_manager(); |
| 47 | + manager.swap(a, b).await?; |
| 48 | + Ok(None) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub struct InternalSessionMutation(Uuid); |
| 53 | + |
| 54 | +#[Object] |
| 55 | +impl InternalSessionMutation { |
| 56 | + pub async fn send_in_queue(&self, ctx: &Context<'_>) -> Result<Option<bool>, ErrorWrapper> { |
| 57 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 58 | + let manager = app_handle.upload_manager(); |
| 59 | + manager.send_session_in_queue(self.0).await?; |
| 60 | + Ok(None) |
| 61 | + } |
| 62 | + pub async fn remove(&self, ctx: &Context<'_>) -> Result<Option<bool>, ErrorWrapper> { |
| 63 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 64 | + let manager = app_handle.upload_manager(); |
| 65 | + manager.remove_session(self.0).await?; |
| 66 | + Ok(None) |
| 67 | + } |
| 68 | + pub async fn add_file( |
| 69 | + &self, |
| 70 | + ctx: &Context<'_>, |
| 71 | + img_path: String, |
| 72 | + index: Option<u32>, |
| 73 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 74 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 75 | + let manager = app_handle.upload_manager(); |
| 76 | + manager |
| 77 | + .add_file_to_session(self.0, img_path.into(), index) |
| 78 | + .await?; |
| 79 | + Ok(None) |
| 80 | + } |
| 81 | + pub async fn add_files( |
| 82 | + &self, |
| 83 | + ctx: &Context<'_>, |
| 84 | + img_paths: Vec<String>, |
| 85 | + index: Option<u32>, |
| 86 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 87 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 88 | + let manager = app_handle.upload_manager(); |
| 89 | + manager |
| 90 | + .add_files_to_session( |
| 91 | + self.0, |
| 92 | + img_paths.into_iter().map(PathBuf::from).collect(), |
| 93 | + index, |
| 94 | + ) |
| 95 | + .await?; |
| 96 | + Ok(None) |
| 97 | + } |
| 98 | + pub async fn remove_file( |
| 99 | + &self, |
| 100 | + ctx: &Context<'_>, |
| 101 | + img_path: String, |
| 102 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 103 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 104 | + let manager = app_handle.upload_manager(); |
| 105 | + manager.remove_file_from_session(self.0, img_path).await?; |
| 106 | + Ok(None) |
| 107 | + } |
| 108 | + pub async fn remove_files( |
| 109 | + &self, |
| 110 | + ctx: &Context<'_>, |
| 111 | + img_paths: Vec<String>, |
| 112 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 113 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 114 | + let manager = app_handle.upload_manager(); |
| 115 | + manager.remove_files_from_session(self.0, img_paths).await?; |
| 116 | + Ok(None) |
| 117 | + } |
| 118 | + pub async fn set_commit_data( |
| 119 | + &self, |
| 120 | + ctx: &Context<'_>, |
| 121 | + commit_data: Option<InternUploadSessionCommitData>, |
| 122 | + start_runner: Option<bool>, |
| 123 | + ) -> Result<Option<bool>, ErrorWrapper> { |
| 124 | + let app_handle = ctx.get_app_handle::<tauri::Wry>()?; |
| 125 | + let manager = app_handle.upload_manager(); |
| 126 | + if start_runner.unwrap_or_default() { |
| 127 | + manager |
| 128 | + .set_commit_data_and_send_to_queue(self.0, commit_data) |
| 129 | + .await?; |
| 130 | + } else { |
| 131 | + manager.set_commit_data(self.0, commit_data).await?; |
| 132 | + } |
| 133 | + Ok(None) |
| 134 | + } |
| 135 | +} |
0 commit comments