task_info.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 std::path::{Path, PathBuf};
  19. use darkfi_serial::{SerialDecodable, SerialEncodable};
  20. use log::debug;
  21. use serde::{Deserialize, Serialize};
  22. use darkfi::{
  23. event_graph::gen_id,
  24. util::{
  25. file::{load_json_file, save_json_file},
  26. time::Timestamp,
  27. },
  28. };
  29. use crate::{
  30. error::{TaudError, TaudResult},
  31. month_tasks::MonthTasks,
  32. util::find_free_id,
  33. };
  34. #[derive(Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq, Eq)]
  35. pub struct TaskEvent {
  36. pub action: String,
  37. pub author: String,
  38. pub content: String,
  39. pub timestamp: Timestamp,
  40. }
  41. impl TaskEvent {
  42. pub fn new(action: String, author: String, content: String) -> Self {
  43. Self { action, author, content, timestamp: Timestamp::current_time() }
  44. }
  45. }
  46. #[derive(Clone, Debug, Serialize, Deserialize, SerialDecodable, SerialEncodable, PartialEq, Eq)]
  47. pub struct Comment {
  48. content: String,
  49. author: String,
  50. timestamp: Timestamp,
  51. }
  52. impl Comment {
  53. pub fn new(content: &str, author: &str) -> Self {
  54. Self {
  55. content: content.into(),
  56. author: author.into(),
  57. timestamp: Timestamp::current_time(),
  58. }
  59. }
  60. }
  61. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  62. pub struct TaskEvents(pub Vec<TaskEvent>);
  63. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  64. pub struct TaskComments(Vec<Comment>);
  65. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  66. pub struct TaskProjects(Vec<String>);
  67. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  68. pub struct TaskAssigns(Vec<String>);
  69. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  70. pub struct TaskTags(Vec<String>);
  71. #[derive(Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq)]
  72. pub struct TaskInfo {
  73. pub(crate) ref_id: String,
  74. pub(crate) workspace: String,
  75. pub(crate) id: u32,
  76. pub(crate) title: String,
  77. tags: TaskTags,
  78. desc: String,
  79. pub(crate) owner: String,
  80. assign: TaskAssigns,
  81. project: TaskProjects,
  82. due: Option<Timestamp>,
  83. rank: Option<f32>,
  84. created_at: Timestamp,
  85. state: String,
  86. pub(crate) events: TaskEvents,
  87. comments: TaskComments,
  88. }
  89. impl TaskInfo {
  90. pub fn new(
  91. workspace: String,
  92. title: &str,
  93. desc: &str,
  94. owner: &str,
  95. due: Option<Timestamp>,
  96. rank: Option<f32>,
  97. dataset_path: &Path,
  98. ) -> TaudResult<Self> {
  99. // generate ref_id
  100. let ref_id = gen_id(30);
  101. let created_at = Timestamp::current_time();
  102. let task_ids: Vec<u32> =
  103. MonthTasks::load_current_tasks(dataset_path, workspace.clone(), false)?
  104. .into_iter()
  105. .map(|t| t.id)
  106. .collect();
  107. let id: u32 = find_free_id(&task_ids);
  108. if let Some(d) = &due {
  109. if *d < Timestamp::current_time() {
  110. return Err(TaudError::InvalidDueTime)
  111. }
  112. }
  113. Ok(Self {
  114. ref_id,
  115. workspace,
  116. id,
  117. title: title.into(),
  118. desc: desc.into(),
  119. owner: owner.into(),
  120. tags: TaskTags(vec![]),
  121. assign: TaskAssigns(vec![]),
  122. project: TaskProjects(vec![]),
  123. due,
  124. rank,
  125. created_at,
  126. state: "open".into(),
  127. comments: TaskComments(vec![]),
  128. events: TaskEvents(vec![]),
  129. })
  130. }
  131. pub fn load(ref_id: &str, dataset_path: &Path) -> TaudResult<Self> {
  132. debug!(target: "tau", "TaskInfo::load()");
  133. let task = load_json_file::<Self>(&Self::get_path(ref_id, dataset_path))?;
  134. Ok(task)
  135. }
  136. pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
  137. debug!(target: "tau", "TaskInfo::save()");
  138. save_json_file::<Self>(&Self::get_path(&self.ref_id, dataset_path), self)
  139. .map_err(TaudError::Darkfi)?;
  140. if self.get_state() == "stop" {
  141. self.deactivate(dataset_path)?;
  142. } else {
  143. self.activate(dataset_path)?;
  144. }
  145. Ok(())
  146. }
  147. pub fn activate(&self, path: &Path) -> TaudResult<()> {
  148. debug!(target: "tau", "TaskInfo::activate()");
  149. let mut mt = MonthTasks::load_or_create(Some(&self.created_at), path)?;
  150. mt.add(&self.ref_id);
  151. mt.save(path)
  152. }
  153. pub fn deactivate(&self, path: &Path) -> TaudResult<()> {
  154. debug!(target: "tau", "TaskInfo::deactivate()");
  155. let mut mt = MonthTasks::load_or_create(Some(&self.created_at), path)?;
  156. mt.remove(&self.ref_id);
  157. mt.save(path)
  158. }
  159. pub fn get_state(&self) -> String {
  160. debug!(target: "tau", "TaskInfo::get_state()");
  161. self.state.clone()
  162. }
  163. pub fn get_path(ref_id: &str, dataset_path: &Path) -> PathBuf {
  164. debug!(target: "tau", "TaskInfo::get_path()");
  165. dataset_path.join("task").join(ref_id)
  166. }
  167. pub fn get_id(&self) -> u32 {
  168. debug!(target: "tau", "TaskInfo::get_id()");
  169. self.id
  170. }
  171. pub fn set_title(&mut self, title: &str) {
  172. debug!(target: "tau", "TaskInfo::set_title()");
  173. self.title = title.into();
  174. }
  175. pub fn set_desc(&mut self, desc: &str) {
  176. debug!(target: "tau", "TaskInfo::set_desc()");
  177. self.desc = desc.into();
  178. }
  179. pub fn set_tags(&mut self, tags: &[String]) {
  180. debug!(target: "tau", "TaskInfo::set_tags()");
  181. for tag in tags.iter() {
  182. if tag.starts_with('+') && !self.tags.0.contains(tag) {
  183. self.tags.0.push(tag.to_string());
  184. }
  185. if tag.starts_with('-') {
  186. let t = tag.replace('-', "+");
  187. self.tags.0.retain(|tag| tag != &t);
  188. }
  189. }
  190. }
  191. pub fn set_assign(&mut self, assigns: &[String]) {
  192. debug!(target: "tau", "TaskInfo::set_assign()");
  193. self.assign = TaskAssigns(assigns.to_owned());
  194. }
  195. pub fn set_project(&mut self, projects: &[String]) {
  196. debug!(target: "tau", "TaskInfo::set_project()");
  197. self.project = TaskProjects(projects.to_owned());
  198. }
  199. pub fn set_comment(&mut self, c: Comment) {
  200. debug!(target: "tau", "TaskInfo::set_comment()");
  201. self.comments.0.push(c);
  202. }
  203. pub fn set_rank(&mut self, r: Option<f32>) {
  204. debug!(target: "tau", "TaskInfo::set_rank()");
  205. self.rank = r;
  206. }
  207. pub fn set_due(&mut self, d: Option<Timestamp>) {
  208. debug!(target: "tau", "TaskInfo::set_due()");
  209. self.due = d;
  210. }
  211. pub fn set_state(&mut self, state: &str) {
  212. debug!(target: "tau", "TaskInfo::set_state()");
  213. if self.get_state() == state {
  214. return
  215. }
  216. self.state = state.to_string();
  217. }
  218. }