~kris/9p

glenda

ref: 640f7ec01b44f7ebabef9d7853bb436994a123fa glenda/x/mux/commits.go -rw-r--r-- 4.3 KiB
640f7ec0 — Sean Hinchee fixed(?) a bug(?) in feed subscription 8 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mux

import (
	"github.com/bwmarrin/discordgo"
	"github.com/SlyMarbo/rss"
	"fmt"
	"time"
	"strings"
	"strconv"
)

/* internal functions */

// Listen on the rss feed
func Listener() {
	for {
		for p, _ := range Config.Feeds {
			fmt.Print("Updating feed: ", Config.Feeds[p].Title, "\n")
			err := Config.Feeds[p].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(1 * time.Minute)
		}
		time.Sleep(1 * time.Minute)
	}
}

// Notify subscribed channels to subscribed feeds
func Notify(id int) {
	str := Config.Feeds[id].UpdateURL
	feed, _ := rss.Fetch(str)
	Config.Feeds[id] = *feed

	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
		fmt.Println("Looping through subs to notify...")
		for _, v := range Config.Subs {
			if v.SubID == id {
				Session.ChannelMessageSend(v.ChanID, resp)
			}
			time.Sleep(10 * time.Millisecond)
		}
	}
	fmt.Println("No new notifys for ", Config.Feeds[id].UpdateURL)
	fmt.Println(Config.Feeds[id].Items[0])
	fmt.Println(Config.Feeds[id].Items[len(Config.Feeds[id].Items)-1])
}


/* Commands for Mux */

// Print last commit for a given feed (by subscribed id int [See: List(...)])
func (m *Mux) Last(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) {
		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
		fmt.Println("Last-ing notification: ", Config.Feeds[id].Items[0])
	} else {
		resp += "Denied fetch. Invalid stream id, see: list command"
	}

	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// List all subscribed feeds
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 + ", " + v.Link + "\n"
	}

	resp += "```\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}

// Subscribe to a feed
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
	url := ctx.Fields[len(ctx.Fields) -1]
	fmt.Println("Proposed subscribe for: ", url)
	
	for _, v := range Config.Feeds {
		// this is bad matching, can't have two bitbucket url's?
		if strings.Contains(url, v.UpdateURL) {
			//fmt.Println(url)
			//fmt.Println(v.Link)
			resp += "Denied! Feed already subscribed to."
			resp += "```\n"
			ds.ChannelMessageSend(dm.ChannelID, resp)
			return
		}
	}
	
	feed, err := rss.Fetch(url)

	if err != nil {
		fmt.Println("Error in reading RSS feed, see: x/mux/commits.go")
		fmt.Printf("%s\n\n", err)
		resp += "Denied! Could not parse feed."
		resp += "```\n"
		ds.ChannelMessageSend(dm.ChannelID, resp)
		return
	}
	
	// Might not be thread safe
	Config.Feeds = append(Config.Feeds, *feed)
	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
}