aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2019-06-17 12:27:31 +0300
committerTulir Asokan <tulir@maunium.net>2019-06-17 12:27:31 +0300
commitfe439f076ad3d76c459c9a76c4bbdcda5182ed85 (patch)
treec15399d67bea1661088146e0c28cab0a39a3a4ed /matrix
parent754f8e493df64d6532ace194f3e75c9ccf5bfd95 (diff)
Wrap events in custom struct to add gomuks-specific fields
Diffstat (limited to 'matrix')
-rw-r--r--matrix/event/event.go43
-rw-r--r--matrix/history.go62
-rw-r--r--matrix/matrix.go58
3 files changed, 105 insertions, 58 deletions
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 <https://www.gnu.org/licenses/>.
+
+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
+ if len(resp.Chunk) == 0 {
+ return []*event.Event{}, nil
+ }
+ 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) (*mautrix.Event, error) {
- event, err := c.history.Get(room, eventID)
- if event != nil || err != 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 event, err
+ return evt, err
}
- event, err = c.client.GetEvent(room.ID, eventID)
+ 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.