Fix or break some history related things

This commit is contained in:
Tulir Asokan 2019-06-19 22:27:48 +03:00
parent 439d8371db
commit 772f30b2d6
2 changed files with 47 additions and 48 deletions

View File

@ -85,54 +85,58 @@ func (hm *HistoryManager) Close() error {
return hm.db.Close() 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) { func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (evt *event.Event, err error) {
err = hm.db.View(func(tx *bolt.Tx) error { err = hm.db.View(func(tx *bolt.Tx) error {
rid := []byte(room.ID) if stream, index, err := hm.getStreamIndex(tx, []byte(room.ID), []byte(eventID)); err != nil {
eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) return err
if eventIDs == nil { } else if evt, err = hm.getEvent(tx, stream, index); err != nil {
return nil return err
} }
streamIndex := eventIDs.Get([]byte(eventID)) return nil
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 return
} }
var EventNotFoundError = errors.New("event not found")
func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(evt *event.Event) error) 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 { return hm.db.Update(func(tx *bolt.Tx) error {
rid := []byte(room.ID) if stream, index, err := hm.getStreamIndex(tx, []byte(room.ID), []byte(eventID)); err != nil {
eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) return err
if eventIDs == nil { } else if evt, err := hm.getEvent(tx, stream, index); err != 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 {
return err return err
} else if err = update(evt); err != nil { } else if err = update(evt); err != nil {
return err 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 return err
} else {
return stream.Put(streamIndex, eventData)
} }
return nil
}) })
} }

View File

@ -30,8 +30,8 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"time"
dbg "runtime/debug" dbg "runtime/debug"
"time"
"maunium.net/go/gomuks/matrix/event" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
@ -320,15 +320,12 @@ func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) {
}) })
if err != nil { if err != nil {
debug.Print("Failed to mark", evt.Redacts, "as redacted:", err) debug.Print("Failed to mark", evt.Redacts, "as redacted:", err)
} return
} else if !c.config.AuthCache.InitialSyncDone || !room.Loaded() {
if !room.Loaded() || redactedEvt == nil {
return return
} }
mainView := c.ui.MainView() roomView := c.ui.MainView().GetRoom(evt.RoomID)
roomView := mainView.GetRoom(evt.RoomID)
if roomView == nil { if roomView == nil {
debug.Printf("Failed to handle event %v: No room view found.", evt) debug.Printf("Failed to handle event %v: No room view found.", evt)
return return
@ -349,8 +346,8 @@ func (c *Container) HandleEdit(room *rooms.Room, editsID string, editEvent *even
}) })
if err != nil { if err != nil {
debug.Print("Failed to store edit in history db:", err) debug.Print("Failed to store edit in history db:", err)
} return
if !c.config.AuthCache.InitialSyncDone { } else if !c.config.AuthCache.InitialSyncDone || !room.Loaded() {
return return
} }
@ -360,10 +357,6 @@ func (c *Container) HandleEdit(room *rooms.Room, editsID string, editEvent *even
return return
} }
if !room.Loaded() {
return
}
roomView.AddEdit(origEvt) roomView.AddEdit(origEvt)
if c.syncer.FirstSyncDone { if c.syncer.FirstSyncDone {
c.ui.Render() 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) { func (c *Container) GetEvent(room *rooms.Room, eventID string) (*event.Event, error) {
evt, err := c.history.Get(room, eventID) 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) debug.Printf("Found event %s in local cache", eventID)
return evt, err return evt, err
} }