diff options
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/history.go | 32 | ||||
-rw-r--r-- | matrix/matrix.go | 36 |
2 files changed, 68 insertions, 0 deletions
diff --git a/matrix/history.go b/matrix/history.go index 7275c15..af81ed2 100644 --- a/matrix/history.go +++ b/matrix/history.go @@ -21,6 +21,7 @@ import ( "compress/gzip" "encoding/binary" "encoding/gob" + "errors" sync "github.com/sasha-s/go-deadlock" bolt "go.etcd.io/bbolt" @@ -107,6 +108,37 @@ func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix. return } +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 { + rid := []byte(room.ID) + eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) + if eventIDs == nil { + return nil + } + streamIndex := eventIDs.Get([]byte(eventID)) + if streamIndex == nil { + return nil + } + stream := tx.Bucket(bucketRoomStreams).Bucket(rid) + eventData := stream.Get(streamIndex) + if eventData == nil { + return EventNotFoundError + } + + if event, err := unmarshalEvent(eventData); err != nil { + return err + } else if err = update(event); err != nil { + return err + } else if eventData, err = marshalEvent(event); err != nil { + return err + } else { + return stream.Put(streamIndex, eventData) + } + }) +} + func (hm *HistoryManager) Append(room *rooms.Room, events []*mautrix.Event) error { return hm.store(room, events, true) } diff --git a/matrix/matrix.go b/matrix/matrix.go index 05736ce..9178ca0 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -209,6 +209,7 @@ func (c *Container) OnLogin() { // Just pass encrypted events as messages, they'll show up with an encryption unsupported message. c.syncer.OnEventType(mautrix.EventEncrypted, c.HandleMessage) c.syncer.OnEventType(mautrix.EventSticker, c.HandleMessage) + c.syncer.OnEventType(mautrix.EventRedaction, c.HandleRedaction) c.syncer.OnEventType(mautrix.StateAliases, c.HandleMessage) c.syncer.OnEventType(mautrix.StateCanonicalAlias, c.HandleMessage) c.syncer.OnEventType(mautrix.StateTopic, c.HandleMessage) @@ -307,6 +308,41 @@ 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 { + redacted.Unsigned.RedactedBy = evt.ID + redacted.Unsigned.RedactedBecause = evt + redactedEvt = redacted + return nil + }) + if err != nil { + debug.Print("Failed to mark", evt.Redacts, "as redacted:", err) + } + + if !room.Loaded() { + return + } + + mainView := c.ui.MainView() + + roomView := mainView.GetRoom(evt.RoomID) + if roomView == nil { + debug.Printf("Failed to handle event %v: No room view found.", evt) + return + } + + // TODO make this less hacky? + message := roomView.ParseEvent(redactedEvt) + if message != nil { + roomView.AddMessage(message) + if c.syncer.FirstSyncDone { + c.ui.Render() + } + } +} + // 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) |