1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

Improve OPML package to be more idiomatic

This commit is contained in:
Frédéric Guillot 2017-11-20 19:11:06 -08:00
parent e91a9b4f13
commit c26787f476
7 changed files with 50 additions and 40 deletions

View file

@ -7,21 +7,24 @@ package opml
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/storage"
"io" "io"
"log" "log"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/storage"
) )
type OpmlHandler struct { // Handler handles the logic for OPML import/export.
type Handler struct {
store *storage.Storage store *storage.Storage
} }
func (o *OpmlHandler) Export(userID int64) (string, error) { // Export exports user feeds to OPML.
feeds, err := o.store.GetFeeds(userID) func (h *Handler) Export(userID int64) (string, error) {
feeds, err := h.store.GetFeeds(userID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return "", errors.New("Unable to fetch feeds.") return "", errors.New("unable to fetch feeds")
} }
var subscriptions SubcriptionList var subscriptions SubcriptionList
@ -37,27 +40,28 @@ func (o *OpmlHandler) Export(userID int64) (string, error) {
return Serialize(subscriptions), nil return Serialize(subscriptions), nil
} }
func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) { // Import parses and create feeds from an OPML import.
func (h *Handler) Import(userID int64, data io.Reader) (err error) {
subscriptions, err := Parse(data) subscriptions, err := Parse(data)
if err != nil { if err != nil {
return err return err
} }
for _, subscription := range subscriptions { for _, subscription := range subscriptions {
if !o.store.FeedURLExists(userID, subscription.FeedURL) { if !h.store.FeedURLExists(userID, subscription.FeedURL) {
var category *model.Category var category *model.Category
if subscription.CategoryName == "" { if subscription.CategoryName == "" {
category, err = o.store.GetFirstCategory(userID) category, err = h.store.GetFirstCategory(userID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return errors.New("Unable to find first category.") return errors.New("unable to find first category")
} }
} else { } else {
category, err = o.store.GetCategoryByTitle(userID, subscription.CategoryName) category, err = h.store.GetCategoryByTitle(userID, subscription.CategoryName)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return errors.New("Unable to search category by title.") return errors.New("unable to search category by title")
} }
if category == nil { if category == nil {
@ -66,10 +70,10 @@ func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
Title: subscription.CategoryName, Title: subscription.CategoryName,
} }
err := o.store.CreateCategory(category) err := h.store.CreateCategory(category)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return fmt.Errorf(`Unable to create this category: "%s".`, subscription.CategoryName) return fmt.Errorf(`unable to create this category: "%s"`, subscription.CategoryName)
} }
} }
} }
@ -82,13 +86,14 @@ func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
Category: category, Category: category,
} }
o.store.CreateFeed(feed) h.store.CreateFeed(feed)
} }
} }
return nil return nil
} }
func NewOpmlHandler(store *storage.Storage) *OpmlHandler { // NewHandler creates a new handler for OPML files.
return &OpmlHandler{store: store} func NewHandler(store *storage.Storage) *Handler {
return &Handler{store: store}
} }

View file

