From ac15d47c692caefe25302dbc57e6f79a5b41309a Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Sat, 7 Mar 2020 21:22:07 +0000 Subject: [PATCH] Use timestamp of parent task for imported tags and notes --- tasklite-core/source/ImportExport.hs | 41 ++++++++++++---- tasklite-core/source/Lib.hs | 48 ++++++++++++++----- tasklite-core/test/fixtures/simple.json | 13 +++++ .../taskwarrior.json} | 0 4 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 tasklite-core/test/fixtures/simple.json rename tasklite-core/test/{example-task.json => fixtures/taskwarrior.json} (100%) diff --git a/tasklite-core/source/ImportExport.hs b/tasklite-core/source/ImportExport.hs index fbea0f9..4e6835f 100644 --- a/tasklite-core/source/ImportExport.hs +++ b/tasklite-core/source/ImportExport.hs @@ -76,11 +76,15 @@ data ImportTask = ImportTask -- | Values a suffixed with a prime (') to avoid name collisions instance FromJSON ImportTask where parseJSON = withObject "task" $ \o -> do + utc <- o .:? "utc" entry <- o .:? "entry" creation <- o .:? "creation" created_at <- o .:? "created_at" - let createdUtc = fromMaybe (timeFromElapsedP 0 :: DateTime) - (parseUtc =<< (entry <|> creation <|> created_at)) + let + zeroTime = timeFromElapsedP 0 :: DateTime + parsedCreatedUtc = parseUtc + =<< (utc <|> entry <|> creation <|> created_at) + createdUtc = fromMaybe zeroTime parsedCreatedUtc o_body <- o .:? "body" description <- o .:? "description" @@ -91,8 +95,8 @@ instance FromJSON ImportTask where let state = textToTaskState =<< (o_state <|> status) o_priority_adjustment <- o .:? "priority_adjustment" - urgency <- o .:? "urgency" - priority <- optional (o .: "priority") + urgency <- o .:? "urgency" + priority <- optional (o .: "priority") let priority_adjustment = o_priority_adjustment <|> urgency <|> priority modified <- o .:? "modified" @@ -205,9 +209,22 @@ instance FromJSON ImportTask where annotations <- o .:? "annotations" :: Parser (Maybe [Annotation]) let notes = case (o_notes, annotations) of - (Just theNotes , _ ) -> theNotes + (Nothing, Nothing) -> [] (Nothing, Just values) -> values <$$> annotationToNote - _ -> [] + (Just theNotes , _) -> case parsedCreatedUtc of + Just crUtc -> theNotes <&> (\theNote -> + let + noteUlidTxt = Note.ulid theNote + mbNoteUlid = parseUlidText noteUlidTxt + mbNewUlid = do + noteUlid <- mbNoteUlid + + pure $ show $ setDateTime noteUlid crUtc + in + theNote { Note.ulid = + (T.toLower $ fromMaybe noteUlidTxt mbNewUlid) } + ) + Nothing -> theNotes o_user <- o .:? "user" let user = fromMaybe "" o_user @@ -244,8 +261,10 @@ insertImportTask connection importTaskRecord = do then taskParsed { Task.user = T.pack effectiveUserName } else taskParsed insertTask connection theTask - insertTags connection (primaryKey theTask) (tags importTaskRecord) - insertNotes connection (primaryKey theTask) (notes importTaskRecord) + insertTags connection (ulidTextToDateTime $ Task.ulid taskParsed) + (primaryKey theTask) (tags importTaskRecord) + insertNotes connection (ulidTextToDateTime $ Task.ulid taskParsed) + (primaryKey theTask) (notes importTaskRecord) pure $ "📥 Imported task" <+> dquotes (pretty $ Task.body theTask) <+> "with ulid" <+> dquotes (pretty $ Task.ulid theTask) @@ -364,8 +383,10 @@ editTask conf connection idSubstr = do } replaceTask connection taskFixed - insertTags connection (primaryKey taskFixed) (tags importTaskRecord) - insertNotes connection (primaryKey taskFixed) (notes importTaskRecord) + insertTags connection Nothing + (primaryKey taskFixed) (tags importTaskRecord) + insertNotes connection Nothing + (primaryKey taskFixed) (notes importTaskRecord) pure $ "✏️ Edited task" <+> dquotes (pretty $ Task.body taskFixed) <+> "with ulid" <+> dquotes (pretty $ Task.ulid taskFixed) diff --git a/tasklite-core/source/Lib.hs b/tasklite-core/source/Lib.hs index 4e844b2..397c64a 100644 --- a/tasklite-core/source/Lib.hs +++ b/tasklite-core/source/Lib.hs @@ -114,21 +114,40 @@ replaceTask connection task = do save (_tldbTasks taskLiteDb) task -insertTags :: Connection -> TaskUlid -> [Text] -> IO () -insertTags connection taskUlid tags = do +insertTags :: Connection -> Maybe DateTime -> TaskUlid -> [Text] -> IO () +insertTags connection mbCreatedUtc taskUlid tags = do taskToTags <- forM tags $ \tag -> do - tagUlid <- formatUlid getULID - pure $ TaskToTag tagUlid taskUlid tag + tagUlid <- getULID + pure $ TaskToTag + (mbCreatedUtc + <&> setDateTime tagUlid + & fromMaybe tagUlid + & show + & toLower) + taskUlid + tag runBeamSqlite connection $ runInsert $ insert (_tldbTaskToTag taskLiteDb) $ insertValues taskToTags -insertNotes :: Connection -> TaskUlid -> [Note] -> IO () -insertNotes connection primKey notes = do +insertNotes :: Connection -> Maybe DateTime -> TaskUlid -> [Note] -> IO () +insertNotes connection mbCreatedUtc primKey notes = do taskToNotes <- forM notes $ \theNote -> do - pure $ TaskToNote (Note.ulid theNote) primKey (Note.body theNote) + let + noteUlidTxt = Note.ulid theNote + mbNoteUlid = parseUlidText $ noteUlidTxt + mbNewUlid = do + createdUtc <- mbCreatedUtc + noteUlid <- mbNoteUlid + + pure $ show $ setDateTime noteUlid createdUtc + + pure $ TaskToNote + (toLower $ fromMaybe noteUlidTxt mbNewUlid) + primKey + (Note.body theNote) runBeamSqlite connection $ runInsert $ insert (_tldbTaskToNote taskLiteDb) $ @@ -138,11 +157,14 @@ insertNotes connection primKey notes = do -- | Tuple is (Maybe createdUtc, noteBody) insertNoteTuples :: Connection -> TaskUlid -> [(Maybe DateTime, Text)] -> IO () insertNoteTuples connection taskUlid notes = do - taskToNotes <- forM notes $ \(createdUtc, noteBody) -> do + taskToNotes <- forM notes $ \(mbCreatedUtc, noteBody) -> do noteUlid <- getULID pure $ TaskToNote - ((toLower . show . maybe noteUlid (setDateTime noteUlid)) - createdUtc) + (mbCreatedUtc + <&> setDateTime noteUlid + & fromMaybe noteUlid + & show + & toLower) taskUlid noteBody @@ -213,7 +235,7 @@ addTask conf connection bodyWords = do } insertTask connection task - insertTags connection (primaryKey task) tags + insertTags connection Nothing (primaryKey task) tags pure $ "🆕 Added task" <+> dquotes (pretty $ Task.body task) <+> "with id" <+> dquotes (pretty $ Task.ulid task) @@ -236,7 +258,7 @@ logTask conf connection bodyWords = do } insertTask connection task - insertTags connection (primaryKey task) tags + insertTags connection Nothing (primaryKey task) tags pure $ "📝 Logged task" <+> dquotes (pretty $ Task.body task) <+> "with id" <+> dquotes (pretty $ Task.ulid task) @@ -454,6 +476,7 @@ doTasks conf connection noteWordsMaybe ids = do liftIO $ insertTags connection + Nothing (TaskUlid newUlid) (fmap TaskToTag.tag tags) @@ -1013,6 +1036,7 @@ duplicateTasks conf connection ids = do liftIO $ insertTags connection + Nothing (TaskUlid dupeUlid) (fmap TaskToTag.tag tags) diff --git a/tasklite-core/test/fixtures/simple.json b/tasklite-core/test/fixtures/simple.json new file mode 100644 index 0000000..0cdb0e4 --- /dev/null +++ b/tasklite-core/test/fixtures/simple.json @@ -0,0 +1,13 @@ +{ + "id": 867, + "body": "Generate statistics for each file before uploading it", + "utc": "2017-10-16 12:37:18", + "tags": [ + "upload", + "frontend" + ], + "notes":[ + { "body": "Don't forget to filter out binary files first" }, + { "body": "Include number of newlines" } + ] +} diff --git a/tasklite-core/test/example-task.json b/tasklite-core/test/fixtures/taskwarrior.json similarity index 100% rename from tasklite-core/test/example-task.json rename to tasklite-core/test/fixtures/taskwarrior.json