From 5f0b62585b0478bd4293c2feaaa9835465b46217 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 10 Jan 2017 16:38:45 +0000 Subject: [PATCH] Use one RoomID per slackapi service --- .../go-neb/services/slackapi/slackapi.go | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go b/src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go index de1a4e9..6b79d0b 100644 --- a/src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go +++ b/src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go @@ -14,11 +14,11 @@ const ServiceType = "slackapi" type Service struct { types.DefaultService - // maps from hookID -> roomID - Hooks map[string]struct { - RoomID string `json:"room_id"` - MessageType string `json:"message_type"` - } `json:"hooks"` + webhookEndpointURL string + // The URL which should be added to .travis.yml - Populated by Go-NEB after Service registration. + WebhookURL string `json:"webhook_url"` + RoomID string `json:"room_id"` + MessageType string `json:"message_type"` } func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) { @@ -29,12 +29,11 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli return } - hookID := segments[len(segments)-2] - messageType := s.Hooks[hookID].MessageType + messageType := s.MessageType if messageType == "" { messageType = "m.text" } - roomID := s.Hooks[hookID].RoomID + roomID := s.RoomID slackMessage, err := getSlackMessage(*req) if err != nil { @@ -56,16 +55,15 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli w.WriteHeader(200) } -// Register joins all configured rooms +// Register joins the configured room and sets the public WebhookURL func (s *Service) Register(oldService types.Service, client *matrix.Client) error { - for _, mapping := range s.Hooks { - if _, err := client.JoinRoom(mapping.RoomID, "", ""); err != nil { - log.WithFields(log.Fields{ - log.ErrorKey: err, - "room_id": mapping.RoomID, - "user_id": client.UserID, - }).Error("Failed to join room") - } + s.WebhookURL = s.webhookEndpointURL + if _, err := client.JoinRoom(s.RoomID, "", ""); err != nil { + log.WithFields(log.Fields{ + log.ErrorKey: err, + "room_id": s.RoomID, + "user_id": client.UserID, + }).Error("Failed to join room") } return nil } @@ -74,6 +72,7 @@ func init() { types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), + webhookEndpointURL: webhookEndpointURL, } }) }