diff --git a/matrix/history.go b/matrix/history.go index fc75e1a..bb480f0 100644 --- a/matrix/history.go +++ b/matrix/history.go @@ -85,54 +85,58 @@ func (hm *HistoryManager) Close() error { return hm.db.Close() } +var ( + EventNotFoundError = errors.New("event not found") + RoomNotFoundError = errors.New("room not found") +) + +func (hm *HistoryManager) getStreamIndex(tx *bolt.Tx, roomID []byte, eventID []byte) (*bolt.Bucket, []byte, error) { + eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(roomID) + if eventIDs == nil { + return nil, nil, RoomNotFoundError + } + index := eventIDs.Get(eventID) + if index == nil { + return nil, nil, EventNotFoundError + } + stream := tx.Bucket(bucketRoomStreams).Bucket(roomID) + return stream, index, nil +} + +func (hm *HistoryManager) getEvent(tx *bolt.Tx, stream *bolt.Bucket, index []byte) (*event.Event, error) { + eventData := stream.Get(index) + if eventData == nil || len(eventData) == 0 { + return nil, EventNotFoundError + } + return unmarshalEvent(eventData) +} + 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) - if eventIDs == nil { - return nil + if stream, index, err := hm.getStreamIndex(tx, []byte(room.ID), []byte(eventID)); err != nil { + return err + } else if evt, err = hm.getEvent(tx, stream, index); err != nil { + return err } - streamIndex := eventIDs.Get([]byte(eventID)) - if streamIndex == nil { - return nil - } - stream := tx.Bucket(bucketRoomStreams).Bucket(rid) - eventData := stream.Get(streamIndex) - var umErr error - evt, umErr = unmarshalEvent(eventData) - return umErr + return nil }) return } -var EventNotFoundError = errors.New("event not found") - 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 { - 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 || len(eventData) == 0 { - return EventNotFoundError - } - - if evt, err := unmarshalEvent(eventData); err != nil { + if stream, index, err := hm.getStreamIndex(tx, []byte(room.ID), []byte(eventID)); err != nil { + return err + } else if evt, err := hm.getEvent(tx, stream, index); err != nil { return err } else if err = update(evt); err != nil { return err - } else if eventData, err = marshalEvent(evt); err != nil { + } else if eventData, err := marshalEvent(evt); err != nil { + return err + } else if err := stream.Put(index, eventData); err != nil { return err - } else { - return stream.Put(streamIndex, eventData) } + return nil }) } diff --git a/matrix/matrix.go b/matrix/matrix.go index fe0bfff..87f372d 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -30,8 +30,8 @@ import ( "path/filepath" "regexp" "runtime" - "time" dbg "runtime/debug" + "time" "maunium.net/go/gomuks/matrix/event" "maunium.net/go/mautrix" @@ -320,15 +320,12 @@ func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) { }) if err != nil { debug.Print("Failed to mark", evt.Redacts, "as redacted:", err) - } - - if !room.Loaded() || redactedEvt == nil { + return + } else if !c.config.AuthCache.InitialSyncDone || !room.Loaded() { return } - mainView := c.ui.MainView() - - roomView := mainView.GetRoom(evt.RoomID) + roomView := c.ui.MainView().GetRoom(evt.RoomID) if roomView == nil { debug.Printf("Failed to handle event %v: No room view found.", evt) return @@ -349,8 +346,8 @@ func (c *Container) HandleEdit(room *rooms.Room, editsID string, editEvent *even }) if err != nil { debug.Print("Failed to store edit in history db:", err) - } - if !c.config.AuthCache.InitialSyncDone { + return + } else if !c.config.AuthCache.InitialSyncDone || !room.Loaded() { return } @@ -360,10 +357,6 @@ func (c *Container) HandleEdit(room *rooms.Room, editsID string, editEvent *even return } - if !room.Loaded() { - return - } - roomView.AddEdit(origEvt) if c.syncer.FirstSyncDone { c.ui.Render() @@ -729,7 +722,9 @@ func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*event.Event, err func (c *Container) GetEvent(room *rooms.Room, eventID string) (*event.Event, error) { evt, err := c.history.Get(room, eventID) - if evt != nil || err != nil { + if err != nil && err != EventNotFoundError { + debug.Printf("Failed to get event %s from local cache: %v", eventID, err) + } else if evt != nil { debug.Printf("Found event %s in local cache", eventID) return evt, err }