@ -6,21 +6,21 @@ package opml
import "encoding/xml" import "encoding/xml"
type Opml struct { type opml struct {
XMLName xml.Name `xml:"opml"` XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"` Version string `xml:"version,attr"`
Outlines []Outline `xml:"body>outline"` Outlines []outline `xml:"body>outline"`
} }
type Outline struct { type outline struct {
Title string `xml:"title,attr,omitempty"` Title string `xml:"title,attr,omitempty"`
Text string `xml:"text,attr"` Text string `xml:"text,attr"`
FeedURL string `xml:"xmlUrl,attr,omitempty"` FeedURL string `xml:"xmlUrl,attr,omitempty"`
SiteURL string `xml:"htmlUrl,attr,omitempty"` SiteURL string `xml:"htmlUrl,attr,omitempty"`
Outlines []Outline `xml:"outline,omitempty"` Outlines []outline `xml:"outline,omitempty"`
} }
func (o *Outline) GetTitle() string { func (o *outline) GetTitle() string {
if o.Title != "" { if o.Title != "" {
return o.Title return o.Title
} }
@ -40,7 +40,7 @@ func (o *Outline) GetTitle() string {
return "" return ""
} }
func (o *Outline) GetSiteURL() string { func (o *outline) GetSiteURL() string {
if o.SiteURL != "" { if o.SiteURL != "" {
return o.SiteURL return o.SiteURL
} }
@ -48,11 +48,11 @@ func (o *Outline) GetSiteURL() string {
return o.FeedURL return o.FeedURL
} }
func (o *Outline) IsCategory() bool { func (o *outline) IsCategory() bool {
return o.Text != "" && o.SiteURL == "" && o.FeedURL == "" return o.Text != "" && o.SiteURL == "" && o.FeedURL == ""
} }
func (o *Outline) Append(subscriptions SubcriptionList, category string) SubcriptionList { func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
if o.FeedURL != "" { if o.FeedURL != "" {
subscriptions = append(subscriptions, &Subcription{ subscriptions = append(subscriptions, &Subcription{
Title: o.GetTitle(), Title: o.GetTitle(),
@ -65,7 +65,7 @@ func (o *Outline) Append(subscriptions SubcriptionList, category string) Subcrip
return subscriptions return subscriptions
} }
func (o *Opml) Transform() SubcriptionList { func (o *opml) Transform() SubcriptionList {
var subscriptions SubcriptionList var subscriptions SubcriptionList
for _, outline := range o.Outlines { for _, outline := range o.Outlines {

View file

@ -14,14 +14,14 @@ import (
// Parse reads an OPML file and returns a SubcriptionList. // Parse reads an OPML file and returns a SubcriptionList.
func Parse(data io.Reader) (SubcriptionList, error) { func Parse(data io.Reader) (SubcriptionList, error) {
opml := new(Opml) feeds := new(opml)
decoder := xml.NewDecoder(data) decoder := xml.NewDecoder(data)
decoder.CharsetReader = charset.NewReaderLabel decoder.CharsetReader = charset.NewReaderLabel
err := decoder.Decode(opml) err := decoder.Decode(feeds)
if err != nil { if err != nil {
return nil, errors.NewLocalizedError("Unable to parse OPML file: %v.", err) return nil, errors.NewLocalizedError("Unable to parse OPML file: %v.", err)
} }
return opml.Transform(), nil return feeds.Transform(), nil
} }

View file

@ -17,13 +17,13 @@ func Serialize(subscriptions SubcriptionList) string {
writer := bufio.NewWriter(&b) writer := bufio.NewWriter(&b)
writer.WriteString(xml.Header) writer.WriteString(xml.Header)
opml := new(Opml) feeds := new(opml)
opml.Version = "2.0" feeds.Version = "2.0"
for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) { for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) {
outline := Outline{Text: categoryName} category := outline{Text: categoryName}
for _, subscription := range subs { for _, subscription := range subs {
outline.Outlines = append(outline.Outlines, Outline{ category.Outlines = append(category.Outlines, outline{
Title: subscription.Title, Title: subscription.Title,
Text: subscription.Title, Text: subscription.Title,
FeedURL: subscription.FeedURL, FeedURL: subscription.FeedURL,
@ -31,12 +31,12 @@ func Serialize(subscriptions SubcriptionList) string {
}) })
} }
opml.Outlines = append(opml.Outlines, outline) feeds.Outlines = append(feeds.Outlines, category)
} }
encoder := xml.NewEncoder(writer) encoder := xml.NewEncoder(writer)
encoder.Indent(" ", " ") encoder.Indent(" ", " ")
if err := encoder.Encode(opml); err != nil { if err := encoder.Encode(feeds); err != nil {
log.Println(err) log.Println(err)
return "" return ""
} }

View file

@ -4,6 +4,7 @@
package opml package opml
// Subcription represents a feed that will be imported or exported.
type Subcription struct { type Subcription struct {
Title string Title string
SiteURL string SiteURL string
@ -11,8 +12,11 @@ type Subcription struct {
CategoryName string CategoryName string
} }
// Equals compare two subscriptions.
func (s Subcription) Equals(subscription *Subcription) bool { func (s Subcription) Equals(subscription *Subcription) bool {
return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL && s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL &&
s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName
} }
// SubcriptionList is a list of subscriptions.
type SubcriptionList []*Subcription type SubcriptionList []*Subcription

View file

@ -5,6 +5,8 @@
package server package server
import ( import (
"net/http"
"github.com/miniflux/miniflux2/locale" "github.com/miniflux/miniflux2/locale"
"github.com/miniflux/miniflux2/reader/feed" "github.com/miniflux/miniflux2/reader/feed"
"github.com/miniflux/miniflux2/reader/opml" "github.com/miniflux/miniflux2/reader/opml"
@ -14,7 +16,6 @@ import (
"github.com/miniflux/miniflux2/server/template" "github.com/miniflux/miniflux2/server/template"
ui_controller "github.com/miniflux/miniflux2/server/ui/controller" ui_controller "github.com/miniflux/miniflux2/server/ui/controller"
"github.com/miniflux/miniflux2/storage" "github.com/miniflux/miniflux2/storage"
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -25,7 +26,7 @@ func getRoutes(store *storage.Storage, feedHandler *feed.Handler) *mux.Router {
templateEngine := template.NewTemplateEngine(router, translator) templateEngine := template.NewTemplateEngine(router, translator)
apiController := api_controller.NewController(store, feedHandler) apiController := api_controller.NewController(store, feedHandler)
uiController := ui_controller.NewController(store, feedHandler, opml.NewOpmlHandler(store)) uiController := ui_controller.NewController(store, feedHandler, opml.NewHandler(store))
apiHandler := core.NewHandler(store, router, templateEngine, translator, middleware.NewMiddlewareChain( apiHandler := core.NewHandler(store, router, templateEngine, translator, middleware.NewMiddlewareChain(
middleware.NewBasicAuthMiddleware(store).Handler, middleware.NewBasicAuthMiddleware(store).Handler,

View file

@ -25,7 +25,7 @@ func (t tplParams) Merge(d tplParams) tplParams {
type Controller struct { type Controller struct {
store *storage.Storage store *storage.Storage
feedHandler *feed.Handler feedHandler *feed.Handler
opmlHandler *opml.OpmlHandler opmlHandler *opml.Handler
} }
func (c *Controller) getCommonTemplateArgs(ctx *core.Context) (tplParams, error) { func (c *Controller) getCommonTemplateArgs(ctx *core.Context) (tplParams, error) {
@ -47,7 +47,7 @@ func (c *Controller) getCommonTemplateArgs(ctx *core.Context) (tplParams, error)
return params, nil return params, nil
} }
func NewController(store *storage.Storage, feedHandler *feed.Handler, opmlHandler *opml.OpmlHandler) *Controller { func NewController(store *storage.Storage, feedHandler *feed.Handler, opmlHandler *opml.Handler) *Controller {
return &Controller{ return &Controller{
store: store, store: store,
feedHandler: feedHandler, feedHandler: feedHandler,