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. 25
      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")
}
http.ListenAndServe(bindAddress, nil)
log.Fatal(http.ListenAndServe(bindAddress, nil))
}

25
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 {
logger := log.WithField("feed_url", feedURL).WithField("title", item.Title)
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 {
logger.WithError(err).WithField("room_id", roomID).Error("Failed to send to room")
}
@ -165,10 +156,10 @@ type feedReaderService struct {
serviceUserID string
Feeds map[string]struct { // feed_url => { }
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"`
Rooms map[string][]string `json:"rooms"` // room_id => [ feed_url ]
}
func (s *feedReaderService) ServiceUserID() string { return s.serviceUserID }
@ -193,19 +184,13 @@ func (s *feedReaderService) Register(oldService types.Service, client *matrix.Cl
return nil
}
// Make sure we can parse the feed
for feedURL := range s.Feeds {
for feedURL, feedInfo := range s.Feeds {
fp := gofeed.NewParser()
if _, err := fp.ParseURL(feedURL); err != nil {
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

Loading…
Cancel
Save