aboutsummaryrefslogtreecommitdiff
path: root/ui/messages
diff options
context:
space:
mode:
Diffstat (limited to 'ui/messages')
-rw-r--r--ui/messages/base.go9
-rw-r--r--ui/messages/expandedtextmessage.go3
-rw-r--r--ui/messages/imagemessage.go3
-rw-r--r--ui/messages/parser/htmlparser.go9
-rw-r--r--ui/messages/parser/parser.go48
-rw-r--r--ui/messages/textmessage.go5
6 files changed, 37 insertions, 40 deletions
diff --git a/ui/messages/base.go b/ui/messages/base.go
index 6049ae3..c9da389 100644
--- a/ui/messages/base.go
+++ b/ui/messages/base.go
@@ -18,6 +18,7 @@ package messages
import (
"encoding/gob"
+ "maunium.net/go/gomatrix"
"time"
"maunium.net/go/gomuks/config"
@@ -33,7 +34,7 @@ func init() {
type BaseMessage struct {
MsgID string
- MsgType string
+ MsgType gomatrix.MessageType
MsgSenderID string
MsgSender string
MsgSenderColor tcell.Color
@@ -47,7 +48,7 @@ type BaseMessage struct {
prevPrefs config.UserPreferences
}
-func newBaseMessage(id, sender, displayname, msgtype string, timestamp time.Time) BaseMessage {
+func newBaseMessage(id, sender, displayname string, msgtype gomatrix.MessageType, timestamp time.Time) BaseMessage {
return BaseMessage{
MsgSenderID: sender,
MsgSender: displayname,
@@ -194,11 +195,11 @@ func (msg *BaseMessage) SetID(id string) {
msg.MsgID = id
}
-func (msg *BaseMessage) Type() string {
+func (msg *BaseMessage) Type() gomatrix.MessageType {
return msg.MsgType
}
-func (msg *BaseMessage) SetType(msgtype string) {
+func (msg *BaseMessage) SetType(msgtype gomatrix.MessageType) {
msg.MsgType = msgtype
}
diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go
index c69f3dc..2e77a24 100644
--- a/ui/messages/expandedtextmessage.go
+++ b/ui/messages/expandedtextmessage.go
@@ -18,6 +18,7 @@ package messages
import (
"encoding/gob"
+ "maunium.net/go/gomatrix"
"time"
"maunium.net/go/gomuks/config"
@@ -34,7 +35,7 @@ type ExpandedTextMessage struct {
}
// NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state.
-func NewExpandedTextMessage(id, sender, displayname, msgtype string, text tstring.TString, timestamp time.Time) UIMessage {
+func NewExpandedTextMessage(id, sender, displayname string, msgtype gomatrix.MessageType, text tstring.TString, timestamp time.Time) UIMessage {
return &ExpandedTextMessage{
BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp),
MsgText: text,
diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go
index dcbe1c5..a17c842 100644
--- a/ui/messages/imagemessage.go
+++ b/ui/messages/imagemessage.go
@@ -20,6 +20,7 @@ import (
"bytes"
"encoding/gob"
"fmt"
+ "maunium.net/go/gomatrix"
"time"
"image/color"
@@ -47,7 +48,7 @@ type ImageMessage struct {
}
// NewImageMessage creates a new ImageMessage object with the provided values and the default state.
-func NewImageMessage(matrix ifc.MatrixContainer, id, sender, displayname, msgtype, body, homeserver, fileID string, data []byte, timestamp time.Time) UIMessage {
+func NewImageMessage(matrix ifc.MatrixContainer, id, sender, displayname string, msgtype gomatrix.MessageType, body, homeserver, fileID string, data []byte, timestamp time.Time) UIMessage {
return &ImageMessage{
newBaseMessage(id, sender, displayname, msgtype, timestamp),
body,
diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go
index c8b000e..bcde14d 100644
--- a/ui/messages/parser/htmlparser.go
+++ b/ui/messages/parser/htmlparser.go
@@ -22,6 +22,7 @@ import (
"regexp"
"strings"
+ "github.com/lucasb-eyer/go-colorful"
"golang.org/x/net/html"
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/matrix/rooms"
@@ -29,7 +30,6 @@ import (
"maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell"
"strconv"
- "github.com/lucasb-eyer/go-colorful"
)
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
@@ -173,7 +173,7 @@ func (parser *htmlParser) linkToTString(node *html.Node, stripLinebreak bool) ts
pillTarget := match[1]
if pillTarget[0] == '@' {
if member := parser.room.GetMember(pillTarget); member != nil {
- return tstring.NewColorTString(member.DisplayName, widget.GetHashColor(member.UserID))
+ return tstring.NewColorTString(member.Displayname, widget.GetHashColor(pillTarget))
}
}
return tstring.NewTString(pillTarget)
@@ -271,14 +271,13 @@ func (parser *htmlParser) Parse(htmlData string) tstring.TString {
// ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage.
func ParseHTMLMessage(room *rooms.Room, evt *gomatrix.Event, senderDisplayname string) tstring.TString {
- htmlData, _ := evt.Content["formatted_body"].(string)
+ htmlData := evt.Content.FormattedBody
htmlData = strings.Replace(htmlData, "\t", " ", -1)
parser := htmlParser{room}
str := parser.Parse(htmlData)
- msgtype, _ := evt.Content["msgtype"].(string)
- if msgtype == "m.emote" {
+ if evt.Content.MsgType == gomatrix.MsgEmote {
str = tstring.Join([]tstring.TString{
tstring.NewTString("* "),
tstring.NewColorTString(senderDisplayname, widget.GetHashColor(evt.Sender)),
diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go
index c84a7f4..1d7ced3 100644
--- a/ui/messages/parser/parser.go
+++ b/ui/messages/parser/parser.go
@@ -33,9 +33,9 @@ import (
func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
switch evt.Type {
- case "m.room.message":
+ case gomatrix.EventMessage:
return ParseMessage(matrix, room, evt)
- case "m.room.member":
+ case gomatrix.StateMember:
return ParseMembershipEvent(room, evt)
}
return nil
@@ -53,32 +53,28 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *gomatrix.Ev
displayname := evt.Sender
member := room.GetMember(evt.Sender)
if member != nil {
- displayname = member.DisplayName
+ displayname = member.Displayname
}
- msgtype, _ := evt.Content["msgtype"].(string)
- text, _ := evt.Content["body"].(string)
ts := unixToTime(evt.Timestamp)
- switch msgtype {
+ switch evt.Content.MsgType {
case "m.text", "m.notice", "m.emote":
- format, hasFormat := evt.Content["format"].(string)
- if hasFormat && format == "org.matrix.custom.html" {
+ if evt.Content.Format == gomatrix.FormatHTML {
text := ParseHTMLMessage(room, evt, displayname)
- return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, msgtype, text, ts)
+ return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, text, ts)
}
- text = strings.Replace(text, "\t", " ", -1)
- return messages.NewTextMessage(evt.ID, evt.Sender, displayname, msgtype, text, ts)
+ evt.Content.Body = strings.Replace(evt.Content.Body, "\t", " ", -1)
+ return messages.NewTextMessage(evt.ID, evt.Sender, displayname, evt.Content.MsgType, evt.Content.Body, ts)
case "m.image":
- url, _ := evt.Content["url"].(string)
- data, hs, id, err := matrix.Download(url)
+ data, hs, id, err := matrix.Download(evt.Content.URL)
if err != nil {
- debug.Printf("Failed to download %s: %v", url, err)
+ debug.Printf("Failed to download %s: %v", evt.Content.URL, err)
}
- return messages.NewImageMessage(matrix, evt.ID, evt.Sender, displayname, msgtype, text, hs, id, data, ts)
+ return messages.NewImageMessage(matrix, evt.ID, evt.Sender, displayname, evt.Content.MsgType, evt.Content.Body, hs, id, data, ts)
}
return nil
}
-func getMembershipChangeMessage(evt *gomatrix.Event, membership, prevMembership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
+func getMembershipChangeMessage(evt *gomatrix.Event, membership, prevMembership gomatrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
switch membership {
case "invite":
sender = "---"
@@ -92,12 +88,11 @@ func getMembershipChangeMessage(evt *gomatrix.Event, membership, prevMembership,
case "leave":
sender = "<--"
if evt.Sender != *evt.StateKey {
- if prevMembership == "ban" {
+ if prevMembership == gomatrix.MembershipBan {
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 {
- reason, _ := evt.Content["reason"].(string)
- text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, reason), tcell.ColorRed)
+ text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, evt.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))
@@ -109,8 +104,7 @@ func getMembershipChangeMessage(evt *gomatrix.Event, membership, prevMembership,
text.Colorize(0, len(displayname), widget.GetHashColor(*evt.StateKey))
}
case "ban":
- reason, _ := evt.Content["reason"].(string)
- text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, reason), tcell.ColorRed)
+ text = tstring.NewColorTString(fmt.Sprintf("%s banned %s: %s", senderDisplayname, displayname, evt.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))
}
@@ -121,20 +115,20 @@ func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender st
member := room.GetMember(evt.Sender)
senderDisplayname := evt.Sender
if member != nil {
- senderDisplayname = member.DisplayName
+ senderDisplayname = member.Displayname
}
- membership, _ := evt.Content["membership"].(string)
- displayname, _ := evt.Content["displayname"].(string)
+ membership := evt.Content.Membership
+ displayname := evt.Content.Displayname
if len(displayname) == 0 {
displayname = *evt.StateKey
}
- prevMembership := "leave"
+ prevMembership := gomatrix.MembershipLeave
prevDisplayname := *evt.StateKey
if evt.Unsigned.PrevContent != nil {
- prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
- prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
+ prevMembership = evt.Unsigned.PrevContent.Membership
+ prevDisplayname = evt.Unsigned.PrevContent.Displayname
if len(prevDisplayname) == 0 {
prevDisplayname = *evt.StateKey
}
diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go
index d268688..d5ce324 100644
--- a/ui/messages/textmessage.go
+++ b/ui/messages/textmessage.go
@@ -19,6 +19,7 @@ package messages
import (
"encoding/gob"
"fmt"
+ "maunium.net/go/gomatrix"
"time"
"maunium.net/go/gomuks/config"
@@ -37,7 +38,7 @@ type TextMessage struct {
}
// NewTextMessage creates a new UITextMessage object with the provided values and the default state.
-func NewTextMessage(id, sender, displayname, msgtype, text string, timestamp time.Time) UIMessage {
+func NewTextMessage(id, sender, displayname string, msgtype gomatrix.MessageType, text string, timestamp time.Time) UIMessage {
return &TextMessage{
BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp),
MsgText: text,
@@ -57,7 +58,7 @@ func (msg *TextMessage) getCache() tstring.TString {
return msg.cache
}
-func (msg *TextMessage) SetType(msgtype string) {
+func (msg *TextMessage) SetType(msgtype gomatrix.MessageType) {
msg.BaseMessage.SetType(msgtype)
msg.cache = nil
}