Browse Source

Use one RoomID per slackapi service

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

33
src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go

@ -14,11 +14,11 @@ const ServiceType = "slackapi"
type Service struct { type Service struct {
types.DefaultService 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) { 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 return
} }
hookID := segments[len(segments)-2]
messageType := s.Hooks[hookID].MessageType
messageType := s.MessageType
if messageType == "" { if messageType == "" {
messageType = "m.text" messageType = "m.text"
} }
roomID := s.Hooks[hookID].RoomID
roomID := s.RoomID
slackMessage, err := getSlackMessage(*req) slackMessage, err := getSlackMessage(*req)
if err != nil { if err != nil {
@ -56,16 +55,15 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli
w.WriteHeader(200) 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 { 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 return nil
} }
@ -74,6 +72,7 @@ func init() {
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service {
return &Service{ return &Service{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
webhookEndpointURL: webhookEndpointURL,
} }
}) })
} }
Loading…
Cancel
Save