From 1b1589713592ec9d8f1945e4e61cac423e3e245e Mon Sep 17 00:00:00 2001 From: Wim Date: Wed, 2 Nov 2016 16:12:43 +0100 Subject: [PATCH] Fix tight loop (gitter). Closes #68. --- bridge/gitter/gitter.go | 2 +- .../{sromku => 42wim}/go-gitter/LICENSE | 0 .../{sromku => 42wim}/go-gitter/faye.go | 0 .../{sromku => 42wim}/go-gitter/gitter.go | 79 +++++++++++++++---- .../{sromku => 42wim}/go-gitter/model.go | 0 .../{sromku => 42wim}/go-gitter/stream.go | 9 ++- .../{sromku => 42wim}/go-gitter/test_utils.go | 0 vendor/manifest | 16 ++-- 8 files changed, 76 insertions(+), 30 deletions(-) rename vendor/github.com/{sromku => 42wim}/go-gitter/LICENSE (100%) rename vendor/github.com/{sromku => 42wim}/go-gitter/faye.go (100%) rename vendor/github.com/{sromku => 42wim}/go-gitter/gitter.go (82%) rename vendor/github.com/{sromku => 42wim}/go-gitter/model.go (100%) rename vendor/github.com/{sromku => 42wim}/go-gitter/stream.go (99%) rename vendor/github.com/{sromku => 42wim}/go-gitter/test_utils.go (100%) diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 7728a872..faa7875c 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -1,9 +1,9 @@ package bgitter import ( + "github.com/42wim/go-gitter" "github.com/42wim/matterbridge/bridge/config" log "github.com/Sirupsen/logrus" - "github.com/sromku/go-gitter" "strings" ) diff --git a/vendor/github.com/sromku/go-gitter/LICENSE b/vendor/github.com/42wim/go-gitter/LICENSE similarity index 100% rename from vendor/github.com/sromku/go-gitter/LICENSE rename to vendor/github.com/42wim/go-gitter/LICENSE diff --git a/vendor/github.com/sromku/go-gitter/faye.go b/vendor/github.com/42wim/go-gitter/faye.go similarity index 100% rename from vendor/github.com/sromku/go-gitter/faye.go rename to vendor/github.com/42wim/go-gitter/faye.go diff --git a/vendor/github.com/sromku/go-gitter/gitter.go b/vendor/github.com/42wim/go-gitter/gitter.go similarity index 82% rename from vendor/github.com/sromku/go-gitter/gitter.go rename to vendor/github.com/42wim/go-gitter/gitter.go index 375e6e35..af082881 100644 --- a/vendor/github.com/sromku/go-gitter/gitter.go +++ b/vendor/github.com/42wim/go-gitter/gitter.go @@ -192,7 +192,7 @@ func (gitter *Gitter) SendMessage(roomID, text string) error { message := Message{Text: text} body, _ := json.Marshal(message) - err := gitter.post(gitter.config.apiBaseURL+"rooms/"+roomID+"/chatMessages", body) + _, err := gitter.post(gitter.config.apiBaseURL+"rooms/"+roomID+"/chatMessages", body) if err != nil { gitter.log(err) return err @@ -202,31 +202,37 @@ func (gitter *Gitter) SendMessage(roomID, text string) error { } // JoinRoom joins a room -func (gitter *Gitter) JoinRoom(uri string) (*Room, error) { +func (gitter *Gitter) JoinRoom(roomID, userID string) (*Room, error) { - message := Room{URI: uri} + message := Room{ID: roomID} body, _ := json.Marshal(message) - err := gitter.post(apiBaseURL+"rooms", body) + response, err := gitter.post(gitter.config.apiBaseURL+"user/"+userID+"/rooms", body) + if err != nil { gitter.log(err) return nil, err } - rooms, err := gitter.GetRooms() + var room Room + err = json.Unmarshal(response, &room) if err != nil { gitter.log(err) return nil, err } - for _, room := range rooms { - if room.URI == uri { - return &room, nil - } + return &room, nil +} + +// LeaveRoom removes a user from the room +func (gitter *Gitter) LeaveRoom(roomID, userID string) error { + + _, err := gitter.delete(gitter.config.apiBaseURL + "rooms/" + roomID + "/users/" + userID) + if err != nil { + gitter.log(err) + return err } - err = APIError{What: fmt.Sprintf("Joined room (%v) not found in list of rooms", uri)} - gitter.log(err) - return nil, err + return nil } // SetDebug traces errors if it's set to true. @@ -319,11 +325,11 @@ func (gitter *Gitter) get(url string) ([]byte, error) { return body, nil } -func (gitter *Gitter) post(url string, body []byte) error { +func (gitter *Gitter) post(url string, body []byte) ([]byte, error) { r, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) if err != nil { gitter.log(err) - return err + return nil, err } r.Header.Set("Content-Type", "application/json") @@ -333,17 +339,56 @@ func (gitter *Gitter) post(url string, body []byte) error { resp, err := gitter.config.client.Do(r) if err != nil { gitter.log(err) - return err + return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { err = APIError{What: fmt.Sprintf("Status code: %v", resp.StatusCode)} gitter.log(err) - return err + return nil, err } - return nil + result, err := ioutil.ReadAll(resp.Body) + if err != nil { + gitter.log(err) + return nil, err + } + + return result, nil +} + +func (gitter *Gitter) delete(url string) ([]byte, error) { + r, err := http.NewRequest("delete", url, nil) + if err != nil { + gitter.log(err) + return nil, err + } + + r.Header.Set("Content-Type", "application/json") + r.Header.Set("Accept", "application/json") + r.Header.Set("Authorization", "Bearer "+gitter.config.token) + + resp, err := gitter.config.client.Do(r) + if err != nil { + gitter.log(err) + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + err = APIError{What: fmt.Sprintf("Status code: %v", resp.StatusCode)} + gitter.log(err) + return nil, err + } + + result, err := ioutil.ReadAll(resp.Body) + if err != nil { + gitter.log(err) + return nil, err + } + + return result, nil } func (gitter *Gitter) log(a interface{}) { diff --git a/vendor/github.com/sromku/go-gitter/model.go b/vendor/github.com/42wim/go-gitter/model.go similarity index 100% rename from vendor/github.com/sromku/go-gitter/model.go rename to vendor/github.com/42wim/go-gitter/model.go diff --git a/vendor/github.com/sromku/go-gitter/stream.go b/vendor/github.com/42wim/go-gitter/stream.go similarity index 99% rename from vendor/github.com/sromku/go-gitter/stream.go rename to vendor/github.com/42wim/go-gitter/stream.go index 5f1cd78f..4a5a3c68 100644 --- a/vendor/github.com/sromku/go-gitter/stream.go +++ b/vendor/github.com/42wim/go-gitter/stream.go @@ -57,6 +57,11 @@ Loop: //"The JSON stream returns messages as JSON objects that are delimited by carriage return (\r)" <- Not true crap it's (\n) only reader = bufio.NewReader(resp.Body) line, err := reader.ReadBytes('\n') + if err != nil { + gitter.log("ReadBytes error: " + err.Error()) + stream.connect() + continue + } //Check if the line only consists of whitespace onlyWhitespace := true @@ -77,10 +82,6 @@ Loop: } else if stream.isClosed() { gitter.log("Stream closed") continue - } else if err != nil { - gitter.log("ReadBytes error: " + err.Error()) - stream.connect() - continue } // unmarshal the streamed data diff --git a/vendor/github.com/sromku/go-gitter/test_utils.go b/vendor/github.com/42wim/go-gitter/test_utils.go similarity index 100% rename from vendor/github.com/sromku/go-gitter/test_utils.go rename to vendor/github.com/42wim/go-gitter/test_utils.go diff --git a/vendor/manifest b/vendor/manifest index 705c463d..fc904fad 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -1,6 +1,14 @@ { "version": 0, "dependencies": [ + { + "importpath": "github.com/42wim/go-gitter", + "repository": "https://github.com/42wim/go-gitter", + "vcs": "git", + "revision": "ae777f740326ef6b4d910496022649859232ff38", + "branch": "fixloop", + "notests": true + }, { "importpath": "github.com/42wim/matterbridge-plus/bridge", "repository": "https://github.com/42wim/matterbridge-plus", @@ -158,14 +166,6 @@ "branch": "master", "notests": true }, - { - "importpath": "github.com/sromku/go-gitter", - "repository": "https://github.com/sromku/go-gitter", - "vcs": "git", - "revision": "932bf9af423ac2da1544cb73540b3b08b1bdb181", - "branch": "master", - "notests": true - }, { "importpath": "github.com/thoj/go-ircevent", "repository": "https://github.com/thoj/go-ircevent",