From d1d129f6cffb909516a29f9bdb0da6f13e65dcaf Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Jun 2019 20:42:13 +0300 Subject: Add support for redactions. Fixes #19 --- matrix/matrix.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'matrix/matrix.go') 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) -- cgit v1.2.3