forked from Silverfish/proton-bridge
GODT-2223: Ensure apiLabels map is properly up to date
This commit is contained in:
committed by
Leander Beernaert
parent
c8925cd270
commit
59745e6fb6
@ -366,7 +366,10 @@ func (user *User) handleUpdateLabelEvent(ctx context.Context, event proton.Label
|
|||||||
"name": logging.Sensitive(event.Label.Name),
|
"name": logging.Sensitive(event.Label.Name),
|
||||||
}).Info("Handling label updated event")
|
}).Info("Handling label updated event")
|
||||||
|
|
||||||
user.apiLabels[event.Label.ID] = event.Label
|
// Only update the label if it exists; we don't want to create it as a client may have just deleted it.
|
||||||
|
if _, ok := user.apiLabels[event.Label.ID]; ok {
|
||||||
|
user.apiLabels[event.Label.ID] = event.Label
|
||||||
|
}
|
||||||
|
|
||||||
for _, updateCh := range xslices.Unique(maps.Values(user.updateCh)) {
|
for _, updateCh := range xslices.Unique(maps.Values(user.updateCh)) {
|
||||||
update := imap.NewMailboxUpdated(
|
update := imap.NewMailboxUpdated(
|
||||||
|
|||||||
@ -167,22 +167,24 @@ func (conn *imapConnector) createFolder(ctx context.Context, name []string) (ima
|
|||||||
|
|
||||||
// UpdateMailboxName sets the name of the label with the given ID.
|
// UpdateMailboxName sets the name of the label with the given ID.
|
||||||
func (conn *imapConnector) UpdateMailboxName(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
func (conn *imapConnector) UpdateMailboxName(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
||||||
defer conn.goPollAPIEvents(false)
|
return safe.LockRet(func() error {
|
||||||
|
defer conn.goPollAPIEvents(false)
|
||||||
|
|
||||||
if len(name) < 2 {
|
if len(name) < 2 {
|
||||||
return fmt.Errorf("invalid mailbox name %q", name)
|
return fmt.Errorf("invalid mailbox name %q", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch name[0] {
|
switch name[0] {
|
||||||
case folderPrefix:
|
case folderPrefix:
|
||||||
return conn.updateFolder(ctx, labelID, name[1:])
|
return conn.updateFolder(ctx, labelID, name[1:])
|
||||||
|
|
||||||
case labelPrefix:
|
case labelPrefix:
|
||||||
return conn.updateLabel(ctx, labelID, name[1:])
|
return conn.updateLabel(ctx, labelID, name[1:])
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid mailbox name %q", name)
|
return fmt.Errorf("invalid mailbox name %q", name)
|
||||||
}
|
}
|
||||||
|
}, conn.apiLabelsLock)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *imapConnector) updateLabel(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
func (conn *imapConnector) updateLabel(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
||||||
@ -195,18 +197,21 @@ func (conn *imapConnector) updateLabel(ctx context.Context, labelID imap.Mailbox
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := conn.client.UpdateLabel(ctx, label.ID, proton.UpdateLabelReq{
|
update, err := conn.client.UpdateLabel(ctx, label.ID, proton.UpdateLabelReq{
|
||||||
Name: name[0],
|
Name: name[0],
|
||||||
Color: label.Color,
|
Color: label.Color,
|
||||||
}); err != nil {
|
})
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.apiLabels[label.ID] = update
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *imapConnector) updateFolder(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
func (conn *imapConnector) updateFolder(ctx context.Context, labelID imap.MailboxID, name []string) error {
|
||||||
return safe.RLockRet(func() error {
|
return safe.LockRet(func() error {
|
||||||
var parentID string
|
var parentID string
|
||||||
|
|
||||||
if len(name) > 1 {
|
if len(name) > 1 {
|
||||||
@ -230,23 +235,34 @@ func (conn *imapConnector) updateFolder(ctx context.Context, labelID imap.Mailbo
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := conn.client.UpdateLabel(ctx, string(labelID), proton.UpdateLabelReq{
|
update, err := conn.client.UpdateLabel(ctx, string(labelID), proton.UpdateLabelReq{
|
||||||
Name: name[len(name)-1],
|
Name: name[len(name)-1],
|
||||||
Color: label.Color,
|
Color: label.Color,
|
||||||
ParentID: parentID,
|
ParentID: parentID,
|
||||||
}); err != nil {
|
})
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.apiLabels[label.ID] = update
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}, conn.apiLabelsLock)
|
}, conn.apiLabelsLock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMailbox deletes the label with the given ID.
|
// DeleteMailbox deletes the label with the given ID.
|
||||||
func (conn *imapConnector) DeleteMailbox(ctx context.Context, labelID imap.MailboxID) error {
|
func (conn *imapConnector) DeleteMailbox(ctx context.Context, labelID imap.MailboxID) error {
|
||||||
defer conn.goPollAPIEvents(false)
|
return safe.LockRet(func() error {
|
||||||
|
defer conn.goPollAPIEvents(false)
|
||||||
|
|
||||||
return conn.client.DeleteLabel(ctx, string(labelID))
|
if err := conn.client.DeleteLabel(ctx, string(labelID)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(conn.apiLabels, string(labelID))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, conn.apiLabelsLock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateMessage creates a new message on the remote.
|
// CreateMessage creates a new message on the remote.
|
||||||
|
|||||||
Reference in New Issue
Block a user