Skip to content

Commit

Permalink
Support instances where bookmarks have no created date
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-crane committed Jun 4, 2022
1 parent 8ede6bd commit b5410cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
29 changes: 23 additions & 6 deletions backend/readwise.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"io"
"io/ioutil"
"net/http"
Expand All @@ -14,6 +13,8 @@ import (
"strings"
"time"

"github.com/go-resty/resty/v2"

"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions backend/readwise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down

0 comments on commit b5410cb

Please sign in to comment.