diff --git a/backend/readwise.go b/backend/readwise.go index 98877d4..37201cc 100644 --- a/backend/readwise.go +++ b/backend/readwise.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/go-resty/resty/v2" "io" "io/ioutil" "net/http" @@ -14,6 +13,8 @@ import ( "strings" "time" + "github.com/go-resty/resty/v2" + "github.com/rs/zerolog/log" ) @@ -174,12 +175,28 @@ func BuildPayload(bookmarks []Bookmark, contentIndex map[string]Content) (Respon for _, entry := range bookmarks { source := contentIndex[entry.VolumeID] log.Info().Interface("source", source).Msg("Parsing entry") - t, err := time.Parse("2006-01-02T15:04:05.000", entry.DateCreated) - if err != nil { - log.Error().Err(err).Interface("bookmark", entry).Msg("Failed tp parse timestamp from bookmark") - return Response{}, err + var createdAt string + if entry.DateCreated == "" { + log.Info().Msg("No date created for bookmark. Defaulting to date last modified.") + if entry.DateModified == "" { + log.Info().Msg("No date modified for bookmark. Default to current date.") + createdAt = time.Now().Format("2006-01-02T15:04:05-07:00") + } else { + t, err := time.Parse("2006-01-02T15:04:05Z", entry.DateModified) + if err != nil { + log.Error().Err(err).Interface("bookmark", entry).Msg("Failed to parse a valid timestamp from bookmark") + return Response{}, err + } + createdAt = t.Format("2006-01-02T15:04:05-07:00") + } + } else { + t, err := time.Parse("2006-01-02T15:04:05.000", entry.DateCreated) + if err != nil { + log.Error().Err(err).Interface("bookmark", entry).Msg("Failed to parse a valid timestamp from bookmark") + return Response{}, err + } + createdAt = t.Format("2006-01-02T15:04:05-07:00") } - createdAt := t.Format("2006-01-02T15:04:05-07:00") text := NormaliseText(entry.Text) if entry.Annotation != "" && text == "" { // I feel like this state probably shouldn't be possible but we'll handle it anyway diff --git a/backend/readwise_test.go b/backend/readwise_test.go index 9933938..784b878 100644 --- a/backend/readwise_test.go +++ b/backend/readwise_test.go @@ -92,6 +92,36 @@ func TestBuildPayload_TitleFallbackFailure(t *testing.T) { assert.Equal(t, expected, actual) } +func TestBuildPayload_NoHighlightDateCreated(t *testing.T) { + highlights := []Highlight{{ + Text: "Hello World", + SourceURL: "\t", + SourceType: SourceType, + Category: SourceCategory, + Note: "Making a note here", + HighlightedAt: "2006-01-02T15:04:05+00:00", + }} + expected := Response{Highlights: highlights} + contentIndex := map[string]Content{"\t": {ContentID: "\t"}} + bookmarks := []Bookmark{ + {VolumeID: "\t", Text: "Hello World", DateCreated: "", Annotation: "Making a note here", DateModified: "2006-01-02T15:04:05Z"}, + } + var actual, _ = BuildPayload(bookmarks, contentIndex) + assert.Equal(t, expected, actual) +} + +func TestBuildPayload_NoHighlightDateAtAll(t *testing.T) { + contentIndex := map[string]Content{"\t": {ContentID: "\t"}} + bookmarks := []Bookmark{ + {VolumeID: "abc123", Text: "Hello World", Annotation: "Making a note here"}, + } + var actual, _ = BuildPayload(bookmarks, contentIndex) + assert.Equal(t, actual.Highlights[0].SourceURL, bookmarks[0].VolumeID) + assert.Equal(t, actual.Highlights[0].Text, bookmarks[0].Text) + assert.Equal(t, actual.Highlights[0].Note, bookmarks[0].Annotation) + assert.NotEmpty(t, actual.Highlights[0].HighlightedAt) +} + func TestBuildPayload_SkipMalformedBookmarks(t *testing.T) { var expected Response contentIndex := map[string]Content{"mnt://kobo/blah/Good Book - An Author.epub": {ContentID: "mnt://kobo/blah/Good Book - An Author.epub"}}