Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac15d47

Browse files
committed
Use timestamp of parent task for imported tags and notes
1 parent 86fcf83 commit ac15d47

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed

tasklite-core/source/ImportExport.hs

+31-10
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,15 @@ data ImportTask = ImportTask
7676
-- | Values a suffixed with a prime (') to avoid name collisions
7777
instance FromJSON ImportTask where
7878
parseJSON = withObject "task" $ \o -> do
79+
utc <- o .:? "utc"
7980
entry <- o .:? "entry"
8081
creation <- o .:? "creation"
8182
created_at <- o .:? "created_at"
82-
let createdUtc = fromMaybe (timeFromElapsedP 0 :: DateTime)
83-
(parseUtc =<< (entry <|> creation <|> created_at))
83+
let
84+
zeroTime = timeFromElapsedP 0 :: DateTime
85+
parsedCreatedUtc = parseUtc
86+
=<< (utc <|> entry <|> creation <|> created_at)
87+
createdUtc = fromMaybe zeroTime parsedCreatedUtc
8488

8589
o_body <- o .:? "body"
8690
description <- o .:? "description"
@@ -91,8 +95,8 @@ instance FromJSON ImportTask where
9195
let state = textToTaskState =<< (o_state <|> status)
9296

9397
o_priority_adjustment <- o .:? "priority_adjustment"
94-
urgency <- o .:? "urgency"
95-
priority <- optional (o .: "priority")
98+
urgency <- o .:? "urgency"
99+
priority <- optional (o .: "priority")
96100
let priority_adjustment = o_priority_adjustment <|> urgency <|> priority
97101

98102
modified <- o .:? "modified"
@@ -205,9 +209,22 @@ instance FromJSON ImportTask where
205209
annotations <- o .:? "annotations" :: Parser (Maybe [Annotation])
206210
let
207211
notes = case (o_notes, annotations) of
208-
(Just theNotes , _ ) -> theNotes
212+
(Nothing, Nothing) -> []
209213
(Nothing, Just values) -> values <$$> annotationToNote
210-
_ -> []
214+
(Just theNotes , _) -> case parsedCreatedUtc of
215+
Just crUtc -> theNotes <&> (\theNote ->
216+
let
217+
noteUlidTxt = Note.ulid theNote
218+
mbNoteUlid = parseUlidText noteUlidTxt
219+
mbNewUlid = do
220+
noteUlid <- mbNoteUlid
221+
222+
pure $ show $ setDateTime noteUlid crUtc
223+
in
224+
theNote { Note.ulid =
225+
(T.toLower $ fromMaybe noteUlidTxt mbNewUlid) }
226+
)
227+
Nothing -> theNotes
211228

212229
o_user <- o .:? "user"
213230
let user = fromMaybe "" o_user
@@ -244,8 +261,10 @@ insertImportTask connection importTaskRecord = do
244261
then taskParsed { Task.user = T.pack effectiveUserName }
245262
else taskParsed
246263
insertTask connection theTask
247-
insertTags connection (primaryKey theTask) (tags importTaskRecord)
248-
insertNotes connection (primaryKey theTask) (notes importTaskRecord)
264+
insertTags connection (ulidTextToDateTime $ Task.ulid taskParsed)
265+
(primaryKey theTask) (tags importTaskRecord)
266+
insertNotes connection (ulidTextToDateTime $ Task.ulid taskParsed)
267+
(primaryKey theTask) (notes importTaskRecord)
249268
pure $
250269
"📥 Imported task" <+> dquotes (pretty $ Task.body theTask)
251270
<+> "with ulid" <+> dquotes (pretty $ Task.ulid theTask)
@@ -364,8 +383,10 @@ editTask conf connection idSubstr = do
364383
}
365384

366385
replaceTask connection taskFixed
367-
insertTags connection (primaryKey taskFixed) (tags importTaskRecord)
368-
insertNotes connection (primaryKey taskFixed) (notes importTaskRecord)
386+
insertTags connection Nothing
387+
(primaryKey taskFixed) (tags importTaskRecord)
388+
insertNotes connection Nothing
389+
(primaryKey taskFixed) (notes importTaskRecord)
369390
pure $
370391
"✏️ Edited task" <+> dquotes (pretty $ Task.body taskFixed)
371392
<+> "with ulid" <+> dquotes (pretty $ Task.ulid taskFixed)

tasklite-core/source/Lib.hs

+36-12
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,40 @@ replaceTask connection task = do
114114
save (_tldbTasks taskLiteDb) task
115115

116116

