task_info.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use std::path::PathBuf;
  2. use serde::{Deserialize, Serialize};
  3. use darkfi::Result;
  4. use crate::{
  5. month_tasks::MonthTasks,
  6. util::{find_free_id, get_current_time, random_ref_id, Settings, Timestamp},
  7. };
  8. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
  9. struct TaskEvent {
  10. action: String,
  11. timestamp: Timestamp,
  12. }
  13. impl TaskEvent {
  14. fn new(action: String) -> Self {
  15. Self { action, timestamp: get_current_time() }
  16. }
  17. }
  18. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
  19. pub struct Comment {
  20. content: String,
  21. author: String,
  22. timestamp: Timestamp,
  23. }
  24. impl Comment {
  25. pub fn new(content: &str, author: &str) -> Self {
  26. Self { content: content.into(), author: author.into(), timestamp: get_current_time() }
  27. }
  28. }
  29. // XXX
  30. #[allow(dead_code)]
  31. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
  32. pub struct TaskInfo {
  33. ref_id: String,
  34. id: u32,
  35. title: String,
  36. desc: String,
  37. assign: Vec<String>,
  38. project: Vec<String>,
  39. due: Option<Timestamp>,
  40. rank: u32,
  41. created_at: Timestamp,
  42. events: Vec<TaskEvent>,
  43. comments: Vec<Comment>,
  44. #[serde(skip_serializing, skip_deserializing)]
  45. settings: Settings,
  46. }
  47. impl TaskInfo {
  48. pub fn new(
  49. title: &str,
  50. desc: &str,
  51. due: Option<Timestamp>,
  52. rank: u32,
  53. settings: &Settings,
  54. ) -> Result<Self> {
  55. // generate ref_id
  56. let ref_id = random_ref_id();
  57. let created_at: Timestamp = get_current_time();
  58. let task_ids: Vec<u32> =
  59. MonthTasks::load_current_open_tasks(settings)?.into_iter().map(|t| t.id).collect();
  60. let id: u32 = find_free_id(&task_ids);
  61. Ok(Self {
  62. ref_id,
  63. id,
  64. title: title.into(),
  65. desc: desc.into(),
  66. assign: vec![],
  67. project: vec![],
  68. due,
  69. rank,
  70. created_at,
  71. comments: vec![],
  72. events: vec![],
  73. settings: settings.clone(),
  74. })
  75. }
  76. pub fn load(ref_id: &str, settings: &Settings) -> Result<Self> {
  77. let mut task = crate::util::load::<Self>(&Self::get_path(ref_id, settings))?;
  78. task.set_settings(settings);
  79. Ok(task)
  80. }
  81. pub fn save(&self) -> Result<()> {
  82. crate::util::save::<Self>(&Self::get_path(&self.ref_id, &self.settings), self)
  83. }
  84. pub fn activate(&self) -> Result<()> {
  85. let mut mt = MonthTasks::load_or_create(&self.created_at, &self.settings)?;
  86. mt.add(&self.ref_id);
  87. mt.save()
  88. }
  89. pub fn get_state(&self) -> String {
  90. if let Some(ev) = self.events.last() {
  91. ev.action.clone()
  92. } else {
  93. "open".into()
  94. }
  95. }
  96. fn get_path(ref_id: &str, settings: &Settings) -> PathBuf {
  97. settings.dataset_path.join("task").join(ref_id)
  98. }
  99. pub fn get_id(&self) -> u32 {
  100. self.id
  101. }
  102. pub fn get_ref_id(&self) -> String {
  103. self.ref_id.clone()
  104. }
  105. pub fn set_title(&mut self, title: &str) {
  106. self.title = title.into();
  107. }
  108. pub fn set_desc(&mut self, desc: &str) {
  109. self.desc = desc.into();
  110. }
  111. pub fn set_assign(&mut self, assign: &Vec<String>) {
  112. self.assign = assign.clone();
  113. }
  114. pub fn set_project(&mut self, project: &Vec<String>) {
  115. self.project = project.clone();
  116. }
  117. pub fn set_comment(&mut self, c: Comment) {
  118. self.comments.push(c);
  119. }
  120. pub fn set_rank(&mut self, r: u32) {
  121. self.rank = r;
  122. }
  123. pub fn set_due(&mut self, d: Option<Timestamp>) {
  124. self.due = d;
  125. }
  126. pub fn set_settings(&mut self, settings: &Settings) {
  127. self.settings = settings.clone();
  128. }
  129. pub fn set_state(&mut self, action: &str) {
  130. if self.get_state() == action {
  131. return
  132. }
  133. self.events.push(TaskEvent::new(action.into()));
  134. }
  135. }