|
1 | 1 | pub mod export; |
2 | 2 |
|
3 | | -use crate::Result; |
| 3 | +use crate::{ |
| 4 | + Result, |
| 5 | + store::types::structs::content::ContentFeeder, |
| 6 | + utils::{ |
| 7 | + splittable_param::SendSplitted, |
| 8 | + traits_utils::{MangadexAsyncGraphQLContextExt, MangadexTauriManagerExt}, |
| 9 | + }, |
| 10 | +}; |
4 | 11 | use async_graphql::{Context, Object}; |
5 | | -use mangadex_api_input_types::custom_list::{ |
6 | | - add_manga::CustomListAddMangaParam, create::CustomListCreateParam, |
7 | | - remove_manga::CustomListRemoveMangaParam, update::CustomListUpdateParams, |
| 12 | +use mangadex_api_input_types::{ |
| 13 | + custom_list::{ |
| 14 | + add_manga::CustomListAddMangaParam, create::CustomListCreateParam, |
| 15 | + remove_manga::CustomListRemoveMangaParam, update::CustomListUpdateParams, |
| 16 | + }, |
| 17 | + manga::list::MangaListParams, |
8 | 18 | }; |
9 | | -use mangadex_api_types_rust::RelationshipType; |
| 19 | +use mangadex_api_types_rust::{CustomListVisibility, RelationshipType}; |
10 | 20 | use uuid::Uuid; |
11 | 21 |
|
12 | 22 | use crate::{ |
@@ -147,4 +157,98 @@ impl CustomListMutations { |
147 | 157 | pub async fn export(&self) -> export::CustomListExportMutations { |
148 | 158 | export::CustomListExportMutations |
149 | 159 | } |
| 160 | + pub async fn fork( |
| 161 | + &self, |
| 162 | + ctx: &Context<'_>, |
| 163 | + to_fork: Uuid, |
| 164 | + name: String, |
| 165 | + visibility: Option<CustomListVisibility>, |
| 166 | + filter_content: Option<bool>, |
| 167 | + ) -> Result<CustomList> { |
| 168 | + let to_fork = { |
| 169 | + let client = |
| 170 | + get_mangadex_client_from_graphql_context_with_auth_refresh::<tauri::Wry>(ctx) |
| 171 | + .await?; |
| 172 | + client |
| 173 | + .custom_list() |
| 174 | + .id(to_fork) |
| 175 | + .get() |
| 176 | + .with_auth(true) |
| 177 | + .send() |
| 178 | + .await? |
| 179 | + }; |
| 180 | + let manga_ids = { |
| 181 | + let ids = to_fork |
| 182 | + .data |
| 183 | + .find_relationships(RelationshipType::Manga) |
| 184 | + .into_iter() |
| 185 | + .map(|r| r.id) |
| 186 | + .collect::<Vec<_>>(); |
| 187 | + if filter_content.unwrap_or_default() { |
| 188 | + let app = ctx.get_app_handle::<tauri::Wry>()?; |
| 189 | + let client = app.get_mangadex_client()?; |
| 190 | + app.feed(MangaListParams { |
| 191 | + manga_ids: ids, |
| 192 | + ..Default::default() |
| 193 | + }) |
| 194 | + .send_splitted_default(&client) |
| 195 | + .await? |
| 196 | + .data |
| 197 | + .into_iter() |
| 198 | + .map(|manga| manga.id) |
| 199 | + .collect() |
| 200 | + } else { |
| 201 | + ids |
| 202 | + } |
| 203 | + }; |
| 204 | + let mut create = CustomListCreateParam { |
| 205 | + name, |
| 206 | + visibility, |
| 207 | + version: None, |
| 208 | + manga: { |
| 209 | + let mut m = manga_ids.clone(); |
| 210 | + m.reverse(); |
| 211 | + m |
| 212 | + }, |
| 213 | + }; |
| 214 | + |
| 215 | + let mut skipped = 0; |
| 216 | + loop { |
| 217 | + if size_of_val(&create) >= 8000 { |
| 218 | + skipped += 10; |
| 219 | + create.manga = create.manga.into_iter().skip(skipped).collect(); |
| 220 | + } else { |
| 221 | + break; |
| 222 | + } |
| 223 | + } |
| 224 | + let new_custom_list = { |
| 225 | + let client = |
| 226 | + get_mangadex_client_from_graphql_context_with_auth_refresh::<tauri::Wry>(ctx) |
| 227 | + .await?; |
| 228 | + create.send(&client).await? |
| 229 | + }; |
| 230 | + |
| 231 | + if skipped > 0 { |
| 232 | + self.add_manga_batch( |
| 233 | + ctx, |
| 234 | + new_custom_list.data.id, |
| 235 | + manga_ids.iter().skip(skipped).cloned().collect(), |
| 236 | + ) |
| 237 | + .await?; |
| 238 | + } |
| 239 | + { |
| 240 | + let client = |
| 241 | + get_mangadex_client_from_graphql_context_with_auth_refresh::<tauri::Wry>(ctx) |
| 242 | + .await?; |
| 243 | + Ok(client |
| 244 | + .custom_list() |
| 245 | + .id(new_custom_list.data.id) |
| 246 | + .get() |
| 247 | + .with_auth(true) |
| 248 | + .send() |
| 249 | + .await? |
| 250 | + .data |
| 251 | + .into()) |
| 252 | + } |
| 253 | + } |
150 | 254 | } |
0 commit comments