forked from lug/matterbridge
		
	Relay attachments from mattermost to slack (slack). Closes #260
This commit is contained in:
		@@ -29,6 +29,7 @@ type Message struct {
 | 
				
			|||||||
	Gateway   string    `json:"gateway"`
 | 
						Gateway   string    `json:"gateway"`
 | 
				
			||||||
	Timestamp time.Time `json:"timestamp"`
 | 
						Timestamp time.Time `json:"timestamp"`
 | 
				
			||||||
	ID        string    `json:"id"`
 | 
						ID        string    `json:"id"`
 | 
				
			||||||
 | 
						Extra     []interface{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ChannelInfo struct {
 | 
					type ChannelInfo struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,7 @@ type MMMessage struct {
 | 
				
			|||||||
	UserID   string
 | 
						UserID   string
 | 
				
			||||||
	ID       string
 | 
						ID       string
 | 
				
			||||||
	Event    string
 | 
						Event    string
 | 
				
			||||||
 | 
						Extra    []interface{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Bmattermost struct {
 | 
					type Bmattermost struct {
 | 
				
			||||||
@@ -195,7 +196,7 @@ func (b *Bmattermost) handleMatter() {
 | 
				
			|||||||
		go b.handleMatterClient(mchan)
 | 
							go b.handleMatterClient(mchan)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for message := range mchan {
 | 
						for message := range mchan {
 | 
				
			||||||
		rmsg := config.Message{Username: message.Username, Channel: message.Channel, Account: b.Account, UserID: message.UserID, ID: message.ID, Event: message.Event}
 | 
							rmsg := config.Message{Username: message.Username, Channel: message.Channel, Account: b.Account, UserID: message.UserID, ID: message.ID, Event: message.Event, Extra: message.Extra}
 | 
				
			||||||
		text, ok := b.replaceAction(message.Text)
 | 
							text, ok := b.replaceAction(message.Text)
 | 
				
			||||||
		if ok {
 | 
							if ok {
 | 
				
			||||||
			rmsg.Event = config.EVENT_USER_ACTION
 | 
								rmsg.Event = config.EVENT_USER_ACTION
 | 
				
			||||||
@@ -220,11 +221,17 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
 | 
				
			|||||||
		if (message.Raw.Event == "post_edited") && b.Config.EditDisable {
 | 
							if (message.Raw.Event == "post_edited") && b.Config.EditDisable {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							m := &MMMessage{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		props := message.Post.Props
 | 
							props := message.Post.Props
 | 
				
			||||||
		if props != nil {
 | 
							if props != nil {
 | 
				
			||||||
			if _, ok := props["override_username"].(string); ok {
 | 
								if _, ok := props["override_username"].(string); ok {
 | 
				
			||||||
				message.Username = props["override_username"].(string)
 | 
									message.Username = props["override_username"].(string)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								if _, ok := props["attachments"].([]interface{}); ok {
 | 
				
			||||||
 | 
									m.Extra = props["attachments"].([]interface{})
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// do not post our own messages back to irc
 | 
							// do not post our own messages back to irc
 | 
				
			||||||
		// only listen to message from our team
 | 
							// only listen to message from our team
 | 
				
			||||||
@@ -235,7 +242,6 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
 | 
				
			|||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			flog.Debugf("Receiving from matterclient %#v", message)
 | 
								flog.Debugf("Receiving from matterclient %#v", message)
 | 
				
			||||||
			m := &MMMessage{}
 | 
					 | 
				
			||||||
			m.UserID = message.UserID
 | 
								m.UserID = message.UserID
 | 
				
			||||||
			m.Username = message.Username
 | 
								m.Username = message.Username
 | 
				
			||||||
			m.Channel = message.Channel
 | 
								m.Channel = message.Channel
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -163,6 +163,8 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
 | 
				
			|||||||
		np.IconURL = msg.Avatar
 | 
							np.IconURL = msg.Avatar
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge"})
 | 
						np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge"})
 | 
				
			||||||
 | 
						np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// replace mentions
 | 
						// replace mentions
 | 
				
			||||||
	np.LinkNames = 1
 | 
						np.LinkNames = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -389,3 +391,28 @@ func (b *Bslack) replaceURL(text string) string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return text
 | 
						return text
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (b *Bslack) createAttach(extra []interface{}) []slack.Attachment {
 | 
				
			||||||
 | 
						var attachs []slack.Attachment
 | 
				
			||||||
 | 
						if extra != nil {
 | 
				
			||||||
 | 
							for _, v := range extra {
 | 
				
			||||||
 | 
								entry := v.(map[string]interface{})
 | 
				
			||||||
 | 
								s := slack.Attachment{}
 | 
				
			||||||
 | 
								s.Fallback = entry["fallback"].(string)
 | 
				
			||||||
 | 
								s.Color = entry["color"].(string)
 | 
				
			||||||
 | 
								s.Pretext = entry["pretext"].(string)
 | 
				
			||||||
 | 
								s.AuthorName = entry["author_name"].(string)
 | 
				
			||||||
 | 
								s.AuthorLink = entry["author_link"].(string)
 | 
				
			||||||
 | 
								s.AuthorIcon = entry["author_icon"].(string)
 | 
				
			||||||
 | 
								s.Title = entry["title"].(string)
 | 
				
			||||||
 | 
								s.TitleLink = entry["title_link"].(string)
 | 
				
			||||||
 | 
								s.Text = entry["text"].(string)
 | 
				
			||||||
 | 
								s.ImageURL = entry["image_url"].(string)
 | 
				
			||||||
 | 
								s.ThumbURL = entry["thumb_url"].(string)
 | 
				
			||||||
 | 
								s.Footer = entry["footer"].(string)
 | 
				
			||||||
 | 
								s.FooterIcon = entry["footer_icon"].(string)
 | 
				
			||||||
 | 
								attachs = append(attachs, s)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return attachs
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -147,6 +147,17 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrMsgID {
 | 
					func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrMsgID {
 | 
				
			||||||
	var brMsgIDs []*BrMsgID
 | 
						var brMsgIDs []*BrMsgID
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO refactor
 | 
				
			||||||
 | 
						// only slack now, check will have to be done in the different bridges.
 | 
				
			||||||
 | 
						// we need to check if we can't use fallback or text in other bridges
 | 
				
			||||||
 | 
						if msg.Extra != nil {
 | 
				
			||||||
 | 
							if dest.Protocol != "slack" {
 | 
				
			||||||
 | 
								if msg.Text == "" {
 | 
				
			||||||
 | 
									return brMsgIDs
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	// only relay join/part when configged
 | 
						// only relay join/part when configged
 | 
				
			||||||
	if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
 | 
						if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
 | 
				
			||||||
		return brMsgIDs
 | 
							return brMsgIDs
 | 
				
			||||||
@@ -199,6 +210,10 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
 | 
				
			|||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if msg.Text == "" {
 | 
						if msg.Text == "" {
 | 
				
			||||||
 | 
							// we have an attachment
 | 
				
			||||||
 | 
							if msg.Extra != nil {
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		log.Debugf("ignoring empty message %#v from %s", msg, msg.Account)
 | 
							log.Debugf("ignoring empty message %#v from %s", msg, msg.Account)
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user