diff options
author | Wim <wim@42.be> | 2017-08-28 00:33:17 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-08-28 00:33:17 +0200 |
commit | 7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe (patch) | |
tree | 0abdc66a0c407635b1d8faec3dec13506c5e3931 /bridge | |
parent | e84417430d02663eb83e1ef8757c3b1bf7609bae (diff) | |
download | matterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.tar.gz matterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.tar.bz2 matterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.zip |
Add support for editing messages across bridges. Currently mattermost/discord.
Our Message type has an extra ID field which contains the message ID of the specific bridge.
The Send() function has been modified to return a msg ID (after the message to that specific
bridge has been created).
There is a lru cache of 5000 entries (message IDs). All in memory, so editing messages
will only work for messages the bot has seen.
Currently we go out from the idea that every message ID is unique, so we don't keep
the ID separate for each bridge. (we do for each gateway though)
If there's a new message from a bridge, we put that message ID in the LRU cache as key
and the []*BrMsgID as value (this slice contains the message ID's of each bridge that
received the new message)
If there's a new message and this message ID already exists in the cache, it must be
an updated message. The value from the cache gets checked for each bridge and if there
is a message ID for this bridge, the ID will be added to the Message{} sent to that
bridge. If the bridge sees that the ID isn't empty, it'll know it has to update the
message with that specific ID instead of creating a new message.
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/config/config.go | 1 | ||||
-rw-r--r-- | bridge/discord/discord.go | 37 | ||||
-rw-r--r-- | bridge/mattermost/mattermost.go | 10 |
3 files changed, 30 insertions, 18 deletions
diff --git a/bridge/config/config.go b/bridge/config/config.go index ae937915..7e6786a8 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -27,6 +27,7 @@ type Message struct { Protocol string `json:"protocol"` Gateway string `json:"gateway"` Timestamp time.Time `json:"timestamp"` + ID string `json:"id"` } type ChannelInfo struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index b44ad898..ffbada40 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -129,20 +129,27 @@ func (b *bdiscord) Send(msg config.Message) (string, error) { if wID == "" { flog.Debugf("Broadcasting using token (API)") - b.c.ChannelMessageSend(channelID, msg.Username+msg.Text) - } else { - flog.Debugf("Broadcasting using Webhook") - b.c.WebhookExecute( - wID, - wToken, - true, - &discordgo.WebhookParams{ - Content: msg.Text, - Username: msg.Username, - AvatarURL: msg.Avatar, - }) - } - return "", nil + if msg.ID != "" { + _, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text) + return msg.ID, err + } + res, err := b.c.ChannelMessageSend(channelID, msg.Username+msg.Text) + if err != nil { + return "", err + } + return res.ID, err + } + flog.Debugf("Broadcasting using Webhook") + err := b.c.WebhookExecute( + wID, + wToken, + true, + &discordgo.WebhookParams{ + Content: msg.Text, + Username: msg.Username, + AvatarURL: msg.Avatar, + }) + return "", err } func (b *bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { @@ -185,7 +192,7 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat } rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", - UserID: m.Author.ID} + UserID: m.Author.ID, ID: m.ID} rmsg.Channel = b.getChannelName(m.ChannelID) if b.UseChannelID { diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index e7f125bd..6bc1ebb7 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -23,6 +23,7 @@ type MMMessage struct { Channel string Username string UserID string + ID string } type Bmattermost struct { @@ -162,8 +163,10 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { } return "", nil } - b.mc.PostMessage(b.mc.GetChannelId(channel, ""), message) - return "", nil + if msg.ID != "" { + return b.mc.EditMessage(msg.ID, message) + } + return b.mc.PostMessage(b.mc.GetChannelId(channel, ""), message) } func (b *Bmattermost) handleMatter() { @@ -180,7 +183,7 @@ func (b *Bmattermost) handleMatter() { go b.handleMatterClient(mchan) } for message := range mchan { - rmsg := config.Message{Username: message.Username, Channel: message.Channel, Account: b.Account, UserID: message.UserID} + rmsg := config.Message{Username: message.Username, Channel: message.Channel, Account: b.Account, UserID: message.UserID, ID: message.ID} text, ok := b.replaceAction(message.Text) if ok { rmsg.Event = config.EVENT_USER_ACTION @@ -218,6 +221,7 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) { m.Username = message.Username m.Channel = message.Channel m.Text = message.Text + m.ID = message.Post.Id if message.Raw.Event == "post_edited" && !b.Config.EditDisable { m.Text = message.Text + b.Config.EditSuffix } |