diff --git a/bot/interfaces.go b/bot/interfaces.go index 1050a6f..3fb53a3 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -81,6 +81,7 @@ type Connector interface { Serve() error Who(string) []string + Profile(string) (user.User, error) } // Plugin interface used for compatibility with the Plugin interface diff --git a/bot/user/users.go b/bot/user/users.go index 62298fb..5a6e5f8 100644 --- a/bot/user/users.go +++ b/bot/user/users.go @@ -9,6 +9,7 @@ type User struct { ID string Name string Admin bool + Icon string } func New(name string) User { diff --git a/connectors/irc/irc.go b/connectors/irc/irc.go index cead610..397125b 100644 --- a/connectors/irc/irc.go +++ b/connectors/irc/irc.go @@ -310,3 +310,7 @@ func (i *Irc) buildMessage(inMsg irc.Msg) msg.Message { func (i Irc) Who(channel string) []string { return []string{} } + +func (i Irc) Profile(string) (user.User, error) { + return user.User{}, fmt.Errorf("unimplemented") +} diff --git a/connectors/slack/slack.go b/connectors/slack/slack.go index bb1ad3d..656bfad 100644 --- a/connectors/slack/slack.go +++ b/connectors/slack/slack.go @@ -730,3 +730,7 @@ func (s *Slack) Who(id string) []string { log.Debug().Msgf("Returning %d handles", len(handles)) return handles } + +func (s *Slack) Profile(string) (user.User, error) { + return user.User{}, fmt.Errorf("unimplemented") +} diff --git a/connectors/slackapp/fix_text.go b/connectors/slackapp/fix_text.go index cdce8d8..7745c4e 100644 --- a/connectors/slackapp/fix_text.go +++ b/connectors/slackapp/fix_text.go @@ -2,6 +2,8 @@ package slackapp import ( "unicode/utf8" + + "github.com/nlopes/slack" ) // fixText strips all of the Slack-specific annotations from message text, @@ -13,7 +15,7 @@ import ( // • Strips < and > surrounding links. // // This was directly bogarted from velour/chat with emoji conversion removed. -func fixText(findUser func(id, defaultName string) string, text string) string { +func fixText(findUser func(id, defaultName string) (string, *slack.User), text string) string { var output []rune for len(text) > 0 { r, i := utf8.DecodeRuneInString(text) @@ -48,14 +50,14 @@ func fixText(findUser func(id, defaultName string) string, text string) string { return string(output) } -func fixTag(findUser func(string, string) string, tag []rune) ([]rune, bool) { +func fixTag(findUser func(string, string) (string, *slack.User), tag []rune) ([]rune, bool) { switch { case hasPrefix(tag, "@U"): if i := indexRune(tag, '|'); i >= 0 { return tag[i+1:], true } if findUser != nil { - u := findUser(string(tag[1:]), "unknown") + u, _ := findUser(string(tag[1:]), "unknown") return []rune(u), true } return tag, true diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index 74e494b..3ad2171 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -422,7 +422,7 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message { if m.BotID != "" { defaultName = m.Username } - name := s.getUser(m.User, defaultName) + name, u := s.getUser(m.User, defaultName) if m.Username != "" && name == "unknown" { name = m.Username } @@ -438,6 +438,7 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message { User: &user.User{ ID: m.User, Name: name, + Icon: u.Profile.Image192, }, Body: text, Raw: m, @@ -478,9 +479,9 @@ func stringForUser(user *slack.User) string { } // Get username for Slack user ID -func (s *SlackApp) getUser(id, defaultName string) string { +func (s *SlackApp) getUser(id, defaultName string) (string, *slack.User) { if u, ok := s.users[id]; ok { - return stringForUser(u) + return stringForUser(u), u } log.Debug(). @@ -489,10 +490,10 @@ func (s *SlackApp) getUser(id, defaultName string) string { u, err := s.api.GetUserInfo(id) if err != nil { - return defaultName + return defaultName, nil } s.users[id] = u - return stringForUser(u) + return stringForUser(u), u } // Who gets usernames out of a channel @@ -522,7 +523,7 @@ func (s *SlackApp) Who(id string) []string { if m == "" { log.Error().Msg("empty member") } - u := s.getUser(m, "unknown") + u, _ := s.getUser(m, "unknown") if u == "unknown" { log.Error(). Err(err). @@ -599,7 +600,7 @@ func (s *SlackApp) log(msg, channel string) error { } func (s *SlackApp) reactionReceived(event *slack.ReactionAddedEvent) error { - name := s.getUser(event.User, "unknown") + name, _ := s.getUser(event.User, "unknown") ch, err := s.getChannel(event.Item.Channel) if err != nil { @@ -619,7 +620,7 @@ func (s *SlackApp) reactionReceived(event *slack.ReactionAddedEvent) error { if m.BotID != "" { defaultName = m.Username } - u := s.getUser(m.User, defaultName) + u, _ := s.getUser(m.User, defaultName) body = fmt.Sprintf("%s: %s", u, m.Text) } default: @@ -634,3 +635,16 @@ func (s *SlackApp) reactionReceived(event *slack.ReactionAddedEvent) error { return s.log(msg, channel) } + +func (s *SlackApp) Profile(name string) (user.User, error) { + n, u := s.getUser(name, "unknown") + if n == "unknown" { + return user.User{}, fmt.Errorf("user %s is not known to us", name) + } + return user.User{ + ID: u.ID, + Name: n, + Admin: false, + Icon: u.Profile.Image192, + }, nil +} diff --git a/plugins/cli/cli.go b/plugins/cli/cli.go index 55d8875..b3a42ce 100644 --- a/plugins/cli/cli.go +++ b/plugins/cli/cli.go @@ -5,14 +5,16 @@ package cli import ( "encoding/json" "fmt" - "github.com/jmoiron/sqlx" - "github.com/rs/zerolog/log" - "github.com/velour/catbase/bot" - "github.com/velour/catbase/bot/msg" - "github.com/velour/catbase/bot/user" "html/template" "net/http" "time" + + "github.com/jmoiron/sqlx" + "github.com/rs/zerolog/log" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/bot/user" ) type CliPlugin struct { @@ -115,3 +117,6 @@ func (p *CliPlugin) Send(kind bot.Kind, args ...interface{}) (string, error) { func (p *CliPlugin) GetEmojiList() map[string]string { return nil } func (p *CliPlugin) Serve() error { return nil } func (p *CliPlugin) Who(s string) []string { return nil } +func (s *CliPlugin) Profile(name string) (user.User, error) { + return user.User{}, fmt.Errorf("unimplemented") +}