-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathflags.go
153 lines (129 loc) · 3.3 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package imapsql
import (
"database/sql"
"strings"
"github.com/emersion/go-imap"
)
func (m *Mailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, operation imap.FlagsOp, silent bool, flags []string) error {
defer m.handle.Sync(uid)
seenModified := false
newFlagSet := make([]string, 0, len(flags))
for _, flag := range flags {
if flag == imap.RecentFlag {
continue
}
if flag == imap.SeenFlag {
seenModified = true
}
newFlagSet = append(newFlagSet, flag)
}
flags = newFlagSet
var err error
var addQuery, remQuery *sql.Stmt
switch operation {
case imap.SetFlags, imap.AddFlags:
if len(flags) != 0 {
addQuery, err = m.parent.getFlagsAddStmt(len(flags))
}
case imap.RemoveFlags:
if len(flags) != 0 {
remQuery, err = m.parent.getFlagsRemStmt(len(flags))
}
}
if err != nil {
return wrapErr(err, "UpdateMessagesFlags")
}
tx, err := m.parent.db.BeginLevel(sql.LevelRepeatableRead, false)
if err != nil {
return wrapErr(err, "UpdateMessagesFlags")
}
defer tx.Rollback() // nolint:errcheck
seqset, err = m.handle.ResolveSeq(uid, seqset)
if err != nil {
return err
}
for _, seq := range seqset.Set {
switch operation {
case imap.SetFlags:
_, err = tx.Stmt(m.parent.massClearFlagsUid).Exec(m.id, seq.Start, seq.Stop)
if err != nil {
return err
}
fallthrough
case imap.AddFlags:
if seenModified {
_, err = tx.Stmt(m.parent.setSeenFlagUid).Exec(1, m.id, seq.Start, seq.Stop)
if err != nil {
return err
}
}
if len(flags) == 0 {
continue
}
args := m.makeFlagsAddStmtArgs(flags, seq.Start, seq.Stop)
if _, err := tx.Stmt(addQuery).Exec(args...); err != nil {
return err
}
case imap.RemoveFlags:
if seenModified {
_, err = tx.Stmt(m.parent.setSeenFlagUid).Exec(0, m.id, seq.Start, seq.Stop)
if err != nil {
return err
}
}
if len(flags) == 0 {
continue
}
args := m.makeFlagsRemStmtArgs(flags, seq.Start, seq.Stop)
if _, err := tx.Stmt(remQuery).Exec(args...); err != nil {
return err
}
}
}
// We buffer updates before transaction commit so we
// will not send them if tx.Commit fails.
updatesBuffer, err := m.flagUpdates(tx, uid, seqset)
if err != nil {
return wrapErr(err, "UpdateMessagesFlags")
}
m.parent.Opts.Log.Debugln("UpdateMessageFlags: emitting", len(updatesBuffer), "flag updates")
if err := tx.Commit(); err != nil {
return wrapErr(err, "UpdateMessagesFlags")
}
for _, upd := range updatesBuffer {
m.handle.FlagsChanged(upd.uid, upd.flags, silent)
}
return nil
}
type flagUpdate struct {
uid uint32
flags []string
}
func (m *Mailbox) flagUpdates(tx *sql.Tx, uid bool, seqset *imap.SeqSet) ([]flagUpdate, error) {
var updatesBuffer []flagUpdate
for _, seq := range seqset.Set {
var err error
var rows *sql.Rows
rows, err = tx.Stmt(m.parent.msgFlagsUid).Query(m.id, seq.Start, seq.Stop)
if err != nil {
return nil, err
}
defer rows.Close() // It is fine.
for rows.Next() {
var msgId uint32
var flagsJoined string
if err := rows.Scan(&msgId, &flagsJoined); err != nil {
return nil, err
}
updatesBuffer = append(updatesBuffer, flagUpdate{
uid: msgId,
flags: strings.Split(flagsJoined, flagsSep),
})
}
if err := rows.Err(); err != nil {
return nil, err
}
rows.Close()
}
return updatesBuffer, nil
}