2020-05-13 12:07:44 +00:00
|
|
|
package alpsbase
|
2020-02-12 13:42:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
"git.sr.ht/~emersion/alps"
|
2020-02-12 13:42:51 +00:00
|
|
|
"github.com/emersion/go-message"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrViewUnsupported is returned by Viewer.ViewMessagePart when the message
|
|
|
|
// part isn't supported.
|
|
|
|
var ErrViewUnsupported = fmt.Errorf("cannot generate message view: unsupported part")
|
|
|
|
|
|
|
|
// Viewer is a message part viewer.
|
|
|
|
type Viewer interface {
|
|
|
|
// ViewMessagePart renders a message part. The returned value is displayed
|
|
|
|
// in a template. ErrViewUnsupported is returned if the message part isn't
|
|
|
|
// supported.
|
2020-05-13 12:07:44 +00:00
|
|
|
ViewMessagePart(*alps.Context, *IMAPMessage, *message.Entity) (interface{}, error)
|
2020-02-12 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var viewers []Viewer
|
|
|
|
|
|
|
|
// RegisterViewer registers a message part viewer.
|
|
|
|
func RegisterViewer(viewer Viewer) {
|
|
|
|
viewers = append(viewers, viewer)
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
func viewMessagePart(ctx *alps.Context, msg *IMAPMessage, part *message.Entity) (interface{}, error) {
|
2020-02-12 13:42:51 +00:00
|
|
|
for _, viewer := range viewers {
|
|
|
|
v, err := viewer.ViewMessagePart(ctx, msg, part)
|
|
|
|
if err == ErrViewUnsupported {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
return nil, ErrViewUnsupported
|
|
|
|
}
|