Add initial support for rendering replies

This commit is contained in:
Tulir Asokan 2019-06-17 13:46:02 +03:00
parent fe439f076a
commit b76c301145
8 changed files with 80 additions and 33 deletions

View File

@ -60,9 +60,10 @@ type RoomView interface {
SetTyping(users []string) SetTyping(users []string)
UpdateUserList() UpdateUserList()
ParseEvent(evt *event.Event) Message AddEvent(evt *event.Event) Message
AddRedaction(evt *event.Event)
AddEdit(evt *event.Event)
GetEvent(eventID string) Message GetEvent(eventID string) Message
AddMessage(message Message)
AddServiceMessage(message string) AddServiceMessage(message string)
} }

View File

@ -334,13 +334,39 @@ func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) {
return return
} }
// TODO make this less hacky? roomView.AddRedaction(redactedEvt)
message := roomView.ParseEvent(redactedEvt) if c.syncer.FirstSyncDone {
if message != nil { c.ui.Render()
roomView.AddMessage(message) }
if c.syncer.FirstSyncDone { }
c.ui.Render()
} func (c *Container) HandleEdit(room *rooms.Room, editsID string, editEvent *event.Event) {
var origEvt *event.Event
err := c.history.Update(room, editsID, func(evt *event.Event) error {
evt.Gomuks.Edits = append(evt.Gomuks.Edits, editEvent)
origEvt = evt
return nil
})
if err != nil {
debug.Print("Failed to store edit in history db:", err)
}
if !c.config.AuthCache.InitialSyncDone {
return
}
roomView := c.ui.MainView().GetRoom(editEvent.RoomID)
if roomView == nil {
debug.Printf("Failed to handle edit event %v: No room view found.", editEvent)
return
}
if !room.Loaded() {
return
}
roomView.AddEdit(origEvt)
if c.syncer.FirstSyncDone {
c.ui.Render()
} }
} }
@ -354,6 +380,11 @@ func (c *Container) HandleMessage(source EventSource, mxEvent *mautrix.Event) {
return return
} }
if editID := mxEvent.Content.GetRelatesTo().GetReplaceID(); len(editID) > 0 {
c.HandleEdit(room, editID, event.Wrap(mxEvent))
return
}
events, err := c.history.Append(room, []*mautrix.Event{mxEvent}) events, err := c.history.Append(room, []*mautrix.Event{mxEvent})
if err != nil { if err != nil {
debug.Printf("Failed to add event %s to history: %v", mxEvent.ID, err) debug.Printf("Failed to add event %s to history: %v", mxEvent.ID, err)
@ -384,10 +415,8 @@ func (c *Container) HandleMessage(source EventSource, mxEvent *mautrix.Event) {
} }
} }
// TODO switch to roomView.AddEvent message := roomView.AddEvent(evt)
message := roomView.ParseEvent(evt)
if message != nil { if message != nil {
roomView.AddMessage(message)
roomView.MxRoom().LastReceivedMessage = message.Time() roomView.MxRoom().LastReceivedMessage = message.Time()
if c.syncer.FirstSyncDone { if c.syncer.FirstSyncDone {
pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt.Event).Should() pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt.Event).Should()

View File

@ -143,8 +143,6 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
view.replaceMessage(oldMsg, message) view.replaceMessage(oldMsg, message)
view.deleteMessageID(message.TxnID) view.deleteMessageID(message.TxnID)
direction = IgnoreMessage direction = IgnoreMessage
} else if oldMsg = view.getMessageByID(message.Relation.GetReplaceID()); oldMsg != nil {
direction = IgnoreMessage
} }
view.updateWidestSender(message.Sender()) view.updateWidestSender(message.Sender())
@ -608,6 +606,10 @@ func (view *MessageView) Draw(screen mauview.Screen) {
usernameX, line, view.widestSender(), usernameX, line, view.widestSender(),
msg.SenderColor()) msg.SenderColor())
//} //}
if msg.Edited {
// TODO add better indicator for edits
screen.SetCell(usernameX + view.widestSender(), line, tcell.StyleDefault.Foreground(tcell.ColorDarkRed), '*')
}
prevMsg = msg prevMsg = msg
} }

View File

