Repository: inbucket/inbucket
Pull request #333 (2023-02-13 21:34:16)

Replace message.Metadata usage with event.MessageMetadata
Required for #332



Patch:
diff --git a/pkg/extension/event/events.go b/pkg/extension/event/events.go
index 38fcd61..429e9d5 100644
--- a/pkg/extension/event/events.go
+++ b/pkg/extension/event/events.go
@@ -20,4 +20,5 @@ type MessageMetadata struct {
 	Date    time.Time
 	Subject string
 	Size    int64
+	Seen    bool
 }
diff --git a/pkg/message/manager.go b/pkg/message/manager.go
index 8cdb66a..b62f268 100644
--- a/pkg/message/manager.go
+++ b/pkg/message/manager.go
@@ -24,7 +24,7 @@ type Manager interface {
 		prefix string,
 		content []byte,
 	) (id string, err error)
-	GetMetadata(mailbox string) ([]*Metadata, error)
+	GetMetadata(mailbox string) ([]*event.MessageMetadata, error)
 	GetMessage(mailbox, id string) (*Message, error)
 	MarkSeen(mailbox, id string) error
 	PurgeMessages(mailbox string) error
@@ -68,7 +68,7 @@ func (s *StoreManager) Deliver(
 
 	log.Debug().Str("module", "message").Str("mailbox", to.Mailbox).Msg("Delivering message")
 	delivery := &Delivery{
-		Meta: Metadata{
+		Meta: event.MessageMetadata{
 			Mailbox: to.Mailbox,
 			From:    fromaddr[0],
 			To:      toaddr,
@@ -83,27 +83,20 @@ func (s *StoreManager) Deliver(
 	}
 
 	// Emit message stored event.
-	event := event.MessageMetadata{
-		Mailbox: to.Mailbox,
-		ID:      id,
-		From:    delivery.From(),
-		To:      delivery.To(),
-		Subject: delivery.Subject(),
-		Date:    delivery.Date(),
-		Size:    delivery.Size(),
-	}
+	event := delivery.Meta
+	event.ID = id
 	go s.ExtHost.Events.AfterMessageStored.Emit(&event)
 
 	return id, nil
 }
 
 // GetMetadata returns a slice of metadata for the specified mailbox.
-func (s *StoreManager) GetMetadata(mailbox string) ([]*Metadata, error) {
+func (s *StoreManager) GetMetadata(mailbox string) ([]*event.MessageMetadata, error) {
 	messages, err := s.Store.GetMessages(mailbox)
 	if err != nil {
 		return nil, err
 	}
-	metas := make([]*Metadata, len(messages))
+	metas := make([]*event.MessageMetadata, len(messages))
 	for i, sm := range messages {
 		metas[i] = makeMetadata(sm)
 	}
@@ -126,7 +119,7 @@ func (s *StoreManager) GetMessage(mailbox, id string) (*Message, error) {
 	}
 	_ = r.Close()
 	header := makeMetadata(sm)
-	return &Message{Metadata: *header, env: env}, nil
+	return &Message{MessageMetadata: *header, env: env}, nil
 }
 
 // MarkSeen marks the message as having been read.
@@ -161,8 +154,8 @@ func (s *StoreManager) MailboxForAddress(mailbox string) (string, error) {
 }
 
 // makeMetadata populates Metadata from a storage.Message.
-func makeMetadata(m storage.Message) *Metadata {
-	return &Metadata{
+func makeMetadata(m storage.Message) *event.MessageMetadata {
+	return &event.MessageMetadata{
 		Mailbox: m.Mailbox(),
 		ID:      m.ID(),
 		From:    m.From(),
diff --git a/pkg/message/message.go b/pkg/message/message.go
index acbc361..bb64e99 100644
--- a/pkg/message/message.go
+++ b/pkg/message/message.go
@@ -8,33 +8,22 @@ import (
 	"net/textproto"
 	"time"
 
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/storage"
 	"github.com/jhillyerd/enmime"
 )
 
-// Metadata holds information about a message, but not the content.
-type Metadata struct {
-	Mailbox string
-	ID      string
-	From    *mail.Address
-	To      []*mail.Address
-	Date    time.Time
-	Subject string
-	Size    int64
-	Seen    bool
-}
-
 // Message holds both the metadata and content of a message.
 type Message struct {
-	Metadata
+	event.MessageMetadata
 	env *enmime.Envelope
 }
 
 // New constructs a new Message
-func New(m Metadata, e *enmime.Envelope) *Message {
+func New(m event.MessageMetadata, e *enmime.Envelope) *Message {
 	return &Message{
-		Metadata: m,
-		env:      e,
+		MessageMetadata: m,
+		env:             e,
 	}
 }
 
@@ -65,7 +54,7 @@ func (m *Message) Text() string {
 
 // Delivery is used to add a message to storage.
 type Delivery struct {
-	Meta   Metadata
+	Meta   event.MessageMetadata
 	Reader io.Reader
 }
 


Test patch:
diff --git a/pkg/rest/apiv1_controller_test.go b/pkg/rest/apiv1_controller_test.go
index 8444edc..fc7c9b8 100644
--- a/pkg/rest/apiv1_controller_test.go
+++ b/pkg/rest/apiv1_controller_test.go
@@ -9,6 +9,7 @@ import (
 	"testing"
 	"time"
 
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/message"
 	"github.com/inbucket/inbucket/pkg/test"
 	"github.com/jhillyerd/enmime"
@@ -67,7 +68,7 @@ func TestRestMailboxList(t *testing.T) {
 	// Test JSON message headers
 	tzPDT := time.FixedZone("PDT", -7*3600)
 	tzPST := time.FixedZone("PST", -8*3600)
-	meta1 := message.Metadata{
+	meta1 := event.MessageMetadata{
 		Mailbox: "good",
 		ID:      "0001",
 		From:    &mail.Address{Name: "", Address: "from1@host"},
@@ -75,7 +76,7 @@ func TestRestMailboxList(t *testing.T) {
 		Subject: "subject 1",
 		Date:    time.Date(2012, 2, 1, 10, 11, 12, 253, tzPST),
 	}
-	meta2 := message.Metadata{
+	meta2 := event.MessageMetadata{
 		Mailbox: "good",
 		ID:      "0002",
 		From:    &mail.Address{Name: "", Address: "from2@host"},
@@ -83,8 +84,8 @@ func TestRestMailboxList(t *testing.T) {
 		Subject: "subject 2",
 		Date:    time.Date(2012, 7, 1, 10, 11, 12, 253, tzPDT),
 	}
-	mm.AddMessage("good", &message.Message{Metadata: meta1})
-	mm.AddMessage("good", &message.Message{Metadata: meta2})
+	mm.AddMessage("good", &message.Message{MessageMetadata: meta1})
+	mm.AddMessage("good", &message.Message{MessageMetadata: meta2})
 
 	// Check return code
 	w, err = testRestGet("http://localhost/api/v1/mailbox/good")
@@ -178,7 +179,7 @@ func TestRestMessage(t *testing.T) {
 	// Test JSON message headers
 	tzPST := time.FixedZone("PST", -8*3600)
 	msg1 := message.New(
-		message.Metadata{
+		event.MessageMetadata{
 			Mailbox: "good",
 			ID:      "0001",
 			From:    &mail.Address{Name: "", Address: "from1@host"},
@@ -254,7 +255,7 @@ func TestRestMarkSeen(t *testing.T) {
 	// Create some messages.
 	tzPDT := time.FixedZone("PDT", -7*3600)
 	tzPST := time.FixedZone("PST", -8*3600)
-	meta1 := message.Metadata{
+	meta1 := event.MessageMetadata{
 		Mailbox: "good",
 		ID:      "0001",
 		From:    &mail.Address{Name: "", Address: "from1@host"},
@@ -262,7 +263,7 @@ func TestRestMarkSeen(t *testing.T) {
 		Subject: "subject 1",
 		Date:    time.Date(2012, 2, 1, 10, 11, 12, 253, tzPST),
 	}
-	meta2 := message.Metadata{
+	meta2 := event.MessageMetadata{
 		Mailbox: "good",
 		ID:      "0002",
 		From:    &mail.Address{Name: "", Address: "from2@host"},
@@ -270,8 +271,8 @@ func TestRestMarkSeen(t *testing.T) {
 		Subject: "subject 2",
 		Date:    time.Date(2012, 7, 1, 10, 11, 12, 253, tzPDT),
 	}
-	mm.AddMessage("good", &message.Message{Metadata: meta1})
-	mm.AddMessage("good", &message.Message{Metadata: meta2})
+	mm.AddMessage("good", &message.Message{MessageMetadata: meta1})
+	mm.AddMessage("good", &message.Message{MessageMetadata: meta2})
 	// Mark one read.
 	w, err := testRestPatch("http://localhost/api/v1/mailbox/good/0002", `{"seen":true}`)
 	expectCode := 200
diff --git a/pkg/storage/file/fstore_test.go b/pkg/storage/file/fstore_test.go
index 53c438f..71c6c48 100644
--- a/pkg/storage/file/fstore_test.go
+++ b/pkg/storage/file/fstore_test.go
@@ -14,6 +14,7 @@ import (
 	"time"
 
 	"github.com/inbucket/inbucket/pkg/config"
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/message"
 	"github.com/inbucket/inbucket/pkg/storage"
 	"github.com/inbucket/inbucket/pkg/test"
@@ -211,7 +212,7 @@ func setupDataStore(cfg config.Storage) (*Store, *bytes.Buffer) {
 // the size of the generated message.
 func deliverMessage(ds *Store, mbName string, subject string, date time.Time) (string, int64) {
 	// Build message for delivery
-	meta := message.Metadata{
+	meta := event.MessageMetadata{
 		Mailbox: mbName,
 		To:      []*mail.Address{{Name: "", Address: "somebody@host"}},
 		From:    &mail.Address{Name: "", Address: "somebodyelse@host"},
diff --git a/pkg/storage/retention_test.go b/pkg/storage/retention_test.go
index b9a6e57..3f2b55a 100644
--- a/pkg/storage/retention_test.go
+++ b/pkg/storage/retention_test.go
@@ -7,6 +7,7 @@ import (
 	"time"
 
 	"github.com/inbucket/inbucket/pkg/config"
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/message"
 	"github.com/inbucket/inbucket/pkg/storage"
 	"github.com/inbucket/inbucket/pkg/test"
@@ -59,7 +60,7 @@ func TestDoRetentionScan(t *testing.T) {
 // stubMessage creates a message stub of a specific age
 func stubMessage(mailbox string, ageHours int) storage.Message {
 	return &message.Delivery{
-		Meta: message.Metadata{
+		Meta: event.MessageMetadata{
 			Mailbox: mailbox,
 			ID:      fmt.Sprintf("MSG[age=%vh]", ageHours),
 			Date:    time.Now().Add(time.Duration(ageHours*-1) * time.Hour),
diff --git a/pkg/test/manager.go b/pkg/test/manager.go
index a1df3d8..bef8d8e 100644
--- a/pkg/test/manager.go
+++ b/pkg/test/manager.go
@@ -4,6 +4,7 @@ import (
 	"errors"
 
 	"github.com/inbucket/inbucket/pkg/config"
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/message"
 	"github.com/inbucket/inbucket/pkg/policy"
 	"github.com/inbucket/inbucket/pkg/storage"
@@ -42,14 +43,14 @@ func (m *ManagerStub) GetMessage(mailbox, id string) (*message.Message, error) {
 }
 
 // GetMetadata gets all the metadata for the specified mailbox.
-func (m *ManagerStub) GetMetadata(mailbox string) ([]*message.Metadata, error) {
+func (m *ManagerStub) GetMetadata(mailbox string) ([]*event.MessageMetadata, error) {
 	if mailbox == "messageserr" {
 		return nil, errors.New("internal error")
 	}
 	messages := m.mailboxes[mailbox]
-	metas := make([]*message.Metadata, len(messages))
+	metas := make([]*event.MessageMetadata, len(messages))
 	for i, msg := range messages {
-		metas[i] = &msg.Metadata
+		metas[i] = &msg.MessageMetadata
 	}
 	return metas, nil
 }
@@ -69,7 +70,7 @@ func (m *ManagerStub) MarkSeen(mailbox, id string) error {
 	}
 	for _, msg := range m.mailboxes[mailbox] {
 		if msg.ID == id {
-			msg.Metadata.Seen = true
+			msg.MessageMetadata.Seen = true
 			return nil
 		}
 	}
diff --git a/pkg/test/storage_suite.go b/pkg/test/storage_suite.go
index 8d995fe..3befcdd 100644
--- a/pkg/test/storage_suite.go
+++ b/pkg/test/storage_suite.go
@@ -10,6 +10,7 @@ import (
 	"time"
 
 	"github.com/inbucket/inbucket/pkg/config"
+	"github.com/inbucket/inbucket/pkg/extension/event"
 	"github.com/inbucket/inbucket/pkg/message"
 	"github.com/inbucket/inbucket/pkg/storage"
 )
@@ -61,7 +62,7 @@ func testMetadata(t *testing.T, store storage.Store) {
 	subject := "fantastic test subject line"
 	content := "doesn't matter"
 	delivery := &message.Delivery{
-		Meta: message.Metadata{
+		Meta: event.MessageMetadata{
 			// ID and Size will be determined by the Store.
 			Mailbox: mailbox,
 			From:    from,
@@ -130,7 +131,7 @@ func testContent(t *testing.T, store storage.Store) {
 	date := time.Now()
 	subject := "fantastic test subject line"
 	delivery := &message.Delivery{
-		Meta: message.Metadata{
+		Meta: event.MessageMetadata{
 			// ID and Size will be determined by the Store.
 			Mailbox: mailbox,
 			From:    from,
@@ -408,7 +409,7 @@ func DeliverToStore(
 	date time.Time,
 ) (string, int64) {
 	t.Helper()
-	meta := message.Metadata{
+	meta := event.MessageMetadata{
 		Mailbox: mailbox,
 		To:      []*mail.Address{{Name: "Some Body", Address: "somebody@host"}},
 		From:    &mail.Address{Name: "Some B. Else", Address: "somebodyelse@host"},