| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2023 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::{
- collections::HashMap,
- fmt,
- path::{Path, PathBuf},
- str::FromStr,
- };
- use darkfi_serial::{async_trait, SerialDecodable, SerialEncodable};
- use log::debug;
- use tinyjson::JsonValue;
- use darkfi::{
- event_graph::gen_id,
- util::{
- file::{load_json_file, save_json_file},
- time::Timestamp,
- },
- Error,
- };
- use crate::{
- error::{TaudError, TaudResult},
- month_tasks::MonthTasks,
- util::find_free_id,
- };
- pub enum State {
- Open,
- Start,
- Pause,
- Stop,
- }
- impl State {
- pub const fn is_start(&self) -> bool {
- matches!(*self, Self::Start)
- }
- pub const fn is_pause(&self) -> bool {
- matches!(*self, Self::Pause)
- }
- pub const fn is_stop(&self) -> bool {
- matches!(*self, Self::Stop)
- }
- }
- impl fmt::Display for State {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- State::Open => write!(f, "open"),
- State::Start => write!(f, "start"),
- State::Stop => write!(f, "stop"),
- State::Pause => write!(f, "pause"),
- }
- }
- }
- impl FromStr for State {
- type Err = Error;
- fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let result = match s.to_lowercase().as_str() {
- "open" => State::Open,
- "stop" => State::Stop,
- "start" => State::Start,
- "pause" => State::Pause,
- _ => return Err(Error::ParseFailed("unable to parse state")),
- };
- Ok(result)
- }
- }
- #[derive(Clone, Debug, SerialEncodable, SerialDecodable, PartialEq, Eq)]
- pub struct TaskEvent {
- pub action: String,
- pub author: String,
- pub content: String,
- pub timestamp: Timestamp,
- }
- impl std::fmt::Display for TaskEvent {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "action: {}, timestamp: {}", self.action, self.timestamp)
- }
- }
- impl Default for TaskEvent {
- fn default() -> Self {
- Self {
- action: State::Open.to_string(),
- author: "".to_string(),
- content: "".to_string(),
- timestamp: Timestamp::current_time(),
- }
- }
- }
- impl TaskEvent {
- pub fn new(action: String, author: String, content: String) -> Self {
- Self { action, author, content, timestamp: Timestamp::current_time() }
- }
- }
- impl From<TaskEvent> for JsonValue {
- fn from(task_event: TaskEvent) -> JsonValue {
- JsonValue::Object(HashMap::from([
- ("action".to_string(), JsonValue::String(task_event.action.clone())),
- ("author".to_string(), JsonValue::String(task_event.author.clone())),
- ("content".to_string(), JsonValue::String(task_event.content.clone())),
- ("timestamp".to_string(), JsonValue::String(task_event.timestamp.0.to_string())),
- ]))
- }
- }
- impl From<&JsonValue> for TaskEvent {
- fn from(value: &JsonValue) -> TaskEvent {
- let map = value.get::<HashMap<String, JsonValue>>().unwrap();
- TaskEvent {
- action: map["action"].get::<String>().unwrap().clone(),
- author: map["author"].get::<String>().unwrap().clone(),
- content: map["content"].get::<String>().unwrap().clone(),
- timestamp: Timestamp(map["timestamp"].get::<String>().unwrap().parse::<u64>().unwrap()),
- }
- }
- }
- #[derive(Clone, Debug, SerialDecodable, SerialEncodable, PartialEq, Eq)]
- pub struct Comment {
- content: String,
- author: String,
- timestamp: Timestamp,
- }
- impl std::fmt::Display for Comment {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{} author: {}, content: {} ", self.timestamp, self.author, self.content)
- }
- }
- impl From<Comment> for JsonValue {
- fn from(comment: Comment) -> JsonValue {
- JsonValue::Object(HashMap::from([
- ("content".to_string(), JsonValue::String(comment.content.clone())),
- ("author".to_string(), JsonValue::String(comment.author.clone())),
- ("timestamp".to_string(), JsonValue::String(comment.timestamp.0.to_string())),
- ]))
- }
- }
- impl From<JsonValue> for Comment {
- fn from(value: JsonValue) -> Comment {
- let map = value.get::<HashMap<String, JsonValue>>().unwrap();
- Comment {
- content: map["content"].get::<String>().unwrap().clone(),
- author: map["author"].get::<String>().unwrap().clone(),
- timestamp: Timestamp(map["timestamp"].get::<String>().unwrap().parse::<u64>().unwrap()),
- }
- }
- }
- impl Comment {
- pub fn new(content: &str, author: &str) -> Self {
- Self {
- content: content.into(),
- author: author.into(),
- timestamp: Timestamp::current_time(),
- }
- }
- }
- #[derive(Clone, Debug, SerialEncodable, SerialDecodable, PartialEq)]
- pub struct TaskInfo {
- pub ref_id: String,
- pub workspace: String,
- pub id: u32,
- pub title: String,
- pub tags: Vec<String>,
- pub desc: String,
- pub owner: String,
- pub assign: Vec<String>,
- pub project: Vec<String>,
- pub due: Option<Timestamp>,
- pub rank: Option<f32>,
- pub created_at: Timestamp,
- pub state: String,
- pub events: Vec<TaskEvent>,
- pub comments: Vec<Comment>,
- }
- impl From<&TaskInfo> for JsonValue {
- fn from(task: &TaskInfo) -> JsonValue {
- let ref_id = JsonValue::String(task.ref_id.clone());
- let workspace = JsonValue::String(task.workspace.clone());
- let id = JsonValue::Number(task.id.into());
- let title = JsonValue::String(task.title.clone());
- let tags: Vec<JsonValue> = task.tags.iter().map(|x| JsonValue::String(x.clone())).collect();
- let desc = JsonValue::String(task.desc.clone());
- let owner = JsonValue::String(task.owner.clone());
- let assign: Vec<JsonValue> =
- task.assign.iter().map(|x| JsonValue::String(x.clone())).collect();
- let project: Vec<JsonValue> =
- task.project.iter().map(|x| JsonValue::String(x.clone())).collect();
- let due = if let Some(ts) = task.due {
- JsonValue::String(ts.0.to_string())
- } else {
- JsonValue::Null
- };
- let rank = if let Some(rank) = task.rank {
- JsonValue::Number(rank.into())
- } else {
- JsonValue::Null
- };
- let created_at = JsonValue::String(task.created_at.0.to_string());
- let state = JsonValue::String(task.state.clone());
- let events: Vec<JsonValue> = task.events.iter().map(|x| x.clone().into()).collect();
- let comments: Vec<JsonValue> = task.comments.iter().map(|x| x.clone().into()).collect();
- JsonValue::Object(HashMap::from([
- ("ref_id".to_string(), ref_id),
- ("workspace".to_string(), workspace),
- ("id".to_string(), id),
- ("title".to_string(), title),
- ("tags".to_string(), JsonValue::Array(tags)),
- ("desc".to_string(), desc),
- ("owner".to_string(), owner),
- ("assign".to_string(), JsonValue::Array(assign)),
- ("project".to_string(), JsonValue::Array(project)),
- ("due".to_string(), due),
- ("rank".to_string(), rank),
- ("created_at".to_string(), created_at),
- ("state".to_string(), state),
- ("events".to_string(), JsonValue::Array(events)),
- ("comments".to_string(), JsonValue::Array(comments)),
- ]))
- }
- }
- impl From<JsonValue> for TaskInfo {
- fn from(value: JsonValue) -> TaskInfo {
- let tags = value["tags"].get::<Vec<JsonValue>>().unwrap();
- let assign = value["assign"].get::<Vec<JsonValue>>().unwrap();
- let project = value["project"].get::<Vec<JsonValue>>().unwrap();
- let events = value["events"].get::<Vec<JsonValue>>().unwrap();
- let comments = value["comments"].get::<Vec<JsonValue>>().unwrap();
- let due = {
- if value["due"].is_null() {
- None
- } else {
- let u64_str = value["due"].get::<String>().unwrap();
- Some(Timestamp(u64_str.parse::<u64>().unwrap()))
- }
- };
- let rank = {
- if value["rank"].is_null() {
- None
- } else {
- Some(*value["rank"].get::<f64>().unwrap() as f32)
- }
- };
- let created_at = {
- let u64_str = value["created_at"].get::<String>().unwrap();
- Timestamp(u64_str.parse::<u64>().unwrap())
- };
- let events: Vec<TaskEvent> = events.iter().map(|x| x.into()).collect();
- let comments: Vec<Comment> = comments.iter().map(|x| (*x).clone().into()).collect();
- TaskInfo {
- ref_id: value["ref_id"].get::<String>().unwrap().clone(),
- workspace: value["workspace"].get::<String>().unwrap().clone(),
- id: *value["id"].get::<f64>().unwrap() as u32,
- title: value["title"].get::<String>().unwrap().clone(),
- tags: tags.iter().map(|x| x.get::<String>().unwrap().clone()).collect(),
- desc: value["desc"].get::<String>().unwrap().clone(),
- owner: value["owner"].get::<String>().unwrap().clone(),
- assign: assign.iter().map(|x| x.get::<String>().unwrap().clone()).collect(),
- project: project.iter().map(|x| x.get::<String>().unwrap().clone()).collect(),
- due,
- rank,
- created_at,
- state: value["state"].get::<String>().unwrap().clone(),
- events,
- comments,
- }
- }
- }
- impl TaskInfo {
- pub fn new(
- workspace: String,
- title: &str,
- desc: &str,
- owner: &str,
- due: Option<Timestamp>,
- rank: Option<f32>,
- dataset_path: &Path,
- ) -> TaudResult<Self> {
- // generate ref_id
- let ref_id = gen_id(30);
- let created_at = Timestamp::current_time();
- let task_ids: Vec<u32> =
- MonthTasks::load_current_tasks(dataset_path, workspace.clone(), false)?
- .into_iter()
- .map(|t| t.id)
- .collect();
- let id: u32 = find_free_id(&task_ids);
- if let Some(d) = &due {
- if *d < Timestamp::current_time() {
- return Err(TaudError::InvalidDueTime)
- }
- }
- Ok(Self {
- ref_id,
- workspace,
- id,
- title: title.into(),
- desc: desc.into(),
- owner: owner.into(),
- tags: vec![],
- assign: vec![],
- project: vec![],
- due,
- rank,
- created_at,
- state: "open".into(),
- comments: vec![],
- events: vec![],
- })
- }
- pub fn load(ref_id: &str, dataset_path: &Path) -> TaudResult<Self> {
- debug!(target: "tau", "TaskInfo::load()");
- let task = load_json_file(&Self::get_path(ref_id, dataset_path))?;
- Ok(task.into())
- }
- pub fn save(&self, dataset_path: &Path) -> TaudResult<()> {
- debug!(target: "tau", "TaskInfo::save()");
- save_json_file(&Self::get_path(&self.ref_id, dataset_path), &self.into(), true)
- .map_err(TaudError::Darkfi)?;
- if self.get_state() == "stop" {
- self.deactivate(dataset_path)?;
- } else {
- self.activate(dataset_path)?;
- }
- Ok(())
- }
- pub fn activate(&self, path: &Path) -> TaudResult<()> {
- debug!(target: "tau", "TaskInfo::activate()");
- let mut mt = MonthTasks::load_or_create(Some(&self.created_at), path)?;
- mt.add(&self.ref_id);
- mt.save(path)
- }
- pub fn deactivate(&self, path: &Path) -> TaudResult<()> {
- debug!(target: "tau", "TaskInfo::deactivate()");
- let mut mt = MonthTasks::load_or_create(Some(&self.created_at), path)?;
- mt.remove(&self.ref_id);
- mt.save(path)
- }
- pub fn get_state(&self) -> String {
- debug!(target: "tau", "TaskInfo::get_state()");
- self.state.clone()
- }
- pub fn get_path(ref_id: &str, dataset_path: &Path) -> PathBuf {
- debug!(target: "tau", "TaskInfo::get_path()");
- dataset_path.join("task").join(ref_id)
- }
- pub fn get_id(&self) -> u32 {
- debug!(target: "tau", "TaskInfo::get_id()");
- self.id
- }
- pub fn set_title(&mut self, title: &str) {
- debug!(target: "tau", "TaskInfo::set_title()");
- self.title = title.into();
- }
- pub fn set_desc(&mut self, desc: &str) {
- debug!(target: "tau", "TaskInfo::set_desc()");
- self.desc = desc.into();
- }
- pub fn set_tags(&mut self, tags: &[String]) {
- debug!(target: "tau", "TaskInfo::set_tags()");
- for tag in tags.iter() {
- if tag.starts_with('+') && !self.tags.contains(tag) {
- self.tags.push(tag.to_string());
- }
- if tag.starts_with('-') {
- let t = tag.replace('-', "+");
- self.tags.retain(|tag| tag != &t);
- }
- }
- }
- pub fn set_assign(&mut self, assigns: &[String]) {
- debug!(target: "tau", "TaskInfo::set_assign()");
- self.assign = assigns.to_owned();
- }
- pub fn set_project(&mut self, projects: &[String]) {
- debug!(target: "tau", "TaskInfo::set_project()");
- self.project = projects.to_owned();
- }
- pub fn set_comment(&mut self, c: Comment) {
- debug!(target: "tau", "TaskInfo::set_comment()");
- self.comments.push(c);
- }
- pub fn set_rank(&mut self, r: Option<f32>) {
- debug!(target: "tau", "TaskInfo::set_rank()");
- self.rank = r;
- }
- pub fn set_due(&mut self, d: Option<Timestamp>) {
- debug!(target: "tau", "TaskInfo::set_due()");
- self.due = d;
- }
- pub fn set_state(&mut self, state: &str) {
- debug!(target: "tau", "TaskInfo::set_state()");
- if self.get_state() == state {
- return
- }
- self.state = state.to_string();
- }
- }
|