Skip to content

Commit

Permalink
Merge pull request #40 from MoeRT09/post-customization
Browse files Browse the repository at this point in the history
Post customization
  • Loading branch information
William B Ernest authored Feb 18, 2021
2 parents 3e08ee5 + d97566c commit 760f884
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 51 additions & 3 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)."

}

]
}
}
11 changes: 9 additions & 2 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 70 additions & 15 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand All @@ -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 {
Expand All @@ -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")
}

Expand All @@ -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,
Expand Down

0 comments on commit 760f884

Please sign in to comment.