diff --git a/go.mod b/go.mod index eefc244..4577b04 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 // indirect golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect + golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b google.golang.org/appengine v1.6.1 // indirect google.golang.org/genproto v0.0.0-20190626174449-989357319d63 // indirect google.golang.org/grpc v1.21.1 // indirect diff --git a/plugin.json b/plugin.json index c97bdb9..264b0c3 100644 --- a/plugin.json +++ b/plugin.json @@ -18,15 +18,63 @@ "key": "Heartbeat", "display_name": "Time window between RSS feed checks (minutes).", "type": "text", - "help_text": "This is used to set a timer for the system to know when to go check to see if there is any new data in the subscribed RSS feeds. Defaults to 15 minutes." - }, + "help_text": "This is used to set a timer for the system to know when to go check to see if there is any new data in the subscribed rss feeds. Defaults to 15 minutes." + }, + { + "key": "FormatTitle", + "display_name": "Print post title in bold", + "type": "bool", + "help_text": "(Optional) If enabled, the title of posts will be formatted in bold." + }, { "key": "ShowDescription", "display_name": "Show Description in RSS post.", "type": "bool", + "help_text": "(Optional) Use this field to hide the description in rss post (Useful if link already returns a valid link back to post)." + }, + { + "key": "ShowSummary", + "display_name": "Show Summary in Atom post.", + "type": "bool", + "help_text": "(Optional) Use this field to hide the summary in Atom post (Useful if link already returns a valid link back to post)." + }, + { + "key": "ShowContent", + "display_name": "Show Content in Atom post.", + "type": "bool", + "help_text": "(Optional) Specify, whether the content of an Atom feed should be posted", + "default": true + }, + { + "key": "ShowRSSLink", + "display_name": "Show Link in RSS post.", + "type": "bool", + "help_text": "(Optional) Specify, whether the link of an RSS feed should be posted", + "default": true + }, + { + "key": "ShowAtomLink", + "display_name": "Show Link in Atom post.", + "type": "bool", + "help_text": "(Optional) Specify, whether the Link of an Atom feed should be posted", + "default": true + }, + { + "key": "ShowRSSItemTitle", + "display_name": "Show Title in RSS post.", + "type": "bool", + "help_text": "(Optional) Specify, whether the title of an RSS feed should be posted", + "default": true + }, + { + "key": "ShowAtomItemTitle", + "display_name": "Show Title in Atom post.", + "type": "bool", + "help_text": "(Optional) Specify, whether the title of an Atom feed should be posted", + "default": true "help_text": "(Optional) Use this field to hide the description in RSS post (Useful if link already returns a valid link back to post)." + } - ] } } diff --git a/server/configuration.go b/server/configuration.go index 909b7ad..dc613fb 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -18,8 +18,15 @@ import ( // If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep // copy appropriate for your types. type configuration struct { - Heartbeat string - ShowDescription bool + Heartbeat string + ShowDescription bool + ShowSummary bool + ShowContent bool + ShowRSSLink bool + ShowAtomLink bool + ShowRSSItemTitle bool + ShowAtomItemTitle bool + FormatTitle bool } // Clone shallow copies the configuration. Your implementation may require a deep copy if diff --git a/server/plugin.go b/server/plugin.go index 6d8c217..052f1bc 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -6,9 +6,11 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "sync" "time" + "golang.org/x/tools/blog/atom" "github.com/lunny/html2md" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/plugin" @@ -148,10 +150,27 @@ func (p *RSSFeedPlugin) processRSSV2Subscription(subscription *Subscription) err } for _, item := range items { - post := newRssFeed.Channel.Title + "\n" + item.Title + "\n" + item.Link + "\n" + post := "" + + if config.FormatTitle { + post = post + "##### " + } + post = post + newRssFeed.Channel.Title + "\n" + + if config.ShowRSSItemTitle { + if config.FormatTitle { + post = post + "###### " + } + post = post + item.Title + "\n" + } + + if config.ShowRSSLink { + post = post + strings.TrimSpace(item.Link) + "\n" + } if config.ShowDescription { post = post + html2md.Convert(item.Description) + "\n" } + p.createBotPost(subscription.ChannelID, post, "custom_git_pr") } @@ -164,6 +183,8 @@ func (p *RSSFeedPlugin) processRSSV2Subscription(subscription *Subscription) err } func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) error { + config := p.getConfiguration() + // get new rss feed string from url newFeed, newFeedString, err := atomparser.ParseURL(subscription.URL) if err != nil { @@ -185,25 +206,46 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro } for _, item := range items { - post := newFeed.Title + "\n" + item.Title + "\n" + post := "" - for _, link := range item.Link { - if link.Rel == "alternate" { - post = post + link.Href + "\n" + if config.FormatTitle { + post = post + "##### " + } + post = post + newFeed.Title + "\n" + + if config.ShowAtomItemTitle { + if config.FormatTitle { + post = post + "###### " } + post = post + item.Title + "\n" } - if item.Content != nil { - if item.Content.Type != "text" { - post = post + html2md.Convert(item.Content.Body) + "\n" - } else { - post = post + item.Content.Body + "\n" + + if config.ShowAtomLink { + for _, link := range item.Link { + if link.Rel == "alternate" { + post = post + strings.TrimSpace(link.Href) + "\n" + } } - } else { - p.API.LogInfo("Missing content in atom feed item", - "subscription_url", subscription.URL, - "item_title", item.Title) - post = post + "\n" } + + if config.ShowSummary { + if !tryParseRichNode(item.Summary, &post) { + p.API.LogInfo("Missing summary in atom feed item", + "subscription_url", subscription.URL, + "item_title", item.Title) + post = post + "\n" + } + } + + if config.ShowContent { + if !tryParseRichNode(item.Content, &post) { + p.API.LogInfo("Missing content in atom feed item", + "subscription_url", subscription.URL, + "item_title", item.Title) + post = post + "\n" + } + } + p.createBotPost(subscription.ChannelID, post, "custom_git_pr") } @@ -215,6 +257,19 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro return nil } +func tryParseRichNode(node *atom.Text, post *string) bool { + if node != nil { + if node.Type != "text" { + *post = *post + html2md.Convert(strings.TrimSpace(node.Body)) + "\n" + } else { + *post = *post + node.Body + "\n" + } + return true + } else { + return false + } +} + func (p *RSSFeedPlugin) createBotPost(channelID string, message string, postType string) error { post := &model.Post{ UserId: p.botUserID,