From 929041c692843c3a83b71f0b1d822ab2c259ceca Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sat, 6 Apr 2019 13:28:36 -0700 Subject: [PATCH] Truncate long episode descriptions --- pkg/feeds/feeds.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/feeds/feeds.go b/pkg/feeds/feeds.go index f868c56..06d0b9c 100644 --- a/pkg/feeds/feeds.go +++ b/pkg/feeds/feeds.go @@ -131,9 +131,19 @@ func makeEnclosure(feed *model.Feed, id string, lengthInBytes int64) (string, it return url, contentType, lengthInBytes } +func short(str string, i int) string { + runes := []rune(str) + if len(runes) > i { + return string(runes[:i]) + " ..." + } + + return str +} + func (s *Service) BuildFeed(hashID string) ([]byte, error) { const ( - cacheTTL = 30 * time.Minute + cacheTTL = 30 * time.Minute + maxDescriptionLen = 384 ) cached, err := s.cache.Get(hashID) @@ -164,6 +174,12 @@ func (s *Service) BuildFeed(hashID string) ([]byte, error) { return nil, err } + if len(feed.Episodes) > 300 { + for _, episode := range feed.Episodes { + episode.Description = short(episode.Description, maxDescriptionLen) + } + } + if oldLastID != feed.LastID { if err := s.storage.UpdateFeed(feed); err != nil { log.WithError(err).WithField("feed_id", hashID).Error("failed to save feed")