Skip to content

Commit

Permalink
storage: change GetReadTime() function to use entries_feed_id_hash_ke…
Browse files Browse the repository at this point in the history
…y index
  • Loading branch information
fguillot committed Apr 10, 2024
1 parent 35edd8e commit 38b80d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 9 additions & 5 deletions internal/reader/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ func getUrlFromEntry(feed *model.Feed, entry *model.Entry) string {
}

func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) {
if !user.ShowReadingTime {
slog.Debug("Skip reading time estimation for this user", slog.Int64("user_id", user.ID))
return
}

if shouldFetchYouTubeWatchTime(entry) {
if entryIsNew {
watchTime, err := fetchYouTubeWatchTime(entry.URL)
Expand All @@ -266,7 +271,7 @@ func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *mod
}
entry.ReadingTime = watchTime
} else {
entry.ReadingTime = store.GetReadTime(entry, feed)
entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
}
}

Expand All @@ -285,14 +290,13 @@ func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *mod
}
entry.ReadingTime = watchTime
} else {
entry.ReadingTime = store.GetReadTime(entry, feed)
entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
}
}

// Handle YT error case and non-YT entries.
if entry.ReadingTime == 0 {
if user.ShowReadingTime {
entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
}
entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
}
}

Expand Down
17 changes: 7 additions & 10 deletions internal/storage/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,21 @@ func (s *Storage) IsNewEntry(feedID int64, entryHash string) bool {
return !result
}

// GetReadTime fetches the read time of an entry based on its hash, and the feed id and user id from the feed.
// It's intended to be used on entries objects created by parsing a feed as they don't contain much information.
// The feed param helps to scope the search to a specific user and feed in order to avoid hash clashes.
func (s *Storage) GetReadTime(entry *model.Entry, feed *model.Feed) int {
func (s *Storage) GetReadTime(feedID int64, entryHash string) int {
var result int

// Note: This query uses entries_feed_id_hash_key index
s.db.QueryRow(
`SELECT
reading_time
FROM
entries
WHERE
user_id=$1 AND
feed_id=$2 AND
hash=$3
feed_id=$1 AND
hash=$2
`,
feed.UserID,
feed.ID,
entry.Hash,
feedID,
entryHash,
).Scan(&result)
return result
}
Expand Down

0 comments on commit 38b80d9

Please sign in to comment.