tod 0.11.2

An unofficial Todoist command-line client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
use futures::future;
use serde_json::{Number, Value, json};
use std::collections::HashMap;
use urlencoding::encode;
mod request;

use crate::comments::{Comment, CommentResponse};
use crate::config::Config;
use crate::debug::maybe_print;
use crate::errors::Error;
use crate::id::{self, Resource};
use crate::labels::{self, Label, LabelResponse};
use crate::oauth::{CLIENT_ID, CLIENT_SECRET};
use crate::projects::{Project, ProjectResponse};
use crate::sections::{Section, SectionResponse};
use crate::shell::execute_command;
use crate::tasks::priority::Priority;
use crate::tasks::{Task, TaskResponse};
use crate::users;
use crate::users::User;
use crate::{color, projects, sections, tasks, time};
use crate::{comments, oauth};
use regex::Regex;

// TODOIST URLS
pub const TASKS_URL: &str = "/api/v1/tasks/";
pub const COMMENTS_URL: &str = "/api/v1/comments/";
const SECTIONS_URL: &str = "/api/v1/sections";
const USER_URL: &str = "/api/v1/user";
const PROJECTS_URL: &str = "/api/v1/projects";
const LABELS_URL: &str = "/api/v1/labels";
const IDS_URL: &str = "/api/v1/id_mappings/";
const ACCESS_TOKEN_URL: &str = "/oauth/access_token";
pub const OAUTH_URL: &str = "/oauth/authorize";

/// Number of items that can be requested from API at once
pub const QUERY_LIMIT: u8 = 200;

/// Used to sanity check all the Todoist API endpoints to make sure that we are able to process the JSON payloads they are sending back.
pub async fn test_all_endpoints(config: &Config) -> Result<String, Error> {
    let name = "TEST".to_string();
    let date = time::date_string_today(config)?;
    let priority = Priority::None;
    let labels: Vec<String> = vec!["one".into(), "two".into()];

    println!("Creating project");
    let project = create_project(config, &name, &name, false, false).await?;

    println!("List projects");
    let _projects = all_projects(config, Some(1)).await?;

    println!("Creating section");
    let section = create_section(config, &name, &project, false).await?;

    println!("Creating task with add_task");
    let task = create_task(
        config,
        &name,
        &project,
        Some(section.clone()),
        priority.clone(),
        &name,
        None,
        &[],
    )
    .await?;

    println!("Getting sections for project");
    let _sections = all_sections_by_project(config, &project, Some(1)).await?;

    println!("Moving task to section");
    let _task = move_task_to_section(config, &task, &section, false).await?;

    println!("Getting task with get_task");
    let task = get_task(config, &task.id).await?;

    println!("Commenting on task twice");
    let _comment = create_comment(config, &task, &name, false).await?;

    let _comment = create_comment(config, &task, &name, false).await?;

    println!("Getting comments for task");
    let _comments = all_comments(config, &task, Some(1)).await?;

    println!("Deleting task");
    delete_task(config, &task, false).await?;

    println!("Creating two tasks with quick_add_task");
    let _task = quick_create_task(config, &name, None).await?;
    let task = quick_create_task(config, &name, Some(String::from("tomorrow"))).await?;

    println!("Finding tasks with tasks_for_project");
    let _tasks = all_tasks_by_project(config, &project, Some(1)).await?;

    println!("Finding tasks with tasks_for_filter");
    let _tasks = all_tasks_by_filter(config, "tod", Some(1)).await?;

    println!("Updating task priority");
    let _task = update_task_priority(config, &task, &priority, false).await?;

    println!("Updating task content");
    let _task = update_task_content(config, &task, &name, false).await?;

    println!("Updating task description");
    let _task = update_task_description(config, &task, &name, false).await?;

    println!("Updating task deadline");
    let _task = update_task_deadline(config, &task, Some(date), false).await?;

    println!("Updating task labels");
    let _task = update_task_labels(config, &task, labels, false).await?;

    println!("Adding task label");
    let _task = add_task_label(config, task.clone(), "three".into(), false).await?;

    println!("Updating task due with natural language");
    let _task =
        update_task_due_natural_language(config, &task, "today".into(), None, false).await?;

    println!("Moving task to project");
    let task = move_task_to_project(config, &task, &project, false).await?;

    println!("Completing task");
    let _task = complete_task(config, &task, false).await?;

    println!("Deleting task");
    delete_task(config, &task, false).await?;

    println!("Deleting project");
    delete_project(config, &project, false).await?;

    println!("List labels");
    let _labels = all_labels(config, false, Some(1)).await?;

    println!("Get user data");
    let _data = get_user_data(config).await?;

    Ok(color::green_string("Completed successfully"))
}

