Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 3529af6fe6fd54c05df7cb6658a3769017770c98 Author: Wim <wim@42.be> Date: Sun Nov 06 00:46:32 2016 +0000 Add support for using avatars from discord,slack and gitter in slack diff --git a/bridge/config/config.go b/bridge/config/config.go index 32c8c74..1575bff 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -156 +157 @@ type Message struct {   Origin string   FullOrigin string   Protocol string + Avatar string  }    type Protocol struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 7995a06..62bc85c 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -1277 +1277 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat   channelName = "ID:" + m.ChannelID   }   b.Remote <- config.Message{Username: m.Author.Username, Text: m.ContentWithMentionsReplaced(), Channel: channelName, - Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg"}  }    func (b *bdiscord) getChannelID(name string) string { diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 5daf777..176b6ce 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -136 +137 @@ type Bgitter struct {   Remote chan config.Message   protocol string   origin string + Users []gitter.User   Rooms []gitter.Room  }   @@ -646 +658 @@ func (b *Bgitter) JoinChannel(channel string) error {   if err != nil {   return err   } + users, _ := b.c.GetUsersInRoom(roomID) + b.Users = append(b.Users, users...)   stream := b.c.Stream(roomID)   go b.c.Listen(stream)   @@ -767 +797 @@ func (b *Bgitter) JoinChannel(channel string) error {   if !strings.HasSuffix(ev.Message.Text, "​") {   flog.Debugf("Sending message from %s on %s to gateway", ev.Message.From.Username, b.FullOrigin())   b.Remote <- config.Message{Username: ev.Message.From.Username, Text: ev.Message.Text, Channel: room, - Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: b.getAvatar(ev.Message.From.Username)}   }   case *gitter.GitterConnectionClosed:   flog.Errorf("connection with gitter closed for room %s", room) @@ -1183 +12115 @@ func (b *Bgitter) getRoomID(channel string) string {   }   return ""  } + +func (b *Bgitter) getAvatar(user string) string { + var avatar string + if b.Users != nil { + for _, u := range b.Users { + if user == u.Username { + return u.AvatarURLSmall + } + } + } + return avatar +} diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 412c925..f700aee 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -246 +247 @@ type Bslack struct {   rtm *slack.RTM   Plus bool   Remote chan config.Message + Users []slack.User   protocol string   origin string   si *slack.Info @@ -1266 +1279 @@ func (b *Bslack) Send(msg config.Message) error {   }   np.Username = nick   np.IconURL = config.GetIconURL(&msg, b.Config) + if msg.Avatar != "" { + np.IconURL = msg.Avatar + }   b.sc.PostMessage(schannel.ID, message, np)     /* @@ -1366 +14018 @@ func (b *Bslack) Send(msg config.Message) error {   return nil  }   +func (b *Bslack) getAvatar(user string) string { + var avatar string + if b.Users != nil { + for _, u := range b.Users { + if user == u.Name { + return u.Profile.Image48 + } + } + } + return avatar +} +  func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {   if b.channels == nil {   return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.FullOrigin(), name) @@ -1667 +1827 @@ func (b *Bslack) handleSlack() {   texts := strings.Split(message.Text, "\n")   for _, text := range texts {   flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.FullOrigin()) - b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: b.getAvatar(message.Username)}   }   }  } @@ -2016 +2177 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {   case *slack.ConnectedEvent:   b.channels = ev.Info.Channels   b.si = ev.Info + b.Users, _ = b.sc.GetUsers()   case *slack.InvalidAuthEvent:   flog.Fatalf("Invalid Token %#v", ev)   default: