8000 [pull] main from miniflux:main by pull[bot] · Pull Request #297 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 117 additions & 35 deletions internal/reader/opml/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/storage"
"miniflux.app/v2/internal/validator"
)

// Handler handles the logic for OPML import/export.
Expand All @@ -31,6 +32,24 @@ func (h *Handler) Export(userID int64) (string, error) {
SiteURL: feed.SiteURL,
Description: feed.Description,
CategoryName: feed.Category.Title,

ScraperRules: feed.ScraperRules,
RewriteRules: feed.RewriteRules,
UrlRewriteRules: feed.UrlRewriteRules,
BlocklistRules: feed.BlocklistRules,
KeeplistRules: feed.KeeplistRules,
BlockFilterEntryRules: feed.BlockFilterEntryRules,
KeepFilterEntryRules: feed.KeepFilterEntryRules,
UserAgent: feed.UserAgent,
Crawler: feed.Crawler,
IgnoreHTTPCache: feed.IgnoreHTTPCache,
FetchViaProxy: feed.FetchViaProxy,
Disabled: feed.Disabled,
NoMediaPlayer: feed.NoMediaPlayer,
HideGlobally: feed.HideGlobally,
AllowSelfSignedCertificates: feed.AllowSelfSignedCertificates,
DisableHTTP2: feed.DisableHTTP2,
IgnoreEntryUpdates: feed.IgnoreEntryUpdates,
})
}

Expand All @@ -45,42 +64,105 @@ func (h *Handler) Import(userID int64, data io.Reader) error {
}

for _, subscription := range subscriptions {
if !h.store.FeedURLExists(userID, subscription.FeedURL) {
var category *model.Category
var err error

if subscription.CategoryName == "" {
category, err = h.store.FirstCategory(userID)
if err != nil {
return fmt.Errorf("opml: unable to find first category: %w", err)
}
} else {
category, err = h.store.CategoryByTitle(userID, subscription.CategoryName)
if err != nil {
return fmt.Errorf("opml: unable to search category by title: %w", err)
}

if category == nil {
category, err = h.store.CreateCategory(userID, &model.CategoryCreationRequest{Title: subscription.CategoryName})
if err != nil {
return fmt.Errorf(`opml: unable to create this category: %q`, subscription.CategoryName)
}
}
}

feed := &model.Feed{
UserID: userID,
Title: subscription.Title,
FeedURL: subscription.FeedURL,
SiteURL: subscription.SiteURL,
Description: subscription.Description,
Category: category,
}

if err := h.store.CreateFeed(feed); err != nil {
return fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
}
if h.store.FeedURLExists(userID, subscription.FeedURL) {
continue
}

category, err := h.resolveCategory(userID, subscription.CategoryName)
if err != nil {
return err
}

if validationErr := validateSubscription(userID, category.ID, h.store, subscription); validationErr != nil {
return fmt.Errorf(`opml: invalid feed settings for %q: %w`, subscription.FeedURL, validationErr)
}

feed := &model.Feed{
UserID: userID,
Title: subscription.Title,
FeedURL: subscription.FeedURL,
SiteURL: subscription.SiteURL,
Description: subscription.Description,
Category: category,
}
applySubscriptionSettings(feed, subscription)
if err := h.store.CreateFeed(feed); err != nil {
return fmt.Errorf(`opml: unable to create this feed: %q`, subscription.FeedURL)
}
}

return nil
}

func (h *Handler) resolveCategory(userID int64, categoryName string) (*model.Category, error) {
if categoryName == "" {
category, err := h.store.FirstCategory(userID)
if err != nil {
return nil, fmt.Errorf("opml: unable to find first category: %w", err)
}
return category, nil
}

category, err := h.store.CategoryByTitle(userID, categoryName)
if err != nil {
return nil, fmt.Errorf("opml: unable to search category by title: %w", err)
}

if category == nil {
category, err = h.store.CreateCategory(userID, &model.CategoryCreationRequest{Title: categoryName})
if err != nil {
return nil, fmt.Errorf(`opml: unable to create this category: %q`, categoryName)
}
}

return category, nil
}