pub async fn get_v1_ids(
    config: &Config,
    resource: Resource,
    ids: Vec<String>,
) -> Result<Vec<String>, Error> {
    let ids = ids.join(",");
    let url = format!("{IDS_URL}{resource}/{ids}");
    let json = request::get_todoist(config, &url, true).await?;
    let ids = id::json_to_ids(json)?
        .into_iter()
        .map(|i| i.new_id)
        .collect();

    Ok(ids)
}

/// Add a new task to the inbox with natural language support
pub async fn quick_create_task(
    config: &Config,
    content: &str,
    reminder: Option<String>,
) -> Result<Task, Error> {
    let url = format!("{TASKS_URL}quick");
    let body = json!({"text": content, "auto_reminder": true, "reminder": reminder});

    let json = request::post_todoist(config, &url, body, true).await?;
    maybe_run_command(config.task_create_command.as_deref()).await;
    tasks::json_to_task(json)
}

pub async fn get_task(config: &Config, id: &str) -> Result<Task, Error> {
    let url = format!("{TASKS_URL}{id}");
    let json = request::get_todoist(config, &url, true).await?;
    tasks::json_to_task(json)
}

pub async fn get_access_token(config: &Config, code: &str) -> Result<String, Error> {
    let url = ACCESS_TOKEN_URL.to_string();
    let body = json!({"code": code, "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET});

    let json = request::post_todoist_no_token(config, &url, body, true).await?;

    oauth::json_to_access_token(json).map(|t| t.access_token)
}

/// Add Task without natural language support but supports additional parameters
#[allow(clippy::too_many_arguments)]
pub async fn create_task(
    config: &Config,
    content: &str,
    project: &Project,
    section: Option<Section>,
    priority: Priority,
    description: &str,
    due: Option<&str>,
    labels: &[String],
) -> Result<Task, Error> {
    let project_id = project.id.clone();
    let url = TASKS_URL;
    let mut body: HashMap<String, Value> = HashMap::new();
    body.insert("content".to_owned(), Value::String(content.to_owned()));
    body.insert(
        "description".to_owned(),
        Value::String(description.to_owned()),
    );
    body.insert("project_id".to_owned(), Value::String(project_id));

    body.insert("auto_reminder".to_owned(), Value::Bool(true));
    body.insert(
        "priority".to_owned(),
        Value::Number(Number::from(priority.to_integer())),
    );
    let labels = labels.iter().map(|l| Value::String(l.to_owned())).collect();
    body.insert("labels".to_owned(), Value::Array(labels));

    if let Some(date) = due {
        if time::is_date(date) || time::is_datetime(date) {
            body.insert("due_date".to_owned(), Value::String(date.to_owned()));
        } else {
            body.insert("due_string".to_owned(), Value::String(date.to_owned()));
        }
    }

    if let Some(section) = section {
        body.insert("section_id".to_owned(), Value::String(section.id.clone()));
    }

    let body = json!(body);

    let json = request::post_todoist(config, url, body, true).await?;
    maybe_run_command(config.task_create_command.as_deref()).await;
    tasks::json_to_task(json)
}

/// Get a vector of all tasks for a project
pub async fn all_tasks_by_project(
    config: &Config,
    project: &Project,
    limit: Option<u8>,
) -> Result<Vec<Task>, Error> {
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let project_id = project.id.clone();
    let mut tasks = Vec::new();
    let mut url = format!("{TASKS_URL}?project_id={project_id}&limit={limit}");
    let title_regex = config.task_exclude_regex.as_ref();

    loop {
        let json = request::get_todoist(config, &url, true).await?;
        let TaskResponse {
            results,
            next_cursor,
        } = tasks::json_to_tasks_response(json)?;

        let results = filter_tasks_by_title(results, title_regex, config);
        tasks.extend(results);

        match next_cursor {
            None => break,
            Some(cursor) => {
                url = format!("{TASKS_URL}?project_id={project_id}&limit={limit}&cursor={cursor}");
            }
        }
    }
    Ok(tasks)
}

/// Uses multiple filters (comma-separated) to fetch multiple lists of tasks in parallel. Returns each list of tasks with the filter query that was used to find it.
pub async fn all_tasks_by_filters(
    config: &Config,
    filter: &str,
) -> Result<Vec<(String, Vec<Task>)>, Error> {
    let filters: Vec<_> = filter
        .split(',')
        .map(|f| all_tasks_by_filter(config, f, None))
        .collect();

    let mut acc = Vec::new();
    for result in future::join_all(filters).await {
        acc.push(result?);
    }

    Ok(acc)
}

/// Fetches a list of tasks by a single filter query.
pub async fn all_tasks_by_filter(
    config: &Config,
    filter: &str,
    limit: Option<u8>,
) -> Result<(String, Vec<Task>), Error> {
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let encoded = encode(filter);
    let mut tasks: Vec<Task> = Vec::new();
    let mut url = format!("{TASKS_URL}filter?query={encoded}&limit={limit}");
    let title_regex = config.task_exclude_regex.as_ref();

    loop {
        let json = request::get_todoist(config, &url, true).await?;
        let TaskResponse {
            results,
            next_cursor,
        } = tasks::json_to_tasks_response(json)?;

        let results = filter_tasks_by_title(results, title_regex, config);
        tasks.extend(results);

        match next_cursor {
            None => break,
            Some(string) => {
                url = format!("{TASKS_URL}filter?query={encoded}&limit={limit}&cursor={string}");
            }
        };
    }

    Ok((filter.to_string(), tasks))
}

pub async fn all_sections_by_project(
    config: &Config,
    project: &Project,
    limit: Option<u8>,
) -> Result<Vec<Section>, Error> {
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let project_id = project.id.clone();
    let mut url = format!("{SECTIONS_URL}?project_id={project_id}&limit={limit}");
    let mut sections: Vec<Section> = Vec::new();

    loop {
        let json = request::get_todoist(config, &url, true).await?;
        let SectionResponse {
            results,
            next_cursor,
        } = sections::json_to_sections_response(&json)?;
        sections.extend(results);
        match next_cursor {
            None => break,
            Some(string) => {
                url =
                    format!("{SECTIONS_URL}?project_id={project_id}&limit={limit}&cursor={string}");
            }
        };
    }
    Ok(sections)
}

pub async fn all_projects(config: &Config, limit: Option<u8>) -> Result<Vec<Project>, Error> {
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let mut url = format!("{PROJECTS_URL}?limit={limit}");
    let mut projects: Vec<Project> = Vec::new();

    loop {
        let json = request::get_todoist(config, &url, true).await?;
        let ProjectResponse {
            results,
            next_cursor,
        } = projects::json_to_projects_response(json)?;
        projects.extend(results);
        match next_cursor {
            None => break,
            Some(string) => {
                url = format!("{PROJECTS_URL}?limit={limit}&cursor={string}");
            }
        };
    }
    Ok(projects)
}

pub async fn all_labels(
    config: &Config,
    spinner: bool,
    limit: Option<u8>,
) -> Result<Vec<Label>, Error> {
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let mut url = format!("{LABELS_URL}?limit={limit}");
    let mut labels: Vec<Label> = Vec::new();
    loop {
        let json = request::get_todoist(config, &url, spinner).await?;
        let LabelResponse {
            results,
            next_cursor,
        } = labels::json_to_labels_response(&json)?;
        labels.extend(results);
        match next_cursor {
            None => break,
            Some(string) => {
                url = format!("{LABELS_URL}?limit={limit}&cursor={string}");
            }
        }
    }
    Ok(labels)
}

/// Move an task to a different project
pub async fn move_task_to_project(
    config: &Config,
    task: &Task,
    project: &Project,
    spinner: bool,
) -> Result<Task, Error> {
    let project_id = project.id.clone();
    let task_id = task.id.clone();
    let body = json!({"project_id": project_id});
    let url = format!("{TASKS_URL}{task_id}/move");

    let response = request::post_todoist(config, &url, body, spinner).await?;
    tasks::json_to_task(response)
}

pub async fn move_task_to_section(
    config: &Config,
    task: &Task,
    section: &Section,
    spinner: bool,
) -> Result<Task, Error> {
    let section_id = section.id.clone();
    let task_id = task.id.clone();
    let body = json!({"section_id": section_id});
    let url = format!("{TASKS_URL}{task_id}/move");

    let response = request::post_todoist(config, &url, body, spinner).await?;
    tasks::json_to_task(response)
}

/// Update the priority of an task by ID
pub async fn update_task_priority(
    config: &Config,
    task: &Task,
    priority: &Priority,
    spinner: bool,
) -> Result<String, Error> {
    let body = json!({ "priority": priority });
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back an task
    Ok("".into())
}

/// Add a label to task by ID
pub async fn add_task_label(
    config: &Config,
    task: Task,
    label: String,
    spinner: bool,
) -> Result<String, Error> {
    let mut labels = task.labels;
    labels.push(label);
    let body = json!({ "labels": labels});
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back an task
    Ok("".into())
}

/// Update due date for task using natural language
pub async fn update_task_due_natural_language(
    config: &Config,
    task: &Task,
    due_string: String,
    duration: Option<u32>,
    spinner: bool,
) -> Result<String, Error> {
    let due_string = if let Some(due) = &task.due {
        if task.is_recurring() {
            format!("{} starting {due_string}", due.string)
        } else {
            due_string
        }
    } else {
        due_string
    };

    let body = if let Some(duration) = duration {
        json!({ "due_string": due_string, "duration": duration, "duration_unit": "minute" })
    } else {
        json!({ "due_string": due_string })
    };
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back a task
    Ok("".into())
}

/// Update the content of a task by ID
pub async fn update_task_content(
    config: &Config,
    task: &Task,
    content: &str,
    spinner: bool,
) -> Result<String, Error> {
    let body = json!({ "content": content});
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back a task
    Ok("".into())
}

/// Update the content of a task by ID
pub async fn update_task_deadline(
    config: &Config,
    task: &Task,
    date: Option<String>,
    spinner: bool,
) -> Result<String, Error> {
    let body = match date {
        Some(date) => {
            if !time::is_date(&date) {
                return Err(Error {
                    message: "Not a valid date in format YYYY-MM-DD, got: {date}".to_string(),
                    source: "update_task_deadline".to_string(),
                });
            }
            json!({"deadline_date": date, "deadline_lang": "en"})
        }
        None => json!({"deadline_date": null, "deadline_lang": null}),
    };
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back a task
    Ok("".into())
}

/// Update the description of a task by ID
pub async fn update_task_description(
    config: &Config,
    task: &Task,
    description: &str,
    spinner: bool,
) -> Result<String, Error> {
    let body = json!({ "description": description});
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back a task
    Ok("".into())
}

/// Update the labels of a task by ID
/// Replaces the old labels
pub async fn update_task_labels(
    config: &Config,
    task: &Task,
    labels: Vec<String>,
    spinner: bool,
) -> Result<String, Error> {
    let body = json!({ "labels": labels});
    let url = format!("{}{}", TASKS_URL, task.id);

    request::post_todoist(config, &url, body, spinner).await?;
    // Does not pass back a task
    Ok("".into())
}

/// Complete the last task returned by "next task"
/// The API does not return any data, so we can't return a new task
pub async fn complete_task(config: &Config, task: &Task, spinner: bool) -> Result<String, Error> {
    let task_id = task.id.clone();
    let url = format!("{TASKS_URL}{task_id}/close");

    request::post_todoist(config, &url, Value::Null, spinner).await?;

    if !cfg!(test) {
        maybe_run_command(config.task_complete_command.as_deref()).await;
        config.reload().await?.clear_next_task().save().await?;
    }
    // Execute the execute_command() complete_task_command if set in config

    // API does not pass back a task
    Ok("".into())
}

pub async fn delete_task(config: &Config, task: &Task, spinner: bool) -> Result<String, Error> {
    let body = json!({});
    let url = format!("{}{}", TASKS_URL, task.id);

    request::delete_todoist(config, &url, body, spinner).await?;
    Ok("".into())
}

pub async fn delete_project(
    config: &Config,
    project: &Project,
    spinner: bool,
) -> Result<String, Error> {
    let url = format!("{}/{}", PROJECTS_URL, project.id);
    let body = json!({});

    request::delete_todoist(config, &url, body, spinner).await?;
    Ok("".into())
}
pub async fn create_project(
    config: &Config,
    name: &str,
    description: &str,
    is_favorite: bool,
    spinner: bool,
) -> Result<Project, Error> {
    let url = PROJECTS_URL.to_string();
    let body = json!({"name": name, "description": description, "is_favorite": is_favorite});

    let json = request::post_todoist(config, &url, body, spinner).await?;
    projects::json_to_project(json)
}

pub async fn create_section(
    config: &Config,
    name: &str,
    project: &Project,
    spinner: bool,
) -> Result<Section, Error> {
    let url = SECTIONS_URL.to_string();
    let body = json!({"name": name, "project_id": project.id});

    let json = request::post_todoist(config, &url, body, spinner).await?;
    sections::json_to_section(&json)
}

pub async fn create_comment(
    config: &Config,
    task: &Task,
    content: &str,
    spinner: bool,
) -> Result<Comment, Error> {
    let task_id = task.id.clone();
    let body = json!({"task_id": task_id, "content": content});
    let url = COMMENTS_URL.to_string();

    let response = request::post_todoist(config, &url, body, spinner).await?;
    maybe_run_command(config.task_comment_command.as_deref()).await;
    comments::json_to_comment(response)
}

pub async fn get_user_data(config: &Config) -> Result<User, Error> {
    let url = USER_URL.to_string();
    let json = request::get_todoist(config, &url, true).await?;
    users::json_to_user(&json)
}

/// Returns all of the comments for a task from the Todoist JSON API
/// Paginates through the results until all comments are retrieved.
/// Then will filter out deleted and excluded comments based on the Regex Config.
pub async fn all_comments(
    config: &Config,
    task: &Task,
    limit: Option<u8>,
) -> Result<Vec<Comment>, Error> {
    let task_id = &task.id;
    let limit = limit.unwrap_or(QUERY_LIMIT);
    let mut url = format!("{COMMENTS_URL}?task_id={task_id}&limit={limit}");
    let mut comments: Vec<Comment> = Vec::new();

    let exclude_regex = config.comment_exclude_regex.as_ref();

    loop {
        let json = request::get_todoist(config, &url, true).await?;
        let CommentResponse {
            results,
            next_cursor,
        } = comments::json_to_comment_response(json)?;

        comments.extend(results.into_iter().filter(|c| {
            !c.is_deleted
                && match exclude_regex {
                    Some(regex) => !regex.is_match(&c.content),
                    None => true,
                }
        }));

        match next_cursor {
            None => break,
            Some(cursor) => {
                url =
                    format!("{COMMENTS_URL}?task_id={task_id}&limit={QUERY_LIMIT}&cursor={cursor}");
            }
        };
    }

    Ok(comments)
}

// Executes a CLI command (if set in the configuration).
async fn maybe_run_command(command: Option<&str>) {
    if let Some(command) = command {
        execute_command(command);
    }
}

/// Filters (Excludes) tasks based on task title and configured task_exclude_regex
pub fn filter_tasks_by_title(
    tasks: Vec<Task>,
    regex: Option<&Regex>,
    config: &Config,
) -> Vec<Task> {
    match regex {
        Some(re) => tasks
            .into_iter()
            .filter(|task| {
                let exclude = re.is_match(&task.content);
                if exclude {
                    maybe_print(
                        config,
                        &format!("Task '{}' excluded by regex", task.content),
                    );
                }
                !exclude // exclude matching tasks
            })
            .collect(),
        None => tasks,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tasks::priority::{self, Priority};
    use crate::test;
    use crate::test::responses::ResponseFromFile;
    use crate::test_time::FixedTimeProvider;
    use crate::time::TimeProviderEnum;
    use crate::users::TzInfo;
    use pretty_assertions::assert_eq;

    #[tokio::test]
    async fn test_get_user_data() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/user")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::User.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        assert_eq!(
            get_user_data(&config).await,
            Ok(User {
                tz_info: TzInfo {
                    timezone: "America/Vancouver".to_string()
                }
            })
        );
        mock.assert();
    }

    #[tokio::test]
    async fn test_quick_create_task() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/tasks/quick")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .with_time_provider(TimeProviderEnum::Fixed(FixedTimeProvider));

        assert_eq!(
            quick_create_task(&config, "testy test", None).await,
            Ok(test::fixtures::today_task().await)
        );
        mock.assert();
    }

    #[tokio::test]
    async fn test_all_labels() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/labels?limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Labels.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        assert_eq!(
            all_labels(&config, false, None).await,
            Ok(vec![test::fixtures::label()])
        );
        mock.assert();
    }

    #[tokio::test]
    async fn test_create_task() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/tasks/")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .with_time_provider(TimeProviderEnum::Fixed(FixedTimeProvider));

        let project = test::fixtures::project();

        let priority = priority::Priority::None;
        let section = test::fixtures::section();
        assert_eq!(
            create_task(
                &config,
                "New task",
                &project,
                Some(section),
                priority,
                "",
                None,
                &[]
            )
            .await,
            Ok(test::fixtures::today_task().await)
        );
        mock.assert();
    }
    #[tokio::test]
    async fn test_create_section() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/sections")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Section.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let project = test::fixtures::project();

        assert_eq!(
            create_section(&config, "New task", &project, false).await,
            Ok(test::fixtures::section())
        );
        mock.assert();
    }

    #[tokio::test]
    async fn test_create_comment() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/comments/")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Comment.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());
        let task = test::fixtures::today_task().await;
        let comment = test::fixtures::comment();
        assert_eq!(
            create_comment(&config, &task, "New comment", true).await,
            Ok(comment)
        );
        mock.assert();
    }

    #[tokio::test]
    async fn test_all_tasks_by_project() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTasks.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());
        let config_with_timezone = config.with_timezone("US/Pacific");
        let binding = config_with_timezone
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously");
        let project = binding
            .first()
            .expect("Expected at least one project in binding");

        assert_eq!(
            all_tasks_by_project(&config_with_timezone, project, None).await,
            Ok(vec![test::fixtures::today_task().await])
        );

        mock.assert();
    }

    #[tokio::test]
    async fn test_complete_task() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/tasks/6Xqhv4cwxgjwG9w8/close")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let task = test::fixtures::today_task().await;
        let response = complete_task(&config, &task, false)
            .await
            .expect("Did not complete task");
        mock.assert();
        assert_eq!(response, String::from(""));
    }

    #[tokio::test]
    async fn test_move_task_to_project() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/api/v1/tasks/6Xqhv4cwxgjwG9w8/move")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let task = test::fixtures::today_task().await;
        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .with_time_provider(TimeProviderEnum::Fixed(FixedTimeProvider));

        let binding = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously");
        let project = binding
            .first()
            .expect("Expected at least one project in binding");
        let response = move_task_to_project(&config, &task, project, false)
            .await
            .expect("Could not move task to project");

        assert_eq!(response, task);
        mock.assert();
    }
    #[tokio::test]
    async fn test_move_task_to_section() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/api/v1/tasks/6Xqhv4cwxgjwG9w8/move")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let task = test::fixtures::today_task().await;
        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .with_time_provider(TimeProviderEnum::Fixed(FixedTimeProvider));

        let section = test::fixtures::section();
        let response = move_task_to_section(&config, &task, &section, false)
            .await
            .expect("Could not move task to section");

        assert_eq!(response, task);
        mock.assert();
    }

    #[tokio::test]
    async fn test_delete_task() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("DELETE", "/api/v1/tasks/6Xqhv4cwxgjwG9w8")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let task = test::fixtures::today_task().await;
        let config = test::fixtures::config().await.with_mock_url(server.url());

        let response = delete_task(&config, &task, false).await;
        mock.assert();

        assert_eq!(response, Ok(String::from("")));
    }

    #[tokio::test]
    async fn test_get_task() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/api/v1/tasks/5149481867")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let response = get_task(&config, "5149481867")
            .await
            .expect("could not get task");
        mock.assert();

        assert_eq!(response.id, String::from("6Xqhv4cwxgjwG9w8"));
        assert_eq!(response.project_id, String::from("6VRRxv8CM6GVmmgf"));
    }

    #[tokio::test]
    async fn test_forbidden() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/api/v1/tasks/5149481867")
            .with_status(403)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let error = get_task(&config, "5149481867").await.unwrap_err();
        mock.assert();
        assert_eq!(error.source, String::from("reqwest"));
        assert_eq!(
            error.message,
            String::from(
                "Unauthorized or Forbidden response from Todoist\nRun tod auth login to reauthenticate"
            )
        );
    }

    #[tokio::test]
    async fn test_update_task_priority() {
        let task = test::fixtures::today_task().await;
        let url: &str = &format!("{}{}", "/api/v1/tasks/", task.id);
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", url)
            .with_status(204)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let response = update_task_priority(&config, &task, &Priority::High, true).await;
        mock.assert();
        assert_eq!(response, Ok(String::from("")));
    }

    #[tokio::test]
    async fn test_update_task_due_natural_language() {
        let task = test::fixtures::today_task().await;
        let url: &str = &format!("{}{}", "/api/v1/tasks/", task.id);
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", url)
            .with_status(204)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTasks.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let response =
            update_task_due_natural_language(&config, &task, "today".to_string(), None, true).await;
        mock.assert();
        assert_eq!(response, Ok(String::from("")));
    }

    #[tokio::test]
    async fn test_all_comments_filters_deleted() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock(
                "GET",
                "/api/v1/comments/?task_id=6Xqhv4cwxgjwG9w8&limit=200",
            )
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::CommentsAllTypes.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let task = test::fixtures::today_task().await;

        let comments = all_comments(&config, &task, None)
            .await
            .expect("Could not get all comments");
        mock.assert();

        assert_eq!(comments.len(), 7); // One comment in the JSON is_deleted = true
        assert!(comments.iter().all(|c| !c.is_deleted));
    }
    #[tokio::test]
    async fn test_task_is_filtered_out_by_regex() {
        let mut task = test::fixtures::today_task().await;
        task.content = "Brush Teeth".to_string();

        let mut config = test::fixtures::config().await;
        config.task_exclude_regex =
            Some(regex::Regex::new(r"^Brush").expect("Could not create regex"));

        let result = filter_tasks_by_title(vec![task], config.task_exclude_regex.as_ref(), &config);
        assert!(result.is_empty(), "Expected task to be excluded by regex");
    }

    #[tokio::test]
    async fn test_task_is_retained_if_not_matching_regex() {
        let mut task = test::fixtures::today_task().await;
        task.content = "Eat Breakfast".to_string();

        let mut config = test::fixtures::config().await;
        config.task_exclude_regex =
            Some(regex::Regex::new(r"^Brush").expect("Could not create regex"));

        let result = filter_tasks_by_title(
            vec![task.clone()],
            config.task_exclude_regex.as_ref(),
            &config,
        );
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].content, task.content);
    }
}