Change MarkedMessages to return uids

Especially if one tries to interact with all marked messages there could be
the case that not all headers are fetched yet, hence the messageInfo is still nil.

This segfaults a lot of commands which in principle only need the uid to complete.

If we switch to uids, this issue can be alleviated for those commands.
This commit is contained in:
Reto Brunner 2020-05-09 11:50:31 +02:00 committed by Drew DeVault
parent 381c1fc05f
commit ea2646fc03
5 changed files with 30 additions and 14 deletions

View file

@ -18,12 +18,7 @@ func newHelper(aerc *widgets.Aerc) *helper {
} }
func (h *helper) markedOrSelectedUids() ([]uint32, error) { func (h *helper) markedOrSelectedUids() ([]uint32, error) {
msgs, err := commands.MarkedOrSelected(h.msgProvider) return commands.MarkedOrSelected(h.msgProvider)
if err != nil {
return nil, err
}
uids := commands.UidsFromMessageInfos(msgs)
return uids, nil
} }
func (h *helper) store() (*lib.MessageStore, error) { func (h *helper) store() (*lib.MessageStore, error) {
@ -43,5 +38,13 @@ func (h *helper) account() (*widgets.AccountView, error) {
} }
func (h *helper) messages() ([]*models.MessageInfo, error) { func (h *helper) messages() ([]*models.MessageInfo, error) {
return commands.MarkedOrSelected(h.msgProvider) uid, err := commands.MarkedOrSelected(h.msgProvider)
if err != nil {
return nil, err
}
store, err := h.store()
if err != nil {
return nil, err
}
return commands.MsgInfoFromUids(store, uid)
} }

View file

@ -10,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/models" "git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -152,7 +153,7 @@ func listDir(path string, hidden bool) []string {
// MarkedOrSelected returns either all marked messages if any are marked or the // MarkedOrSelected returns either all marked messages if any are marked or the
// selected message instead // selected message instead
func MarkedOrSelected(pm widgets.ProvidesMessages) ([]*models.MessageInfo, error) { func MarkedOrSelected(pm widgets.ProvidesMessages) ([]uint32, error) {
// marked has priority over the selected message // marked has priority over the selected message
marked, err := pm.MarkedMessages() marked, err := pm.MarkedMessages()
if err != nil { if err != nil {
@ -165,7 +166,7 @@ func MarkedOrSelected(pm widgets.ProvidesMessages) ([]*models.MessageInfo, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
return []*models.MessageInfo{msg}, nil return []uint32{msg.Uid}, nil
} }
// UidsFromMessageInfos extracts a uid slice from a slice of MessageInfos // UidsFromMessageInfos extracts a uid slice from a slice of MessageInfos
@ -178,3 +179,15 @@ func UidsFromMessageInfos(msgs []*models.MessageInfo) []uint32 {
} }
return uids return uids
} }
func MsgInfoFromUids(store *lib.MessageStore, uids []uint32) ([]*models.MessageInfo, error) {
infos := make([]*models.MessageInfo, len(uids))
for i, uid := range uids {
var ok bool
infos[i], ok = store.Messages[uid]
if !ok {
return nil, fmt.Errorf("uid not found")
}
}
return infos, nil
}

View file

@ -215,9 +215,9 @@ func (acct *AccountView) SelectedMessage() (*models.MessageInfo, error) {
return msg, nil return msg, nil
} }
func (acct *AccountView) MarkedMessages() ([]*models.MessageInfo, error) { func (acct *AccountView) MarkedMessages() ([]uint32, error) {
store := acct.Store() store := acct.Store()
return msgInfoFromUids(store, store.Marked()) return store.Marked(), nil
} }
func (acct *AccountView) SelectedMessagePart() *PartInfo { func (acct *AccountView) SelectedMessagePart() *PartInfo {

View file

@ -262,9 +262,9 @@ func (mv *MessageViewer) SelectedMessage() (*models.MessageInfo, error) {
return mv.msg.MessageInfo(), nil return mv.msg.MessageInfo(), nil
} }
func (mv *MessageViewer) MarkedMessages() ([]*models.MessageInfo, error) { func (mv *MessageViewer) MarkedMessages() ([]uint32, error) {
store := mv.Store() store := mv.Store()
return msgInfoFromUids(store, store.Marked()) return store.Marked(), nil
} }
func (mv *MessageViewer) ToggleHeaders() { func (mv *MessageViewer) ToggleHeaders() {

View file

@ -25,5 +25,5 @@ type ProvidesMessages interface {
Store() *lib.MessageStore Store() *lib.MessageStore
SelectedAccount() *AccountView SelectedAccount() *AccountView
SelectedMessage() (*models.MessageInfo, error) SelectedMessage() (*models.MessageInfo, error)
MarkedMessages() ([]*models.MessageInfo, error) MarkedMessages() ([]uint32, error)
} }