From fe439f076ad3d76c459c9a76c4bbdcda5182ed85 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 17 Jun 2019 12:27:31 +0300 Subject: [PATCH] Wrap events in custom struct to add gomuks-specific fields --- go.mod | 2 +- go.sum | 2 + interface/matrix.go | 9 +++-- interface/ui.go | 4 +- matrix/event/event.go | 43 ++++++++++++++++++++ matrix/history.go | 62 ++++++++++++++--------------- matrix/matrix.go | 64 ++++++++++++++++-------------- 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 +-- ui/room-view.go | 9 +++-- 16 files changed, 167 insertions(+), 113 deletions(-) create mode 100644 matrix/event/event.go diff --git a/go.mod b/go.mod index c1a6062..75c6e2f 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( gopkg.in/russross/blackfriday.v2 v2.0.1 gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 gopkg.in/yaml.v2 v2.2.2 - maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a + maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9 maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed ) diff --git a/go.sum b/go.sum index 25767fc..61c25c0 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,8 @@ maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616163219-6b4bce05f314 h1:wGlIqs/L+ maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616163219-6b4bce05f314/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a h1:DenEIDOOumsU8zDzj5ePQOSaKsxjAxW7UESJ2xo1lxM= maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= +maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9 h1:GB0c9wXX4JrFUuPft1KGvUU1RKjM04XYQp0Uaj2K3bU= +maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d h1:H4wZ4vMVnOh5QFsb4xZtssgpv3DDEkBRzQ8iyEg2fX0= maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d/go.mod h1:GL+akv58wNFzzX4IKLvryKx0F/AcYKHql35DiBzBc/w= maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed h1:sAcUrUZG2LFWBTkTtLKPQvHPHFM5d6huAhr5ZZuxtbQ= diff --git a/interface/matrix.go b/interface/matrix.go index 6a1a977..424273b 100644 --- a/interface/matrix.go +++ b/interface/matrix.go @@ -17,6 +17,7 @@ package ifc import ( + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/rooms" @@ -34,16 +35,16 @@ type MatrixContainer interface { Logout() SendPreferencesToMatrix() - PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, message string) *mautrix.Event - SendEvent(event *mautrix.Event) (string, error) + PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, message string) *event.Event + SendEvent(evt *event.Event) (string, error) SendTyping(roomID string, typing bool) MarkRead(roomID, eventID string) JoinRoom(roomID, server string) (*rooms.Room, error) LeaveRoom(roomID string) error CreateRoom(req *mautrix.ReqCreateRoom) (*rooms.Room, error) - GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, error) - GetEvent(room *rooms.Room, eventID string) (*mautrix.Event, error) + GetHistory(room *rooms.Room, limit int) ([]*event.Event, error) + GetEvent(room *rooms.Room, eventID string) (*event.Event, error) GetRoom(roomID string) *rooms.Room GetOrCreateRoom(roomID string) *rooms.Room diff --git a/interface/ui.go b/interface/ui.go index ad2458a..32925a8 100644 --- a/interface/ui.go +++ b/interface/ui.go @@ -19,9 +19,9 @@ package ifc import ( "time" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/rooms" - "maunium.net/go/mautrix" ) type UIProvider func(gmx Gomuks) GomuksUI @@ -60,7 +60,7 @@ type RoomView interface { SetTyping(users []string) UpdateUserList() - ParseEvent(evt *mautrix.Event) Message + ParseEvent(evt *event.Event) Message GetEvent(eventID string) Message AddMessage(message Message) AddServiceMessage(message string) diff --git a/matrix/event/event.go b/matrix/event/event.go new file mode 100644 index 0000000..143d35d --- /dev/null +++ b/matrix/event/event.go @@ -0,0 +1,43 @@ +// gomuks - A terminal Matrix client written in Go. +// Copyright (C) 2019 Tulir Asokan +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package event + +import ( + "maunium.net/go/mautrix" +) + +type Event struct { + *mautrix.Event + Gomuks GomuksContent `json:"-"` +} + +func Wrap(event *mautrix.Event) *Event { + return &Event{Event: event} +} + +type OutgoingState int + +const ( + StateDefault OutgoingState = iota + StateLocalEcho + StateSendFail +) + +type GomuksContent struct { + OutgoingState OutgoingState + Edits []*Event +} diff --git a/matrix/history.go b/matrix/history.go index bcec304..fc75e1a 100644 --- a/matrix/history.go +++ b/matrix/history.go @@ -26,14 +26,11 @@ import ( sync "github.com/sasha-s/go-deadlock" bolt "go.etcd.io/bbolt" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/mautrix" ) -func init() { - gob.Register(&mautrix.Event{}) -} - type HistoryManager struct { sync.Mutex @@ -88,7 +85,7 @@ func (hm *HistoryManager) Close() error { return hm.db.Close() } -func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix.Event, err error) { +func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (evt *event.Event, err error) { err = hm.db.View(func(tx *bolt.Tx) error { rid := []byte(room.ID) eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) @@ -102,7 +99,7 @@ func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix. stream := tx.Bucket(bucketRoomStreams).Bucket(rid) eventData := stream.Get(streamIndex) var umErr error - event, umErr = unmarshalEvent(eventData) + evt, umErr = unmarshalEvent(eventData) return umErr }) return @@ -110,8 +107,8 @@ func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix. var EventNotFoundError = errors.New("event not found") -func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(event *mautrix.Event) error) error { - return hm.db.Update(func (tx *bolt.Tx) error { +func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(evt *event.Event) error) error { + return hm.db.Update(func(tx *bolt.Tx) error { rid := []byte(room.ID) eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) if eventIDs == nil { @@ -127,11 +124,11 @@ func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(e return EventNotFoundError } - if event, err := unmarshalEvent(eventData); err != nil { + if evt, err := unmarshalEvent(eventData); err != nil { return err - } else if err = update(event); err != nil { + } else if err = update(evt); err != nil { return err - } else if eventData, err = marshalEvent(event); err != nil { + } else if eventData, err = marshalEvent(evt); err != nil { return err } else { return stream.Put(streamIndex, eventData) @@ -139,17 +136,18 @@ func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(e }) } -func (hm *HistoryManager) Append(room *rooms.Room, events []*mautrix.Event) error { +func (hm *HistoryManager) Append(room *rooms.Room, events []*mautrix.Event) ([]*event.Event, error) { return hm.store(room, events, true) } -func (hm *HistoryManager) Prepend(room *rooms.Room, events []*mautrix.Event) error { +func (hm *HistoryManager) Prepend(room *rooms.Room, events []*mautrix.Event) ([]*event.Event, error) { return hm.store(room, events, false) } -func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, append bool) error { +func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, append bool) ([]*event.Event, error) { hm.Lock() defer hm.Unlock() + newEvents := make([]*event.Event, len(events)) err := hm.db.Update(func(tx *bolt.Tx) error { streamPointers := tx.Bucket(bucketStreamPointers) rid := []byte(room.ID) @@ -174,8 +172,9 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen if err != nil { return err } - for i, event := range events { - if err := put(stream, eventIDs, event, ptrStart+uint64(i)); err != nil { + for i, evt := range events { + newEvents[i] = event.Wrap(evt) + if err := put(stream, eventIDs, newEvents[i], ptrStart+uint64(i)); err != nil { return err } } @@ -194,8 +193,9 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen } } eventCount := uint64(len(events)) - for i, event := range events { - if err := put(stream, eventIDs, event, -ptrStart-uint64(i)); err != nil { + for i, evt := range events { + newEvents[i] = event.Wrap(evt) + if err := put(stream, eventIDs, newEvents[i], -ptrStart-uint64(i)); err != nil { return err } } @@ -208,10 +208,10 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen return nil }) - return err + return newEvents, err } -func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*mautrix.Event, err error) { +func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*event.Event, err error) { hm.Lock() defer hm.Unlock() err = hm.db.View(func(tx *bolt.Tx) error { @@ -232,11 +232,11 @@ func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*mautrix.Eve } hm.historyLoadPtr[room] = ptrStartFound - 1 for ; k != nil && btoi(k) < ptrStart; k, v = c.Next() { - event, parseError := unmarshalEvent(v) + evt, parseError := unmarshalEvent(v) if parseError != nil { return parseError } - events = append(events, event) + events = append(events, evt) } return nil }) @@ -261,10 +261,10 @@ func btoi(b []byte) uint64 { return binary.BigEndian.Uint64(b) } -func marshalEvent(event *mautrix.Event) ([]byte, error) { +func marshalEvent(evt *event.Event) ([]byte, error) { var buf bytes.Buffer enc := gzip.NewWriter(&buf) - if err := gob.NewEncoder(enc).Encode(event); err != nil { + if err := gob.NewEncoder(enc).Encode(evt); err != nil { _ = enc.Close() return nil, err } else if err := enc.Close(); err != nil { @@ -273,21 +273,21 @@ func marshalEvent(event *mautrix.Event) ([]byte, error) { return buf.Bytes(), nil } -func unmarshalEvent(data []byte) (*mautrix.Event, error) { - event := &mautrix.Event{} +func unmarshalEvent(data []byte) (*event.Event, error) { + evt := &event.Event{} if cmpReader, err := gzip.NewReader(bytes.NewReader(data)); err != nil { return nil, err - } else if err := gob.NewDecoder(cmpReader).Decode(event); err != nil { + } else if err := gob.NewDecoder(cmpReader).Decode(evt); err != nil { _ = cmpReader.Close() return nil, err } else if err := cmpReader.Close(); err != nil { return nil, err } - return event, nil + return evt, nil } -func put(streams, eventIDs *bolt.Bucket, event *mautrix.Event, key uint64) error { - data, err := marshalEvent(event) +func put(streams, eventIDs *bolt.Bucket, evt *event.Event, key uint64) error { + data, err := marshalEvent(evt) if err != nil { return err } @@ -295,7 +295,7 @@ func put(streams, eventIDs *bolt.Bucket, event *mautrix.Event, key uint64) error if err = streams.Put(keyBytes, data); err != nil { return err } - if err = eventIDs.Put([]byte(event.ID), keyBytes); err != nil { + if err = eventIDs.Put([]byte(evt.ID), keyBytes); err != nil { return err } return nil diff --git a/matrix/matrix.go b/matrix/matrix.go index 6c2809c..7856ac7 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -33,6 +33,7 @@ import ( "time" dbg "runtime/debug" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" "maunium.net/go/mautrix/format" @@ -310,8 +311,8 @@ func (c *Container) SendPreferencesToMatrix() { func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) { room := c.GetOrCreateRoom(evt.RoomID) - var redactedEvt *mautrix.Event - err := c.history.Update(room, evt.Redacts, func(redacted *mautrix.Event) error { + var redactedEvt *event.Event + err := c.history.Update(room, evt.Redacts, func(redacted *event.Event) error { redacted.Unsigned.RedactedBy = evt.ID redacted.Unsigned.RedactedBecause = evt redactedEvt = redacted @@ -344,8 +345,8 @@ func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) { } // HandleMessage is the event handler for the m.room.message timeline event. -func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { - room := c.GetOrCreateRoom(evt.RoomID) +func (c *Container) HandleMessage(source EventSource, mxEvent *mautrix.Event) { + room := c.GetOrCreateRoom(mxEvent.RoomID) if source&EventSourceLeave != 0 { room.HasLeft = true return @@ -353,10 +354,11 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { return } - err := c.history.Append(room, []*mautrix.Event{evt}) + events, err := c.history.Append(room, []*mautrix.Event{mxEvent}) if err != nil { - debug.Printf("Failed to add event %s to history: %v", evt.ID, err) + debug.Printf("Failed to add event %s to history: %v", mxEvent.ID, err) } + evt := events[0] if !c.config.AuthCache.InitialSyncDone { room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000) @@ -372,7 +374,7 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { } if !room.Loaded() { - pushRules := c.PushRules().GetActions(room, evt).Should() + pushRules := c.PushRules().GetActions(room, evt.Event).Should() shouldNotify := pushRules.Notify || !pushRules.NotifySpecified if !shouldNotify { room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000) @@ -388,7 +390,7 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { roomView.AddMessage(message) roomView.MxRoom().LastReceivedMessage = message.Time() if c.syncer.FirstSyncDone { - pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() + pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt.Event).Should() mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) c.ui.Render() } @@ -581,7 +583,7 @@ func (c *Container) MarkRead(roomID, eventID string) { var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") -func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) *mautrix.Event { +func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) *event.Event { content := format.RenderMarkdown(text) content.MsgType = msgtype @@ -590,7 +592,7 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag content.Body = roomRegex.ReplaceAllString(content.Body, "$1") txnID := c.client.TxnID() - localEcho := &mautrix.Event{ + localEcho := event.Wrap(&mautrix.Event{ ID: txnID, Sender: c.config.UserID, Type: mautrix.EventMessage, @@ -599,14 +601,14 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag Content: content, Unsigned: mautrix.Unsigned{ TransactionID: txnID, - OutgoingState: mautrix.EventStateLocalEcho, }, - } + }) + localEcho.Gomuks.OutgoingState = event.StateLocalEcho return localEcho } // SendMarkdownMessage sends a message with the given markdown text to the given room. -func (c *Container) SendEvent(event *mautrix.Event) (string, error) { +func (c *Container) SendEvent(event *event.Event) (string, error) { defer debug.Recover() c.SendTyping(event.RoomID, false) @@ -670,7 +672,7 @@ func (c *Container) LeaveRoom(roomID string) error { } // GetHistory fetches room history. -func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, error) { +func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*event.Event, error) { events, err := c.history.Load(room, limit) if err != nil { return nil, err @@ -683,30 +685,32 @@ func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, e if err != nil { return nil, err } - if len(resp.Chunk) > 0 { - err = c.history.Prepend(room, resp.Chunk) - if err != nil { - return nil, err - } - } debug.Printf("Loaded %d events for %s from server from %s to %s", len(resp.Chunk), room.ID, resp.Start, resp.End) room.PrevBatch = resp.End c.config.Rooms.Put(room) - return resp.Chunk, nil -} - -func (c *Container) GetEvent(room *rooms.Room, eventID string) (*mautrix.Event, error) { - event, err := c.history.Get(room, eventID) - if event != nil || err != nil { - debug.Printf("Found event %s in local cache", eventID) - return event, err + if len(resp.Chunk) == 0 { + return []*event.Event{}, nil } - event, err = c.client.GetEvent(room.ID, eventID) + events, err = c.history.Prepend(room, resp.Chunk) if err != nil { return nil, err } + return events, nil +} + +func (c *Container) GetEvent(room *rooms.Room, eventID string) (*event.Event, error) { + evt, err := c.history.Get(room, eventID) + if evt != nil || err != nil { + debug.Printf("Found event %s in local cache", eventID) + return evt, err + } + mxEvent, err := c.client.GetEvent(room.ID, eventID) + if err != nil { + return nil, err + } + evt = event.Wrap(mxEvent) debug.Printf("Loaded event %s from server", eventID) - return event, nil + return evt, nil } // GetOrCreateRoom gets the room instance stored in the session. 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, }) } diff --git a/ui/room-view.go b/ui/room-view.go index a257f63..344b873 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -27,6 +27,7 @@ import ( "github.com/mattn/go-runewidth" "maunium.net/go/gomuks/debug" + "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mauview" @@ -414,7 +415,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) { view.AddMessage(msg) eventID, err := view.parent.matrix.SendEvent(evt) if err != nil { - msg.State = mautrix.EventStateSendFail + msg.State = event.StateSendFail // Show shorter version if available if httpErr, ok := err.(mautrix.HTTPError); ok { err = httpErr @@ -427,7 +428,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) { } else { debug.Print("Event ID received:", eventID) msg.EventID = eventID - msg.State = mautrix.EventStateDefault + msg.State = event.StateDefault view.MessageView().setMessageID(msg) view.parent.parent.Render() } @@ -465,11 +466,11 @@ func (view *RoomView) AddMessage(message ifc.Message) { view.content.AddMessage(message, AppendMessage) } -func (view *RoomView) parseEvent(evt *mautrix.Event) *messages.UIMessage { +func (view *RoomView) parseEvent(evt *event.Event) *messages.UIMessage { return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt) } -func (view *RoomView) ParseEvent(evt *mautrix.Event) ifc.Message { +func (view *RoomView) ParseEvent(evt *event.Event) ifc.Message { msg := view.parseEvent(evt) if msg == nil { return nil