diff options
author | Tulir Asokan <tulir@maunium.net> | 2019-04-10 01:04:39 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2019-04-10 01:04:39 +0300 |
commit | bbde1219479bb257c7bc1f9546bd67e8a151198c (patch) | |
tree | 01ae23d19ed4ff44fa218633ae94511778c17ffa /ui | |
parent | dbee49476d47bb391dc6af0b774f9fd101b0c917 (diff) |
It compiles. Ship it!
Diffstat (limited to 'ui')
-rw-r--r-- | ui/commands.go | 10 | ||||
-rw-r--r-- | ui/message-view.go | 22 | ||||
-rw-r--r-- | ui/messages/base.go | 15 | ||||
-rw-r--r-- | ui/messages/message.go | 2 | ||||
-rw-r--r-- | ui/messages/parser.go | 1 | ||||
-rw-r--r-- | ui/messages/textmessage.go | 18 | ||||
-rw-r--r-- | ui/room-view.go | 72 | ||||
-rw-r--r-- | ui/view-main.go | 65 |
8 files changed, 90 insertions, 115 deletions
diff --git a/ui/commands.go b/ui/commands.go index a652518..3a24836 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -27,16 +27,13 @@ import ( "github.com/lucasb-eyer/go-colorful" - "maunium.net/go/mautrix" - "maunium.net/go/mautrix/format" - "maunium.net/go/gomuks/debug" + "maunium.net/go/mautrix" ) func cmdMe(cmd *Command) { text := strings.Join(cmd.Args, " ") - tempMessage := cmd.Room.NewTempMessage("m.emote", text) - go cmd.MainView.sendTempMessage(cmd.Room, tempMessage, text) + go cmd.Room.SendMessage(mautrix.MsgEmote, text) cmd.UI.Render() } @@ -97,8 +94,7 @@ func cmdRainbow(cmd *Command) { color := rainbow.GetInterpolatedColorFor(float64(i) / float64(len(text))).Hex() fmt.Fprintf(&html, "<font color=\"%s\">%c</font>", color, char) } - tempMessage := cmd.Room.NewTempMessage("m.text", format.HTMLToText(html.String())) - go cmd.MainView.sendTempMessage(cmd.Room, tempMessage, html.String()) + go cmd.Room.SendMessage("m.text", html.String()) cmd.UI.Render() } diff --git a/ui/message-view.go b/ui/message-view.go index 5eedabc..87889fb 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -89,7 +89,7 @@ func (view *MessageView) updateWidestSender(sender string) { } } -func (view *MessageView) UpdateMessageID(ifcMessage ifc.Message, newID string) { +/*func (view *MessageView) UpdateMessageID(ifcMessage ifc.Message, newID string) { message, ok := ifcMessage.(messages.UIMessage) if !ok { debug.Print("[Warning] Passed non-UIMessage ifc.Message object to UpdateMessageID().") @@ -99,9 +99,17 @@ func (view *MessageView) UpdateMessageID(ifcMessage ifc.Message, newID string) { delete(view.messageIDs, message.ID()) message.SetID(newID) view.messageIDs[message.ID()] = message -} +}*/ + +type MessageDirection int + +const ( + AppendMessage MessageDirection = iota + PrependMessage + IgnoreMessage +) -func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.MessageDirection) { +func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDirection) { if ifcMessage == nil { return } @@ -117,11 +125,11 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.Messag var messageExists bool if oldMsg, messageExists = view.messageIDs[message.ID()]; messageExists { view.replaceMessage(oldMsg, message) - direction = ifc.IgnoreMessage + direction = IgnoreMessage } else if oldMsg, messageExists = view.messageIDs[message.TxnID()]; messageExists { view.replaceMessage(oldMsg, message) delete(view.messageIDs, message.TxnID()) - direction = ifc.IgnoreMessage + direction = IgnoreMessage } view.updateWidestSender(message.Sender()) @@ -133,13 +141,13 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction ifc.Messag } message.CalculateBuffer(view.config.Preferences, width) - if direction == ifc.AppendMessage { + if direction == AppendMessage { if view.ScrollOffset > 0 { view.ScrollOffset += message.Height() } view.messages = append(view.messages, message) view.appendBuffer(message) - } else if direction == ifc.PrependMessage { + } else if direction == PrependMessage { view.messages = append([]messages.UIMessage{message}, view.messages...) } else if oldMsg != nil { view.replaceBuffer(oldMsg, message) diff --git a/ui/messages/base.go b/ui/messages/base.go index b8b1694..d9db866 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -192,6 +192,9 @@ func (msg *BaseMessage) FormatDate() string { } func (msg *BaseMessage) ID() string { + if len(msg.MsgID) == 0 { + return msg.MsgTxnID + } return msg.MsgID } @@ -199,6 +202,10 @@ func (msg *BaseMessage) SetID(id string) { msg.MsgID = id } +func (msg *BaseMessage) TxnID() string { + return msg.MsgTxnID +} + func (msg *BaseMessage) Type() mautrix.MessageType { return msg.MsgType } @@ -219,14 +226,6 @@ func (msg *BaseMessage) SetIsHighlight(isHighlight bool) { msg.MsgIsHighlight = isHighlight } -func (msg *BaseMessage) IsService() bool { - return msg.MsgIsService -} - -func (msg *BaseMessage) SetIsService(isService bool) { - msg.MsgIsService = isService -} - func (msg *BaseMessage) Source() json.RawMessage { return msg.MsgSource } diff --git a/ui/messages/message.go b/ui/messages/message.go index 634bd11..bd78e5d 100644 --- a/ui/messages/message.go +++ b/ui/messages/message.go @@ -19,6 +19,7 @@ package messages import ( "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/interface" + "maunium.net/go/mautrix" "maunium.net/go/mauview" "maunium.net/go/tcell" ) @@ -27,6 +28,7 @@ import ( type UIMessage interface { ifc.Message + Type() mautrix.MessageType Sender() string SenderColor() tcell.Color TextColor() tcell.Color diff --git a/ui/messages/parser.go b/ui/messages/parser.go index f45eea2..7b76a69 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -29,7 +29,6 @@ import ( "maunium.net/go/gomuks/ui/messages/html" "maunium.net/go/gomuks/ui/messages/tstring" "maunium.net/go/gomuks/ui/widget" - htmlp "maunium.net/go/gomuks/ui/messages/html" ) func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) UIMessage { diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go index f7c54ca..588dee4 100644 --- a/ui/messages/textmessage.go +++ b/ui/messages/textmessage.go @@ -18,6 +18,7 @@ package messages import ( "fmt" + "time" "maunium.net/go/mautrix" @@ -39,6 +40,18 @@ func NewTextMessage(event *mautrix.Event, displayname string, text string) UIMes } } +func NewServiceMessage(text string) UIMessage { + return &TextMessage{ + BaseMessage: BaseMessage{ + MsgSenderID: "*", + MsgSender: "*", + MsgTimestamp: time.Now(), + MsgIsService: true, + }, + MsgText: text, + } +} + func (msg *TextMessage) getCache() tstring.TString { if msg.cache == nil { switch msg.MsgType { @@ -57,11 +70,6 @@ func (msg *TextMessage) SetIsHighlight(isHighlight bool) { msg.cache = nil } -func (msg *TextMessage) SetIsService(isService bool) { - msg.BaseMessage.SetIsService(isService) - msg.cache = nil -} - func (msg *TextMessage) NotificationContent() string { return msg.MsgText } diff --git a/ui/room-view.go b/ui/room-view.go index 2711338..e37760e 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -20,7 +20,6 @@ import ( "fmt" "path/filepath" "sort" - "strconv" "strings" "time" @@ -251,7 +250,7 @@ func (view *RoomView) OnKeyEvent(event mauview.KeyEvent) bool { msgView.AddScrollOffset(-msgView.Height() / 2) return true case tcell.KeyEnter: - if event.Modifiers() & tcell.ModShift == 0 && event.Modifiers() & tcell.ModCtrl == 0 && view.inputSubmitFunc != nil { + if event.Modifiers()&tcell.ModShift == 0 && event.Modifiers()&tcell.ModCtrl == 0 && view.inputSubmitFunc != nil { view.inputSubmitFunc(view, view.input.GetText()) return true } @@ -379,6 +378,44 @@ func (view *RoomView) InputTabComplete(text string, cursorOffset int) { view.SetCompletions(strCompletions) } +func (view *RoomView) InputSubmit(text string) { + if len(text) == 0 { + return + } else if cmd := view.parent.cmdProcessor.ParseCommand(view, text); cmd != nil { + go view.parent.cmdProcessor.HandleCommand(cmd) + } else { + go view.SendMessage(mautrix.MsgText, text) + } + view.SetInputText("") +} + +func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) { + defer debug.Recover() + debug.Print("Sending message", msgtype, text, "to", view.Room.ID) + if !view.config.Preferences.DisableEmojis { + text = emoji.Sprint(text) + } + evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text) + msg := view.ParseEvent(evt) + view.AddMessage(msg) + eventID, err := view.parent.matrix.SendEvent(evt) + if err != nil { + msg.SetState(mautrix.EventStateSendFail) + // Show shorter version if available + if httpErr, ok := err.(mautrix.HTTPError); ok { + err = httpErr + if respErr := httpErr.RespError; respErr != nil { + err = respErr + } + } + view.AddServiceMessage(fmt.Sprintf("Failed to send message: %v", err)) + view.parent.parent.Render() + } else { + debug.Print("Event ID received:", eventID) + //view.MessageView().UpdateMessageID(msg, eventID) + } +} + func (view *RoomView) MessageView() *MessageView { return view.content } @@ -406,37 +443,12 @@ func (view *RoomView) UpdateUserList() { } } -func (view *RoomView) newUIMessage(id, sender string, msgtype mautrix.MessageType, text string, timestamp time.Time) messages.UIMessage { - member := view.Room.GetMember(sender) - displayname := sender - if member != nil { - displayname = member.Displayname - } - msg := messages.NewTextMessage(id, sender, displayname, msgtype, text, timestamp) - return msg -} - -func (view *RoomView) NewTempMessage(msgtype mautrix.MessageType, text string) ifc.Message { - now := time.Now() - id := strconv.FormatInt(now.UnixNano(), 10) - sender := "" - if ownerMember := view.Room.GetMember(view.Room.GetSessionOwner()); ownerMember != nil { - sender = ownerMember.Displayname - } - message := view.newUIMessage(id, sender, msgtype, text, now) - message.SetState(ifc.MessageStateSending) - view.AddMessage(message, ifc.AppendMessage) - return message -} - func (view *RoomView) AddServiceMessage(text string) { - message := view.newUIMessage(view.parent.matrix.Client().TxnID(), "*", "gomuks.service", text, time.Now()) - message.SetIsService(true) - view.AddMessage(message, ifc.AppendMessage) + view.content.AddMessage(messages.NewServiceMessage(text), AppendMessage) } -func (view *RoomView) AddMessage(message ifc.Message, direction ifc.MessageDirection) { - view.content.AddMessage(message, direction) +func (view *RoomView) AddMessage(message ifc.Message) { + view.content.AddMessage(message, AppendMessage) } func (view *RoomView) ParseEvent(evt *mautrix.Event) ifc.Message { diff --git a/ui/view-main.go b/ui/view-main.go index d9d9247..5b6efa8 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -23,9 +23,6 @@ import ( "time" "unicode" - "github.com/kyokomi/emoji" - - "maunium.net/go/mautrix" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -152,45 +149,6 @@ func findWordToTabComplete(text string) string { return output } -func (view *MainView) InputSubmit(roomView *RoomView, text string) { - if len(text) == 0 { - return - } else if text[0] == '/' { - cmd := view.cmdProcessor.ParseCommand(roomView, text) - go view.cmdProcessor.HandleCommand(cmd) - } else { - view.SendMessage(roomView, text) - } - roomView.SetInputText("") -} - -func (view *MainView) SendMessage(roomView *RoomView, text string) { - go view.goSendMessage(roomView, text) -} - -func (view *MainView) goSendMessage(roomView *RoomView, text string) { - defer debug.Recover() - debug.Print("Sending message", tempMessage.Type(), text) - if !roomView.config.Preferences.DisableEmojis { - text = emoji.Sprint(text) - } - eventID, err := view.matrix.SendMarkdownMessage(roomView.Room.ID, tempMessage.Type(), text) - if err != nil { - tempMessage.SetState(ifc.MessageStateFailed) - if httpErr, ok := err.(mautrix.HTTPError); ok { - if respErr := httpErr.RespError; respErr != nil { - // Show shorter version if available - err = respErr - } - } - roomView.AddServiceMessage(fmt.Sprintf("Failed to send message: %v", err)) - view.parent.Render() - } else { - debug.Print("Event ID received:", eventID) - roomView.MessageView().UpdateMessageID(tempMessage, eventID) - } -} - func (view *MainView) ShowBare(roomView *RoomView) { if roomView == nil { return @@ -204,7 +162,7 @@ func (view *MainView) ShowBare(roomView *RoomView) { fmt.Println(roomView.MessageView().CapturePlaintext(height)) fmt.Println("Press enter to return to normal mode.") reader := bufio.NewReader(os.Stdin) - reader.ReadRune() + _, _, _ = reader.ReadRune() print("\033[2J\033[0;0H") }) } @@ -310,7 +268,6 @@ func (view *MainView) SwitchRoom(tag string, room *rooms.Room) { func (view *MainView) addRoomPage(room *rooms.Room) { if _, ok := view.rooms[room.ID]; !ok { roomView := NewRoomView(view, room). - SetInputSubmitFunc(view.InputSubmit). SetInputChangedFunc(view.InputChanged) view.rooms[room.ID] = roomView roomView.UpdateUserList() @@ -428,8 +385,8 @@ func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, shoul sendNotification(room, message.NotificationSenderName(), message.NotificationContent(), should.Highlight, shouldPlaySound) } - // TODO Make sure this happens somewhere else - //message.SetIsHighlight(should.Highlight) + // TODO this should probably happen somewhere else + message.SetIsHighlight(should.Highlight) } func (view *MainView) InitialSyncDone() { @@ -443,15 +400,16 @@ func (view *MainView) InitialSyncDone() { func (view *MainView) LoadHistory(room string) { defer debug.Recover() roomView := view.rooms[room] + msgView := roomView.MessageView() batch := roomView.Room.PrevBatch lockTime := time.Now().Unix() + 1 roomView.Room.LockHistory() - roomView.MessageView().LoadingMessages = true + msgView.LoadingMessages = true defer func() { roomView.Room.UnlockHistory() - roomView.MessageView().LoadingMessages = false + msgView.LoadingMessages = false }() // There's no clean way to try to lock a mutex, so we just check if we still @@ -468,16 +426,9 @@ func (view *MainView) LoadHistory(room string) { return } for _, evt := range history { - message := roomView.ParseEvent(evt) - if message != nil { - roomView.AddMessage(message, ifc.PrependMessage) + if message := roomView.ParseEvent(evt); message != nil { + msgView.AddMessage(message, PrependMessage) } } - // TODO? - /*err = roomView.SaveHistory(view.config.HistoryDir) - if err != nil { - debug.Printf("Failed to save history of %s: %v", roomView.Room.GetTitle(), err) - }*/ - view.config.PutRoom(roomView.Room) view.parent.Render() } |