From 544397dee90330230b92f0d906825d7cd568c095 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Wed, 28 Sep 2016 22:10:04 +0200 Subject: [PATCH] Fix some bugs and adding some features --- TODO.md | 33 +++++++++++++++++++-------------- src/components/channels.go | 3 +-- src/components/chat.go | 10 ++++++++-- src/components/input.go | 4 ++-- src/config/config.go | 1 + src/handlers/event.go | 14 +++++++++++++- src/service/slack.go | 10 ++++++---- src/views/chat.go | 8 +++++++- 8 files changed, 57 insertions(+), 26 deletions(-) diff --git a/TODO.md b/TODO.md index 4af28d9..db58cc3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,17 +1,22 @@ -Sources: +Bugs: -Slack: -- https://api.slack.com/rtm -- https://github.com/evanyeung/terminal-slack -- https://github.com/nlopes/slack +- [ ] when switching channels sometimes messages are persisted in the new + channel, the Buffer() in Chat will probably not go further than the + latest message. Could be that items are added to List and not cleared + when switching channels +- [ ] GetMessages for a channel can result in `json: cannot unmarshal number + into Go value of type string` https://github.com/nlopes/slack/issues/92 +- [ ] send message as user, now it will send it as a bot +- [ ] 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 -UI -- https://github.com/jroimartin/gocui -- https://github.com/fatih/color -- https://github.com/nsf/termbox-go -- https://github.com/gizak/termui +Features: -Examples: -- https://github.com/nsf/godit -- https://github.com/moncho/dry -- https://github.com/mikepea/go-jira-ui +- [ ] scrolling in chat pane +- [ ] scrolling in channel pane +- [x] channel name in chat pane diff --git a/src/components/channels.go b/src/components/channels.go index 81a5a85..9a44b91 100644 --- a/src/components/channels.go +++ b/src/components/channels.go @@ -24,8 +24,7 @@ func CreateChannels(svc *service.SlackService, inputHeight int) *Channels { channels.List.BorderLabel = "Channels" channels.List.Height = termui.TermHeight() - inputHeight - // TODO: should be SetSelectedChannel - channels.SelectedChannel = 4 + channels.SelectedChannel = 0 channels.GetChannels(svc) diff --git a/src/components/chat.go b/src/components/chat.go index de59935..812e5da 100644 --- a/src/components/chat.go +++ b/src/components/chat.go @@ -12,14 +12,16 @@ type Chat struct { SelectedChannel string } -func CreateChat(svc *service.SlackService, inputHeight int) *Chat { +func CreateChat(svc *service.SlackService, inputHeight int, selectedChannel SlackChannel) *Chat { chat := &Chat{ List: termui.NewList(), } chat.List.Height = termui.TermHeight() - inputHeight chat.List.Overflow = "wrap" - // chat.GetMessages(svc) + + chat.GetMessages(svc, selectedChannel.ID) + chat.SetBorderLabel(selectedChannel.Name) return chat } @@ -145,3 +147,7 @@ func (c *Chat) ScrollUp() { } func (c *Chat) ScrollDown() {} + +func (c *Chat) SetBorderLabel(label string) { + c.List.BorderLabel = label +} diff --git a/src/components/input.go b/src/components/input.go index 420c48d..3e2a713 100644 --- a/src/components/input.go +++ b/src/components/input.go @@ -62,8 +62,8 @@ func (i *Input) SetY(y int) { i.Par.SetY(y) } -func (i *Input) SendMessage(svc *service.SlackService, channel string, message string) { - svc.SendMessage(channel, message) +func (i *Input) SendMessage(svc *service.SlackService, channel string, user string, message string) { + svc.SendMessage(channel, user, message) } // Insert will insert a given key at the place of the current CursorPosition diff --git a/src/config/config.go b/src/config/config.go index 39d7d26..f37837c 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -7,6 +7,7 @@ import ( type Config struct { SlackToken string `json:"slack_token"` + User string `json:"user"` } func NewConfig(filepath string) (*Config, error) { diff --git a/src/handlers/event.go b/src/handlers/event.go index 65682ca..60bce8b 100644 --- a/src/handlers/event.go +++ b/src/handlers/event.go @@ -93,7 +93,10 @@ func incomingMessageHandler(ctx *context.AppContext) { if err == nil { name = user.Name } else { - name = "unknown" + name = ev.Username + if name == "" { + name = "unknown" + } } // Parse the time we get from slack which is a Unix time float @@ -154,6 +157,7 @@ func actionSend(ctx *context.AppContext) { ctx.View.Input.SendMessage( ctx.Service, ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID, + ctx.Config.User, ctx.View.Input.Text(), ) ctx.View.Input.Clear() @@ -199,6 +203,10 @@ func actionMoveCursorUpChannels(ctx *context.AppContext) { ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID, ) + ctx.View.Chat.SetBorderLabel( + ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].Name, + ) + termui.Render(ctx.View.Channels) termui.Render(ctx.View.Chat) } @@ -211,6 +219,10 @@ func actionMoveCursorDownChannels(ctx *context.AppContext) { ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].ID, ) + ctx.View.Chat.SetBorderLabel( + ctx.View.Channels.SlackChannels[ctx.View.Channels.SelectedChannel].Name, + ) + termui.Render(ctx.View.Channels) termui.Render(ctx.View.Chat) } diff --git a/src/service/slack.go b/src/service/slack.go index dd23e19..d1bfdf2 100644 --- a/src/service/slack.go +++ b/src/service/slack.go @@ -52,9 +52,11 @@ func (s *SlackService) GetChannels() []Channel { return chans } -func (s *SlackService) SendMessage(channel, message string) { +func (s *SlackService) SendMessage(channel string, user string, message string) { // https://godoc.org/github.com/nlopes/slack#PostMessageParameters - postParams := slack.PostMessageParameters{} + postParams := slack.PostMessageParameters{ + Username: user, + } // https://godoc.org/github.com/nlopes/slack#Client.PostMessage s.Client.PostMessage(channel, message, postParams) @@ -90,8 +92,8 @@ func (s *SlackService) GetMessages(channel string, count int) []string { name = user.Name users[message.User] = user.Name } else { - name = "unknown" - users[message.User] = user.Name + name = message.Username + users[message.User] = name } } diff --git a/src/views/chat.go b/src/views/chat.go index 435ddc6..6585aac 100644 --- a/src/views/chat.go +++ b/src/views/chat.go @@ -16,8 +16,14 @@ type View struct { func CreateChatView(svc *service.SlackService) *View { input := components.CreateInput() + channels := components.CreateChannels(svc, input.Par.Height) - chat := components.CreateChat(svc, input.Par.Height) + + chat := components.CreateChat( + svc, input.Par.Height, + channels.SlackChannels[channels.SelectedChannel], + ) + mode := components.CreateMode() view := &View{