diff options
author | Wim <wim@42.be> | 2017-04-15 23:46:01 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-04-15 23:46:01 +0200 |
commit | 17b8b86d68badfe27c29c7448548f9eb97dd26e9 (patch) | |
tree | 8161517112de284f216b4f0a54bd2190eb17fdee /bridge/gitter | |
parent | fdb57230a363d1326f597253bb230f0e7e325aee (diff) | |
download | matterbridge-msglm-17b8b86d68badfe27c29c7448548f9eb97dd26e9.tar.gz matterbridge-msglm-17b8b86d68badfe27c29c7448548f9eb97dd26e9.tar.bz2 matterbridge-msglm-17b8b86d68badfe27c29c7448548f9eb97dd26e9.zip |
Add support for edited messages (gitter)
Diffstat (limited to 'bridge/gitter')
-rw-r--r-- | bridge/gitter/gitter.go | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index af71d892..a962f923 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -2,10 +2,12 @@ package bgitter import ( "fmt" + "strings" + "time" + "github.com/42wim/matterbridge/bridge/config" log "github.com/Sirupsen/logrus" "github.com/sromku/go-gitter" - "strings" ) type Bgitter struct { @@ -71,27 +73,48 @@ func (b *Bgitter) JoinChannel(channel string) error { } users, _ := b.c.GetUsersInRoom(roomID) b.Users = append(b.Users, users...) + + // we need to use (experimental) faye for edits. + // streaming API doesn't show edits. + if !b.Config.EditDisable { + faye := b.c.Faye(roomID) + go faye.Listen() + go func(stream *gitter.Faye, room string) { + for event := range stream.Event { + b.handleEvent(event, room) + } + }(faye, room.Name) + return nil + } stream := b.c.Stream(roomID) go b.c.Listen(stream) go func(stream *gitter.Stream, room string) { for event := range stream.Event { - switch ev := event.Data.(type) { - case *gitter.MessageReceived: - // check for ZWSP to see if it's not an echo - if !strings.HasSuffix(ev.Message.Text, "") { - flog.Debugf("Sending message from %s on %s to gateway", ev.Message.From.Username, b.Account) - b.Remote <- config.Message{Username: ev.Message.From.Username, Text: ev.Message.Text, Channel: room, - Account: b.Account, Avatar: b.getAvatar(ev.Message.From.Username)} - } - case *gitter.GitterConnectionClosed: - flog.Errorf("connection with gitter closed for room %s", room) - } + b.handleEvent(event, room) } }(stream, room.Name) return nil } +func (b *Bgitter) handleEvent(event gitter.Event, room string) { + flog.Debugf("event: %#v %#v", event.Data, event.Data.(*gitter.MessageReceived).Message.EditedAt.Sub(time.Now()).String()) + switch ev := event.Data.(type) { + case *gitter.MessageReceived: + // check for ZWSP to see if it's not an echo + if !strings.HasSuffix(ev.Message.Text, "") { + if !ev.Message.EditedAt.IsZero() && !b.Config.EditDisable { + ev.Message.Text = ev.Message.Text + b.Config.EditSuffix + } + flog.Debugf("Sending message from %s on %s to gateway", ev.Message.From.Username, b.Account) + b.Remote <- config.Message{Username: ev.Message.From.Username, Text: ev.Message.Text, Channel: room, + Account: b.Account, Avatar: b.getAvatar(ev.Message.From.Username)} + } + case *gitter.GitterConnectionClosed: + flog.Errorf("connection with gitter closed for room %s", room) + } +} + func (b *Bgitter) Send(msg config.Message) error { flog.Debugf("Receiving %#v", msg) roomID := b.getRoomID(msg.Channel) |