~kris/9p

glenda

ef7b860f7d5ccc11abb189224a6cb75d2e0c2ac8 — Sean Hinchee 8 years ago 1f41901
fixes for feed subscription and notification;; notification has not been properly tested as of yet. get on it 9front devs
4 files changed, 85 insertions(+), 15 deletions(-)

M main.go
M mux.go
M x/mux/commits.go
M x/mux/config.go
M main.go => main.go +1 -1
@@ 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")


M mux.go => mux.go +3 -1
@@ 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)
	

M x/mux/commits.go => x/mux/commits.go +70 -11
@@ 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
}

M x/mux/config.go => x/mux/config.go +11 -2
@@ 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