Преглед на файлове

tau: add nickname for tasks and attached to comments

ghassmo преди 4 години
родител
ревизия
95fab04625

+ 3 - 3
bin/tau/tau-cli/src/jsonrpc.rs

@@ -84,10 +84,10 @@ pub async fn get_state(url: &str, id: u64) -> Result<Value> {
 }
 }
 
 
 // Set comment for a task and returns `true` upon success.
 // Set comment for a task and returns `true` upon success.
-// --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_author, comment_content], "id": 1}
+// --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_content], "id": 1}
 // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
 // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
-pub async fn set_comment(url: &str, id: u64, author: &str, content: &str) -> Result<Value> {
-    let req = jsonrpc::request(json!("set_comment"), json!([id, author, content]));
+pub async fn set_comment(url: &str, id: u64, content: &str) -> Result<Value> {
+    let req = jsonrpc::request(json!("set_comment"), json!([id, content]));
     request(req, url.to_string()).await
     request(req, url.to_string()).await
 }
 }
 
 

+ 6 - 8
bin/tau/tau-cli/src/main.rs

@@ -68,8 +68,8 @@ async fn start(mut options: CliTau) -> Result<()> {
             let rank = rank.unwrap_or(0.0);
             let rank = rank.unwrap_or(0.0);
 
 
             add(rpc_addr,
             add(rpc_addr,
-                            json!([{"title": title, "desc": desc, "assign": assign, "project": project, "due": due, "rank": rank}]),
-                            ).await?;
+                json!([{"title": title, "desc": desc, "assign": assign, "project": project, "due": due, "rank": rank}]),
+                ).await?;
         }
         }
 
 
         Some(CliTauSubCommands::Update { id, key, value }) => {
         Some(CliTauSubCommands::Update { id, key, value }) => {
@@ -111,18 +111,16 @@ async fn start(mut options: CliTau) -> Result<()> {
             }
             }
         },
         },
 
 