func applySubscriptionSettings(feed *model.Feed, s subcription) {
feed.ScraperRules = s.ScraperRules
feed.RewriteRules = s.RewriteRules
feed.UrlRewriteRules = s.UrlRewriteRules
feed.BlocklistRules = s.BlocklistRules
feed.KeeplistRules = s.KeeplistRules
feed.BlockFilterEntryRules = s.BlockFilterEntryRules
feed.KeepFilterEntryRules = s.KeepFilterEntryRules
feed.UserAgent = s.UserAgent
feed.Crawler = s.Crawler
feed.IgnoreHTTPCache = s.IgnoreHTTPCache
feed.FetchViaProxy = s.FetchViaProxy
feed.Disabled = s.Disabled
feed.NoMediaPlayer = s.NoMediaPlayer
feed.HideGlobally = s.HideGlobally
feed.AllowSelfSignedCertificates = s.AllowSelfSignedCertificates
feed.DisableHTTP2 = s.DisableHTTP2
feed.IgnoreEntryUpdates = s.IgnoreEntryUpdates
}

func validateSubscription(userID, categoryID int64, store *storage.Storage, s subcription) error {
feedCreationRequest := &model.FeedCreationRequest{
FeedURL: s.FeedURL,
CategoryID: categoryID,
UserAgent: s.UserAgent,
Crawler: s.Crawler,
IgnoreEntryUpdates: s.IgnoreEntryUpdates,
Disabled: s.Disabled,
NoMediaPlayer: s.NoMediaPlayer,
IgnoreHTTPCache: s.IgnoreHTTPCache,
AllowSelfSignedCertificates: s.AllowSelfSignedCertificates,
FetchViaProxy: s.FetchViaProxy,
HideGlobally: s.HideGlobally,
DisableHTTP2: s.DisableHTTP2,
ScraperRules: s.ScraperRules,
RewriteRules: s.RewriteRules,
BlocklistRules: s.BlocklistRules,
KeeplistRules: s.KeeplistRules,
BlockFilterEntryRules: s.BlockFilterEntryRules,
KeepFilterEntryRules: s.KeepFilterEntryRules,
UrlRewriteRules: s.UrlRewriteRules,
}

if validationErr := validator.ValidateFeedCreation(store, userID, feedCreationRequest); validationErr != nil {
return validationErr.Error()
}

return nil
Expand Down
102 changes: 98 additions & 4 deletions internal/reader/opml/opml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ package opml // import "miniflux.app/v2/internal/reader/opml"

import (
"encoding/xml"
"fmt"
"strconv"
"strings"
)

const minifluxOPMLNamespace = "https://miniflux.app/opml"

// Specs: http://opml.org/spec2.opml
type opmlDocument struct {
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
Header opmlHeader `xml:"head"`
Outlines opmlOutlineCollection `xml:"body>outline"`
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
MinifluxNamespace string `xml:"xmlns:miniflux,attr,omitempty"`
Header opmlHeader `xml:"head"`
Outlines opmlOutlineCollection `xml:"body>outline"`
}

type opmlHeader struct {
Expand All @@ -29,6 +34,25 @@ type opmlOutline struct {
SiteURL string `xml:"htmlUrl,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
Outlines opmlOutlineCollection `xml:"outline,omitempty"`

// Miniflux-specific feed settings
ScraperRules string `xml:"miniflux:scraperRules,attr,omitempty"`
RewriteRules string `xml:"miniflux:rewriteRules,attr,omitempty"`
UrlRewriteRules string `xml:"miniflux:urlRewriteRules,attr,omitempty"`
BlocklistRules string `xml:"miniflux:blocklistRules,attr,omitempty"`
KeeplistRules string `xml:"miniflux:keeplistRules,attr,omitempty"`
BlockFilterEntryRules string `xml:"miniflux:blockFilterEntryRules,attr,omitempty"`
KeepFilterEntryRules string `xml:"miniflux:keepFilterEntryRules,attr,omitempty"`
UserAgent string `xml:"miniflux:userAgent,attr,omitempty"`
Crawler bool `xml:"miniflux:crawler,attr,omitempty"`
IgnoreHTTPCache bool `xml:"miniflux:ignoreHTTPCache,attr,omitempty"`
FetchViaProxy bool `xml:"miniflux:fetchViaProxy,attr,omitempty"`
Disabled bool `xml:"miniflux:disabled,attr,omitempty"`
NoMediaPlayer bool `xml:"miniflux:noMediaPlayer,attr,omitempty"`
HideGlobally bool `xml:"miniflux:hideGlobally,attr,omitempty"`
AllowSelfSignedCertificates bool `xml:"miniflux:allowSelfSignedCertificates,attr,omitempty"`
DisableHTTP2 bool `xml:"miniflux:disableHTTP2,attr,omitempty"`
IgnoreEntryUpdates bool `xml:"miniflux:ignoreEntryUpdates,attr,omitempty"`
}

func (o opmlOutline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
Expand All @@ -48,6 +72,25 @@ func (o opmlOutline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
}, start)
}

func (o *opmlOutline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
*o = opmlOutline{}

type opmlOutlineAlias opmlOutline
if err := d.DecodeElement((*opmlOutlineAlias)(o), &start); err != nil {
return err
}

for _, attr := range start.Attr {
if attr.Name.Space == minifluxOPMLNamespace {
if err := o.setMinifluxAttribute(attr.Name.Local, attr.Value); err != nil {
return err
}
}
}

return nil
}

func (o opmlOutline) IsSubscription() bool {
return strings.TrimSpace(o.FeedURL) != ""
}
Expand Down Expand Up @@ -85,3 +128,54 @@ type opmlOutlineCollection []opmlOutline
func (o opmlOutlineCollection) HasChildren() bool {
return len(o) > 0
}

func (o *opmlOutline) setMinifluxAttribute(name, value string) error {
switch name {
case "scraperRules":
o.ScraperRules = value
case "rewriteRules":
o.RewriteRules = value
case "urlRewriteRules":
o.UrlRewriteRules = value
case "blocklistRules":
o.BlocklistRules = value
case "keeplistRules":
o.KeeplistRules = value
case "blockFilterEntryRules":
o.BlockFilterEntryRules = value
case "keepFilterEntryRules":
o.KeepFilterEntryRules = value
case "userAgent":
o.UserAgent = value
case "crawler":
return setMinifluxBoolAttribute(name, value, &o.Crawler)
case "ignoreHTTPCache":
return setMinifluxBoolAttribute(name, value, &o.IgnoreHTTPCache)
case "fetchViaProxy":
return setMinifluxBoolAttribute(name, value, &o.FetchViaProxy)
case "disabled":
return setMinifluxBoolAttribute(name, value, &o.Disabled)
case "noMediaPlayer":
return setMinifluxBoolAttribute(name, value, &o.NoMediaPlayer)
case "hideGlobally":
return setMinifluxBoolAttribute(name, value, &o.HideGlobally)
case "allowSelfSignedCertificates":
return setMinifluxBoolAttribute(name, value, &o.AllowSelfSignedCertificates)
case "disableHTTP2":
return setMinifluxBoolAttribute(name, value, &o.DisableHTTP2)
case "ignoreEntryUpdates":
return setMinifluxBoolAttribute(name, value, &o.IgnoreEntryUpdates)
}

return nil
}

func setMinifluxBoolAttribute(name, value string, target *bool) error {
parsedValue, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("opml: invalid miniflux attribute %q: %w", name, err)
}

*target = parsedValue
return nil
}
18 changes: 18 additions & 0 deletions internal/reader/opml/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func getSubscriptionsFromOutlines(outlines opmlOutlineCollection, category strin
SiteURL: outline.GetSiteURL(),
Description: outline.Description,
CategoryName: category,

ScraperRules: outline.ScraperRules,
RewriteRules: outline.RewriteRules,
UrlRewriteRules: outline.UrlRewriteRules,
BlocklistRules: outline.BlocklistRules,
KeeplistRules: outline.KeeplistRules,
BlockFilterEntryRules: outline.BlockFilterEntryRules,
KeepFilterEntryRules: outline.KeepFilterEntryRules,
UserAgent: outline.UserAgent,
Crawler: outline.Crawler,
IgnoreHTTPCache: outline.IgnoreHTTPCache,
FetchViaProxy: outline.FetchViaProxy,
Disabled: outline.Disabled,
NoMediaPlayer: outline.NoMediaPlayer,
HideGlobally: outline.HideGlobally,
AllowSelfSignedCertificates: outline.AllowSelfSignedCertificates,
DisableHTTP2: outline.DisableHTTP2,
IgnoreEntryUpdates: outline.IgnoreEntryUpdates,
})
} else if outline.Outlines.HasChildren() {
subscriptions = append(subscriptions, getSubscriptionsFromOutlines(outline.Outlines, outline.GetTitle())...)
Expand Down
Loading
Loading
0