Update message event

This commit is contained in:
erroneousboat 2018-12-25 15:09:03 +01:00
parent d2b1037568
commit d7a3dc7a8c
2 changed files with 8 additions and 73 deletions

View File

@ -113,7 +113,7 @@ func messageHandler(ctx *context.AppContext) {
case *slack.MessageEvent:
// Construct message
msg, err := ctx.Service.CreateMessageFromMessageEvent(ev)
msg, err := ctx.Service.CreateMessageFromMessageEvent(ev, ev.Channel)
if err != nil {
continue
}
@ -121,14 +121,7 @@ func messageHandler(ctx *context.AppContext) {
// Add message to the selected channel
if ev.Channel == ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID {
// Reverse order of messages, mainly done
// when attachments are added to message
for i := len(msg) - 1; i >= 0; i-- {
ctx.View.Chat.AddMessage(
msg[i],
)
}
ctx.View.Chat.AddMessage(msg)
termui.Render(ctx.View.Chat)
// TODO: set Chat.Offset to 0, to automatically scroll

View File

@ -448,78 +448,20 @@ func (s *SlackService) CreateMessageFromReplies(message slack.Message, channelID
return replies
}
func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent) ([]components.Message, error) {
var msgs []components.Message
var name string
func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent, channelID string) (components.Message, error) {
msg := slack.Message{Msg: message.Msg}
switch message.SubType {
case "message_changed":
// Append (edited) when an edited message is received
message = &slack.MessageEvent{Msg: *message.SubMessage}
message.Text = fmt.Sprintf("%s (edited)", message.Text)
msg = slack.Message{Msg: *message.SubMessage}
msg.Text = fmt.Sprintf("%s (edited)", msg.Text)
case "message_replied":
// Ignore reply events
return nil, errors.New("ignoring reply events")
return components.Message{}, errors.New("ignoring reply events")
}
// Get username from cache
name, ok := s.UserCache[message.User]
// Name not in cache
if !ok {
if message.BotID != "" {
// Name not found, perhaps a bot, use 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
}
}
}
if name == "" {
name = "unknown"
}
// When there are attachments append them
if len(message.Attachments) > 0 {
msgs = append(msgs, s.CreateMessageFromAttachments(message.Attachments)...)
}
// Parse time
floatTime, err := strconv.ParseFloat(message.Timestamp, 64)
if err != nil {
floatTime = 0.0
}
intTime := int64(floatTime)
// Format message
msg := components.Message{
ID: message.Timestamp,
Time: time.Unix(intTime, 0),
Name: name,
Content: parseMessage(s, message.Text),
StyleTime: s.Config.Theme.Message.Time,
StyleName: s.Config.Theme.Message.Name,
StyleText: s.Config.Theme.Message.Text,
FormatTime: s.Config.Theme.Message.TimeFormat,
}
msgs = append(msgs, msg)
return msgs, nil
return s.CreateMessage(msg, channelID), nil
}
// parseMessage will parse a message string and find and replace: