From b0463742809288668b3b4f6ca3b6b59be57c59da Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 6 Jun 2024 15:28:49 +0200 Subject: [PATCH] add assign + login --- src/api/mod.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/args.rs | 16 ++++++++++++++++ src/main.rs | 19 +++++++++++++++++++ src/ui/task.rs | 10 ++++++++-- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index eb6e2e7..142e6f1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -259,4 +259,46 @@ impl VikunjaAPI { ); serde_json::from_str(&resp).unwrap() } + + pub fn login(&self, username: &str, password: &str, totp: Option<&str>) -> String { + let resp = self.post_request( + "/login", + &serde_json::json!({ + "username": username, + "password": password, + "totp_passcode": totp + }), + ); + + let val: serde_json::Value = serde_json::from_str(&resp).unwrap(); + val.as_object() + .unwrap() + .get("token") + .unwrap() + .as_str() + .unwrap() + .to_string() + } + + pub fn search_user(&self, search: &str) -> Option> { + let resp = self.get_request(&format!("/users?s={search}")); + serde_json::from_str(&resp).ok() + } + + pub fn assign_to_task(&self, user: &str, task_id: isize) { + let user = self.search_user(user).unwrap(); + + self.put_request( + &format!("/tasks/{task_id}/assignees"), + &serde_json::json!({ + "user_id": user.first().unwrap().id + }), + ); + } + + pub fn remove_assign_to_task(&self, user: &str, task_id: isize) { + let user = self.search_user(user).unwrap(); + let user_id = user.first().unwrap().id; + self.delete_request(&format!("/tasks/{task_id}/assignees/{user_id}")); + } } diff --git a/src/args.rs b/src/args.rs index d88ae8d..98463f5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -32,6 +32,22 @@ pub fn get_args() -> clap::ArgMatches { .default_value("Inbox"), ), ) + .subcommand( + command!() + .name("login") + .about("Get a JWT Token for authentication") + .arg(arg!(-u --username "Username").required(true)) + .arg(arg!(-p --password "Password").required(true)) + .arg(arg!(--totp "TOTP Code").required(false)), + ) + .subcommand( + command!() + .name("assign") + .about("Assign a user to a task") + .arg(arg!(-u --undo "Remove label from task").required(false)) + .arg(arg!([user] "User").required(true)) + .arg(arg!([task_id] "Task ID").required(true)), + ) .subcommand( command!() .name("label") diff --git a/src/main.rs b/src/main.rs index 7a31e08..3411e97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,14 @@ fn main() { let task_id: &String = task_info_arg.get_one("task_id").unwrap(); ui::task::print_task_info(task_id.parse().unwrap(), &api); } + Some(("login", login_arg)) => { + let username: &String = login_arg.get_one("username").unwrap(); + let password: &String = login_arg.get_one("password").unwrap(); + let totp: Option<&String> = login_arg.get_one("totp"); + + let token = api.login(username, password, totp.map(|x| x.as_str())); + println!("\"token\" = \"{token}\""); + } Some(("prj", prj_arg)) => match prj_arg.subcommand() { Some(("ls", _)) => { ui::project::list_projects(&api); @@ -25,6 +33,17 @@ fn main() { ui::project::list_projects(&api); } }, + Some(("assign", assign_arg)) => { + let user: &String = assign_arg.get_one("user").unwrap(); + let task_id: &String = assign_arg.get_one("task_id").unwrap(); + let undo = assign_arg.get_flag("undo"); + + if undo { + api.remove_assign_to_task(user, task_id.parse().unwrap()); + } else { + api.assign_to_task(user, task_id.parse().unwrap()); + } + } Some(("labels", label_args)) => match label_args.subcommand() { Some(("ls", _)) => { ui::print_all_labels(&api); diff --git a/src/ui/task.rs b/src/ui/task.rs index f7b484b..a7db370 100644 --- a/src/ui/task.rs +++ b/src/ui/task.rs @@ -135,7 +135,13 @@ pub fn print_task_info(task_id: isize, api: &VikunjaAPI) { println!("---\n{}", task.description); } - //pub assignees: Option>, - //pub labels: Option>, + if let Some(assigned) = task.assignees { + print!("Assigned to: "); + for assignee in assigned { + print!("{} ", assignee.username); + } + println!(); + } + // pub percent_done: f64, }