aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/viewer.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/base/viewer.go')
-rw-r--r--plugins/base/viewer.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/plugins/base/viewer.go b/plugins/base/viewer.go
new file mode 100644
index 0000000..a76ecf9
--- /dev/null
+++ b/plugins/base/viewer.go
@@ -0,0 +1,38 @@
+package koushinbase
+
+import (
+ "fmt"
+
+ "git.sr.ht/~emersion/koushin"
+ "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.
+ ViewMessagePart(*koushin.Context, *IMAPMessage, *message.Entity) (interface{}, error)
+}
+
+var viewers []Viewer
+
+// RegisterViewer registers a message part viewer.
+func RegisterViewer(viewer Viewer) {
+ viewers = append(viewers, viewer)
+}
+
+func viewMessagePart(ctx *koushin.Context, msg *IMAPMessage, part *message.Entity) (interface{}, error) {
+ for _, viewer := range viewers {
+ v, err := viewer.ViewMessagePart(ctx, msg, part)
+ if err == ErrViewUnsupported {
+ continue
+ }
+ return v, err
+ }
+ return nil, ErrViewUnsupported
+}