aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader
diff options
context:
space:
mode:
authorGravatar Ilya Glotov <contact@ilyaglotov.com>2019-07-05 19:05:42 +0300
committerGravatar Ilya Glotov <contact@ilyaglotov.com>2019-07-05 20:34:49 +0300
commitc840268678897c4bd95857a9b099fa4f149fcd8f (patch)
treed1c1612637cb227d1f6edd39fd0ef6040f7b5c01 /reader
parentf783b135c770449f5890e4ef71ff682e95f36b55 (diff)
Sort feed categories before serialization
A function is added for feeds and its categories normalization. The test will ensure that the order is right.
Diffstat (limited to 'reader')
-rw-r--r--reader/opml/serializer.go47
-rw-r--r--reader/opml/serializer_test.go24
2 files changed, 54 insertions, 17 deletions
diff --git a/reader/opml/serializer.go b/reader/opml/serializer.go
index c094f4c..31619e1 100644
--- a/reader/opml/serializer.go
+++ b/reader/opml/serializer.go
@@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"encoding/xml"
+ "sort"
"miniflux.app/logger"
)
@@ -18,23 +19,7 @@ func Serialize(subscriptions SubcriptionList) string {
writer := bufio.NewWriter(&b)
writer.WriteString(xml.Header)
- feeds := new(opml)
- feeds.Version = "2.0"
- for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) {
- category := outline{Text: categoryName}
-
- for _, subscription := range subs {
- category.Outlines = append(category.Outlines, outline{
- Title: subscription.Title,
- Text: subscription.Title,
- FeedURL: subscription.FeedURL,
- SiteURL: subscription.SiteURL,
- })
- }
-
- feeds.Outlines = append(feeds.Outlines, category)
- }
-
+ feeds := normalizeFeeds(subscriptions)
encoder := xml.NewEncoder(writer)
encoder.Indent(" ", " ")
if err := encoder.Encode(feeds); err != nil {
@@ -54,3 +39,31 @@ func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]Subcript
return groups
}
+
+func normalizeFeeds(subscriptions SubcriptionList) *opml {
+ feeds := new(opml)
+ feeds.Version = "2.0"
+
+ groupedSubs := groupSubscriptionsByFeed(subscriptions)
+ var categories []string
+ for k := range groupedSubs {
+ categories = append(categories, k)
+ }
+ sort.Strings(categories)
+
+ for _, categoryName := range categories {
+ category := outline{Text: categoryName}
+ for _, subscription := range groupedSubs[categoryName] {
+ category.Outlines = append(category.Outlines, outline{
+ Title: subscription.Title,
+ Text: subscription.Title,
+ FeedURL: subscription.FeedURL,
+ SiteURL: subscription.SiteURL,
+ })
+ }
+
+ feeds.Outlines = append(feeds.Outlines, category)
+ }
+
+ return feeds
+}
diff --git a/reader/opml/serializer_test.go b/reader/opml/serializer_test.go
index 9c598ee..c0acd90 100644
--- a/reader/opml/serializer_test.go
+++ b/reader/opml/serializer_test.go
@@ -40,3 +40,27 @@ func TestSerialize(t *testing.T) {
t.Error("Serialized feed is incorrect")
}
}
+
+func TestNormalizedCategoriesOrder(t *testing.T) {
+ var orderTests = []struct {
+ naturalOrderName string
+ correctOrderName string
+ }{
+ {"Category 2", "Category 1"},
+ {"Category 3", "Category 2"},
+ {"Category 1", "Category 3"},
+ }
+
+ var subscriptions SubcriptionList
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName})
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName})
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName})
+
+ feeds := normalizeFeeds(subscriptions)
+
+ for i, o := range orderTests {
+ if feeds.Outlines[i].Text != o.correctOrderName {
+ t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text)
+ }
+ }
+}