Browse Source

Use one RoomID per slackapi service

pull/116/head
Luke Barnard 8 years ago
parent
commit
5f0b62585b
  1. 21
      src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go

21
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 {
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"`
} `json:"hooks"`
}
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,17 +55,16 @@ 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 {
s.WebhookURL = s.webhookEndpointURL
if _, err := client.JoinRoom(s.RoomID, "", ""); err != nil {
log.WithFields(log.Fields{
log.ErrorKey: err,
"room_id": mapping.RoomID,
"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,
}
})
}
Loading…
Cancel
Save