|
117 | 117 | <script lang="ts"> |
118 | 118 | import { useDialogPluginComponent } from 'quasar'; |
119 | 119 | import ctfnote from 'src/ctfnote'; |
120 | | -import { Ctf, makeId } from 'src/ctfnote/models'; |
| 120 | +import { Ctf, makeId, Id, Task } from 'src/ctfnote/models'; |
121 | 121 | import parsers, { ParsedTask } from 'src/ctfnote/parsers'; |
122 | 122 | import { defineComponent, ref } from 'vue'; |
123 | 123 | import TaskTagsList from 'src/components/Task/TaskTagsList.vue'; |
@@ -284,27 +284,66 @@ export default defineComponent({ |
284 | 284 | const batchSize = 10; |
285 | 285 | for (let i = 0; i < tasks.length; i += batchSize) { |
286 | 286 | const batch = tasks.slice(i, i + batchSize); |
| 287 | +
|
287 | 288 | // Process the first task in the batch serially |
288 | 289 | // to make sure the Discord bot will create the categories correctly. |
| 290 | + let firstTaskResult: { |
| 291 | + task: ParsedTask; |
| 292 | + taskId: ReturnType<typeof makeId>; |
| 293 | + } | null = null; |
289 | 294 | if (batch.length > 0) { |
290 | 295 | const firstTask = batch[0]; |
291 | 296 | const r = await this.createTask(this.ctf.id, firstTask); |
292 | 297 | const newTask = r?.data?.createTask?.task; |
293 | 298 | if (newTask) { |
294 | | - await this.addTagsForTask(firstTask.tags, makeId(newTask.id)); |
| 299 | + firstTaskResult = { task: firstTask, taskId: makeId(newTask.id) }; |
295 | 300 | } |
296 | 301 | } |
297 | | - // Process the rest of the batch in parallel |
| 302 | +
|
| 303 | + // Process the rest of the batch in parallel for task creation |
| 304 | + const remainingTaskResults: Array<{ |
| 305 | + task: ParsedTask; |
| 306 | + taskId: ReturnType<typeof makeId>; |
| 307 | + }> = []; |
298 | 308 | if (batch.length > 1) { |
299 | | - await Promise.all( |
| 309 | + const results = await Promise.all( |
300 | 310 | batch.slice(1).map(async (task) => { |
301 | 311 | const r = await this.createTask(this.ctf.id, task); |
302 | 312 | const newTask = r?.data?.createTask?.task; |
303 | 313 | if (newTask) { |
304 | | - return this.addTagsForTask(task.tags, makeId(newTask.id)); |
| 314 | + return { task, taskId: makeId(newTask.id) }; |
305 | 315 | } |
| 316 | + return null; |
306 | 317 | }), |
307 | 318 | ); |
| 319 | + remainingTaskResults.push( |
| 320 | + ...results.filter( |
| 321 | + ( |
| 322 | + t, |
| 323 | + ): t is { task: ParsedTask; taskId: ReturnType<typeof makeId> } => |
| 324 | + t != null, |
| 325 | + ), |
| 326 | + ); |
| 327 | + } |
| 328 | +
|
| 329 | + // Now process all tag additions in parallel |
| 330 | + const tagPromises: Promise<any>[] = []; |
| 331 | + if (firstTaskResult) { |
| 332 | + tagPromises.push( |
| 333 | + this.addTagsForTask( |
| 334 | + firstTaskResult.task.tags, |
| 335 | + firstTaskResult.taskId as Id<Task>, |
| 336 | + ), |
| 337 | + ); |
| 338 | + } |
| 339 | + tagPromises.push( |
| 340 | + ...remainingTaskResults.map((result) => |
| 341 | + this.addTagsForTask(result.task.tags, result.taskId as Id<Task>), |
| 342 | + ), |
| 343 | + ); |
| 344 | +
|
| 345 | + if (tagPromises.length > 0) { |
| 346 | + await Promise.all(tagPromises); |
308 | 347 | } |
309 | 348 | } |
310 | 349 | }, |
|
0 commit comments