diff options
Diffstat (limited to 'ui')
-rw-r--r-- | ui/commands.go | 3 | ||||
-rw-r--r-- | ui/fuzzy-search-modal.go | 2 | ||||
-rw-r--r-- | ui/messages/parser/parser.go | 112 | ||||
-rw-r--r-- | ui/room-list.go | 2 | ||||
-rw-r--r-- | ui/room-view.go | 6 |
5 files changed, 114 insertions, 11 deletions
diff --git a/ui/commands.go b/ui/commands.go index 0feda29..50aedd2 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "maunium.net/go/mautrix" + "maunium.net/go/mautrix/format" "strings" "unicode" @@ -79,7 +80,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", html.String()) + tempMessage := cmd.Room.NewTempMessage("m.text", format.HTMLToText(html.String())) go cmd.MainView.sendTempMessage(cmd.Room, tempMessage, html.String()) cmd.UI.Render() } diff --git a/ui/fuzzy-search-modal.go b/ui/fuzzy-search-modal.go index 0ccd72d..b58a367 100644 --- a/ui/fuzzy-search-modal.go +++ b/ui/fuzzy-search-modal.go @@ -21,7 +21,7 @@ import ( "sort" "strconv" - "github.com/renstrom/fuzzysearch/fuzzy" + "github.com/lithammer/fuzzysearch/fuzzy" "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/widget" diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go index ef6578f..05dd0e9 100644 --- a/ui/messages/parser/parser.go +++ b/ui/messages/parser/parser.go @@ -39,6 +39,8 @@ func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event fallthrough case mautrix.EventMessage: return ParseMessage(matrix, room, evt) + case mautrix.StateTopic, mautrix.StateRoomName, mautrix.StateAliases, mautrix.StateCanonicalAlias: + return ParseStateEvent(matrix, room, evt) case mautrix.StateMember: return ParseMembershipEvent(room, evt) } @@ -53,6 +55,45 @@ func unixToTime(unix int64) time.Time { return timestamp } +func ParseStateEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) messages.UIMessage { + displayname := evt.Sender + member := room.GetMember(evt.Sender) + if member != nil { + displayname = member.Displayname + } + text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender)) + switch evt.Type { + case mautrix.StateTopic: + if len(evt.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)). + AppendColor(".", tcell.ColorGreen) + } + case mautrix.StateRoomName: + if len(evt.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)). + AppendColor(".", tcell.ColorGreen) + } + case mautrix.StateCanonicalAlias: + if len(evt.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(evt.Content.Alias, tcell.StyleDefault.Underline(true)). + AppendColor(".", tcell.ColorGreen) + } + case mautrix.StateAliases: + text = ParseAliasEvent(evt, displayname) + } + ts := unixToTime(evt.Timestamp) + return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, mautrix.MessageType(evt.Type.Type), text, ts) +} + func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) messages.UIMessage { displayname := evt.Sender member := room.GetMember(evt.Sender) @@ -153,10 +194,12 @@ func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender str sender, text = getMembershipChangeMessage(evt, membership, prevMembership, senderDisplayname, displayname, prevDisplayname) } else if displayname != prevDisplayname { sender = "---" - text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorGreen) color := widget.GetHashColor(*evt.StateKey) - text.Colorize(0, len(prevDisplayname), color) - text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), color) + text = tstring.NewBlankTString(). + AppendColor(prevDisplayname, color). + AppendColor(" changed their display name to ", tcell.ColorGreen). + AppendColor(displayname, color). + AppendColor(".", tcell.ColorGreen) } return } @@ -170,3 +213,66 @@ func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) messages.UIMessa ts := unixToTime(evt.Timestamp) return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.member", text, ts) } + +func ParseAliasEvent(evt *mautrix.Event, displayname string) tstring.TString { + var prevAliases []string + if evt.Unsigned.PrevContent != nil { + prevAliases = evt.Unsigned.PrevContent.Aliases + } + aliases := evt.Content.Aliases + var added, removed []tstring.TString +Outer1: + for _, oldAlias := range prevAliases { + for _, newAlias := range aliases { + if oldAlias == newAlias { + continue Outer1 + } + } + removed = append(removed, tstring.NewStyleTString(oldAlias, tcell.StyleDefault.Foreground(widget.GetHashColor(oldAlias)).Underline(true))) + } +Outer2: + for _, newAlias := range aliases { + for _, oldAlias := range prevAliases { + if oldAlias == newAlias { + continue Outer2 + } + } + added = append(added, tstring.NewStyleTString(newAlias, tcell.StyleDefault.Foreground(widget.GetHashColor(newAlias)).Underline(true))) + } + var addedStr, removedStr tstring.TString + if len(added) == 1 { + addedStr = added[0] + } else if len(added) > 1 { + addedStr = tstring. + Join(added[:len(added)-1], ", "). + Append(" and "). + AppendTString(added[len(added)-1]) + } + if len(removed) == 1 { + removedStr = removed[0] + } else if len(removed) > 1 { + removedStr = tstring. + Join(removed[:len(removed)-1], ", "). + Append(" and "). + AppendTString(removed[len(removed)-1]) + } + text := tstring.NewBlankTString() + if len(addedStr) > 0 && len(removedStr) > 0 { + text = text.AppendColor(fmt.Sprintf("%s added ", displayname), tcell.ColorGreen). + AppendTString(addedStr). + AppendColor(" and removed ", tcell.ColorGreen). + AppendTString(removedStr). + AppendColor(" as addresses for this room.", tcell.ColorGreen) + } else if len(addedStr) > 0 { + text = text.AppendColor(fmt.Sprintf("%s added ", displayname), tcell.ColorGreen). + AppendTString(addedStr). + AppendColor(" as addresses for this room.", tcell.ColorGreen) + } else if len(removedStr) > 0 { + text = text.AppendColor(fmt.Sprintf("%s removed ", displayname), tcell.ColorGreen). + AppendTString(removedStr). + AppendColor(" as addresses for this room.", tcell.ColorGreen) + } else { + return nil + } + return text +} diff --git a/ui/room-list.go b/ui/room-list.go index 6b81e60..d82f870 100644 --- a/ui/room-list.go +++ b/ui/room-list.go @@ -456,7 +456,7 @@ func (list *RoomList) Draw(screen tcell.Screen) { for _, tag := range list.tags { trl := list.items[tag] tagDisplayName := list.GetTagDisplayName(tag) - if len(tagDisplayName) == 0 { + if trl == nil || len(tagDisplayName) == 0 { continue } diff --git a/ui/room-view.go b/ui/room-view.go index 6eda559..a978346 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -357,10 +357,6 @@ func (view *RoomView) newUIMessage(id, sender string, msgtype mautrix.MessageTyp return msg } -func (view *RoomView) NewMessage(id, sender string, msgtype mautrix.MessageType, text string, timestamp time.Time) ifc.Message { - return view.newUIMessage(id, sender, msgtype, text, timestamp) -} - func (view *RoomView) NewTempMessage(msgtype mautrix.MessageType, text string) ifc.Message { now := time.Now() id := strconv.FormatInt(now.UnixNano(), 10) @@ -375,7 +371,7 @@ func (view *RoomView) NewTempMessage(msgtype mautrix.MessageType, text string) i } func (view *RoomView) AddServiceMessage(text string) { - message := view.newUIMessage("", "*", "gomuks.service", text, time.Now()) + message := view.newUIMessage(view.parent.matrix.Client().TxnID(), "*", "gomuks.service", text, time.Now()) message.SetIsService(true) view.AddMessage(message, ifc.AppendMessage) } |