mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-15 08:29:25 +01:00
Fix tight loop (gitter). Closes #68.
This commit is contained in:
parent
8e606e3cef
commit
1b15897135
@ -1,9 +1,9 @@
|
|||||||
package bgitter
|
package bgitter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/42wim/go-gitter"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/sromku/go-gitter"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ func (gitter *Gitter) SendMessage(roomID, text string) error {
|
|||||||
|
|
||||||
message := Message{Text: text}
|
message := Message{Text: text}
|
||||||
body, _ := json.Marshal(message)
|
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 {
|
if err != nil {
|
||||||
gitter.log(err)
|
gitter.log(err)
|
||||||
return err
|
return err
|
||||||
@ -202,31 +202,37 @@ func (gitter *Gitter) SendMessage(roomID, text string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JoinRoom joins a room
|
// 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)
|
body, _ := json.Marshal(message)
|
||||||
err := gitter.post(apiBaseURL+"rooms", body)
|
response, err := gitter.post(gitter.config.apiBaseURL+"user/"+userID+"/rooms", body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gitter.log(err)
|
gitter.log(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rooms, err := gitter.GetRooms()
|
var room Room
|
||||||
|
err = json.Unmarshal(response, &room)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gitter.log(err)
|
gitter.log(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, room := range rooms {
|
return &room, nil
|
||||||
if room.URI == uri {
|
}
|
||||||
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)}
|
return nil
|
||||||
gitter.log(err)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDebug traces errors if it's set to true.
|
// SetDebug traces errors if it's set to true.
|
||||||
@ -319,11 +325,11 @@ func (gitter *Gitter) get(url string) ([]byte, error) {
|
|||||||
return body, nil
|
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))
|
r, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gitter.log(err)
|
gitter.log(err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Set("Content-Type", "application/json")
|
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)
|
resp, err := gitter.config.client.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gitter.log(err)
|
gitter.log(err)
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
err = APIError{What: fmt.Sprintf("Status code: %v", resp.StatusCode)}
|
err = APIError{What: fmt.Sprintf("Status code: %v", resp.StatusCode)}
|
||||||
gitter.log(err)
|
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{}) {
|
func (gitter *Gitter) log(a interface{}) {
|
@ -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
|
//"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)
|
reader = bufio.NewReader(resp.Body)
|
||||||
line, err := reader.ReadBytes('\n')
|
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
|
//Check if the line only consists of whitespace
|
||||||
onlyWhitespace := true
|
onlyWhitespace := true
|
||||||
@ -77,10 +82,6 @@ Loop:
|
|||||||
} else if stream.isClosed() {
|
} else if stream.isClosed() {
|
||||||
gitter.log("Stream closed")
|
gitter.log("Stream closed")
|
||||||
continue
|
continue
|
||||||
} else if err != nil {
|
|
||||||
gitter.log("ReadBytes error: " + err.Error())
|
|
||||||
stream.connect()
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshal the streamed data
|
// unmarshal the streamed data
|
16
vendor/manifest
vendored
16
vendor/manifest
vendored
@ -1,6 +1,14 @@
|
|||||||
{
|
{
|
||||||
"version": 0,
|
"version": 0,
|
||||||
"dependencies": [
|
"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",
|
"importpath": "github.com/42wim/matterbridge-plus/bridge",
|
||||||
"repository": "https://github.com/42wim/matterbridge-plus",
|
"repository": "https://github.com/42wim/matterbridge-plus",
|
||||||
@ -158,14 +166,6 @@
|
|||||||
"branch": "master",
|
"branch": "master",
|
||||||
"notests": true
|
"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",
|
"importpath": "github.com/thoj/go-ircevent",
|
||||||
"repository": "https://github.com/thoj/go-ircevent",
|
"repository": "https://github.com/thoj/go-ircevent",
|
||||||
|
Loading…
Reference in New Issue
Block a user