66
77 "github.com/ProtonMail/gluon/imap"
88 "github.com/ProtonMail/gluon/internal/backend/ent"
9- "github.com/ProtonMail/gluon/internal/backend/ent/mailbox"
109 "github.com/ProtonMail/gluon/internal/parser/proto"
1110 "github.com/ProtonMail/gluon/internal/response"
1211 "github.com/ProtonMail/gluon/rfc822"
@@ -16,7 +15,6 @@ import (
1615)
1716
1817type Mailbox struct {
19- tx * ent.Tx
2018 mbox * ent.Mailbox
2119
2220 state * State
@@ -26,9 +24,8 @@ type Mailbox struct {
2624 readOnly bool
2725}
2826
29- func newMailbox (tx * ent. Tx , mbox * ent.Mailbox , state * State , snap * snapshot ) * Mailbox {
27+ func newMailbox (mbox * ent.Mailbox , state * State , snap * snapshot ) * Mailbox {
3028 return & Mailbox {
31- tx : tx ,
3229 mbox : mbox ,
3330
3431 state : state ,
@@ -133,23 +130,32 @@ func (m *Mailbox) Append(ctx context.Context, literal []byte, flags imap.FlagSet
133130
134131 if len (internalID ) > 0 {
135132 msgID := imap .InternalMessageID (internalID )
136- if exists , err := DBHasMessageWithID (ctx , m .tx .Client (), msgID ); err != nil || ! exists {
133+
134+ if exists , err := DBReadResult (ctx , m .state .db , func (ctx context.Context , client * ent.Client ) (bool , error ) {
135+ return DBHasMessageWithID (ctx , client , msgID )
136+ }); err != nil || ! exists {
137137 logrus .WithError (err ).Warn ("The message has an unknown internal ID" )
138- } else if res , err := m .state .actionAddMessagesToMailbox (ctx , m .tx , []MessageIDPair {NewMessageIDPairWithoutRemote (msgID )}, NewMailboxIDPair (m .mbox )); err != nil {
138+ } else if res , err := DBWriteResult (ctx , m .state .db , func (ctx context.Context , tx * ent.Tx ) (map [imap.InternalMessageID ]int , error ) {
139+ return m .state .actionAddMessagesToMailbox (ctx , tx , []MessageIDPair {NewMessageIDPairWithoutRemote (msgID )}, NewMailboxIDPair (m .mbox ))
140+ }); err != nil {
139141 return 0 , err
140142 } else {
141143 return res [msgID ], nil
142144 }
143145 }
144146
145- return m .state .actionCreateMessage (ctx , m .tx , m .snap .mboxID , literal , flags , date )
147+ return DBWriteResult (ctx , m .state .db , func (ctx context.Context , tx * ent.Tx ) (int , error ) {
148+ return m .state .actionCreateMessage (ctx , tx , m .snap .mboxID , literal , flags , date )
149+ })
146150}
147151
148152// Copy copies the messages represented by the given sequence set into the mailbox with the given name.
149153// If the context is a UID context, the sequence set refers to message UIDs.
150154// If no items are copied the response object will be nil.
151155func (m * Mailbox ) Copy (ctx context.Context , seq * proto.SequenceSet , name string ) (response.Item , error ) {
152- mbox , err := m .tx .Mailbox .Query ().Where (mailbox .Name (name )).Only (ctx )
156+ mbox , err := DBReadResult (ctx , m .state .db , func (ctx context.Context , client * ent.Client ) (* ent.Mailbox , error ) {
157+ return DBGetMailboxByName (ctx , client , name )
158+ })
153159 if err != nil {
154160 return nil , ErrNoSuchMailbox
155161 }
@@ -167,7 +173,9 @@ func (m *Mailbox) Copy(ctx context.Context, seq *proto.SequenceSet, name string)
167173 return msg .UID
168174 })
169175
170- destUIDs , err := m .state .actionAddMessagesToMailbox (ctx , m .tx , msgIDs , NewMailboxIDPair (mbox ))
176+ destUIDs , err := DBWriteResult (ctx , m .state .db , func (ctx context.Context , tx * ent.Tx ) (map [imap.InternalMessageID ]int , error ) {
177+ return m .state .actionAddMessagesToMailbox (ctx , tx , msgIDs , NewMailboxIDPair (mbox ))
178+ })
171179 if err != nil {
172180 return nil , err
173181 }
@@ -187,7 +195,9 @@ func (m *Mailbox) Copy(ctx context.Context, seq *proto.SequenceSet, name string)
187195// If the context is a UID context, the sequence set refers to message UIDs.
188196// If no items are moved the response object will be nil.
189197func (m * Mailbox ) Move (ctx context.Context , seq * proto.SequenceSet , name string ) (response.Item , error ) {
190- mbox , err := m .tx .Mailbox .Query ().Where (mailbox .Name (name )).Only (ctx )
198+ mbox , err := DBReadResult (ctx , m .state .db , func (ctx context.Context , client * ent.Client ) (* ent.Mailbox , error ) {
199+ return DBGetMailboxByName (ctx , client , name )
200+ })
191201 if err != nil {
192202 return nil , ErrNoSuchMailbox
193203 }
@@ -205,7 +215,9 @@ func (m *Mailbox) Move(ctx context.Context, seq *proto.SequenceSet, name string)
205215 return msg .UID
206216 })
207217
208- destUIDs , err := m .state .actionMoveMessages (ctx , m .tx , msgIDs , m .snap .mboxID , NewMailboxIDPair (mbox ))
218+ destUIDs , err := DBWriteResult (ctx , m .state .db , func (ctx context.Context , tx * ent.Tx ) (map [imap.InternalMessageID ]int , error ) {
219+ return m .state .actionMoveMessages (ctx , tx , msgIDs , m .snap .mboxID , NewMailboxIDPair (mbox ))
220+ })
209221 if err != nil {
210222 return nil , err
211223 }
@@ -231,24 +243,26 @@ func (m *Mailbox) Store(ctx context.Context, seq *proto.SequenceSet, operation p
231243 return msg .ID
232244 })
233245
234- switch operation {
235- case proto .Operation_Add :
236- if _ , err := m .state .actionAddMessageFlags (ctx , m .tx , msgIDs , flags ); err != nil {
237- return err
246+ return m .state .db .Write (ctx , func (ctx context.Context , tx * ent.Tx ) error {
247+ switch operation {
248+ case proto .Operation_Add :
249+ if _ , err := m .state .actionAddMessageFlags (ctx , tx , msgIDs , flags ); err != nil {
250+ return err
251+ }
252+
253+ case proto .Operation_Remove :
254+ if _ , err := m .state .actionRemoveMessageFlags (ctx , tx , msgIDs , flags ); err != nil {
255+ return err
256+ }
257+
258+ case proto .Operation_Replace :
259+ if err := m .state .actionSetMessageFlags (ctx , tx , msgIDs , flags ); err != nil {
260+ return err
261+ }
238262 }
239263
240- case proto .Operation_Remove :
241- if _ , err := m .state .actionRemoveMessageFlags (ctx , m .tx , msgIDs , flags ); err != nil {
242- return err
243- }
244-
245- case proto .Operation_Replace :
246- if err := m .state .actionSetMessageFlags (ctx , m .tx , msgIDs , flags ); err != nil {
247- return err
248- }
249- }
250-
251- return nil
264+ return nil
265+ })
252266}
253267
254268func (m * Mailbox ) Expunge (ctx context.Context , seq * proto.SequenceSet ) error {
@@ -276,17 +290,21 @@ func (m *Mailbox) expunge(ctx context.Context, messages []*snapMsg) error {
276290 return msg .ID
277291 })
278292
279- return m .state .actionRemoveMessagesFromMailbox (ctx , m .tx , msgIDs , m .snap .mboxID )
293+ return m .state .db .Write (ctx , func (ctx context.Context , tx * ent.Tx ) error {
294+ return m .state .actionRemoveMessagesFromMailbox (ctx , tx , msgIDs , m .snap .mboxID )
295+ })
280296}
281297
282298func (m * Mailbox ) Flush (ctx context.Context , permitExpunge bool ) ([]response.Response , error ) {
283- return m .state .flushResponses (ctx , m .tx , permitExpunge )
299+ return DBWriteResult (ctx , m .state .db , func (ctx context.Context , tx * ent.Tx ) ([]response.Response , error ) {
300+ return m .state .flushResponses (ctx , tx , permitExpunge )
301+ })
284302}
285303
286304func (m * Mailbox ) Close (ctx context.Context ) error {
287305 if err := m .state .deleteConnMetadata (); err != nil {
288306 return err
289307 }
290308
291- return m .state .close (ctx , m . tx )
309+ return m .state .close ()
292310}
0 commit comments