connectors: add profile to get user icon

This commit is contained in:
Chris Sexton 2020-05-08 10:31:27 -04:00 committed by Chris Sexton
parent c6559530e7
commit 0057f91ce6
7 changed files with 47 additions and 16 deletions

View File

@ -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

View File

@ -9,6 +9,7 @@ type User struct {
ID string
Name string
Admin bool
Icon string
}
func New(name string) User {

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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

View File

@ -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
}

View File

@ -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")
}