@ -54,6 +54,7 @@ type UIMessage struct {
State event.OutgoingState State event.OutgoingState
IsHighlight bool IsHighlight bool
IsService bool IsService bool
Edited bool
Source json.RawMessage Source json.RawMessage
ReplyTo *UIMessage ReplyTo *UIMessage
Renderer MessageRenderer Renderer MessageRenderer
@ -80,6 +81,7 @@ func newUIMessage(evt *event.Event, displayname string, renderer MessageRenderer
State: evt.Gomuks.OutgoingState, State: evt.Gomuks.OutgoingState,
IsHighlight: false, IsHighlight: false,
IsService: false, IsService: false,
Edited: len(evt.Gomuks.Edits) > 0,
Source: evt.Content.VeryRaw, Source: evt.Content.VeryRaw,
Renderer: renderer, Renderer: renderer,
} }
@ -231,7 +233,6 @@ func (msg *UIMessage) SetID(id string) {
} }
func (msg *UIMessage) SetIsHighlight(isHighlight bool) { func (msg *UIMessage) SetIsHighlight(isHighlight bool) {
// TODO Textmessage cache needs to be cleared
msg.IsHighlight = isHighlight msg.IsHighlight = isHighlight
} }

View File

@ -127,8 +127,8 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event
if len(evt.Content.GetReplyTo()) > 0 { if len(evt.Content.GetReplyTo()) > 0 {
evt.Content.RemoveReplyFallback() evt.Content.RemoveReplyFallback()
} }
if evt.Content.GetRelatesTo().Type == mautrix.RelReplace && evt.Content.NewContent != nil { if len(evt.Gomuks.Edits) > 0 {
evt.Content = *evt.Content.NewContent evt.Content = *evt.Gomuks.Edits[len(evt.Gomuks.Edits)-1].Content.NewContent
} }
switch evt.Content.MsgType { switch evt.Content.MsgType {
case "m.text", "m.notice", "m.emote": case "m.text", "m.notice", "m.emote":

View File

@ -29,9 +29,10 @@ import (
) )
type TextMessage struct { type TextMessage struct {
cache tstring.TString cache tstring.TString
buffer []tstring.TString buffer []tstring.TString
Text string isHighlight bool
Text string
} }
// NewTextMessage creates a new UITextMessage object with the provided values and the default state. // NewTextMessage creates a new UITextMessage object with the provided values and the default state.
@ -85,6 +86,9 @@ func (msg *TextMessage) String() string {
} }
func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) { func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
if uiMsg.IsHighlight != msg.isHighlight {
msg.cache = nil
}
msg.buffer = calculateBufferWithText(prefs, msg.getCache(uiMsg), width, uiMsg) msg.buffer = calculateBufferWithText(prefs, msg.getCache(uiMsg), width, uiMsg)
} }

View File

@ -412,7 +412,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
} }
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text) evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text)
msg := view.parseEvent(evt) msg := view.parseEvent(evt)
view.AddMessage(msg) view.content.AddMessage(msg, AppendMessage)
eventID, err := view.parent.matrix.SendEvent(evt) eventID, err := view.parent.matrix.SendEvent(evt)
if err != nil { if err != nil {
msg.State = event.StateSendFail msg.State = event.StateSendFail
@ -462,20 +462,32 @@ func (view *RoomView) AddServiceMessage(text string) {
view.content.AddMessage(messages.NewServiceMessage(text), AppendMessage) view.content.AddMessage(messages.NewServiceMessage(text), AppendMessage)
} }
func (view *RoomView) AddMessage(message ifc.Message) {
view.content.AddMessage(message, AppendMessage)
}
func (view *RoomView) parseEvent(evt *event.Event) *messages.UIMessage { func (view *RoomView) parseEvent(evt *event.Event) *messages.UIMessage {
return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt) return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt)
} }
func (view *RoomView) ParseEvent(evt *event.Event) ifc.Message { func (view *RoomView) AddHistoryEvent(evt *event.Event) {
msg := view.parseEvent(evt) if msg := view.parseEvent(evt); msg != nil {
if msg == nil { view.content.AddMessage(msg, PrependMessage)
return nil }
}
func (view *RoomView) AddEvent(evt *event.Event) ifc.Message {
if msg := view.parseEvent(evt); msg != nil {
view.content.AddMessage(msg, AppendMessage)
return msg
}
return nil
}
func (view *RoomView) AddRedaction(redactedEvt *event.Event) {
view.AddEvent(redactedEvt)
}
func (view *RoomView) AddEdit(evt *event.Event) {
if msg := view.parseEvent(evt); msg != nil {
view.content.AddMessage(msg, IgnoreMessage)
} }
return msg
} }
func (view *RoomView) GetEvent(eventID string) ifc.Message { func (view *RoomView) GetEvent(eventID string) ifc.Message {

View File

@ -453,9 +453,7 @@ func (view *MainView) LoadHistory(roomID string) {
return return
} }
for _, evt := range history { for _, evt := range history {
if message := roomView.ParseEvent(evt); message != nil { roomView.AddHistoryEvent(evt)
msgView.AddMessage(message, PrependMessage)
}
} }
view.parent.Render() view.parent.Render()
} }