summaryrefslogtreecommitdiffstats
path: root/bridge/gitter
diff options
context:
space:
mode:
authorWim <wim@42.be>2018-02-23 23:31:37 +0100
committerWim <wim@42.be>2018-02-27 23:22:09 +0100
commitcac5d56d60bdd11c637b31251416b306909135e7 (patch)
tree3cec806fa26ff224d40f7e1caf2cebd7ed04ac98 /bridge/gitter
parentbd2a672c1436737b285c3a05f85b2757887a181c (diff)
downloadmatterbridge-msglm-cac5d56d60bdd11c637b31251416b306909135e7.tar.gz
matterbridge-msglm-cac5d56d60bdd11c637b31251416b306909135e7.tar.bz2
matterbridge-msglm-cac5d56d60bdd11c637b31251416b306909135e7.zip
Refactor gitter
Diffstat (limited to 'bridge/gitter')
-rw-r--r--bridge/gitter/gitter.go63
1 files changed, 38 insertions, 25 deletions
diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go
index f11af6a7..69af8d2c 100644
--- a/bridge/gitter/gitter.go
+++ b/bridge/gitter/gitter.go
@@ -34,11 +34,13 @@ func (b *Bgitter) Connect() error {
b.c = gitter.New(b.Config.Token)
b.User, err = b.c.GetUser()
if err != nil {
- flog.Debugf("%#v", err)
+ return err
+ }
+ b.Rooms, err = b.c.GetRooms()
+ if err != nil {
return err
}
flog.Info("Connection succeeded")
- b.Rooms, _ = b.c.GetRooms()
return nil
}
@@ -74,6 +76,7 @@ func (b *Bgitter) JoinChannel(channel config.ChannelInfo) error {
for event := range stream.Event {
switch ev := event.Data.(type) {
case *gitter.MessageReceived:
+ // ignore message sent from ourselves
if ev.Message.From.ID != b.User.ID {
flog.Debugf("Sending message from %s on %s to gateway", ev.Message.From.Username, b.Account)
rmsg := config.Message{Username: ev.Message.From.Username, Text: ev.Message.Text, Channel: room,
@@ -101,48 +104,41 @@ func (b *Bgitter) Send(msg config.Message) (string, error) {
flog.Errorf("Could not find roomID for %v", msg.Channel)
return "", nil
}
+
+ // Delete message
if msg.Event == config.EVENT_MSG_DELETE {
if msg.ID == "" {
return "", nil
}
- // gitter has no delete message api
+ // gitter has no delete message api so we edit message to ""
_, err := b.c.UpdateMessage(roomID, msg.ID, "")
if err != nil {
return "", err
}
return "", nil
}
- if msg.ID != "" {
- flog.Debugf("updating message with id %s", msg.ID)
- _, err := b.c.UpdateMessage(roomID, msg.ID, msg.Username+msg.Text)
- if err != nil {
- return "", err
- }
- return "", nil
- }
+ // Upload a file (in gitter case send the upload URL because gitter has no native upload support)
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
b.c.SendMessage(roomID, rmsg.Username+rmsg.Text)
}
if len(msg.Extra["file"]) > 0 {
- for _, f := range msg.Extra["file"] {
- fi := f.(config.FileInfo)
- if fi.Comment != "" {
- msg.Text += fi.Comment + ": "
- }
- if fi.URL != "" {
- msg.Text = fi.URL
- }
- _, err := b.c.SendMessage(roomID, msg.Username+msg.Text)
- if err != nil {
- return "", err
- }
- }
- return "", nil
+ return b.handleUploadFile(&msg, roomID)
}
}
+ // Edit message
+ if msg.ID != "" {
+ flog.Debugf("updating message with id %s", msg.ID)
+ _, err := b.c.UpdateMessage(roomID, msg.ID, msg.Username+msg.Text)
+ if err != nil {
+ return "", err
+ }
+ return "", nil
+ }
+
+ // Post normal message
resp, err := b.c.SendMessage(roomID, msg.Username+msg.Text)
if err != nil {
return "", err
@@ -170,3 +166,20 @@ func (b *Bgitter) getAvatar(user string) string {
}
return avatar
}
+
+func (b *Bgitter) handleUploadFile(msg *config.Message, roomID string) (string, error) {
+ for _, f := range msg.Extra["file"] {
+ fi := f.(config.FileInfo)
+ if fi.Comment != "" {
+ msg.Text += fi.Comment + ": "
+ }
+ if fi.URL != "" {
+ msg.Text = fi.URL
+ }
+ _, err := b.c.SendMessage(roomID, msg.Username+msg.Text)
+ if err != nil {
+ return "", err
+ }
+ }
+ return "", nil
+}