Skip to content

Commit

Permalink
Use timestamp of parent task for imported tags and notes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Mar 7, 2020
1 parent 86fcf83 commit ac15d47
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 deletions.
41 changes: 31 additions & 10 deletions tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 36 additions & 12 deletions tasklite-core/source/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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) $
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -454,6 +476,7 @@ doTasks conf connection noteWordsMaybe ids = do

liftIO $ insertTags
connection
Nothing
(TaskUlid newUlid)
(fmap TaskToTag.tag tags)

Expand Down Expand Up @@ -1013,6 +1036,7 @@ duplicateTasks conf connection ids = do

liftIO $ insertTags
connection
Nothing
(TaskUlid dupeUlid)
(fmap TaskToTag.tag tags)

Expand Down
13 changes: 13 additions & 0 deletions tasklite-core/test/fixtures/simple.json
Original file line number Diff line number Diff line change
@@ -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" }
]
}
File renamed without changes.

0 comments on commit ac15d47

Please sign in to comment.