From 35a5a2e6f6633ee1f9b76aad6bed6d98ae8403f2 Mon Sep 17 00:00:00 2001 From: Martin Meszaros Date: Tue, 26 Jan 2021 16:59:36 +0100 Subject: [PATCH 1/3] Make plugin more customizable Now elements like link and title can be optionally enabled or disabled. Furthermore, thesummary of an Atom item can be shown. --- go.mod | 1 + plugin.json | 42 ++++++++++++++++++++++++- server/configuration.go | 10 ++++-- server/plugin.go | 68 ++++++++++++++++++++++++++++++++--------- 4 files changed, 103 insertions(+), 18 deletions(-) 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 79aa08b..80fecdc 100644 --- a/plugin.json +++ b/plugin.json @@ -25,8 +25,48 @@ "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 } - ] } } diff --git a/server/configuration.go b/server/configuration.go index 909b7ad..be1ddbd 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -18,8 +18,14 @@ 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 } // Clone shallow copies the configuration. Your implementation may require a deep copy if diff --git a/server/plugin.go b/server/plugin.go index 29fa588..e3ad30a 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -12,6 +12,7 @@ import ( "strconv" "sync" "time" + "golang.org/x/tools/blog/atom" ) //const RSSFEED_ICON_URL = "./plugins/rssfeed/assets/rss.png" @@ -140,10 +141,19 @@ func (p *RSSFeedPlugin) processRSSV2Subscription(subscription *Subscription) err items := rssv2parser.CompareItemsBetweenOldAndNew(oldRssFeed, newRssFeed) for _, item := range items { - post := newRssFeed.Channel.Title + "\n" + item.Title + "\n" + item.Link + "\n" + post := newRssFeed.Channel.Title + "\n" + + if config.ShowRSSItemTitle { + post = post + item.Title + "\n" + } + + if config.ShowRSSLink { + post = post + item.Link + "\n" + } if config.ShowDescription { post = post + html2md.Convert(item.Description) + "\n" } + p.createBotPost(subscription.ChannelID, post, "custom_git_pr") } @@ -156,6 +166,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 { @@ -171,25 +183,38 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro items := atomparser.CompareItemsBetweenOldAndNew(oldFeed, newFeed) for _, item := range items { - post := newFeed.Title + "\n" + item.Title + "\n" + post := newFeed.Title + "\n" + + if config.ShowAtomItemTitle { + post = post + item.Title + "\n" + } - for _, link := range item.Link { - if link.Rel == "alternate" { - post = post + link.Href + "\n" + if config.ShowAtomLink { + for _, link := range item.Link { + if link.Rel == "alternate" { + post = post + link.Href + "\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.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" } - } else { - p.API.LogInfo("Missing content 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") } @@ -201,6 +226,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(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, From 7794062e136f4908c7364484be7cd2f334bebb31 Mon Sep 17 00:00:00 2001 From: Martin Meszaros Date: Tue, 26 Jan 2021 17:48:38 +0100 Subject: [PATCH 2/3] Add option for post formatting Allows printing feed title in bold for better distinguishing titles from the rest of the feed. Additionally, whitespaces bevore and after links will be stripped out, as this those could lead to formatting issues (unclickable links). --- plugin.json | 8 +++++++- server/configuration.go | 1 + server/plugin.go | 33 +++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/plugin.json b/plugin.json index 80fecdc..ccb1cdd 100644 --- a/plugin.json +++ b/plugin.json @@ -19,7 +19,13 @@ "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." - }, + }, + { + "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.", diff --git a/server/configuration.go b/server/configuration.go index be1ddbd..dc613fb 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -26,6 +26,7 @@ type configuration struct { 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 e3ad30a..b83acd4 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "sync" "time" "golang.org/x/tools/blog/atom" @@ -141,14 +142,22 @@ func (p *RSSFeedPlugin) processRSSV2Subscription(subscription *Subscription) err items := rssv2parser.CompareItemsBetweenOldAndNew(oldRssFeed, newRssFeed) for _, item := range items { - post := newRssFeed.Channel.Title + "\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 + item.Link + "\n" + post = post + trimWhitespace(item.Link) + "\n" } if config.ShowDescription { post = post + html2md.Convert(item.Description) + "\n" @@ -183,16 +192,24 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro items := atomparser.CompareItemsBetweenOldAndNew(oldFeed, newFeed) for _, item := range items { - post := newFeed.Title + "\n" + post := "" + + if config.FormatTitle { + post = post + "##### " + } + post = post + newFeed.Title + "\n" if config.ShowAtomItemTitle { + if config.FormatTitle { + post = post + "###### " + } post = post + item.Title + "\n" } if config.ShowAtomLink { for _, link := range item.Link { if link.Rel == "alternate" { - post = post + link.Href + "\n" + post = post + trimWhitespace(link.Href) + "\n" } } } @@ -239,6 +256,14 @@ func tryParseRichNode(node *atom.Text, post *string) bool { } } +func trimWhitespace(text string) string { + text = strings.TrimPrefix(text, "\n") + text = strings.TrimSuffix(text, "\n") + text = strings.TrimSpace(text) + + return text +} + func (p *RSSFeedPlugin) createBotPost(channelID string, message string, postType string) error { post := &model.Post{ UserId: p.botUserID, From 094da16ccad28828119e0947dc7070d3206e6417 Mon Sep 17 00:00:00 2001 From: Martin Meszaros Date: Tue, 26 Jan 2021 19:20:16 +0100 Subject: [PATCH 3/3] Trim Atom Description/Summary whitespace as well --- server/plugin.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index b83acd4..f04a644 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -157,7 +157,7 @@ func (p *RSSFeedPlugin) processRSSV2Subscription(subscription *Subscription) err } if config.ShowRSSLink { - post = post + trimWhitespace(item.Link) + "\n" + post = post + strings.TrimSpace(item.Link) + "\n" } if config.ShowDescription { post = post + html2md.Convert(item.Description) + "\n" @@ -209,7 +209,7 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro if config.ShowAtomLink { for _, link := range item.Link { if link.Rel == "alternate" { - post = post + trimWhitespace(link.Href) + "\n" + post = post + strings.TrimSpace(link.Href) + "\n" } } } @@ -246,7 +246,7 @@ func (p *RSSFeedPlugin) processAtomSubscription(subscription *Subscription) erro func tryParseRichNode(node *atom.Text, post *string) bool { if node != nil { if node.Type != "text" { - *post = *post + html2md.Convert(node.Body) + "\n" + *post = *post + html2md.Convert(strings.TrimSpace(node.Body)) + "\n" } else { *post = *post + node.Body + "\n" } @@ -256,14 +256,6 @@ func tryParseRichNode(node *atom.Text, post *string) bool { } } -func trimWhitespace(text string) string { - text = strings.TrimPrefix(text, "\n") - text = strings.TrimSuffix(text, "\n") - text = strings.TrimSpace(text) - - return text -} - func (p *RSSFeedPlugin) createBotPost(channelID string, message string, postType string) error { post := &model.Post{ UserId: p.botUserID,