Browse Source

Merge pull request #80 from matrix-org/kegan/feedreader-struct

Rejig how feedreaders stores rooms to send updates to
kegan/rssbot
Kegsay 8 years ago
committed by GitHub
parent
commit
ccb1c6c779
  1. 2
      src/github.com/matrix-org/go-neb/goneb.go
  2. 31
      src/github.com/matrix-org/go-neb/services/feedreader/feedreader.go

2
src/github.com/matrix-org/go-neb/goneb.go

@ -77,5 +77,5 @@ func main() {
log.WithError(err).Panic("Failed to start polling") log.WithError(err).Panic("Failed to start polling")
} }
http.ListenAndServe(bindAddress, nil)
log.Fatal(http.ListenAndServe(bindAddress, nil))
} }

31
src/github.com/matrix-org/go-neb/services/feedreader/feedreader.go

@ -134,16 +134,7 @@ func (p *feedPoller) updateFeedInfo(s *feedReaderService, feedURL string, nextPo
func (p *feedPoller) sendToRooms(s *feedReaderService, cli *matrix.Client, feedURL string, feed *gofeed.Feed, item gofeed.Item) error { func (p *feedPoller) sendToRooms(s *feedReaderService, cli *matrix.Client, feedURL string, feed *gofeed.Feed, item gofeed.Item) error {
logger := log.WithField("feed_url", feedURL).WithField("title", item.Title) logger := log.WithField("feed_url", feedURL).WithField("title", item.Title)
logger.Info("New feed item") logger.Info("New feed item")
var rooms []string
for roomID, urls := range s.Rooms {
for _, u := range urls {
if u == feedURL {
rooms = append(rooms, roomID)
break
}
}
}
for _, roomID := range rooms {
for _, roomID := range s.Feeds[feedURL].Rooms {
if _, err := cli.SendMessageEvent(roomID, "m.room.message", itemToHTML(feed, item)); err != nil { if _, err := cli.SendMessageEvent(roomID, "m.room.message", itemToHTML(feed, item)); err != nil {
logger.WithError(err).WithField("room_id", roomID).Error("Failed to send to room") logger.WithError(err).WithField("room_id", roomID).Error("Failed to send to room")
} }
@ -164,11 +155,11 @@ type feedReaderService struct {
id string id string
serviceUserID string serviceUserID string
Feeds map[string]struct { // feed_url => { } Feeds map[string]struct { // feed_url => { }
PollIntervalMins int `json:"poll_interval_mins"`
NextPollTimestampSecs int64 // Internal: When we should poll again
FeedUpdatedTimestampSecs int64 // Internal: The last time the feed was updated
PollIntervalMins int `json:"poll_interval_mins"`
Rooms []string `json:"rooms"`
NextPollTimestampSecs int64 // Internal: When we should poll again
FeedUpdatedTimestampSecs int64 // Internal: The last time the feed was updated
} `json:"feeds"` } `json:"feeds"`
Rooms map[string][]string `json:"rooms"` // room_id => [ feed_url ]
} }
func (s *feedReaderService) ServiceUserID() string { return s.serviceUserID } func (s *feedReaderService) ServiceUserID() string { return s.serviceUserID }
@ -193,19 +184,13 @@ func (s *feedReaderService) Register(oldService types.Service, client *matrix.Cl
return nil return nil
} }
// Make sure we can parse the feed // Make sure we can parse the feed
for feedURL := range s.Feeds {
for feedURL, feedInfo := range s.Feeds {
fp := gofeed.NewParser() fp := gofeed.NewParser()
if _, err := fp.ParseURL(feedURL); err != nil { if _, err := fp.ParseURL(feedURL); err != nil {
return fmt.Errorf("Failed to read URL %s: %s", feedURL, err.Error()) return fmt.Errorf("Failed to read URL %s: %s", feedURL, err.Error())
} }
}
// Make sure all feeds are accounted for (appear at least once) in the room map, AND make sure there
// are no weird new feeds in those rooms
for roomID, roomFeeds := range s.Rooms {
for _, f := range roomFeeds {
if _, exists := s.Feeds[f]; !exists {
return fmt.Errorf("Feed URL %s in room %s does not exist in the Feeds section", f, roomID)
}
if len(feedInfo.Rooms) == 0 {
return fmt.Errorf("Feed %s has no rooms to send updates to", feedURL)
} }
} }
return nil return nil

Loading…
Cancel
Save