Reload view when new thread is created

This commit is contained in:
erroneousboat 2019-10-05 12:40:12 +02:00
parent 14dae825e3
commit 69db448dd3
2 changed files with 19 additions and 5 deletions

View File

@ -195,6 +195,17 @@ func (c *Chat) AddReply(parentID string, message Message) {
}
}
// IsNewThread check whether a message that is going to be added as
// a child to a parent message, is the first one or not
func (c *Chat) IsNewThread(parentID string) bool {
if parent, ok := c.Messages[parentID]; ok {
if len(parent.Messages) > 0 {
return true
}
}
return false
}
// ClearMessages clear the c.Messages
func (c *Chat) ClearMessages() {
c.Messages = make(map[string]Message)

View File

@ -138,7 +138,7 @@ func messageHandler(ctx *context.AppContext) {
var threadTimestamp string
if ev.ThreadTimestamp != "" {
threadTimestamp = ev.ThreadTimestamp
} else if ev.PreviousMessage.ThreadTimestamp != "" {
} else if ev.PreviousMessage != nil && ev.PreviousMessage.ThreadTimestamp != "" {
threadTimestamp = ev.PreviousMessage.ThreadTimestamp
} else {
threadTimestamp = ""
@ -147,15 +147,18 @@ func messageHandler(ctx *context.AppContext) {
// When timestamp isn't set this is a thread reply,
// handle as such
if threadTimestamp != "" {
ctx.View.Debug.Println(
fmt.Sprint("here"),
)
ctx.View.Chat.AddReply(threadTimestamp, msg)
} else if threadTimestamp == "" && ctx.Focus == context.ChatFocus {
ctx.View.Chat.AddMessage(msg)
}
termui.Render(ctx.View.Chat)
// we (mis)use actionChangeChannel, to rerender, the
// view when a new thread has been started
if ctx.View.Chat.IsNewThread(threadTimestamp) {
actionChangeChannel(ctx)
} else {
termui.Render(ctx.View.Chat)
}
// TODO: set Chat.Offset to 0, to automatically scroll
// down?