rpc.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{rpc::jsonrpc::JsonRequest, Result};
  19. use log::debug;
  20. use tinyjson::JsonValue;
  21. use crate::{
  22. primitives::{BaseTask, State, TaskInfo},
  23. Tau,
  24. };
  25. impl Tau {
  26. pub async fn close_connection(&self) {
  27. self.rpc_client.stop().await
  28. }
  29. /// Add a new task.
  30. pub async fn add(&self, task: BaseTask) -> Result<u32> {
  31. let mut params = vec![
  32. JsonValue::String(task.title.clone()),
  33. JsonValue::Array(task.tags.iter().map(|x| JsonValue::String(x.clone())).collect()),
  34. JsonValue::String(task.desc.unwrap_or("".to_string())),
  35. JsonValue::Array(task.assign.iter().map(|x| JsonValue::String(x.clone())).collect()),
  36. JsonValue::Array(task.project.iter().map(|x| JsonValue::String(x.clone())).collect()),
  37. ];
  38. let due = if let Some(num) = task.due {
  39. JsonValue::String(num.to_string())
  40. } else {
  41. JsonValue::Null
  42. };
  43. params.push(due);
  44. let rank =
  45. if let Some(num) = task.rank { JsonValue::Number(num.into()) } else { JsonValue::Null };
  46. params.push(rank);
  47. let req = JsonRequest::new("add", params);
  48. let rep = self.rpc_client.request(req).await?;
  49. debug!("Got reply: {:?}", rep);
  50. Ok(*rep.get::<f64>().unwrap() as u32)
  51. }
  52. /// Get current open tasks ids.
  53. pub async fn get_ids(&self) -> Result<Vec<u32>> {
  54. let req = JsonRequest::new("get_ids", vec![]);
  55. let rep = self.rpc_client.request(req).await?;
  56. debug!("Got reply: {:?}", rep);
  57. let mut ret = vec![];
  58. for i in rep.get::<Vec<JsonValue>>().unwrap() {
  59. ret.push(*i.get::<f64>().unwrap() as u32)
  60. }
  61. Ok(ret)
  62. }
  63. /// Update existing task given it's ID and some params.
  64. pub async fn update(&self, id: u32, task: BaseTask) -> Result<bool> {
  65. let mut params = vec![
  66. JsonValue::String(task.title.clone()),
  67. JsonValue::Array(task.tags.iter().map(|x| JsonValue::String(x.clone())).collect()),
  68. JsonValue::String(task.desc.unwrap_or("".to_string())),
  69. JsonValue::Array(task.assign.iter().map(|x| JsonValue::String(x.clone())).collect()),
  70. JsonValue::Array(task.project.iter().map(|x| JsonValue::String(x.clone())).collect()),
  71. ];
  72. let due = if let Some(num) = task.due {
  73. JsonValue::String(num.to_string())
  74. } else {
  75. JsonValue::Null
  76. };
  77. params.push(due);
  78. let rank =
  79. if let Some(num) = task.rank { JsonValue::Number(num.into()) } else { JsonValue::Null };
  80. params.push(rank);
  81. let req = JsonRequest::new(
  82. "update",
  83. vec![JsonValue::Number(id.into()), JsonValue::Array(params)],
  84. );
  85. let rep = self.rpc_client.request(req).await?;
  86. debug!("Got reply: {:?}", rep);
  87. Ok(*rep.get::<bool>().unwrap())
  88. }
  89. /// Set the state for a task.
  90. pub async fn set_state(&self, id: u32, state: &State) -> Result<bool> {
  91. let req = JsonRequest::new(
  92. "set_state",
  93. vec![JsonValue::Number(id.into()), JsonValue::String(state.to_string())],
  94. );
  95. let rep = self.rpc_client.request(req).await?;
  96. debug!("Got reply: {:?}", rep);
  97. Ok(*rep.get::<bool>().unwrap())
  98. }
  99. /// Set a comment for a task.
  100. pub async fn set_comment(&self, id: u32, content: &str) -> Result<bool> {
  101. let req = JsonRequest::new(
  102. "set_comment",
  103. vec![JsonValue::Number(id.into()), JsonValue::String(content.to_string())],
  104. );
  105. let rep = self.rpc_client.request(req).await?;
  106. debug!("Got reply: {:?}", rep);
  107. Ok(*rep.get::<bool>().unwrap())
  108. }
  109. /// Get task data by its ID.
  110. pub async fn get_task_by_id(&self, id: u32) -> Result<TaskInfo> {
  111. let req = JsonRequest::new("get_task_by_id", vec![JsonValue::Number(id.into())]);
  112. let rep = self.rpc_client.request(req).await?;
  113. debug!("Got reply: {:?}", rep);
  114. let rep = rep.into();
  115. Ok(rep)
  116. }
  117. /// Get month's stopped tasks.
  118. pub async fn get_stop_tasks(&self, month: Option<u64>) -> Result<Vec<TaskInfo>> {
  119. let param = if let Some(month) = month {
  120. JsonValue::String(month.to_string())
  121. } else {
  122. JsonValue::Null
  123. };
  124. let req = JsonRequest::new("get_stop_tasks", vec![param]);
  125. let rep = self.rpc_client.request(req).await?;
  126. debug!("Got reply: {:?}", rep);
  127. let rep =
  128. rep.get::<Vec<JsonValue>>().unwrap().iter().map(|x| (*x).clone().into()).collect();
  129. Ok(rep)
  130. }
  131. /// Switch workspace.
  132. pub async fn switch_ws(&self, workspace: String) -> Result<bool> {
  133. let req = JsonRequest::new("switch_ws", vec![JsonValue::String(workspace)]);
  134. let rep = self.rpc_client.request(req).await?;
  135. debug!("Got reply: {:?}", rep);
  136. Ok(*rep.get::<bool>().unwrap())
  137. }
  138. /// Get current workspace.
  139. pub async fn get_ws(&self) -> Result<String> {
  140. let req = JsonRequest::new("get_ws", vec![]);
  141. let rep = self.rpc_client.request(req).await?;
  142. debug!("Got reply: {:?}", rep);
  143. Ok(rep.get::<String>().unwrap().clone())
  144. }
  145. /// Export tasks.
  146. pub async fn export_to(&self, path: String) -> Result<bool> {
  147. let req = JsonRequest::new("export", vec![JsonValue::String(path)]);
  148. let rep = self.rpc_client.request(req).await?;
  149. debug!("Got reply: {:?}", rep);
  150. Ok(*rep.get::<bool>().unwrap())
  151. }
  152. /// Import tasks.
  153. pub async fn import_from(&self, path: String) -> Result<bool> {
  154. let req = JsonRequest::new("import", vec![JsonValue::String(path)]);
  155. let rep = self.rpc_client.request(req).await?;
  156. debug!("Got reply: {:?}", rep);
  157. Ok(*rep.get::<bool>().unwrap())
  158. }
  159. }