diff --git a/goneb.go b/goneb.go index 3c4cabc..ed3ea67 100644 --- a/goneb.go +++ b/goneb.go @@ -20,19 +20,19 @@ import ( _ "github.com/matrix-org/go-neb/realms/github" _ "github.com/matrix-org/go-neb/realms/jira" - //_ "github.com/matrix-org/go-neb/services/alertmanager" + _ "github.com/matrix-org/go-neb/services/alertmanager" _ "github.com/matrix-org/go-neb/services/echo" _ "github.com/matrix-org/go-neb/services/giphy" _ "github.com/matrix-org/go-neb/services/github" - //_ "github.com/matrix-org/go-neb/services/google" + _ "github.com/matrix-org/go-neb/services/google" _ "github.com/matrix-org/go-neb/services/guggy" _ "github.com/matrix-org/go-neb/services/imgur" - //_ "github.com/matrix-org/go-neb/services/jira" + _ "github.com/matrix-org/go-neb/services/jira" _ "github.com/matrix-org/go-neb/services/rssbot" - //_ "github.com/matrix-org/go-neb/services/slackapi" - //_ "github.com/matrix-org/go-neb/services/travisci" + _ "github.com/matrix-org/go-neb/services/slackapi" + _ "github.com/matrix-org/go-neb/services/travisci" _ "github.com/matrix-org/go-neb/services/wikipedia" "github.com/matrix-org/go-neb/types" "github.com/matrix-org/util" diff --git a/services/alertmanager/alertmanager.go b/services/alertmanager/alertmanager.go index 1bdfe37..a02e969 100644 --- a/services/alertmanager/alertmanager.go +++ b/services/alertmanager/alertmanager.go @@ -5,14 +5,17 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/matrix-org/go-neb/database" - "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" - log "github.com/sirupsen/logrus" html "html/template" "net/http" "strings" text "text/template" + + "github.com/matrix-org/go-neb/database" + "github.com/matrix-org/go-neb/types" + log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" ) // ServiceType of the Alertmanager service. @@ -46,10 +49,10 @@ type Service struct { // The URL which should be added to alertmanagers config - Populated by Go-NEB after Service registration. WebhookURL string `json:"webhook_url"` // A map of matrix rooms to templates - Rooms map[string]struct { - TextTemplate string `json:"text_template"` - HTMLTemplate string `json:"html_template"` - MsgType string `json:"msg_type"` + Rooms map[id.RoomID]struct { + TextTemplate string `json:"text_template"` + HTMLTemplate string `json:"html_template"` + MsgType mevt.MessageType `json:"msg_type"` } `json:"rooms"` } @@ -75,7 +78,7 @@ type WebhookNotification struct { } // OnReceiveWebhook receives requests from Alertmanager and sends requests to Matrix as a result. -func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) { +func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *mautrix.Client) { decoder := json.NewDecoder(req.Body) var notif WebhookNotification if err := decoder.Decode(¬if); err != nil { @@ -115,14 +118,14 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli w.WriteHeader(500) return } - msg = gomatrix.HTMLMessage{ + msg = mevt.MessageEventContent{ Body: bodyBuffer.String(), MsgType: templates.MsgType, - Format: "org.matrix.custom.html", + Format: mevt.FormatHTML, FormattedBody: formattedBodyBuffer.String(), } } else { - msg = gomatrix.TextMessage{ + msg = mevt.MessageEventContent{ Body: bodyBuffer.String(), MsgType: templates.MsgType, } @@ -132,7 +135,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli "message": msg, "room_id": roomID, }).Print("Sending Alertmanager notification to room") - if _, e := cli.SendMessageEvent(roomID, "m.room.message", msg); e != nil { + if _, e := cli.SendMessageEvent(roomID, mevt.EventMessage, msg); e != nil { log.WithError(e).WithField("room_id", roomID).Print( "Failed to send Alertmanager notification to room.") } @@ -141,7 +144,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli } // Register makes sure the Config information supplied is valid. -func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error { +func (s *Service) Register(oldService types.Service, client *mautrix.Client) error { s.WebhookURL = s.webhookEndpointURL for _, templates := range s.Rooms { // validate that we have at least a plain text template @@ -188,9 +191,9 @@ func (s *Service) PostRegister(oldService types.Service) { } } -func (s *Service) joinRooms(client *gomatrix.Client) { +func (s *Service) joinRooms(client *mautrix.Client) { for roomID := range s.Rooms { - if _, err := client.JoinRoom(roomID, "", nil); err != nil { + if _, err := client.JoinRoom(roomID.String(), "", nil); err != nil { log.WithFields(log.Fields{ log.ErrorKey: err, "room_id": roomID, @@ -201,7 +204,7 @@ func (s *Service) joinRooms(client *gomatrix.Client) { } func init() { - types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), webhookEndpointURL: webhookEndpointURL, diff --git a/services/alertmanager/alertmanager_test.go b/services/alertmanager/alertmanager_test.go index a498863..e752144 100644 --- a/services/alertmanager/alertmanager_test.go +++ b/services/alertmanager/alertmanager_test.go @@ -4,10 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/matrix-org/go-neb/database" - "github.com/matrix-org/go-neb/testutils" - "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" "io/ioutil" "net/http" "net/http/httptest" @@ -15,13 +11,19 @@ import ( "regexp" "strings" "testing" + + "github.com/matrix-org/go-neb/database" + "github.com/matrix-org/go-neb/testutils" + "github.com/matrix-org/go-neb/types" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" ) func TestNotify(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) // Intercept message sending to Matrix and mock responses - msgs := []gomatrix.HTMLMessage{} + msgs := []mevt.MessageEventContent{} matrixCli := buildTestClient(&msgs) // create the service @@ -91,13 +93,13 @@ func TestNotify(t *testing.T) { } } -func buildTestClient(msgs *[]gomatrix.HTMLMessage) *gomatrix.Client { +func buildTestClient(msgs *[]mevt.MessageEventContent) *mautrix.Client { matrixTrans := struct{ testutils.MockTransport }{} matrixTrans.RT = func(req *http.Request) (*http.Response, error) { if !strings.Contains(req.URL.String(), "/send/m.room.message") { return nil, fmt.Errorf("Unhandled URL: %s", req.URL.String()) } - var msg gomatrix.HTMLMessage + var msg mevt.MessageEventContent if err := json.NewDecoder(req.Body).Decode(&msg); err != nil { return nil, fmt.Errorf("Failed to decode request JSON: %s", err) } @@ -107,7 +109,7 @@ func buildTestClient(msgs *[]gomatrix.HTMLMessage) *gomatrix.Client { Body: ioutil.NopCloser(bytes.NewBufferString(`{"event_id":"$yup:event"}`)), }, nil } - matrixCli, _ := gomatrix.NewClient("https://hs", "@neb:hs", "its_a_secret") + matrixCli, _ := mautrix.NewClient("https://hs", "@neb:hs", "its_a_secret") matrixCli.Client = &http.Client{Transport: matrixTrans} return matrixCli } diff --git a/services/google/google.go b/services/google/google.go index 430af61..6d456f2 100644 --- a/services/google/google.go +++ b/services/google/google.go @@ -12,8 +12,10 @@ import ( "strings" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" ) // ServiceType of the Google service @@ -68,23 +70,23 @@ type Service struct { // Commands supported: // !google image some_search_query_without_quotes // Responds with a suitable image into the same room as the command. -func (s *Service) Commands(client *gomatrix.Client) []types.Command { +func (s *Service) Commands(client *mautrix.Client) []types.Command { return []types.Command{ - types.Command{ + { Path: []string{"google", "image"}, - Command: func(roomID, userID string, args []string) (interface{}, error) { + Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { return s.cmdGoogleImgSearch(client, roomID, userID, args) }, }, - types.Command{ + { Path: []string{"google", "help"}, - Command: func(roomID, userID string, args []string) (interface{}, error) { + Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { return usageMessage(), nil }, }, - types.Command{ + { Path: []string{"google"}, - Command: func(roomID, userID string, args []string) (interface{}, error) { + Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { return usageMessage(), nil }, }, @@ -92,12 +94,14 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { } // usageMessage returns a matrix TextMessage representation of the service usage -func usageMessage() *gomatrix.TextMessage { - return &gomatrix.TextMessage{"m.notice", - `Usage: !google image image_search_text`} +func usageMessage() *mevt.MessageEventContent { + return &mevt.MessageEventContent{ + MsgType: mevt.MsgNotice, + Body: `Usage: !google image image_search_text`, + } } -func (s *Service) cmdGoogleImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { +func (s *Service) cmdGoogleImgSearch(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { if len(args) < 1 { return usageMessage(), nil @@ -114,8 +118,8 @@ func (s *Service) cmdGoogleImgSearch(client *gomatrix.Client, roomID, userID str var imgURL = searchResult.Link if imgURL == "" { - return gomatrix.TextMessage{ - MsgType: "m.notice", + return mevt.MessageEventContent{ + MsgType: mevt.MsgNotice, Body: "No image found!", }, nil } @@ -126,14 +130,14 @@ func (s *Service) cmdGoogleImgSearch(client *gomatrix.Client, roomID, userID str return nil, fmt.Errorf("Failed to upload Google image at URL %s (content type %s) to matrix: %s", imgURL, searchResult.Mime, err.Error()) } - return gomatrix.ImageMessage{ - MsgType: "m.image", + return mevt.MessageEventContent{ + MsgType: mevt.MsgImage, Body: querySentence, - URL: resUpload.ContentURI, - Info: gomatrix.ImageInfo{ - Height: uint(math.Floor(searchResult.Image.Height)), - Width: uint(math.Floor(searchResult.Image.Width)), - Mimetype: searchResult.Mime, + URL: resUpload.ContentURI.CUString(), + Info: &mevt.FileInfo{ + Height: int(math.Floor(searchResult.Image.Height)), + Width: int(math.Floor(searchResult.Image.Width)), + MimeType: searchResult.Mime, }, }, nil } @@ -195,7 +199,7 @@ func response2String(res *http.Response) string { // Initialise the service func init() { - types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), } diff --git a/services/google/google_test.go b/services/google/google_test.go index d3a5cf7..f60ba07 100644 --- a/services/google/google_test.go +++ b/services/google/google_test.go @@ -12,7 +12,7 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/testutils" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" + "maunium.net/go/mautrix" ) // TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make @@ -101,7 +101,7 @@ func TestCommand(t *testing.T) { } return nil, fmt.Errorf("Unknown URL: %s", req.URL.String()) } - matrixCli, _ := gomatrix.NewClient("https://hyrule", "@googlebot:hyrule", "its_a_secret") + matrixCli, _ := mautrix.NewClient("https://hyrule", "@googlebot:hyrule", "its_a_secret") matrixCli.Client = &http.Client{Transport: matrixTrans} // Execute the matrix !command diff --git a/services/jira/jira.go b/services/jira/jira.go index 212b09a..9d1b0f6 100644 --- a/services/jira/jira.go +++ b/services/jira/jira.go @@ -18,9 +18,12 @@ import ( "github.com/matrix-org/go-neb/realms/jira" "github.com/matrix-org/go-neb/realms/jira/urls" "github.com/matrix-org/go-neb/services/jira/webhook" + "github.com/matrix-org/go-neb/services/utils" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" ) // ServiceType of the JIRA Service @@ -54,9 +57,9 @@ type Service struct { webhookEndpointURL string // The user ID to create issues as, or to create/delete webhooks as. This user // is also used to look up issues for expansions. - ClientUserID string + ClientUserID id.UserID // A map from Matrix room ID to JIRA realms and project keys. - Rooms map[string]struct { + Rooms map[id.RoomID]struct { // A map of realm IDs to project keys. The realm IDs determine the JIRA // endpoint used. Realms map[string]struct { @@ -73,7 +76,7 @@ type Service struct { // Register ensures that the given realm IDs are valid JIRA realms and registers webhooks // with those JIRA endpoints. -func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error { +func (s *Service) Register(oldService types.Service, client *mautrix.Client) error { // We only ever make 1 JIRA webhook which listens for all projects and then filter // on receive. So we simply need to know if we need to make a webhook or not. We // need to do this for each unique realm. @@ -94,7 +97,7 @@ func (s *Service) Register(oldService types.Service, client *gomatrix.Client) er return nil } -func (s *Service) cmdJiraCreate(roomID, userID string, args []string) (interface{}, error) { +func (s *Service) cmdJiraCreate(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { // E.g jira create PROJ "Issue title" "Issue desc" if len(args) <= 1 { return nil, errors.New("Missing project key (e.g 'ABC') and/or title") @@ -164,13 +167,13 @@ func (s *Service) cmdJiraCreate(roomID, userID string, args []string) (interface return nil, fmt.Errorf("Failed to create issue: JIRA returned %d", res.StatusCode) } - return &gomatrix.TextMessage{ - "m.notice", - fmt.Sprintf("Created issue: %sbrowse/%s", r.JIRAEndpoint, i.Key), + return &mevt.MessageEventContent{ + MsgType: mevt.MsgNotice, + Body: fmt.Sprintf("Created issue: %sbrowse/%s", r.JIRAEndpoint, i.Key), }, nil } -func (s *Service) expandIssue(roomID, userID string, issueKeyGroups []string) interface{} { +func (s *Service) expandIssue(roomID id.RoomID, userID id.UserID, issueKeyGroups []string) interface{} { // issueKeyGroups => ["SYN-123", "SYN", "123"] if len(issueKeyGroups) != 3 { log.WithField("groups", issueKeyGroups).Error("Bad number of groups") @@ -221,8 +224,8 @@ func (s *Service) expandIssue(roomID, userID string, issueKeyGroups []string) in logger.WithError(err).Print("Failed to GET issue") return err } - return gomatrix.GetHTMLMessage( - "m.notice", + return utils.StrippedHTMLMessage( + mevt.MsgNotice, fmt.Sprintf( "%sbrowse/%s : %s", jrealm.JIRAEndpoint, issueKey, htmlSummaryForIssue(issue), @@ -239,11 +242,11 @@ func (s *Service) expandIssue(roomID, userID string, issueKeyGroups []string) in // same project key, which project is chosen is undefined. If there // is no JIRA account linked to the Matrix user ID, it will return a Starter Link // if there is a known public project with that project key. -func (s *Service) Commands(cli *gomatrix.Client) []types.Command { +func (s *Service) Commands(cli *mautrix.Client) []types.Command { return []types.Command{ types.Command{ Path: []string{"jira", "create"}, - Command: func(roomID, userID string, args []string) (interface{}, error) { + Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { return s.cmdJiraCreate(roomID, userID, args) }, }, @@ -256,11 +259,11 @@ func (s *Service) Commands(cli *gomatrix.Client) []types.Command { // to map the project key to a realm, and subsequently the JIRA endpoint to hit. // If there are multiple projects with the same project key in the Service Config, one will // be chosen arbitrarily. -func (s *Service) Expansions(cli *gomatrix.Client) []types.Expansion { +func (s *Service) Expansions(cli *mautrix.Client) []types.Expansion { return []types.Expansion{ types.Expansion{ Regexp: issueKeyRegex, - Expand: func(roomID, userID string, issueKeyGroups []string) interface{} { + Expand: func(roomID id.RoomID, userID id.UserID, issueKeyGroups []string) interface{} { return s.expandIssue(roomID, userID, issueKeyGroups) }, }, @@ -268,7 +271,7 @@ func (s *Service) Expansions(cli *gomatrix.Client) []types.Expansion { } // OnReceiveWebhook receives requests from JIRA and possibly sends requests to Matrix as a result. -func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) { +func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *mautrix.Client) { eventProjectKey, event, httpErr := webhook.OnReceiveRequest(req) if httpErr != nil { log.Print("Failed to handle JIRA webhook") @@ -297,7 +300,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli continue } _, msgErr := cli.SendMessageEvent( - roomID, "m.room.message", gomatrix.GetHTMLMessage("m.notice", htmlText), + roomID, mevt.EventMessage, utils.StrippedHTMLMessage(mevt.MsgNotice, htmlText), ) if msgErr != nil { log.WithFields(log.Fields{ @@ -312,7 +315,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli w.WriteHeader(200) } -func (s *Service) realmIDForProject(roomID, projectKey string) string { +func (s *Service) realmIDForProject(roomID id.RoomID, projectKey string) string { // TODO: Multiple realms with the same pkey will be randomly chosen. for r, realmConfig := range s.Rooms[roomID].Realms { for pkey, projectConfig := range realmConfig.Projects { @@ -324,7 +327,7 @@ func (s *Service) realmIDForProject(roomID, projectKey string) string { return "" } -func (s *Service) projectToRealm(userID, pkey string) (*jira.Realm, error) { +func (s *Service) projectToRealm(userID id.UserID, pkey string) (*jira.Realm, error) { // We don't know which JIRA installation this project maps to, so: // - Get all known JIRA realms and f.e query their endpoints with the // given user ID's credentials (so if it is a private project they @@ -447,7 +450,7 @@ func htmlForEvent(whe *webhook.Event, jiraBaseURL string) string { } func init() { - types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), webhookEndpointURL: webhookEndpointURL, diff --git a/services/jira/webhook/webhook.go b/services/jira/webhook/webhook.go index ea6a509..fc18c7b 100644 --- a/services/jira/webhook/webhook.go +++ b/services/jira/webhook/webhook.go @@ -11,6 +11,7 @@ import ( "github.com/matrix-org/go-neb/realms/jira" "github.com/matrix-org/util" log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix/id" ) type jiraWebhook struct { @@ -32,7 +33,7 @@ type Event struct { } // RegisterHook checks to see if this user is allowed to track the given projects and then tracks them. -func RegisterHook(jrealm *jira.Realm, projects []string, userID, webhookEndpointURL string) error { +func RegisterHook(jrealm *jira.Realm, projects []string, userID id.UserID, webhookEndpointURL string) error { // Tracking means that a webhook may need to be created on the remote JIRA installation. // We need to make sure that the user has permission to do this. If they don't, it may still be okay if // there is an existing webhook set up for this installation by someone else, *PROVIDED* that the projects @@ -120,7 +121,7 @@ func OnReceiveRequest(req *http.Request) (string, *Event, *util.JSONResponse) { return projKey, &whe, nil } -func createWebhook(jrealm *jira.Realm, webhookEndpointURL, userID string) error { +func createWebhook(jrealm *jira.Realm, webhookEndpointURL string, userID id.UserID) error { cli, err := jrealm.JIRAClient(userID, false) if err != nil { return err @@ -182,7 +183,7 @@ func getWebhook(cli *gojira.Client, webhookEndpointURL string) (*jiraWebhook, bo return nebWH, false, nil } -func checkProjectsArePublic(jrealm *jira.Realm, projects []string, userID string) error { +func checkProjectsArePublic(jrealm *jira.Realm, projects []string, userID id.UserID) error { publicCli, err := jrealm.JIRAClient("", true) if err != nil { return fmt.Errorf("Cannot create public JIRA client") diff --git a/services/slackapi/message.go b/services/slackapi/message.go index c24325b..54099bf 100644 --- a/services/slackapi/message.go +++ b/services/slackapi/message.go @@ -12,9 +12,9 @@ import ( "regexp" "time" - "github.com/matrix-org/gomatrix" "github.com/russross/blackfriday" log "github.com/sirupsen/logrus" + mevt "maunium.net/go/mautrix/event" ) type slackAttachment struct { @@ -195,7 +195,7 @@ func renderSlackAttachment(attachment *slackAttachment) { } } -func slackMessageToHTMLMessage(message slackMessage) (html gomatrix.HTMLMessage, err error) { +func slackMessageToHTMLMessage(message slackMessage) (html mevt.MessageEventContent, err error) { text := linkifyString(message.Text) if message.Mrkdwn == nil || *message.Mrkdwn == true { message.TextRendered = template.HTML(blackfriday.MarkdownBasic([]byte(text))) diff --git a/services/slackapi/slackapi.go b/services/slackapi/slackapi.go index fd0d75d..327d4ad 100644 --- a/services/slackapi/slackapi.go +++ b/services/slackapi/slackapi.go @@ -5,8 +5,10 @@ import ( "strings" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" ) // ServiceType of the Slack API service @@ -26,16 +28,16 @@ type Service struct { types.DefaultService webhookEndpointURL string // The URL which should be given to an outgoing slack webhook - Populated by Go-NEB after Service registration. - WebhookURL string `json:"webhook_url"` - RoomID string `json:"room_id"` - MessageType string `json:"message_type"` + WebhookURL string `json:"webhook_url"` + RoomID id.RoomID `json:"room_id"` + MessageType event.MessageType `json:"message_type"` } // OnReceiveWebhook receives requests from a slack outgoing webhook and possibly sends requests // to Matrix as a result. // // This requires that the WebhookURL is given to an outgoing slack webhook (see https://api.slack.com/outgoing-webhooks) -func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) { +func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *mautrix.Client) { segments := strings.Split(req.URL.Path, "/") if len(segments) < 2 { @@ -45,7 +47,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli messageType := s.MessageType if messageType == "" { - messageType = "m.text" + messageType = event.MsgText } roomID := s.RoomID @@ -64,15 +66,15 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli } htmlMessage.MsgType = messageType cli.SendMessageEvent( - roomID, "m.room.message", htmlMessage, + roomID, event.EventMessage, htmlMessage, ) w.WriteHeader(200) } // Register joins the configured room and sets the public WebhookURL -func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error { +func (s *Service) Register(oldService types.Service, client *mautrix.Client) error { s.WebhookURL = s.webhookEndpointURL - if _, err := client.JoinRoom(s.RoomID, "", nil); err != nil { + if _, err := client.JoinRoom(s.RoomID.String(), "", nil); err != nil { log.WithFields(log.Fields{ log.ErrorKey: err, "room_id": s.RoomID, @@ -83,7 +85,7 @@ func (s *Service) Register(oldService types.Service, client *gomatrix.Client) er } func init() { - types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), webhookEndpointURL: webhookEndpointURL, diff --git a/services/travisci/travisci.go b/services/travisci/travisci.go index d1953bf..837389c 100644 --- a/services/travisci/travisci.go +++ b/services/travisci/travisci.go @@ -12,8 +12,10 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" log "github.com/sirupsen/logrus" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" ) // ServiceType of the Travis-CI service. @@ -54,7 +56,7 @@ type Service struct { // The URL which should be added to .travis.yml - Populated by Go-NEB after Service registration. WebhookURL string `json:"webhook_url"` // A map from Matrix room ID to Github-style owner/repo repositories. - Rooms map[string]struct { + Rooms map[id.RoomID]struct { // A map of "owner/repo" to configuration information Repos map[string]struct { // The template string to use when creating notifications. @@ -178,7 +180,7 @@ func outputForTemplate(travisTmpl string, tmpl map[string]string) (out string) { // webhooks: http://go-neb-endpoint.com/notifications // // See https://docs.travis-ci.com/user/notifications#Webhook-notifications for more information. -func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) { +func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *mautrix.Client) { if err := req.ParseForm(); err != nil { log.WithError(err).Error("Failed to read incoming Travis-CI webhook form") w.WriteHeader(400) @@ -222,7 +224,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli if ownerRepo != whForRepo { continue } - msg := gomatrix.TextMessage{ + msg := mevt.MessageEventContent{ Body: outputForTemplate(repoData.Template, tmplData), MsgType: "m.notice", } @@ -231,7 +233,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli "message": msg, "room_id": roomID, }).Print("Sending Travis-CI notification to room") - if _, e := cli.SendMessageEvent(roomID, "m.room.message", msg); e != nil { + if _, e := cli.SendMessageEvent(roomID, mevt.EventMessage, msg); e != nil { logger.WithError(e).WithField("room_id", roomID).Print( "Failed to send Travis-CI notification to room.") } @@ -241,7 +243,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli } // Register makes sure the Config information supplied is valid. -func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error { +func (s *Service) Register(oldService types.Service, client *mautrix.Client) error { s.WebhookURL = s.webhookEndpointURL for _, roomData := range s.Rooms { for repo := range roomData.Repos { @@ -273,9 +275,9 @@ func (s *Service) PostRegister(oldService types.Service) { } } -func (s *Service) joinRooms(client *gomatrix.Client) { +func (s *Service) joinRooms(client *mautrix.Client) { for roomID := range s.Rooms { - if _, err := client.JoinRoom(roomID, "", nil); err != nil { + if _, err := client.JoinRoom(roomID.String(), "", nil); err != nil { log.WithFields(log.Fields{ log.ErrorKey: err, "room_id": roomID, @@ -286,7 +288,7 @@ func (s *Service) joinRooms(client *gomatrix.Client) { } func init() { - types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { + types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { return &Service{ DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), webhookEndpointURL: webhookEndpointURL, diff --git a/services/travisci/travisci_test.go b/services/travisci/travisci_test.go index 7bcbf61..91a0091 100644 --- a/services/travisci/travisci_test.go +++ b/services/travisci/travisci_test.go @@ -13,7 +13,8 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/testutils" "github.com/matrix-org/go-neb/types" - "github.com/matrix-org/gomatrix" + "maunium.net/go/mautrix" + mevt "maunium.net/go/mautrix/event" ) const travisOrgPEMPublicKey = (`-----BEGIN PUBLIC KEY----- @@ -114,13 +115,13 @@ func TestTravisCI(t *testing.T) { httpClient = &http.Client{Transport: travisTransport} // Intercept message sending to Matrix and mock responses - msgs := []gomatrix.TextMessage{} + msgs := []mevt.MessageEventContent{} matrixTrans := struct{ testutils.MockTransport }{} matrixTrans.RT = func(req *http.Request) (*http.Response, error) { if !strings.Contains(req.URL.String(), "/send/m.room.message") { return nil, fmt.Errorf("Unhandled URL: %s", req.URL.String()) } - var msg gomatrix.TextMessage + var msg mevt.MessageEventContent if err := json.NewDecoder(req.Body).Decode(&msg); err != nil { return nil, fmt.Errorf("Failed to decode request JSON: %s", err) } @@ -130,13 +131,13 @@ func TestTravisCI(t *testing.T) { Body: ioutil.NopCloser(bytes.NewBufferString(`{"event_id":"$yup:event"}`)), }, nil } - matrixCli, _ := gomatrix.NewClient("https://hyrule", "@travisci:hyrule", "its_a_secret") + matrixCli, _ := mautrix.NewClient("https://hyrule", "@travisci:hyrule", "its_a_secret") matrixCli.Client = &http.Client{Transport: matrixTrans} // BEGIN running the Travis-CI table tests // --------------------------------------- for _, test := range travisTests { - msgs = []gomatrix.TextMessage{} // reset sent messages + msgs = []mevt.MessageEventContent{} // reset sent messages mockWriter := httptest.NewRecorder() travis := makeService(t, test.Template) if travis == nil { @@ -172,7 +173,7 @@ func TestTravisCI(t *testing.T) { } } -func assertResponse(t *testing.T, w *httptest.ResponseRecorder, msgs []gomatrix.TextMessage, expectCode int, expectMsgLength int) bool { +func assertResponse(t *testing.T, w *httptest.ResponseRecorder, msgs []mevt.MessageEventContent, expectCode int, expectMsgLength int) bool { if w.Code != expectCode { t.Errorf("TestTravisCI OnReceiveWebhook want HTTP code %d, got %d", expectCode, w.Code) return false