-        Some(CliTauSubCommands::Comment { id, author, content }) => match (author, content) {
-            (Some(author), Some(content)) => {
-                set_comment(rpc_addr, id, author.trim(), content.trim()).await?;
+        Some(CliTauSubCommands::Comment { id, content }) => match content {
+            Some(content) => {
+                set_comment(rpc_addr, id, content.trim()).await?;
             }
             }
-            (None, None) => {
+            None => {
                 let rep = get_by_id(rpc_addr, id).await?;
                 let rep = get_by_id(rpc_addr, id).await?;
                 let comments = get_comments(rep)?;
                 let comments = get_comments(rep)?;
 
 
                 println!("Comments on Task with id {}:\n{}", id, comments);
                 println!("Comments on Task with id {}:\n{}", id, comments);
             }
             }
-            (None, Some(_)) => error!("Please provide the author name"),
-            (Some(_), None) => error!("Please provide some content"),
         },
         },
 
 
         Some(CliTauSubCommands::List {}) | None => {
         Some(CliTauSubCommands::List {}) | None => {

+ 2 - 2
bin/tau/tau-cli/src/util.rs

@@ -58,8 +58,6 @@ pub enum CliTauSubCommands {
     Comment {
     Comment {
         /// Task ID
         /// Task ID
         id: u64,
         id: u64,
-        /// Comment author
-        author: Option<String>,
         /// Comment content
         /// Comment content
         content: Option<String>,
         content: Option<String>,
     },
     },
@@ -73,6 +71,7 @@ pub struct TaskInfo {
     pub id: u32,
     pub id: u32,
     pub title: String,
     pub title: String,
     pub desc: String,
     pub desc: String,
+    pub owner: String,
     pub assign: Vec<String>,
     pub assign: Vec<String>,
     pub project: Vec<String>,
     pub project: Vec<String>,
     pub due: Option<i64>,
     pub due: Option<i64>,
@@ -190,6 +189,7 @@ pub fn desc_in_editor() -> Result<Option<String>> {
 pub fn show_task(task: Value, taskinfo: TaskInfo, current_state: String) -> Result<()> {
 pub fn show_task(task: Value, taskinfo: TaskInfo, current_state: String) -> Result<()> {
     let mut table = table!([Bd => "ref_id", &taskinfo.ref_id],
     let mut table = table!([Bd => "ref_id", &taskinfo.ref_id],
                                             ["id", &taskinfo.id.to_string()],
                                             ["id", &taskinfo.id.to_string()],
+                                            [Bd =>"owner", &taskinfo.owner],
                                             [Bd =>"title", &taskinfo.title],
                                             [Bd =>"title", &taskinfo.title],
                                             ["desc", &taskinfo.desc],
                                             ["desc", &taskinfo.desc],
                                             [Bd =>"assign", get_from_task(task.clone(), "assign")?],
                                             [Bd =>"assign", get_from_task(task.clone(), "assign")?],

+ 16 - 9
bin/tau/taud/src/jsonrpc.rs

@@ -24,10 +24,11 @@ use crate::{
 pub struct JsonRpcInterface {
 pub struct JsonRpcInterface {
     dataset_path: PathBuf,
     dataset_path: PathBuf,
     notify_queue_sender: async_channel::Sender<Option<TaskInfo>>,
     notify_queue_sender: async_channel::Sender<Option<TaskInfo>>,
+    nickname: String,
 }
 }
 
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 #[derive(Clone, Debug, Serialize, Deserialize)]
-pub struct BaseTaskInfo {
+struct BaseTaskInfo {
     title: String,
     title: String,
     desc: String,
     desc: String,
     assign: Vec<String>,
     assign: Vec<String>,
@@ -70,8 +71,9 @@ impl JsonRpcInterface {
     pub fn new(
     pub fn new(
         notify_queue_sender: async_channel::Sender<Option<TaskInfo>>,
         notify_queue_sender: async_channel::Sender<Option<TaskInfo>>,
         dataset_path: PathBuf,
         dataset_path: PathBuf,
+        nickname: String,
     ) -> Self {
     ) -> Self {
-        Self { notify_queue_sender, dataset_path }
+        Self { notify_queue_sender, dataset_path, nickname }
     }
     }
 
 
     // RPCAPI:
     // RPCAPI:
@@ -94,8 +96,14 @@ impl JsonRpcInterface {
         let args = params.as_array().unwrap();
         let args = params.as_array().unwrap();
 
 
         let task: BaseTaskInfo = serde_json::from_value(args[0].clone())?;
         let task: BaseTaskInfo = serde_json::from_value(args[0].clone())?;
-        let mut new_task: TaskInfo =
-            TaskInfo::new(&task.title, &task.desc, task.due, task.rank, &self.dataset_path)?;
+        let mut new_task: TaskInfo = TaskInfo::new(
+            &task.title,
+            &task.desc,
+            &self.nickname,
+            task.due,
+            task.rank,
+            &self.dataset_path,
+        )?;
         new_task.set_project(&task.project);
         new_task.set_project(&task.project);
         new_task.set_assign(&task.assign);
         new_task.set_assign(&task.assign);
 
 
@@ -174,21 +182,20 @@ impl JsonRpcInterface {
 
 
     // RPCAPI:
     // RPCAPI:
     // Set comment for a task and returns `true` upon success.
     // Set comment for a task and returns `true` upon success.
-    // --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_author, comment_content], "id": 1}
+    // --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_content], "id": 1}
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     async fn set_comment(&self, params: Value) -> TaudResult<Value> {
     async fn set_comment(&self, params: Value) -> TaudResult<Value> {
         debug!(target: "tau", "JsonRpc::set_comment() params {}", params);
         debug!(target: "tau", "JsonRpc::set_comment() params {}", params);
         let args = params.as_array().unwrap();
         let args = params.as_array().unwrap();
 
 
-        if args.len() != 3 {
+        if args.len() != 2 {
             return Err(TaudError::InvalidData("len of params should be 3".into()))
             return Err(TaudError::InvalidData("len of params should be 3".into()))
         }
         }
 
 
-        let comment_author: String = serde_json::from_value(args[1].clone())?;
-        let comment_content: String = serde_json::from_value(args[2].clone())?;
+        let comment_content: String = serde_json::from_value(args[1].clone())?;
 
 
         let mut task: TaskInfo = self.load_task_by_id(&args[0])?;
         let mut task: TaskInfo = self.load_task_by_id(&args[0])?;
-        task.set_comment(Comment::new(&comment_content, &comment_author));
+        task.set_comment(Comment::new(&comment_content, &self.nickname));
 
 
         self.notify_queue_sender.send(Some(task)).await.map_err(Error::from)?;
         self.notify_queue_sender.send(Some(task)).await.map_err(Error::from)?;
         Ok(json!(true))
         Ok(json!(true))

+ 22 - 10
bin/tau/taud/src/main.rs

@@ -1,5 +1,5 @@
 use async_std::sync::{Arc, Mutex};
 use async_std::sync::{Arc, Mutex};
-use std::fs::create_dir_all;
+use std::{env, fs::create_dir_all};
 
 
 use async_executor::Executor;
 use async_executor::Executor;
 use crypto_box::{aead::Aead, Box, SecretKey, KEY_SIZE};
 use crypto_box::{aead::Aead, Box, SecretKey, KEY_SIZE};
@@ -86,6 +86,14 @@ async_daemonize!(realmain);
 async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
 async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
     let datastore_path = expand_path(&settings.datastore)?;
     let datastore_path = expand_path(&settings.datastore)?;
 
 
+    let nickname =
+        if settings.nickname.is_some() { settings.nickname } else { env::var("USER").ok() };
+
+    if nickname.is_none() {
+        error!("Provide a nickname in config file");
+        return Ok(())
+    }
+
     // mkdir datastore_path if not exists
     // mkdir datastore_path if not exists
     create_dir_all(datastore_path.join("month"))?;
     create_dir_all(datastore_path.join("month"))?;
     create_dir_all(datastore_path.join("task"))?;
     create_dir_all(datastore_path.join("task"))?;
@@ -99,14 +107,17 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
         save::<String>(&datastore_path.join("secret_key"), &sk_string)?;
         save::<String>(&datastore_path.join("secret_key"), &sk_string)?;
         secret
         secret
     } else {
     } else {
-        let loaded_key = match load::<String>(&datastore_path.join("secret_key")) {
-            Ok(key) => key,
-            Err(_) => {
-                error!("Could not load secret key from file, please run \"taud --help\" for more information");
-                return Ok(())
-            }
-        };
-        let sk_bytes = hex::decode(loaded_key)?;
+        let loaded_key = load::<String>(&datastore_path.join("secret_key"));
+
+        if loaded_key.is_err() {
+            error!(
+                "Could not load secret key from file, \
+                 please run \"taud --help\" for more information"
+            );
+            return Ok(())
+        }
+
+        let sk_bytes = hex::decode(loaded_key.unwrap())?;
         let sk_bytes: [u8; KEY_SIZE] = sk_bytes.as_slice().try_into()?;
         let sk_bytes: [u8; KEY_SIZE] = sk_bytes.as_slice().try_into()?;
         SecretKey::try_from(sk_bytes)?
         SecretKey::try_from(sk_bytes)?
     };
     };
@@ -124,7 +135,8 @@ async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
 
 
     let (rpc_snd, rpc_rcv) = async_channel::unbounded::<Option<TaskInfo>>();
     let (rpc_snd, rpc_rcv) = async_channel::unbounded::<Option<TaskInfo>>();
 
 
-    let rpc_interface = Arc::new(JsonRpcInterface::new(rpc_snd, datastore_path.clone()));
+    let rpc_interface =
+        Arc::new(JsonRpcInterface::new(rpc_snd, datastore_path.clone(), nickname.unwrap()));
 
 
     let executor_cloned = executor.clone();
     let executor_cloned = executor.clone();
     let rpc_listener_task =
     let rpc_listener_task =

+ 4 - 2
bin/tau/taud/src/month_tasks.rs

@@ -154,7 +154,8 @@ mod tests {
         // load and save TaskInfo
         // load and save TaskInfo
         ///////////////////////
         ///////////////////////
 
 
-        let mut task = TaskInfo::new("test_title", "test_desc", None, 0.0, &dataset_path)?;
+        let mut task =
+            TaskInfo::new("test_title", "test_desc", "NICKNAME", None, 0.0, &dataset_path)?;
 
 
         task.save(&dataset_path)?;
         task.save(&dataset_path)?;
 
 
@@ -194,7 +195,8 @@ mod tests {
         // activate task
         // activate task
         ///////////////////////
         ///////////////////////
 
 
-        let task = TaskInfo::new("test_title_3", "test_desc", None, 0.0, &dataset_path)?;
+        let task =
+            TaskInfo::new("test_title_3", "test_desc", "NICKNAME", None, 0.0, &dataset_path)?;
 
 
         task.save(&dataset_path)?;
         task.save(&dataset_path)?;
 
 

+ 3 - 0
bin/tau/taud/src/settings.rs

@@ -31,4 +31,7 @@ pub struct Args {
     /// Generate a new secret key
     /// Generate a new secret key
     #[structopt(long)]
     #[structopt(long)]
     pub key_gen: bool,
     pub key_gen: bool,
+    /// Current display name    
+    #[structopt(long)]
+    pub nickname: Option<String>,
 }
 }

+ 3 - 0
bin/tau/taud/src/task_info.rs

@@ -53,6 +53,7 @@ pub struct TaskInfo {
     id: u32,
     id: u32,
     title: String,
     title: String,
     desc: String,
     desc: String,
+    owner: String,
     assign: TaskAssigns,
     assign: TaskAssigns,
     project: TaskProjects,
     project: TaskProjects,
     due: Option<Timestamp>,
     due: Option<Timestamp>,
@@ -66,6 +67,7 @@ impl TaskInfo {
     pub fn new(
     pub fn new(
         title: &str,
         title: &str,
         desc: &str,
         desc: &str,
+        owner: &str,
         due: Option<Timestamp>,
         due: Option<Timestamp>,
         rank: f32,
         rank: f32,
         dataset_path: &Path,
         dataset_path: &Path,
@@ -91,6 +93,7 @@ impl TaskInfo {
             id,
             id,
             title: title.into(),
             title: title.into(),
             desc: desc.into(),
             desc: desc.into(),
+            owner: owner.into(),
             assign: TaskAssigns(vec![]),
             assign: TaskAssigns(vec![]),
             project: TaskProjects(vec![]),
             project: TaskProjects(vec![]),
             due,
             due,

+ 3 - 0
bin/tau/taud_config.toml

@@ -4,6 +4,9 @@
 ## Sets Datastore Path
 ## Sets Datastore Path
 #datastore="~/.config/tau"
 #datastore="~/.config/tau"
 
 
+## Current display name    
+#nickname="NICKNAME"
+
 ## Raft net settings
 ## Raft net settings
 [net]
 [net]
 ## P2P accept address
 ## P2P accept address