summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/42wim/go-gitter/faye.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-08-28 22:59:52 +0200
committerWim <wim@42.be>2017-08-28 23:07:11 +0200
commite59d338d4e43ed8cad3227186d0c341a44abedf6 (patch)
tree4f2d36b824db7eb0a79fb8f1cd859e89c4b347fd /vendor/github.com/42wim/go-gitter/faye.go
parent7a86044f7aa8ff70c37be1b806718e39291c0494 (diff)
downloadmatterbridge-msglm-e59d338d4e43ed8cad3227186d0c341a44abedf6.tar.gz
matterbridge-msglm-e59d338d4e43ed8cad3227186d0c341a44abedf6.tar.bz2
matterbridge-msglm-e59d338d4e43ed8cad3227186d0c341a44abedf6.zip
Use github.com/42wim/go-gitter for now
Diffstat (limited to 'vendor/github.com/42wim/go-gitter/faye.go')
-rw-r--r--vendor/github.com/42wim/go-gitter/faye.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/vendor/github.com/42wim/go-gitter/faye.go b/vendor/github.com/42wim/go-gitter/faye.go
new file mode 100644
index 00000000..dcd3e210
--- /dev/null
+++ b/vendor/github.com/42wim/go-gitter/faye.go
@@ -0,0 +1,70 @@
+package gitter
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/mrexodia/wray"
+)
+
+type Faye struct {
+ endpoint string
+ Event chan Event
+ client *wray.FayeClient
+ gitter *Gitter
+}
+
+func (gitter *Gitter) Faye(roomID string) *Faye {
+ wray.RegisterTransports([]wray.Transport{
+ &wray.HttpTransport{
+ SendHook: func(data map[string]interface{}) {
+ if channel, ok := data["channel"]; ok && channel == "/meta/handshake" {
+ data["ext"] = map[string]interface{}{"token": gitter.config.token}
+ }
+ },
+ },
+ })
+ return &Faye{
+ endpoint: "/api/v1/rooms/" + roomID + "/chatMessages",
+ Event: make(chan Event),
+ client: wray.NewFayeClient(fayeBaseURL),
+ gitter: gitter,
+ }
+}
+
+func (faye *Faye) Listen() {
+ defer faye.destroy()
+
+ faye.client.Subscribe(faye.endpoint, false, func(message wray.Message) {
+ dataBytes, err := json.Marshal(message.Data["model"])
+ if err != nil {
+ fmt.Printf("JSON Marshal error: %v\n", err)
+ return
+ }
+ var gitterMessage Message
+ err = json.Unmarshal(dataBytes, &gitterMessage)
+ if err != nil {
+ fmt.Printf("JSON Unmarshal error: %v\n", err)
+ return
+ }
+ faye.Event <- Event{
+ Data: &MessageReceived{
+ Message: gitterMessage,
+ },
+ }
+ })
+
+ //TODO: this might be needed in the future
+ /*go func() {
+ for {
+ faye.client.Publish("/api/v1/ping2", map[string]interface{}{"reason": "ping"})
+ time.Sleep(60 * time.Second)
+ }
+ }()*/
+
+ faye.client.Listen()
+}
+
+func (faye *Faye) destroy() {
+ close(faye.Event)
+}