Fix possible panics. (from @recht matterircd fork)

This commit is contained in:
Wim 2017-07-01 22:43:53 +02:00
parent d261949db2
commit 8064744d3a

View File

@ -263,7 +263,7 @@ func (m *MMClient) WsReceiver() {
var event model.WebSocketEvent var event model.WebSocketEvent
if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() { if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() {
m.log.Debugf("WsReceiver: %#v", event) m.log.Debugf("WsReceiver event: %#v", event)
msg := &Message{Raw: &event, Team: m.Credentials.Team} msg := &Message{Raw: &event, Team: m.Credentials.Team}
m.parseMessage(msg) m.parseMessage(msg)
m.MessageChan <- msg m.MessageChan <- msg
@ -272,7 +272,7 @@ func (m *MMClient) WsReceiver() {
var response model.WebSocketResponse var response model.WebSocketResponse
if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() { if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() {
m.log.Debugf("WsReceiver: %#v", response) m.log.Debugf("WsReceiver response: %#v", response)
m.parseResponse(response) m.parseResponse(response)
continue continue
} }
@ -305,7 +305,8 @@ func (m *MMClient) parseActionPost(rmsg *Message) {
data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string))) data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
// we don't have the user, refresh the userlist // we don't have the user, refresh the userlist
if m.GetUser(data.UserId) == nil { if m.GetUser(data.UserId) == nil {
m.UpdateUsers() m.log.Infof("User %s is not known, ignoring message %s", data)
return
} }
rmsg.Username = m.GetUserName(data.UserId) rmsg.Username = m.GetUserName(data.UserId)
rmsg.Channel = m.GetChannelName(data.ChannelId) rmsg.Channel = m.GetChannelName(data.ChannelId)
@ -366,12 +367,24 @@ func (m *MMClient) GetChannelName(channelId string) string {
m.RLock() m.RLock()
defer m.RUnlock() defer m.RUnlock()
for _, t := range m.OtherTeams { for _, t := range m.OtherTeams {
for _, channel := range append(*t.Channels, *t.MoreChannels...) { if t == nil {
continue
}
if t.Channels != nil {
for _, channel := range *t.Channels {
if channel.Id == channelId { if channel.Id == channelId {
return channel.Name return channel.Name
} }
} }
} }
if t.MoreChannels != nil {
for _, channel := range *t.MoreChannels {
if channel.Id == channelId {
return channel.Name
}
}
}
}
return "" return ""
} }
@ -616,7 +629,9 @@ func (m *MMClient) GetTeamFromChannel(channelId string) string {
var channels []*model.Channel var channels []*model.Channel
for _, t := range m.OtherTeams { for _, t := range m.OtherTeams {
channels = append(channels, *t.Channels...) channels = append(channels, *t.Channels...)
if t.MoreChannels != nil {
channels = append(channels, *t.MoreChannels...) channels = append(channels, *t.MoreChannels...)
}
for _, c := range channels { for _, c := range channels {
if c.Id == channelId { if c.Id == channelId {
return t.Id return t.Id
@ -648,8 +663,8 @@ func (m *MMClient) GetUsers() map[string]*model.User {
} }
func (m *MMClient) GetUser(userId string) *model.User { func (m *MMClient) GetUser(userId string) *model.User {
m.RLock() m.Lock()
defer m.RUnlock() defer m.Unlock()
return m.Users[userId] return m.Users[userId]
} }