From 7c3b08d1de06507bb84a593acc0efbf8baf8d878 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 1 Sep 2018 15:57:26 +0200 Subject: [PATCH] Remove components dependency out of slack.go --- components/channels.go | 61 +++++-- context/context.go | 2 +- handlers/event.go | 79 ++++++--- service/slack.go | 377 +++++++---------------------------------- views/view.go | 8 +- 5 files changed, 173 insertions(+), 354 deletions(-) diff --git a/components/channels.go b/components/channels.go index b18e5d9..24410db 100644 --- a/components/channels.go +++ b/components/channels.go @@ -22,6 +22,7 @@ const ( ChannelTypeChannel = "channel" ChannelTypeGroup = "group" ChannelTypeIM = "im" + ChannelTypeMpIM = "mpim" ) type ChannelItem struct { @@ -93,6 +94,7 @@ func (c ChannelItem) GetChannelName() string { // Channels is the definition of a Channels component type Channels struct { + ChannelItems []ChannelItem List *termui.List SelectedChannel int // index of which channel is selected from the List Offset int // from what offset are channels rendered @@ -122,7 +124,7 @@ func CreateChannelsComponent(inputHeight int) *Channels { func (c *Channels) Buffer() termui.Buffer { buf := c.List.Buffer() - for i, item := range c.List.Items[c.Offset:] { + for i, item := range c.ChannelItems[c.Offset:] { y := c.List.InnerBounds().Min.Y + i @@ -134,10 +136,10 @@ func (c *Channels) Buffer() termui.Buffer { var cells []termui.Cell if y == c.CursorPosition { cells = termui.DefaultTxBuilder.Build( - item, c.List.ItemBgColor, c.List.ItemFgColor) + item.ToString(), c.List.ItemBgColor, c.List.ItemFgColor) } else { cells = termui.DefaultTxBuilder.Build( - item, c.List.ItemFgColor, c.List.ItemBgColor) + item.ToString(), c.List.ItemFgColor, c.List.ItemBgColor) } cells = termui.DTrimTxCls(cells, c.List.InnerWidth()) @@ -196,8 +198,33 @@ func (c *Channels) SetY(y int) { c.List.SetY(y) } -func (c *Channels) SetChannels(channels []string) { - c.List.Items = channels +func (c *Channels) SetChannels(channels []ChannelItem) { + c.ChannelItems = channels +} + +func (c *Channels) MarkAsRead(channelID int) { + c.ChannelItems[channelID].Notification = false +} + +func (c *Channels) MarkAsUnread(channelID string) { + index := c.FindChannel(channelID) + c.ChannelItems[index].Notification = true +} + +func (c *Channels) SetPresence(channelID string, presence string) { + index := c.FindChannel(channelID) + c.ChannelItems[index].Presence = presence +} + +func (c *Channels) FindChannel(channelID string) int { + var index int + for i, channel := range c.ChannelItems { + if channel.ID == channelID { + index = i + break + } + } + return index } // SetSelectedChannel sets the SelectedChannel given the index @@ -205,11 +232,6 @@ func (c *Channels) SetSelectedChannel(index int) { c.SelectedChannel = index } -// GetSelectedChannel returns the SelectedChannel -func (c *Channels) GetSelectedChannel() string { - return c.List.Items[c.SelectedChannel] -} - // MoveCursorUp will decrease the SelectedChannel by 1 func (c *Channels) MoveCursorUp() { if c.SelectedChannel > 0 { @@ -220,7 +242,7 @@ func (c *Channels) MoveCursorUp() { // MoveCursorDown will increase the SelectedChannel by 1 func (c *Channels) MoveCursorDown() { - if c.SelectedChannel < len(c.List.Items)-1 { + if c.SelectedChannel < len(c.ChannelItems)-1 { c.SetSelectedChannel(c.SelectedChannel + 1) c.ScrollDown() } @@ -235,9 +257,9 @@ func (c *Channels) MoveCursorTop() { // MoveCursorBottom will move the cursor to the bottom of the channels func (c *Channels) MoveCursorBottom() { - c.SetSelectedChannel(len(c.List.Items) - 1) + c.SetSelectedChannel(len(c.ChannelItems) - 1) - offset := len(c.List.Items) - (c.List.InnerBounds().Max.Y - 1) + offset := len(c.ChannelItems) - (c.List.InnerBounds().Max.Y - 1) if offset < 0 { c.Offset = 0 @@ -264,7 +286,7 @@ func (c *Channels) ScrollUp() { func (c *Channels) ScrollDown() { // Is the cursor at the bottom of the channel view? if c.CursorPosition == c.List.InnerBounds().Max.Y-1 { - if c.Offset < len(c.List.Items)-1 { + if c.Offset < len(c.ChannelItems)-1 { c.Offset++ } } else { @@ -278,11 +300,16 @@ func (c *Channels) ScrollDown() { func (c *Channels) Search(term string) { c.SearchMatches = make([]int, 0) - matches := fuzzy.Find(term, c.List.Items) + targets := make([]string, 0) + for _, c := range c.ChannelItems { + targets = append(targets, c.ToString()) + } + + matches := fuzzy.Find(term, targets) for _, m := range matches { - for i, item := range c.List.Items { - if m == item { + for i, item := range c.ChannelItems { + if m == item.Name { c.SearchMatches = append(c.SearchMatches, i) break } diff --git a/context/context.go b/context/context.go index 24cdda9..3a58be1 100644 --- a/context/context.go +++ b/context/context.go @@ -42,7 +42,7 @@ func CreateAppContext(flgConfig string, flgToken string, flgDebug bool) (*AppCon } // Loading screen - // views.Loading() + views.Loading() // Load config config, err := config.NewConfig(flgConfig) diff --git a/handlers/event.go b/handlers/event.go index 65fefcf..1f436c3 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -3,7 +3,9 @@ package handlers import ( "fmt" "os" + "regexp" "strconv" + "strings" "time" "github.com/0xAX/notificator" @@ -11,6 +13,7 @@ import ( "github.com/nlopes/slack" termbox "github.com/nsf/termbox-go" + "github.com/erroneousboat/slack-term/components" "github.com/erroneousboat/slack-term/config" "github.com/erroneousboat/slack-term/context" "github.com/erroneousboat/slack-term/views" @@ -115,7 +118,7 @@ func messageHandler(ctx *context.AppContext) { } // Add message to the selected channel - if ev.Channel == ctx.Service.Channels[ctx.View.Channels.SelectedChannel].ID { + if ev.Channel == ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID { // Reverse order of messages, mainly done // when attachments are added to message @@ -241,7 +244,7 @@ func actionSend(ctx *context.AppContext) { // Send message err := ctx.Service.SendMessage( - ctx.View.Channels.SelectedChannel, + ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID, message, ) if err != nil { @@ -251,8 +254,11 @@ func actionSend(ctx *context.AppContext) { } // Clear notification icon if there is any - ctx.Service.MarkAsRead(ctx.View.Channels.SelectedChannel) - ctx.View.Channels.SetChannels(ctx.Service.ChannelsToString()) + channelItem := ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel] + if channelItem.Notification { + ctx.Service.MarkAsRead(channelItem.ID) + ctx.View.Channels.MarkAsRead(ctx.View.Channels.SelectedChannel) + } termui.Render(ctx.View.Channels) } } @@ -304,7 +310,7 @@ func actionSearchMode(ctx *context.AppContext) { func actionGetMessages(ctx *context.AppContext) { msgs := ctx.Service.GetMessages( - ctx.Service.Channels[ctx.View.Channels.SelectedChannel], + ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID, ctx.View.Chat.GetMaxItems(), ) @@ -374,13 +380,15 @@ func actionSearchPrevChannels(ctx *context.AppContext) { } func actionChangeChannel(ctx *context.AppContext) { + ctx.View.Debug.Println(fmt.Sprintf("%d", ctx.View.Channels.SelectedChannel)) + // Clear messages from Chat pane ctx.View.Chat.ClearMessages() // Get messages of the SelectedChannel, and get the count of messages // that fit into the Chat component msgs := ctx.Service.GetMessages( - ctx.Service.GetSlackChannel(ctx.View.Channels.SelectedChannel), + ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].ID, ctx.View.Chat.GetMaxItems(), ) @@ -389,12 +397,15 @@ func actionChangeChannel(ctx *context.AppContext) { // Set channel name for the Chat pane ctx.View.Chat.SetBorderLabel( - ctx.Service.Channels[ctx.View.Channels.SelectedChannel].GetChannelName(), + ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel].GetChannelName(), ) // Clear notification icon if there is any - ctx.Service.MarkAsRead(ctx.View.Channels.SelectedChannel) - ctx.View.Channels.SetChannels(ctx.Service.ChannelsToString()) + channelItem := ctx.View.Channels.ChannelItems[ctx.View.Channels.SelectedChannel] + if channelItem.Notification { + ctx.Service.MarkAsRead(channelItem.ID) + ctx.View.Channels.MarkAsRead(ctx.View.Channels.SelectedChannel) + } termui.Render(ctx.View.Channels) termui.Render(ctx.View.Chat) @@ -403,8 +414,7 @@ func actionChangeChannel(ctx *context.AppContext) { // actionNewMessage will set the new message indicator for a channel, and // if configured will also display a desktop notification func actionNewMessage(ctx *context.AppContext, ev *slack.MessageEvent) { - ctx.Service.MarkAsUnread(ev.Channel) - ctx.View.Channels.SetChannels(ctx.Service.ChannelsToString()) + ctx.View.Channels.MarkAsUnread(ev.Channel) termui.Render(ctx.View.Channels) // Terminal bell @@ -412,7 +422,7 @@ func actionNewMessage(ctx *context.AppContext, ev *slack.MessageEvent) { // Desktop notification if ctx.Config.Notify == config.NotifyMention { - if ctx.Service.CheckNotifyMention(ev) { + if isMention(ctx, ev) { createNotifyMessage(ctx, ev) } } else if ctx.Config.Notify == config.NotifyAll { @@ -421,8 +431,7 @@ func actionNewMessage(ctx *context.AppContext, ev *slack.MessageEvent) { } func actionSetPresence(ctx *context.AppContext, channelID string, presence string) { - ctx.Service.SetPresenceChannelEvent(channelID, presence) - ctx.View.Channels.SetChannels(ctx.Service.ChannelsToString()) + ctx.View.Channels.SetPresence(channelID, presence) termui.Render(ctx.View.Channels) } @@ -491,6 +500,29 @@ func getKeyString(e termbox.Event) string { return ek } +// isMention check if the message event either contains a +// mention or is posted on an IM channel. +func isMention(ctx *context.AppContext, ev *slack.MessageEvent) bool { + channel := ctx.View.Channels.ChannelItems[ctx.View.Channels.FindChannel(ev.Channel)] + + if channel.Type == components.ChannelTypeIM { + return true + } + + // Mentions have the following format: + // <@U12345|erroneousboat> + // <@U12345> + r := regexp.MustCompile(`\<@(\w+\|*\w+)\>`) + matches := r.FindAllString(ev.Text, -1) + for _, match := range matches { + if strings.Contains(match, ctx.Service.CurrentUserID) { + return true + } + } + + return false +} + func createNotifyMessage(ctx *context.AppContext, ev *slack.MessageEvent) { go func() { if notifyTimer != nil { @@ -500,11 +532,20 @@ func createNotifyMessage(ctx *context.AppContext, ev *slack.MessageEvent) { notifyTimer = time.NewTimer(time.Second * 2) <-notifyTimer.C + var message string + channel := ctx.View.Channels.ChannelItems[ctx.View.Channels.FindChannel(ev.Channel)] + switch channel.Type { + case components.ChannelTypeChannel: + message = fmt.Sprintf("Message received on channel: %s", channel.Name) + case components.ChannelTypeGroup: + message = fmt.Sprintf("Message received in group: %s", channel.Name) + case components.ChannelTypeIM: + message = fmt.Sprintf("Message received from: %s", channel.Name) + default: + message = fmt.Sprintf("Message received from: %s", channel.Name) + } + // Only actually notify when time expires - ctx.Notify.Push( - "slack-term", - ctx.Service.CreateNotifyMessage(ev.Channel), "", - notificator.UR_NORMAL, - ) + ctx.Notify.Push("slack-term", message, "", notificator.UR_NORMAL) }() } diff --git a/service/slack.go b/service/slack.go index ae75bd0..d49030f 100644 --- a/service/slack.go +++ b/service/slack.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "log" - "os" "regexp" "strconv" "strings" @@ -27,8 +26,7 @@ type SlackService struct { Config *config.Config Client *slack.Client RTM *slack.RTM - SlackChannels []interface{} - Channels []components.ChannelItem + Conversations []slack.Channel UserCache map[string]string CurrentUserID string CurrentUsername string @@ -76,7 +74,7 @@ func NewSlackService(config *config.Config) (*SlackService, error) { return svc, nil } -func (s *SlackService) GetChannelsV2() []string { +func (s *SlackService) GetChannels() []components.ChannelItem { slackChans := make([]slack.Channel, 0) // Initial request @@ -118,39 +116,48 @@ func (s *SlackService) GetChannelsV2() []string { log.Fatal(err) // FIXME } - log.Printf("len(channels): %d", len(channels)) - log.Printf("nextCur: %s", nextCur) - log.Printf("cursor: %s", cursor) - log.Println("---") - slackChans = append(slackChans, channels...) nextCur = cursor } - // os.Exit(0) var chans []components.ChannelItem for _, chn := range slackChans { // Defaults + var chanType string + var presence string + + // TODO: shared channels? + if chn.IsChannel { if !chn.IsMember { continue - os.Exit(0) } + + chanType = components.ChannelTypeChannel } if chn.IsGroup { if !chn.IsMember { continue } + + chanType = components.ChannelTypeGroup } if chn.IsMpIM { + // TODO: does it have an IsMember? + // TODO: same api as im? + chanType = components.ChannelTypeMpIM } if chn.IsIM { // TODO: check if user is deleted. IsUsedDeleted is not present // in the `conversation` struct. + chanType = components.ChannelTypeIM + + // TODO: way to speed this up? see SetPresenceChannels + presence, _ = s.GetUserPresence(chn.ID) } var chanName string @@ -166,200 +173,41 @@ func (s *SlackService) GetChannelsV2() []string { ID: chn.ID, Name: chanName, Topic: chn.Topic.Value, - Type: components.ChannelTypeChannel, + Type: chanType, UserID: chn.User, - Presence: "", + Presence: presence, StylePrefix: s.Config.Theme.Channel.Prefix, StyleIcon: s.Config.Theme.Channel.Icon, StyleText: s.Config.Theme.Channel.Text, }, ) - s.SlackChannels = append(s.SlackChannels, chn) + s.Conversations = append(s.Conversations, chn) } - s.Channels = chans - - var channels []string - for _, chn := range s.Channels { - channels = append(channels, chn.ToString()) - } - - return channels -} - -// GetChannels will retrieve all available channels, groups, and im channels. -// Because the channels are of different types, we will append them to -// an []interface as well as to a []Channel which will give us easy access -// to the id and name of the Channel. -func (s *SlackService) GetChannels() []string { - var chans []components.ChannelItem - - var wg sync.WaitGroup - - // Channels - wg.Add(1) - var slackChans []slack.Channel - go func() { - var err error - slackChans, err = s.Client.GetChannels(true) - if err != nil { - chans = append(chans, components.ChannelItem{}) - } - wg.Done() - }() - - // Groups - wg.Add(1) - var slackGroups []slack.Group - go func() { - var err error - slackGroups, err = s.Client.GetGroups(true) - if err != nil { - chans = append(chans, components.ChannelItem{}) - } - wg.Done() - }() - - // IM - wg.Add(1) - var slackIM []slack.IM - go func() { - var err error - slackIM, err = s.Client.GetIMChannels() - if err != nil { - chans = append(chans, components.ChannelItem{}) - } - wg.Done() - }() - - wg.Wait() - - // Channels - for _, chn := range slackChans { - if chn.IsMember { - s.SlackChannels = append(s.SlackChannels, chn) - chans = append( - chans, components.ChannelItem{ - ID: chn.ID, - Name: chn.Name, - Topic: chn.Topic.Value, - Type: components.ChannelTypeChannel, - UserID: "", - StylePrefix: s.Config.Theme.Channel.Prefix, - StyleIcon: s.Config.Theme.Channel.Icon, - StyleText: s.Config.Theme.Channel.Text, - }, - ) - } - } - - // Groups - for _, grp := range slackGroups { - s.SlackChannels = append(s.SlackChannels, grp) - chans = append( - chans, components.ChannelItem{ - ID: grp.ID, - Name: grp.Name, - Topic: grp.Topic.Value, - Type: components.ChannelTypeGroup, - UserID: "", - StylePrefix: s.Config.Theme.Channel.Prefix, - StyleIcon: s.Config.Theme.Channel.Icon, - StyleText: s.Config.Theme.Channel.Text, - }, - ) - } - - // IM - for _, im := range slackIM { - - // Uncover name, when we can't uncover name for - // IM channel this is then probably a deleted - // user, because we won't add deleted users - // to the UserCache, so we skip it - name, ok := s.UserCache[im.User] - - if ok { - chans = append( - chans, - components.ChannelItem{ - ID: im.ID, - Name: name, - Topic: "", - Type: components.ChannelTypeIM, - UserID: im.User, - Presence: "", - StylePrefix: s.Config.Theme.Channel.Prefix, - StyleIcon: s.Config.Theme.Channel.Icon, - StyleText: s.Config.Theme.Channel.Text, - }, - ) - s.SlackChannels = append(s.SlackChannels, im) - } - } - - s.Channels = chans - - // We set presence of IM channels here because we need to separately - // issue an API call for every channel, this will speed up that process - s.SetPresenceChannels() - - var channels []string - for _, chn := range s.Channels { - channels = append(channels, chn.ToString()) - } - - return channels -} - -// ChannelsToString will relay the string representation for a channel -func (s *SlackService) ChannelsToString() []string { - var channels []string - for _, chn := range s.Channels { - channels = append(channels, chn.ToString()) - } - return channels + return chans } +// TODO: +// We set presence of IM channels here because we need to separately +// issue an API call for every channel, this will speed up that process // SetPresence will set presence for all IM channels func (s *SlackService) SetPresenceChannels() { var wg sync.WaitGroup - for i, channel := range s.SlackChannels { - - switch channel := channel.(type) { - case slack.IM: + for i, channel := range s.Conversations { + if channel.IsIM { wg.Add(1) go func(i int) { - presence, _ := s.GetUserPresence(channel.User) - s.Channels[i].Presence = presence + // presence, _ := s.GetUserPresence(channel.User) + // s.Channels[i].Presence = presence wg.Done() }(i) } - } wg.Wait() } -// SetPresenceChannelEvent will set the presence of a IM channel -func (s *SlackService) SetPresenceChannelEvent(userID string, presence string) { - // Get the correct Channel from svc.Channels - var index int - for i, channel := range s.Channels { - if userID == channel.UserID { - index = i - break - } - } - s.Channels[index].Presence = presence -} - -// GetSlackChannel returns the representation of a slack channel -func (s *SlackService) GetSlackChannel(selectedChannel int) interface{} { - return s.SlackChannels[selectedChannel] -} - // GetUserPresence will get the presence of a specific user func (s *SlackService) GetUserPresence(userID string) (string, error) { presence, err := s.Client.GetUserPresence(userID) @@ -370,82 +218,36 @@ func (s *SlackService) GetUserPresence(userID string) (string, error) { return presence.Presence, nil } -// SetChannelReadMark will set the read mark for a channel, group, and im -// channel based on the current time. -func (s *SlackService) SetChannelReadMark(channel interface{}) { - switch channel := channel.(type) { - case slack.Channel: - s.Client.SetChannelReadMark( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - case slack.Group: - s.Client.SetGroupReadMark( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - case slack.IM: - s.Client.MarkIMChannel( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - } -} - // MarkAsRead will set the channel as read -func (s *SlackService) MarkAsRead(channelID int) { - channel := s.Channels[channelID] +func (s *SlackService) MarkAsRead(channelID string) { - if channel.Notification { - s.Channels[channelID].Notification = false + // TODO: does this work with other channel types? See old one below + s.Client.SetChannelReadMark( + channelID, fmt.Sprintf("%f", + float64(time.Now().Unix())), + ) - switch channel.Type { - case ChannelTypeChannel: - s.Client.SetChannelReadMark( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - case ChannelTypeGroup: - s.Client.SetGroupReadMark( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - case ChannelTypeIM: - s.Client.MarkIMChannel( - channel.ID, fmt.Sprintf("%f", - float64(time.Now().Unix())), - ) - } - } -} - -// FindChannel will loop over s.Channels to find the index where the -// channelID equals the ID -func (s *SlackService) FindChannel(channelID string) int { - var index int - for i, channel := range s.Channels { - if channel.ID == channelID { - index = i - break - } - } - return index -} - -// MarkAsUnread will set the channel as unread -func (s *SlackService) MarkAsUnread(channelID string) { - index := s.FindChannel(channelID) - s.Channels[index].Notification = true -} - -// GetChannelName will return the name for a specific channelID -func (s *SlackService) GetChannelName(channelID string) string { - index := s.FindChannel(channelID) - return s.Channels[index].Name + // switch channel.Type { + // case ChannelTypeChannel: + // s.Client.SetChannelReadMark( + // channel.ID, fmt.Sprintf("%f", + // float64(time.Now().Unix())), + // ) + // case ChannelTypeGroup: + // s.Client.SetGroupReadMark( + // channel.ID, fmt.Sprintf("%f", + // float64(time.Now().Unix())), + // ) + // case ChannelTypeIM: + // s.Client.MarkIMChannel( + // channel.ID, fmt.Sprintf("%f", + // float64(time.Now().Unix())), + // ) + // } } // SendMessage will send a message to a particular channel -func (s *SlackService) SendMessage(channelID int, message string) error { +func (s *SlackService) SendMessage(channelID string, message string) error { // https://godoc.org/github.com/nlopes/slack#PostMessageParameters postParams := slack.PostMessageParameters{ @@ -455,7 +257,7 @@ func (s *SlackService) SendMessage(channelID int, message string) error { } // https://godoc.org/github.com/nlopes/slack#Client.PostMessage - _, _, err := s.Client.PostMessage(s.Channels[channelID].ID, message, postParams) + _, _, err := s.Client.PostMessage(channelID, message, postParams) if err != nil { return err } @@ -465,33 +267,18 @@ func (s *SlackService) SendMessage(channelID int, message string) error { // GetMessages will get messages for a channel, group or im channel delimited // by a count. -func (s *SlackService) GetMessages(channel interface{}, count int) []components.Message { - // https://api.slack.com/methods/channels.history - historyParams := slack.HistoryParameters{ - Count: count, +func (s *SlackService) GetMessages(channelID string, count int) []components.Message { + // TODO: check other parameters + // https://godoc.org/github.com/nlopes/slack#GetConversationHistoryParameters + historyParams := slack.GetConversationHistoryParameters{ + ChannelID: channelID, + Limit: count, Inclusive: false, - Unreads: false, } - // https://godoc.org/github.com/nlopes/slack#History - history := new(slack.History) - var err error - switch chnType := channel.(type) { - case slack.Channel: - history, err = s.Client.GetChannelHistory(chnType.ID, historyParams) - if err != nil { - log.Fatal(err) // FIXME - } - case slack.Group: - history, err = s.Client.GetGroupHistory(chnType.ID, historyParams) - if err != nil { - log.Fatal(err) // FIXME - } - case slack.IM: - history, err = s.Client.GetIMHistory(chnType.ID, historyParams) - if err != nil { - log.Fatal(err) // FIXME - } + history, err := s.Client.GetConversationHistory(&historyParams) + if err != nil { + log.Fatal(err) // FIXME } // Construct the messages @@ -653,44 +440,6 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent return msgs, nil } -// CheckNotifyMention check if the message event is either contains a -// mention or is posted on an IM channel -func (s *SlackService) CheckNotifyMention(ev *slack.MessageEvent) bool { - channel := s.Channels[s.FindChannel(ev.Channel)] - switch channel.Type { - case ChannelTypeIM: - return true - } - - // Mentions have the following format: - // <@U12345|erroneousboat> - // <@U12345> - r := regexp.MustCompile(`\<@(\w+\|*\w+)\>`) - matches := r.FindAllString(ev.Text, -1) - for _, match := range matches { - if strings.Contains(match, s.CurrentUserID) { - return true - } - } - - return false -} - -func (s *SlackService) CreateNotifyMessage(channelID string) string { - channel := s.Channels[s.FindChannel(channelID)] - - switch channel.Type { - case ChannelTypeChannel: - return fmt.Sprintf("Message received on channel: %s", channel.Name) - case ChannelTypeGroup: - return fmt.Sprintf("Message received in group: %s", channel.Name) - case ChannelTypeIM: - return fmt.Sprintf("Message received from: %s", channel.Name) - } - - return "" -} - // parseMessage will parse a message string and find and replace: // - emoji's // - mentions diff --git a/views/view.go b/views/view.go index 11410b2..bb9bcc4 100644 --- a/views/view.go +++ b/views/view.go @@ -25,7 +25,7 @@ func CreateView(config *config.Config, svc *service.SlackService) *View { channels := components.CreateChannelsComponent(input.Par.Height) // Channels: fill the component - slackChans := svc.GetChannelsV2() + slackChans := svc.GetChannels() channels.SetChannels(slackChans) // Chat: create the component @@ -33,12 +33,14 @@ func CreateView(config *config.Config, svc *service.SlackService) *View { // Chat: fill the component msgs := svc.GetMessages( - svc.GetSlackChannel(channels.SelectedChannel), + channels.ChannelItems[channels.SelectedChannel].ID, chat.GetMaxItems(), ) chat.SetMessages(msgs) - chat.SetBorderLabel(svc.Channels[channels.SelectedChannel].GetChannelName()) + chat.SetBorderLabel( + channels.ChannelItems[channels.SelectedChannel].GetChannelName(), + ) // Debug: create the component debug := components.CreateDebugComponent(input.Par.Height)