117-
insertTags :: Connection -> TaskUlid -> [Text] -> IO ()
118-
insertTags connection taskUlid tags = do
117+
insertTags :: Connection -> Maybe DateTime -> TaskUlid -> [Text] -> IO ()
118+
insertTags connection mbCreatedUtc taskUlid tags = do
119119
taskToTags <- forM tags $ \tag -> do
120-
tagUlid <- formatUlid getULID
121-
pure $ TaskToTag tagUlid taskUlid tag
120+
tagUlid <- getULID
121+
pure $ TaskToTag
122+
(mbCreatedUtc
123+
<&> setDateTime tagUlid
124+
& fromMaybe tagUlid
125+
& show
126+
& toLower)
127+
taskUlid
128+
tag
122129

123130
runBeamSqlite connection $ runInsert $
124131
insert (_tldbTaskToTag taskLiteDb) $
125132
insertValues taskToTags
126133

127134

128-
insertNotes :: Connection -> TaskUlid -> [Note] -> IO ()
129-
insertNotes connection primKey notes = do
135+
insertNotes :: Connection -> Maybe DateTime -> TaskUlid -> [Note] -> IO ()
136+
insertNotes connection mbCreatedUtc primKey notes = do
130137
taskToNotes <- forM notes $ \theNote -> do
131-
pure $ TaskToNote (Note.ulid theNote) primKey (Note.body theNote)
138+
let
139+
noteUlidTxt = Note.ulid theNote
140+
mbNoteUlid = parseUlidText $ noteUlidTxt
141+
mbNewUlid = do
142+
createdUtc <- mbCreatedUtc
143+
noteUlid <- mbNoteUlid
144+
145+
pure $ show $ setDateTime noteUlid createdUtc
146+
147+
pure $ TaskToNote
148+
(toLower $ fromMaybe noteUlidTxt mbNewUlid)
149+
primKey
150+
(Note.body theNote)
132151

133152
runBeamSqlite connection $ runInsert $
134153
insert (_tldbTaskToNote taskLiteDb) $
@@ -138,11 +157,14 @@ insertNotes connection primKey notes = do
138157
-- | Tuple is (Maybe createdUtc, noteBody)
139158
insertNoteTuples :: Connection -> TaskUlid -> [(Maybe DateTime, Text)] -> IO ()
140159
insertNoteTuples connection taskUlid notes = do
141-
taskToNotes <- forM notes $ \(createdUtc, noteBody) -> do
160+
taskToNotes <- forM notes $ \(mbCreatedUtc, noteBody) -> do
142161
noteUlid <- getULID
143162
pure $ TaskToNote
144-
((toLower . show . maybe noteUlid (setDateTime noteUlid))
145-
createdUtc)
163+
(mbCreatedUtc
164+
<&> setDateTime noteUlid
165+
& fromMaybe noteUlid
166+
& show
167+
& toLower)
146168
taskUlid
147169
noteBody
148170

@@ -213,7 +235,7 @@ addTask conf connection bodyWords = do
213235
}
214236

215237
insertTask connection task
216-
insertTags connection (primaryKey task) tags
238+
insertTags connection Nothing (primaryKey task) tags
217239
pure $
218240
"🆕 Added task" <+> dquotes (pretty $ Task.body task)
219241
<+> "with id" <+> dquotes (pretty $ Task.ulid task)
@@ -236,7 +258,7 @@ logTask conf connection bodyWords = do
236258
}
237259

238260
insertTask connection task
239-
insertTags connection (primaryKey task) tags
261+
insertTags connection Nothing (primaryKey task) tags
240262
pure $
241263
"📝 Logged task" <+> dquotes (pretty $ Task.body task)
242264
<+> "with id" <+> dquotes (pretty $ Task.ulid task)
@@ -454,6 +476,7 @@ doTasks conf connection noteWordsMaybe ids = do
454476

455477
liftIO $ insertTags
456478
connection
479+
Nothing
457480
(TaskUlid newUlid)
458481
(fmap TaskToTag.tag tags)
459482

@@ -1013,6 +1036,7 @@ duplicateTasks conf connection ids = do
10131036

10141037
liftIO $ insertTags
10151038
connection
1039+
Nothing
10161040
(TaskUlid dupeUlid)
10171041
(fmap TaskToTag.tag tags)
10181042

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": 867,
3+
"body": "Generate statistics for each file before uploading it",
4+
"utc": "2017-10-16 12:37:18",
5+
"tags": [
6+
"upload",
7+
"frontend"
8+
],
9+
"notes":[
10+
{ "body": "Don't forget to filter out binary files first" },
11+
{ "body": "Include number of newlines" }
12+
]
13+
}

0 commit comments

Comments
 (0)