From fe439f076ad3d76c459c9a76c4bbdcda5182ed85 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 17 Jun 2019 12:27:31 +0300 Subject: Wrap events in custom struct to add gomuks-specific fields --- ui/messages/base.go | 35 ++++++++++++++++++----------------- ui/messages/expandedtextmessage.go | 6 +++--- ui/messages/html/parser.go | 3 ++- ui/messages/htmlmessage.go | 6 +++--- ui/messages/imagemessage.go | 6 +++--- ui/messages/parser.go | 17 +++++++++-------- ui/messages/redactedmessage.go | 6 +++--- ui/messages/textmessage.go | 6 +++--- 8 files changed, 44 insertions(+), 41 deletions(-) (limited to 'ui/messages') diff --git a/ui/messages/base.go b/ui/messages/base.go index ef495fb..d072943 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -22,6 +22,7 @@ import ( "time" "maunium.net/go/gomuks/config" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -50,7 +51,7 @@ type UIMessage struct { SenderName string DefaultSenderColor tcell.Color Timestamp time.Time - State mautrix.OutgoingEventState + State event.OutgoingState IsHighlight bool IsService bool Source json.RawMessage @@ -61,25 +62,25 @@ type UIMessage struct { const DateFormat = "January _2, 2006" const TimeFormat = "15:04:05" -func newUIMessage(event *mautrix.Event, displayname string, renderer MessageRenderer) *UIMessage { - msgtype := event.Content.MsgType +func newUIMessage(evt *event.Event, displayname string, renderer MessageRenderer) *UIMessage { + msgtype := evt.Content.MsgType if len(msgtype) == 0 { - msgtype = mautrix.MessageType(event.Type.String()) + msgtype = mautrix.MessageType(evt.Type.String()) } return &UIMessage{ - SenderID: event.Sender, + SenderID: evt.Sender, SenderName: displayname, - Timestamp: unixToTime(event.Timestamp), - DefaultSenderColor: widget.GetHashColor(event.Sender), + Timestamp: unixToTime(evt.Timestamp), + DefaultSenderColor: widget.GetHashColor(evt.Sender), Type: msgtype, - EventID: event.ID, - TxnID: event.Unsigned.TransactionID, - Relation: *event.Content.GetRelatesTo(), - State: event.Unsigned.OutgoingState, + EventID: evt.ID, + TxnID: evt.Unsigned.TransactionID, + Relation: *evt.Content.GetRelatesTo(), + State: evt.Gomuks.OutgoingState, IsHighlight: false, IsService: false, - Source: event.Content.VeryRaw, + Source: evt.Content.VeryRaw, Renderer: renderer, } } @@ -100,9 +101,9 @@ func unixToTime(unix int64) time.Time { // In any other case, the sender is the display name of the user who sent the message. func (msg *UIMessage) Sender() string { switch msg.State { - case mautrix.EventStateLocalEcho: + case event.StateLocalEcho: return "Sending..." - case mautrix.EventStateSendFail: + case event.StateSendFail: return "Error" } switch msg.Type { @@ -124,11 +125,11 @@ func (msg *UIMessage) NotificationContent() string { func (msg *UIMessage) getStateSpecificColor() tcell.Color { switch msg.State { - case mautrix.EventStateLocalEcho: + case event.StateLocalEcho: return tcell.ColorGray - case mautrix.EventStateSendFail: + case event.StateSendFail: return tcell.ColorRed - case mautrix.EventStateDefault: + case event.StateDefault: fallthrough default: return tcell.ColorDefault diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go index c9cbf0c..424db79 100644 --- a/ui/messages/expandedtextmessage.go +++ b/ui/messages/expandedtextmessage.go @@ -21,7 +21,7 @@ import ( "time" ifc "maunium.net/go/gomuks/interface" - "maunium.net/go/mautrix" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -35,8 +35,8 @@ type ExpandedTextMessage struct { } // NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state. -func NewExpandedTextMessage(event *mautrix.Event, displayname string, text tstring.TString) *UIMessage { - return newUIMessage(event, displayname, &ExpandedTextMessage{ +func NewExpandedTextMessage(evt *event.Event, displayname string, text tstring.TString) *UIMessage { + return newUIMessage(evt, displayname, &ExpandedTextMessage{ Text: text, }) } diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index a0757c4..99f9c29 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -27,6 +27,7 @@ import ( "github.com/lucasb-eyer/go-colorful" "golang.org/x/net/html" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" "maunium.net/go/tcell" @@ -382,7 +383,7 @@ func (parser *htmlParser) Parse(htmlData string) Entity { const TabLength = 4 // Parse parses a HTML-formatted Matrix event into a UIMessage. -func Parse(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) Entity { +func Parse(room *rooms.Room, evt *event.Event, senderDisplayname string) Entity { htmlData := evt.Content.FormattedBody if evt.Content.Format != mautrix.FormatHTML { htmlData = strings.Replace(html.EscapeString(evt.Content.Body), "\n", "
", -1) diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go index f3295b2..e476bc9 100644 --- a/ui/messages/htmlmessage.go +++ b/ui/messages/htmlmessage.go @@ -18,7 +18,7 @@ package messages import ( ifc "maunium.net/go/gomuks/interface" - "maunium.net/go/mautrix" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -33,8 +33,8 @@ type HTMLMessage struct { focused bool } -func NewHTMLMessage(event *mautrix.Event, displayname string, root html.Entity) *UIMessage { - return newUIMessage(event, displayname, &HTMLMessage{ +func NewHTMLMessage(evt *event.Event, displayname string, root html.Entity) *UIMessage { + return newUIMessage(evt, displayname, &HTMLMessage{ Root: root, }) } diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go index 399c941..5e72c88 100644 --- a/ui/messages/imagemessage.go +++ b/ui/messages/imagemessage.go @@ -22,7 +22,7 @@ import ( "image" "image/color" - "maunium.net/go/mautrix" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -44,8 +44,8 @@ type ImageMessage struct { } // NewImageMessage creates a new ImageMessage object with the provided values and the default state. -func NewImageMessage(matrix ifc.MatrixContainer, event *mautrix.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage { - return newUIMessage(event, displayname, &ImageMessage{ +func NewImageMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage { + return newUIMessage(evt, displayname, &ImageMessage{ Body: body, Homeserver: homeserver, FileID: fileID, diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 0b1965e..695dac6 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" "maunium.net/go/tcell" @@ -42,7 +43,7 @@ func getCachedEvent(mainView ifc.MainView, roomID, eventID string) *UIMessage { return nil } -func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *mautrix.Event) *UIMessage { +func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *event.Event) *UIMessage { msg := directParseEvent(matrix, room, evt) if msg == nil { return nil @@ -63,7 +64,7 @@ func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.R return msg } -func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) *UIMessage { +func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event) *UIMessage { displayname := evt.Sender member := room.GetMember(evt.Sender) if member != nil { @@ -89,7 +90,7 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix return nil } -func ParseStateEvent(evt *mautrix.Event, displayname string) *UIMessage { +func ParseStateEvent(evt *event.Event, displayname string) *UIMessage { text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender)) switch evt.Type { case mautrix.StateTopic: @@ -122,7 +123,7 @@ func ParseStateEvent(evt *mautrix.Event, displayname string) *UIMessage { return NewExpandedTextMessage(evt, displayname, text) } -func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event, displayname string) *UIMessage { +func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event, displayname string) *UIMessage { if len(evt.Content.GetReplyTo()) > 0 { evt.Content.RemoveReplyFallback() } @@ -146,7 +147,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Eve return nil } -func getMembershipChangeMessage(evt *mautrix.Event, membership, prevMembership mautrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) { +func getMembershipChangeMessage(evt *event.Event, membership, prevMembership mautrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) { switch membership { case "invite": sender = "---" @@ -183,7 +184,7 @@ func getMembershipChangeMessage(evt *mautrix.Event, membership, prevMembership m return } -func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender string, text tstring.TString) { +func getMembershipEventContent(room *rooms.Room, evt *event.Event) (sender string, text tstring.TString) { member := room.GetMember(evt.Sender) senderDisplayname := evt.Sender if member != nil { @@ -220,7 +221,7 @@ func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender str return } -func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) *UIMessage { +func ParseMembershipEvent(room *rooms.Room, evt *event.Event) *UIMessage { displayname, text := getMembershipEventContent(room, evt) if len(text) == 0 { return nil @@ -229,7 +230,7 @@ func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) *UIMessage { return NewExpandedTextMessage(evt, displayname, text) } -func ParseAliasEvent(evt *mautrix.Event, displayname string) tstring.TString { +func ParseAliasEvent(evt *event.Event, displayname string) tstring.TString { var prevAliases []string if evt.Unsigned.PrevContent != nil { prevAliases = evt.Unsigned.PrevContent.Aliases diff --git a/ui/messages/redactedmessage.go b/ui/messages/redactedmessage.go index b19ccad..fc34682 100644 --- a/ui/messages/redactedmessage.go +++ b/ui/messages/redactedmessage.go @@ -18,7 +18,7 @@ package messages import ( ifc "maunium.net/go/gomuks/interface" - "maunium.net/go/mautrix" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" "maunium.net/go/tcell" @@ -27,8 +27,8 @@ import ( type RedactedMessage struct{} -func NewRedactedMessage(event *mautrix.Event, displayname string) *UIMessage { - return newUIMessage(event, displayname, &RedactedMessage{}) +func NewRedactedMessage(evt *event.Event, displayname string) *UIMessage { + return newUIMessage(evt, displayname, &RedactedMessage{}) } func (msg *RedactedMessage) Clone() MessageRenderer { diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go index 9ace201..5527677 100644 --- a/ui/messages/textmessage.go +++ b/ui/messages/textmessage.go @@ -21,7 +21,7 @@ import ( "time" ifc "maunium.net/go/gomuks/interface" - "maunium.net/go/mautrix" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" "maunium.net/go/gomuks/config" @@ -35,8 +35,8 @@ type TextMessage struct { } // NewTextMessage creates a new UITextMessage object with the provided values and the default state. -func NewTextMessage(event *mautrix.Event, displayname string, text string) *UIMessage { - return newUIMessage(event, displayname, &TextMessage{ +func NewTextMessage(evt *event.Event, displayname string, text string) *UIMessage { + return newUIMessage(evt, displayname, &TextMessage{ Text: text, }) } -- cgit v1.2.3