From ef7b860f7d5ccc11abb189224a6cb75d2e0c2ac8 Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Tue, 30 Jan 2018 05:20:37 +0000 Subject: [PATCH] fixes for feed subscription and notification;; notification has not been properly tested as of yet. get on it 9front devs --- main.go | 2 +- mux.go | 4 ++- x/mux/commits.go | 81 +++++++++++++++++++++++++++++++++++++++++------- x/mux/config.go | 13 ++++++-- 4 files changed, 85 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 310590a95e9e375bae65e2140851b1d8a673211a..66a1e438d9327ca11fc5393f1c09a43c45dc53fd 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ func main() { // go mux.CommMux() // go CommMux() - go mux.Config.Init() + go mux.Config.Init(Session) Session.UpdateStatus(0, "with #cat-v") diff --git a/mux.go b/mux.go index fba7d140cc2c49a78c5ab5ae563d4bf1acacdaf0..2eb1da42c573d0271f60a95aeaf216e30a8f0da1 100644 --- a/mux.go +++ b/mux.go @@ -20,13 +20,15 @@ func init() { Router.Route("fortune", "Display fortunes. Bonus files are (theo troll rsc bullshit terry rob).", Router.Fortunes) - Router.Route("subscribe", "Subscribe to an RSS feed.", Router.Subscribe) + Router.Route("add", "Subscribe to an RSS feed.", Router.Add) Router.Route("list", "List RSS feeds Glenda is subscribed to by id.", Router.List) Router.Route("last", "Show the last commit for a given RSS feed by id.", Router.Last) Router.Route("dump", "Dump config to file.", Router.Dump) + + Router.Route("subscribe", "Subscribe current channel to a given feed id.", Router.Subscribe) Router.Route("about", "General information about the bot.", Router.About) diff --git a/x/mux/commits.go b/x/mux/commits.go index 1876f50295b07249c8efcb4fd6c6ef48bbeb5ce1..eaff1ccc9f55fd124dd97797ea0c257e4c09d9a6 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -14,13 +14,15 @@ import ( // Listen on the rss feed func Listener() { for { - for _, feed := range Config.Feeds { - fmt.Print("Printing feed: ", feed, "\n") + for p, feed := range Config.Feeds { + fmt.Print("Updating feed: ", feed.Title, "\n") err := feed.Update() if err != nil { fmt.Println("Error in updating RSS feed, see: x/mux/commits.go") fmt.Printf("%s\n\n", err) + } else { + Notify(p) } time.Sleep(10 * time.Minute) } @@ -28,6 +30,29 @@ func Listener() { } } +// Notify subscribed channels to subscribed feeds +func Notify(id int) { + if !Config.Feeds[id].Items[0].Read { + resp := ".\n" + + resp += "**" + Config.Feeds[id].Title + ": **" + "\n" + resp += Config.Feeds[id].Items[0].Date.String() + "\n\n" + resp += "`" + Config.Feeds[id].Items[0].Title + "`" + "\n" + resp += "\n" + Config.Feeds[id].Items[0].Link + "\n" + Config.Feeds[id].Items[0].Read = true + resp += "\n" + + // Loop through subbed chans and post notification message + for _, v := range Config.Subs { + if v.SubID == id { + Session.ChannelMessageSend(v.ChanID, resp) + } + time.Sleep(10 * time.Millisecond) + } + } +} + + /* Commands for Mux */ // Print last commit for a given feed (by subscribed id int [See: List(...)]) @@ -37,11 +62,13 @@ func (m *Mux) Last(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { id, _ := strconv.Atoi(ctx.Fields[len(ctx.Fields) -1]) if id >= 0 && id < len(Config.Feeds) { resp += "**" + Config.Feeds[id].Title + ": **" + "\n" - resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Date.String() + "\n\n" - resp += "`" + Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Title + "`" + "\n" - //resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Summary + "\n" - //resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Content + "\n" - resp += "\n" + Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Link + "\n" + resp += Config.Feeds[id].Items[0].Date.String() + "\n\n" + resp += "`" + Config.Feeds[id].Items[0].Title + "`" + "\n" + //resp += Config.Feeds[id].Items[0].Summary + "\n" + //resp += Config.Feeds[id].Items[0].Content + "\n" + resp += "\n" + Config.Feeds[id].Items[0].Link + "\n" + Config.Feeds[id].Items[0].Read = true + fmt.Println("Last-ing notification: ", Config.Feeds[id].Items[0]) } else { resp += "Denied fetch. Invalid stream id, see: list command" } @@ -54,12 +81,11 @@ func (m *Mux) Last(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { } // List all subscribed feeds -// Print last commit for a given feed func (m *Mux) List(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { resp := "```\n" for p, v := range Config.Feeds { - resp += strconv.Itoa(p) + ": " + v.Title + "\n" + resp += strconv.Itoa(p) + ": " + v.Title + ", " + v.Link + "\n" } resp += "```\n" @@ -70,7 +96,7 @@ func (m *Mux) List(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { } // Subscribe to a feed -func (m *Mux) Subscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { +func (m *Mux) Add(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { //http://code.9front.org/hg/plan9front/rss-log resp := "```\n" // URL to feed should be last item @@ -99,10 +125,43 @@ func (m *Mux) Subscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Conte // Might not be thread safe Config.Feeds = append(Config.Feeds, *feed) - resp += "Subscribed." + resp += "Added." resp += "```\n" ds.ChannelMessageSend(dm.ChannelID, resp) return } + +// Subscribe current channel to notifications from a given feed id +func (m *Mux) Subscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + resp := "\n" + + id, _ := strconv.Atoi(ctx.Fields[len(ctx.Fields) -1]) + if id >= 0 && id < len(Config.Feeds) { + var sub Subscription + + // Check if already subscribed + for _, v := range Config.Subs { + if v.ChanID == dm.ChannelID && v.SubID == id { + resp += "Denied subscription. Already subscribed in this channel." + goto NOSUB + } + } + + sub.ChanID = dm.ChannelID + sub.SubID = id + + // Might not be thread-safe + Config.Subs = append(Config.Subs, sub) + resp += "Subscribed." + NOSUB: + } else { + resp += "Denied subscription. Invalid stream id, see: list command" + } + + resp += "\n" + ds.ChannelMessageSend(dm.ChannelID, resp) + + return +} diff --git a/x/mux/config.go b/x/mux/config.go index 8de34900ea09c2171368d929bb40d4ba2bd33743..fbd34a0475dd3c71f60e22901b05693e853c0961 100644 --- a/x/mux/config.go +++ b/x/mux/config.go @@ -8,16 +8,25 @@ import ( "fmt" "strings" "github.com/SlyMarbo/rss" + "github.com/bwmarrin/discordgo" ) +// Stores subscription information for channels related to RSS feeds +type Subscription struct { + ChanID string + SubID int +} + // Stores config for current state type Configuration struct { Feeds []rss.Feed + Subs []Subscription } // Initializes current config (called once at start) ­ just .Read()? -func (c *Configuration) Init() { +func (c *Configuration) Init(s *discordgo.Session) { c.Read() + Session = s } // Writes current config @@ -95,4 +104,4 @@ func (c *Configuration) Setup() { // Global variables are bad var Config Configuration - +var Session *discordgo.Session