From 014897ddc1392f9e3adaba4d482b0ba559efb63a Mon Sep 17 00:00:00 2001 From: ItsNotGoodName Date: Tue, 28 Dec 2021 21:12:37 -0800 Subject: [PATCH] refactor: msg status and service --- app/message.go | 11 +++++------ service/message.go | 40 +++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/app/message.go b/app/message.go index ac97ede9..fda90cc5 100644 --- a/app/message.go +++ b/app/message.go @@ -17,13 +17,12 @@ type Message struct { Status Status `json:"status"` // Status is the status of the message. } -type Status string +type Status uint8 const ( - StatusUnsent Status = "unsent" - StatusPending Status = "pending" - StatusPartial Status = "partial" - StatusSent Status = "sent" + StatusCreated Status = iota + StatusSent + StatusFailed ) func NewMessage(subject, from string, to map[string]bool, text string) *Message { @@ -34,7 +33,7 @@ func NewMessage(subject, from string, to map[string]bool, text string) *Message From: from, To: to, Text: text, - Status: StatusPending, + Status: StatusCreated, } } diff --git a/service/message.go b/service/message.go index cc8a2f1e..81e0d600 100644 --- a/service/message.go +++ b/service/message.go @@ -30,7 +30,7 @@ func (m *Message) Create(subject, from string, to map[string]bool, text string) return nil, err } - return msg, err + return msg, nil } func (m *Message) CreateAttachment(msg *app.Message, name string, data []byte) (*app.Attachment, error) { @@ -59,42 +59,36 @@ func (m *Message) SendBridges(msg *app.Message, bridges []app.Bridge) error { return app.ErrBridgesNotFound } - var errs []error sentCount := 0 for _, bridge := range bridges { emsg := bridge.EndpointMessage(msg) - if !emsg.IsEmpty() { - for _, name := range bridge.Endpoints { - endpoint, err := m.endpointREPO.Get(name) - if err != nil { - return err - } - err = endpoint.Send(emsg) - if err != nil { - errs = append(errs, err) - } else { - sentCount++ - } - } + if emsg.IsEmpty() { + continue } - } - for _, err := range errs { - log.Println("service.Message.SendBridges:", err) + for _, name := range bridge.Endpoints { + endpoint, err := m.endpointREPO.Get(name) + if err != nil { + return err + } + + err = endpoint.Send(emsg) + if err != nil { + log.Println("service.Message.SendBridges:", err) + } else { + sentCount++ + } + } } if sentCount == 0 { - if err := m.UpdateStatus(msg, app.StatusUnsent); err != nil { + if err := m.UpdateStatus(msg, app.StatusFailed); err != nil { return err } return app.ErrEndpointSendFailed } - if len(errs) > 0 { - return m.UpdateStatus(msg, app.StatusPartial) - } - return m.UpdateStatus(msg, app.StatusSent) }