From ed08994a7c11040ec1041a9d8032fc4942828e85 Mon Sep 17 00:00:00 2001 From: Matt Burchett Date: Thu, 4 Feb 2021 17:50:52 -0600 Subject: [PATCH] Adding functionality to join rooms. --- pkg/generic/generic.go | 4 ++++ pkg/matrix/matrix.go | 39 ++++++++++++++++++--------------------- pkg/server/server.go | 2 +- pkg/slack/slack.go | 4 ++++ 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/pkg/generic/generic.go b/pkg/generic/generic.go index f7e930b..b18977b 100644 --- a/pkg/generic/generic.go +++ b/pkg/generic/generic.go @@ -24,6 +24,10 @@ func Handle(cfg config.Config) http.HandlerFunc { log.Error().Err(err).Msg("An error has occurred") } + // Attempt to join room before sending a message. + matrix.JoinRoom(cfg, vars, token) + + // Publish to Matrix resp := matrix.PublishText(cfg, vars, reqBody, token) router.Respond(w, 200, resp) diff --git a/pkg/matrix/matrix.go b/pkg/matrix/matrix.go index 1f3bb11..9349ba0 100644 --- a/pkg/matrix/matrix.go +++ b/pkg/matrix/matrix.go @@ -29,24 +29,7 @@ func GetToken(cfg config.Config, vars map[string]string) string { } s := fmt.Sprintf("%v:%v/_matrix/client/r0/login", cfg.Matrix.Homeserver, cfg.Matrix.Port) - req, err := http.NewRequest(http.MethodPost, s, bytes.NewBuffer(reqBody)) - if err != nil { - log.Error().Err(err).Msg("matrix.GetToken.req" + err.Error()) - } - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - log.Error().Err(err).Msg("matrix.GetToken.resp" + err.Error()) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error().Err(err).Msg("matrix.GetToken.body" + err.Error()) - } + body := postRequest(s, bytes.NewBuffer(reqBody)) respBody := struct { AccessToken string `json:"access_token"` @@ -74,18 +57,32 @@ func PublishText(cfg config.Config, vars map[string]string, data []byte, token s if err != nil { log.Fatal().Err(err).Msg(err.Error()) } + s := fmt.Sprintf("%v:%v/_matrix/client/r0/rooms/%v/send/m.room.message?access_token=%v", cfg.Matrix.Homeserver, cfg.Matrix.Port, vars["matrixRoom"], token) - req, err := http.NewRequest(http.MethodPost, s, bytes.NewBuffer(reqBody)) + body := postRequest(s, bytes.NewBuffer(reqBody)) + + return body +} + +// JoinRoom will attempt to join a matrix rooom, assuming there is an invite pending. +func JoinRoom(cfg config.Config, vars map[string]string, token string) { + s := fmt.Sprintf("%v:%v/_matrix/client/r0/rooms/%v/join?access_token=%v", cfg.Matrix.Homeserver, cfg.Matrix.Port, vars["matrixRoom"], token) + _ = postRequest(s, bytes.NewBuffer(nil)) +} + +// postRequest performs the post requests to the Matrix server. +func postRequest(s string, data *bytes.Buffer) []byte { + req, err := http.NewRequest(http.MethodPost, s, data) if err != nil { - log.Error().Err(err).Msg("matrix.PublishText.req" + err.Error()) + log.Error().Err(err).Msg("matrix.postRequest.req" + err.Error()) } client := &http.Client{} resp, err := client.Do(req) if err != nil { - log.Error().Err(err).Msg("matrix.PublishText.resp" + err.Error()) + log.Error().Err(err).Msg("matrix.postRequest.resp" + err.Error()) } body, err := ioutil.ReadAll(resp.Body) diff --git a/pkg/server/server.go b/pkg/server/server.go index f72ef2e..00ec7b8 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -17,7 +17,7 @@ import ( // Run ... func Run(info *router.BuildInfo) error { - conf, err := config.GetConfig("/config/config.json") + conf, err := config.GetConfig("config.json") if err != nil { log.Fatal().Err(err) } diff --git a/pkg/slack/slack.go b/pkg/slack/slack.go index e23478e..589bfc0 100644 --- a/pkg/slack/slack.go +++ b/pkg/slack/slack.go @@ -27,6 +27,10 @@ func Handle(cfg config.Config) http.HandlerFunc { data := parseSlack(reqBody) + // Attempt to join romo before publishing + matrix.JoinRoom(cfg, vars, token) + + // Publish to Matrix resp := matrix.PublishText(cfg, vars, []byte(data), token) router.Respond(w, 200, resp)