Fix loading user names

This commit is contained in:
erroneousboat 2016-09-30 11:22:40 +02:00
parent ee9f65ff07
commit fcf7ca6f42
2 changed files with 45 additions and 35 deletions

15
TODO.md
View File

@ -5,19 +5,20 @@ Bugs:
latest message. Could be that items are added to List and not cleared
when switching channels
- [x] send message as user, now it will send it as a bot
- [x] alot of usernames 'unknown' should be a better way to uncover this
- [x] message creation in input.go and events.go should be made into function
CreateMessage
- [x] restarting the application will always add the latest sent message
through RTM in the selected channel
- [x] uncovering usernames takes too long, should find a better way
test without uncovering, if that is the problem
- [ ] GetMessages for a channel can result in `json: cannot unmarshal number
into Go value of type string` https://github.com/nlopes/slack/issues/92
- [ ] alot of usernames 'unknown' should be a better way to uncover this
- [ ] uncovering usernames takes too long, should find a better way
- [ ] docs at exported functions
- [ ] message creation in input.go and events.go should be made into function
CreateMessage
- [ ] restarting the application will always add the latest sent message
through RTM in the selected channel
Features:
- [ ] group channels, im channels
- [ ] scrolling in chat pane
- [ ] scrolling in channel pane
- [ ] private chats
- [x] channel name in chat pane

View File

@ -31,6 +31,11 @@ func NewSlackService(token string) *SlackService {
go svc.RTM.ManageConnection()
users, _ := svc.Client.GetUsers()
for _, user := range users {
svc.UserCache[user.ID] = user.Name
}
return svc
}
@ -106,21 +111,23 @@ func (s *SlackService) CreateMessage(message slack.Message) string {
// Name not in cache
if !ok {
user, err := s.Client.GetUserInfo(message.User)
if err == nil {
// Name found
name = user.Name
s.UserCache[message.User] = user.Name
} else {
if message.BotID != "" {
// Name not found, perhaps a bot, use Username
if message.Username != "" {
name, ok = s.UserCache[message.BotID]
if !ok {
// Not found in cache add it
name = message.Username
s.UserCache[message.BotID] = message.Username
}
name, ok = s.UserCache[message.BotID]
if !ok {
// Not found in cache, add it
name = message.Username
s.UserCache[message.BotID] = message.Username
}
} else {
// Not a bot, not in cache, get user info
user, err := s.Client.GetUserInfo(message.User)
if err != nil {
name = "unknown"
s.UserCache[message.User] = name
} else {
name = user.Name
s.UserCache[message.User] = user.Name
}
}
}
@ -156,21 +163,23 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent
// Name not in cache
if !ok {
user, err := s.Client.GetUserInfo(message.User)
if err == nil {
// Name found
name = user.Name
s.UserCache[message.User] = user.Name
} else {
if message.BotID != "" {
// Name not found, perhaps a bot, use Username
if message.Username != "" {
name, ok = s.UserCache[message.BotID]
if !ok {
// Not found in cache add it
name = message.Username
s.UserCache[message.BotID] = message.Username
}
name, ok = s.UserCache[message.BotID]
if !ok {
// Not found in cache, add it
name = message.Username
s.UserCache[message.BotID] = message.Username
}
} else {
// Not a bot, not in cache, get user info
user, err := s.Client.GetUserInfo(message.User)
if err != nil {
name = "unknown"
s.UserCache[message.User] = name
} else {
name = user.Name
s.UserCache[message.User] = user.Name
}
}
}