Skip to content

Commit

Permalink
De-duplicate notes when editing a task
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed May 13, 2024
1 parent a7f2055 commit c668840
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
24 changes: 13 additions & 11 deletions tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,21 @@ insertImportTask connection importTaskRecord = do
then taskParsed{Task.user = T.pack effectiveUserName}
else taskParsed
insertRecord "tasks" connection theTask
warnings <-
tagWarnings <-
insertTags
connection
(ulidTextToDateTime taskParsed.ulid)
theTask
importTaskRecord.tags
insertNotes
connection
(ulidTextToDateTime taskParsed.ulid)
theTask
importTaskRecord.notes
noteWarnings <-
insertNotes
connection
(ulidTextToDateTime taskParsed.ulid)
theTask
importTaskRecord.notes
pure $
warnings
tagWarnings
<$$> noteWarnings
<$$> "📥 Imported task"
<+> dquotes (pretty theTask.body)
<+> "with ulid"
Expand Down Expand Up @@ -754,7 +756,7 @@ editTaskByTask preEdit conn taskToEdit = do
Left error -> die $ show error <> " in task \n" <> show newContent
Right importTaskRecord -> do
effectiveUserName <- getEffectiveUserName
now <- getULIDTimeStamp
now <- getULIDTimeStamp <&> (show >>> T.toLower)
let
taskFixed =
importTaskRecord.task
Expand All @@ -775,8 +777,7 @@ editTaskByTask preEdit conn taskToEdit = do
note
{ Note.ulid =
if zeroUlidTxt `T.isPrefixOf` note.ulid
then
note.ulid & T.replace zeroUlidTxt (show now)
then note.ulid & T.replace zeroUlidTxt now
else note.ulid
}
)
Expand All @@ -791,9 +792,10 @@ editTaskByTask preEdit conn taskToEdit = do
updateTask conn taskFixed{Task.modified_utc = show @DateTime now_}

tagWarnings <- insertTags conn Nothing taskFixed importTaskRecord.tags
insertNotes conn Nothing taskFixed notesCorrectUtc
noteWarnings <- insertNotes conn Nothing taskFixed notesCorrectUtc
pure $
tagWarnings
<$$> noteWarnings
<$$> "✏️ Edited task"
<+> dquotes (pretty taskFixed.body)
<+> "with ulid"
Expand Down
24 changes: 19 additions & 5 deletions tasklite-core/source/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ import Config (
HooksConfig (add),
defaultConfig,
)
import Control.Monad.Catch (catchIf)
import Control.Monad.Catch (catchAll, catchIf)
import FullTask (
FullTask (
awake_utc,
Expand Down Expand Up @@ -399,9 +399,11 @@ insertTags connection mbCreatedUtc task tags = do
pure $ vsepCollapse insertWarnings


insertNotes :: Connection -> Maybe DateTime -> Task -> [Note] -> IO ()
insertNotes
:: Connection -> Maybe DateTime -> Task -> [Note] -> IO (Doc AnsiStyle)
insertNotes connection mbCreatedUtc task notes = do
taskToNotes <- forM notes $ \theNote -> do
let uniqueNotes = nub notes
taskToNotes <- forM uniqueNotes $ \theNote -> do
newUlid <- getULID
pure $
TaskToNote
Expand All @@ -418,8 +420,20 @@ insertNotes connection mbCreatedUtc task notes = do
, note = theNote.body
}

P.forM_ taskToNotes $ \taskToNote ->
insertRecord "task_to_note" connection taskToNote
insertWarnings <- P.forM taskToNotes $ \taskToNote ->
catchAll
(insertRecord "task_to_note" connection taskToNote P.>> pure "")
( \exception ->
pure $
annotate (color Yellow) $
"⚠️ Note "
<> dquotes (pretty taskToNote.note)
<> " could not be inserted"
<+> "ERROR:"
<+> pretty (show exception :: Text)
)

pure $ vsepCollapse insertWarnings


-- | Tuple is (Maybe createdUtc, noteBody)
Expand Down
51 changes: 44 additions & 7 deletions tasklite-core/test/LibSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ normTask task =
}


replaceBs :: P.ByteString -> P.ByteString -> P.ByteString -> P.ByteString
replaceBs needle replacement haystack = do
haystack
& P.decodeUtf8
& T.replace (P.decodeUtf8 needle) (P.decodeUtf8 replacement)
& P.encodeUtf8


spec :: Spec
spec = do
let
Expand Down Expand Up @@ -465,13 +473,6 @@ spec = do
withMemoryDb defaultConfig $ \memConn -> do
insertRecord "tasks" memConn task1

let
replaceBs needle replacement haystack = do
haystack
& P.decodeUtf8
& T.replace (P.decodeUtf8 needle) (P.decodeUtf8 replacement)
& P.encodeUtf8

cliOutput <-
editTaskByTask
( ApplyPreEdit $
Expand Down Expand Up @@ -514,6 +515,42 @@ spec = do
taskToNote.note `shouldBe` "A short note"
_ -> P.die "Found more than one task_to_tag row"

it "de-duplicates notes" $ do
withMemoryDb defaultConfig $ \memConn -> do
insertRecord "tasks" memConn task1
let note = "A short note"
insertRecord
"task_to_note"
memConn
TaskToNote
{ TaskToNote.ulid = "01hxsjgzmdx48yzk39v852razr"
, TaskToNote.task_ulid = task1.ulid
, TaskToNote.note = note
}
taskInfo <- infoTask defaultConfig memConn task1.ulid
show taskInfo `shouldContain` T.unpack note
cliOutput <-
editTaskByTask
( ApplyPreEdit $
replaceBs "notes: []" $
"notes: " <> P.show [note, note]
)
memConn
task1
show cliOutput `shouldStartWith` "✏️ Edited task \"New task 1\""

taskToNotes :: [TaskToNote] <-
query_ memConn "SELECT * FROM task_to_note"
case taskToNotes of
[taskToNoteA, taskToNoteB] -> do
taskToNoteA.ulid
`shouldNotSatisfy` (\ulid -> zeroUlidTxt `T.isPrefixOf` ulid)
taskToNoteB.ulid
`shouldNotSatisfy` (\ulid -> zeroUlidTxt `T.isPrefixOf` ulid)
taskToNoteA.note `shouldBe` "A short note"
taskToNoteB.note `shouldBe` "A short note"
_ -> P.die "Found more than one task_to_tag row"

it "lets you set metadata to null" $ do
withMemoryDb defaultConfig $ \memConn -> do
insertRecord "tasks" memConn task1{metadata = Just "{\"a\":\"b\"}"}
Expand Down

0 comments on commit c668840

Please sign in to comment.