Restrict to subset of teams

This commit is contained in:
Alex 2020-02-21 23:00:52 +01:00
parent d7552b43cd
commit 0f6be9663e

View file

@ -24,7 +24,7 @@ type Mattermost struct {
server string
username string
team string
teams map[string]bool
conn *matterclient.MMClient
handlerStopChan chan bool
@ -60,10 +60,16 @@ func (mm *Mattermost) Configure(c Configuration) error {
return err
}
mm.team, err = c.GetString("team")
teams, err := c.GetString("teams")
if err != nil {
return err
}
mm.teams = map[string]bool{}
anyteam := ""
for _, team := range strings.Split(teams, ",") {
anyteam = strings.TrimSpace(team)
mm.teams[anyteam] = true
}
notls, err := c.GetBool("no_tls", false)
if err != nil {
@ -75,7 +81,7 @@ func (mm *Mattermost) Configure(c Configuration) error {
if token != "" {
password = "token=" + token
}
mm.conn = matterclient.New(mm.username, password, mm.team, mm.server)
mm.conn = matterclient.New(mm.username, password, anyteam, mm.server)
mm.conn.Credentials.NoTLS = notls
err = mm.conn.Login()
if err != nil {
@ -112,7 +118,7 @@ func (mm *Mattermost) getTeamIdByName(name string) string {
func (mm *Mattermost) checkRoomId(id RoomID) (string, error) {
x := strings.Split(string(id), "@")
if len(x) == 1 {
return "", fmt.Errorf("Please write whole room ID with team and server: %s@%s@%s", id, mm.team, mm.server)
return "", fmt.Errorf("Please write whole room ID with team and server: %s@<team>@%s", id, mm.server)
}
if len(x) == 2 {
return x[0], nil
@ -133,15 +139,19 @@ func (mm *Mattermost) checkRoomId(id RoomID) (string, error) {
return ch_id, nil
}
func (mm *Mattermost) reverseRoomId(id string) RoomID {
func (mm *Mattermost) reverseRoomId(id string) (bool, RoomID) {
team := mm.conn.GetChannelTeamId(id)
if team == "" {
return RoomID(fmt.Sprintf("%s@%s", id, mm.server))
return true, RoomID(fmt.Sprintf("%s@%s", id, mm.server))
} else {
teamName := mm.conn.GetTeamName(team)
name := mm.conn.GetChannelName(id)
fmt.Printf("CHANNEL NAME: %s TEAM: %s\n", name, teamName)
return RoomID(fmt.Sprintf("%s@%s@%s", name, teamName, mm.server))
if u, ok := mm.teams[teamName]; ok && u {
name := mm.conn.GetChannelName(id)
fmt.Printf("CHANNEL NAME: %s TEAM: %s\n", name, teamName)
return true, RoomID(fmt.Sprintf("%s@%s@%s", name, teamName, mm.server))
} else {
return false, ""
}
}
}
@ -299,7 +309,11 @@ func (mm *Mattermost) handleConnected() {
continue // This is a DM channel
}
id := mm.reverseRoomId(ch.Id)
interested, id := mm.reverseRoomId(ch.Id)
if !interested {
// Skip channels that are not in teams we want to bridge
continue
}
mm.handler.Joined(id)
// Update room info
@ -440,7 +454,10 @@ func (mm *Mattermost) ensureJoined(user *model.User, roomId RoomID) {
}
func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error {
channel_name := msg.Data["channel_name"].(string)
channel_name, ok := msg.Data["channel_name"].(string)
if !ok {
return nil
}
post_str := msg.Data["post"].(string)
var post model.Post
err := json.Unmarshal([]byte(post_str), &post)
@ -480,14 +497,17 @@ func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_mes
if post.FileIds != nil && len(post.FileIds) > 0 {
msg_ev.Attachments = []MediaObject{}
for _, file := range post.Metadata.Files {
blob, resp := mm.conn.Client.GetFile(file.Id)
if resp.Error != nil {
return resp.Error
}
media_object := &BlobMediaObject{
media_object := &LazyBlobMediaObject{
ObjectFilename: file.Name,
ObjectMimetype: file.MimeType,
ObjectData: blob,
GetFn: func(o *LazyBlobMediaObject) error {
blob, resp := mm.conn.Client.GetFile(file.Id)
if resp.Error != nil {
return resp.Error
}
o.ObjectData = blob
return nil
},
}
if file.Width > 0 {
media_object.ObjectImageSize = &ImageSize{
@ -509,7 +529,10 @@ func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_mes
mm.handler.Event(msg_ev)
} else {
roomId := mm.reverseRoomId(post.ChannelId)
interested, roomId := mm.reverseRoomId(post.ChannelId)
if !interested {
return nil
}
if roomId == "" {
return fmt.Errorf("Invalid channel id")
}