diff options
Diffstat (limited to 'ui/messages')
-rw-r--r-- | ui/messages/base.go | 5 | ||||
-rw-r--r-- | ui/messages/filemessage.go | 9 | ||||
-rw-r--r-- | ui/messages/html/parser.go | 13 | ||||
-rw-r--r-- | ui/messages/parser.go | 86 |
4 files changed, 59 insertions, 54 deletions
diff --git a/ui/messages/base.go b/ui/messages/base.go index eaeb552..c4e10b0 100644 --- a/ui/messages/base.go +++ b/ui/messages/base.go @@ -95,7 +95,8 @@ const DateFormat = "January _2, 2006" const TimeFormat = "15:04:05" func newUIMessage(evt *muksevt.Event, displayname string, renderer MessageRenderer) *UIMessage { - msgtype := evt.Content.MsgType + msgContent := evt.Content.AsMessage() + msgtype := msgContent.MsgType if len(msgtype) == 0 { msgtype = event.MessageType(evt.Type.String()) } @@ -117,7 +118,7 @@ func newUIMessage(evt *muksevt.Event, displayname string, renderer MessageRender Type: msgtype, EventID: evt.ID, TxnID: evt.Unsigned.TransactionID, - Relation: *evt.Content.GetRelatesTo(), + Relation: *msgContent.GetRelatesTo(), State: evt.Gomuks.OutgoingState, IsHighlight: false, IsService: false, diff --git a/ui/messages/filemessage.go b/ui/messages/filemessage.go index baffd0f..5b7b21c 100644 --- a/ui/messages/filemessage.go +++ b/ui/messages/filemessage.go @@ -48,11 +48,12 @@ type FileMessage struct { // NewFileMessage creates a new FileMessage object with the provided values and the default state. func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname string) *UIMessage { - url, _ := evt.Content.URL.Parse() - thumbnail, _ := evt.Content.GetInfo().ThumbnailURL.Parse() + content := evt.Content.AsMessage() + url, _ := content.URL.Parse() + thumbnail, _ := content.GetInfo().ThumbnailURL.Parse() return newUIMessage(evt, displayname, &FileMessage{ - Type: evt.Content.MsgType, - Body: evt.Content.Body, + Type: content.MsgType, + Body: content.Body, URL: url, Thumbnail: thumbnail, matrix: matrix, diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index 86b501d..7bb4a84 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -27,7 +27,6 @@ import ( "github.com/lucasb-eyer/go-colorful" "golang.org/x/net/html" - "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" "maunium.net/go/tcell" @@ -384,10 +383,10 @@ 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 *muksevt.Event, senderDisplayname string) Entity { - htmlData := evt.Content.FormattedBody - if evt.Content.Format != event.FormatHTML { - htmlData = strings.Replace(html.EscapeString(evt.Content.Body), "\n", "<br/>", -1) +func Parse(room *rooms.Room, content *event.MessageEventContent, sender id.UserID, senderDisplayname string) Entity { + htmlData := content.FormattedBody + if content.Format != event.FormatHTML { + htmlData = strings.Replace(html.EscapeString(content.Body), "\n", "<br/>", -1) } htmlData = strings.Replace(htmlData, "\t", strings.Repeat(" ", TabLength), -1) @@ -403,14 +402,14 @@ func Parse(room *rooms.Room, evt *muksevt.Event, senderDisplayname string) Entit } } - if evt.Content.MsgType == event.MsgEmote { + if content.MsgType == event.MsgEmote { root = &ContainerEntity{ BaseEntity: &BaseEntity{ Tag: "emote", }, Children: []Entity{ NewTextEntity("* "), - NewTextEntity(senderDisplayname).AdjustStyle(AdjustStyleTextColor(widget.GetHashColor(evt.Sender))), + NewTextEntity(senderDisplayname).AdjustStyle(AdjustStyleTextColor(widget.GetHashColor(sender))), NewTextEntity(" "), root, }, diff --git a/ui/messages/parser.go b/ui/messages/parser.go index d89bcb9..616e7cb 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" @@ -48,10 +49,10 @@ func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.R if msg == nil { return nil } - if len(evt.Content.GetReplyTo()) > 0 { - if replyToMsg := getCachedEvent(mainView, room.ID, evt.Content.GetReplyTo()); replyToMsg != nil { + if content, ok := evt.Content.Parsed.(*event.MessageEventContent); ok && len(content.GetReplyTo()) > 0 { + if replyToMsg := getCachedEvent(mainView, room.ID, content.GetReplyTo()); replyToMsg != nil { msg.ReplyTo = replyToMsg.Clone() - } else if replyToEvt, _ := matrix.GetEvent(room, evt.Content.GetReplyTo()); replyToEvt != nil { + } else if replyToEvt, _ := matrix.GetEvent(room, content.GetReplyTo()); replyToEvt != nil { if replyToMsg := directParseEvent(matrix, room, replyToEvt); replyToMsg != nil { msg.ReplyTo = replyToMsg msg.ReplyTo.Reactions = nil @@ -74,48 +75,49 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt if evt.Unsigned.RedactedBecause != nil || evt.Type == event.EventRedaction { return NewRedactedMessage(evt, displayname) } - switch evt.Type { - case event.EventSticker: - evt.Content.MsgType = event.MsgImage - fallthrough - case event.EventMessage: + switch content := evt.Content.Parsed.(type) { + case *event.MessageEventContent: + if evt.Type == event.EventSticker { + content.MsgType = event.MsgImage + } return ParseMessage(matrix, room, evt, displayname) - case event.EventEncrypted: + case *event.EncryptedEventContent: return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Encrypted messages are not yet supported", tcell.StyleDefault.Italic(true))) - case event.StateTopic, event.StateRoomName, event.StateAliases, event.StateCanonicalAlias: + case *event.TopicEventContent, *event.RoomNameEventContent, *event.CanonicalAliasEventContent: return ParseStateEvent(evt, displayname) - case event.StateMember: + case *event.MemberEventContent: return ParseMembershipEvent(room, evt) + default: + debug.Printf("Unknown event content type %T in directParseEvent", content) + return nil } - - return nil } func ParseStateEvent(evt *muksevt.Event, displayname string) *UIMessage { text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender)) - switch evt.Type { - case event.StateTopic: - if len(evt.Content.Topic) == 0 { + switch content := evt.Content.Parsed.(type) { + case *event.TopicEventContent: + if len(content.Topic) == 0 { text = text.AppendColor(" removed the topic.", tcell.ColorGreen) } else { text = text.AppendColor(" changed the topic to ", tcell.ColorGreen). - AppendStyle(evt.Content.Topic, tcell.StyleDefault.Underline(true)). + AppendStyle(content.Topic, tcell.StyleDefault.Underline(true)). AppendColor(".", tcell.ColorGreen) } - case event.StateRoomName: - if len(evt.Content.Name) == 0 { + case *event.RoomNameEventContent: + if len(content.Name) == 0 { text = text.AppendColor(" removed the room name.", tcell.ColorGreen) } else { text = text.AppendColor(" changed the room name to ", tcell.ColorGreen). - AppendStyle(evt.Content.Name, tcell.StyleDefault.Underline(true)). + AppendStyle(content.Name, tcell.StyleDefault.Underline(true)). AppendColor(".", tcell.ColorGreen) } - case event.StateCanonicalAlias: - if len(evt.Content.Alias) == 0 { + case *event.CanonicalAliasEventContent: + if len(content.Alias) == 0 { text = text.AppendColor(" removed the main address of the room.", tcell.ColorGreen) } else { text = text.AppendColor(" changed the main address of the room to ", tcell.ColorGreen). - AppendStyle(string(evt.Content.Alias), tcell.StyleDefault.Underline(true)). + AppendStyle(string(content.Alias), tcell.StyleDefault.Underline(true)). AppendColor(".", tcell.ColorGreen) } //case event.StateAliases: @@ -125,19 +127,20 @@ func ParseStateEvent(evt *muksevt.Event, displayname string) *UIMessage { } func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Event, displayname string) *UIMessage { - if len(evt.Content.GetReplyTo()) > 0 { - evt.Content.RemoveReplyFallback() + content := evt.Content.AsMessage() + if len(content.GetReplyTo()) > 0 { + content.RemoveReplyFallback() } if len(evt.Gomuks.Edits) > 0 { - evt.Content = *evt.Gomuks.Edits[len(evt.Gomuks.Edits)-1].Content.NewContent + content = evt.Gomuks.Edits[len(evt.Gomuks.Edits)-1].Content.AsMessage().NewContent } - switch evt.Content.MsgType { + switch content.MsgType { case event.MsgText, event.MsgNotice, event.MsgEmote: - if evt.Content.Format == event.FormatHTML { - return NewHTMLMessage(evt, displayname, html.Parse(room, evt, displayname)) + if content.Format == event.FormatHTML { + return NewHTMLMessage(evt, displayname, html.Parse(room, content, evt.Sender, displayname)) } - evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1) - return NewTextMessage(evt, displayname, evt.Content.Body) + content.Body = strings.Replace(content.Body, "\t", " ", -1) + return NewTextMessage(evt, displayname, content.Body) case event.MsgImage, event.MsgVideo, event.MsgAudio, event.MsgFile: msg := NewFileMessage(matrix, evt, displayname) if !matrix.Preferences().DisableDownloads { @@ -149,8 +152,8 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Eve return nil } -func getMembershipChangeMessage(evt *muksevt.Event, membership, prevMembership event.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) { - switch membership { +func getMembershipChangeMessage(evt *muksevt.Event, content *event.MemberEventContent, prevMembership event.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) { + switch content.Membership { case "invite": sender = "---" text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", senderDisplayname, displayname), tcell.ColorGreen) @@ -171,7 +174,7 @@ func getMembershipChangeMessage(evt *muksevt.Event, membership, prevMembership e text = tstring.NewColorTString(fmt.Sprintf("%s unbanned %s", senderDisplayname, displayname), tcell.ColorGreen) text.Colorize(len(senderDisplayname)+len(" unbanned "), len(displayname), widget.GetHashColor(evt.StateKey)) } else { - text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, evt.Content.Reason), tcell.ColorRed) + text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, content.Reason), tcell.ColorRed) text.Colorize(len(senderDisplayname)+len(" kicked "), len(displayname), widget.GetHashColor(evt.StateKey)) } text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender)) @@ -187,7 +190,7 @@ func getMembershipChangeMessage(evt *muksevt.Event, membership, prevMembership e text.Colorize(0, len(displayname), widget.GetHashColor(evt.StateKey)) } case "ban": - text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, evt.Content.Reason), tcell.ColorRed) + text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, content.Reason), tcell.ColorRed) text.Colorize(len(senderDisplayname)+len(" banned "), len(displayname), widget.GetHashColor(evt.StateKey)) text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender)) } @@ -201,8 +204,8 @@ func getMembershipEventContent(room *rooms.Room, evt *muksevt.Event) (sender str senderDisplayname = member.Displayname } - membership := evt.Content.Membership - displayname := evt.Content.Displayname + content := evt.Content.AsMember() + displayname := content.Displayname if len(displayname) == 0 { displayname = *evt.StateKey } @@ -210,15 +213,16 @@ func getMembershipEventContent(room *rooms.Room, evt *muksevt.Event) (sender str prevMembership := event.MembershipLeave prevDisplayname := *evt.StateKey if evt.Unsigned.PrevContent != nil { - prevMembership = evt.Unsigned.PrevContent.Membership - prevDisplayname = evt.Unsigned.PrevContent.Displayname + prevContent := evt.Unsigned.PrevContent.AsMember() + prevMembership = prevContent.Membership + prevDisplayname = prevContent.Displayname if len(prevDisplayname) == 0 { prevDisplayname = *evt.StateKey } } - if membership != prevMembership { - sender, text = getMembershipChangeMessage(evt, membership, prevMembership, senderDisplayname, displayname, prevDisplayname) + if content.Membership != prevMembership { + sender, text = getMembershipChangeMessage(evt, content, prevMembership, senderDisplayname, displayname, prevDisplayname) } else if displayname != prevDisplayname { sender = "---" color := widget.GetHashColor(evt.StateKey) |