db.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package db
  26. import (
  27. "context"
  28. "crypto/sha1"
  29. "encoding/hex"
  30. "errors"
  31. "fmt"
  32. "log"
  33. "os"
  34. "strings"
  35. "sync"
  36. "time"
  37. common "git.semlanik.org/semlanik/gostfix/common"
  38. "git.semlanik.org/semlanik/gostfix/utils"
  39. "github.com/semlanik/berkeleydb"
  40. bcrypt "golang.org/x/crypto/bcrypt"
  41. bson "go.mongodb.org/mongo-driver/bson"
  42. "go.mongodb.org/mongo-driver/bson/primitive"
  43. mongo "go.mongodb.org/mongo-driver/mongo"
  44. options "go.mongodb.org/mongo-driver/mongo/options"
  45. config "git.semlanik.org/semlanik/gostfix/config"
  46. )
  47. type StuctNotifiers struct {
  48. notifiers []common.Notifier
  49. notifiersLock sync.Mutex
  50. }
  51. var notifiers StuctNotifiers = StuctNotifiers{
  52. notifiers: []common.Notifier{},
  53. }
  54. type Storage struct {
  55. db *mongo.Database
  56. usersCollection *mongo.Collection
  57. tokensCollection *mongo.Collection
  58. emailsCollection *mongo.Collection
  59. allEmailsCollection *mongo.Collection
  60. }
  61. func qualifiedMailCollection(user string) string {
  62. sum := sha1.Sum([]byte(user))
  63. return "mb" + hex.EncodeToString(sum[:])
  64. }
  65. func NewStorage() (s *Storage, err error) {
  66. fullUrl := "mongodb://"
  67. if config.ConfigInstance().MongoUser != "" {
  68. fullUrl += config.ConfigInstance().MongoUser
  69. if config.ConfigInstance().MongoPassword != "" {
  70. fullUrl += ":" + config.ConfigInstance().MongoPassword
  71. }
  72. fullUrl += "@"
  73. }
  74. fullUrl += config.ConfigInstance().MongoAddress
  75. client, err := mongo.NewClient(options.Client().ApplyURI(fullUrl))
  76. if err != nil {
  77. return nil, err
  78. }
  79. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  80. defer cancel()
  81. err = client.Connect(ctx)
  82. if err != nil {
  83. return nil, err
  84. }
  85. db := client.Database("gostfix")
  86. index := mongo.IndexModel{
  87. Keys: bson.M{
  88. "user": 1,
  89. },
  90. Options: options.Index().SetUnique(true),
  91. }
  92. s = &Storage{
  93. db: db,
  94. usersCollection: db.Collection("users"),
  95. tokensCollection: db.Collection("tokens"),
  96. emailsCollection: db.Collection("emails"),
  97. allEmailsCollection: db.Collection("allEmails"),
  98. }
  99. //Initial database setup
  100. s.usersCollection.Indexes().CreateOne(context.Background(), index)
  101. s.tokensCollection.Indexes().CreateOne(context.Background(), index)
  102. s.emailsCollection.Indexes().CreateOne(context.Background(), index)
  103. return
  104. }
  105. func (s *Storage) AddUser(user, password, fullName string) error {
  106. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  107. if err != nil {
  108. return err
  109. }
  110. hashString := string(hash)
  111. userInfo := bson.M{
  112. "user": user,
  113. "password": hashString,
  114. "fullName": fullName,
  115. }
  116. _, err = s.usersCollection.InsertOne(context.Background(), userInfo)
  117. if err != nil {
  118. return err
  119. }
  120. err = s.addEmail(user, user, true)
  121. if err != nil {
  122. s.usersCollection.DeleteOne(context.Background(), bson.M{"user": user})
  123. return err
  124. }
  125. return nil
  126. }
  127. func (s *Storage) UpdateUser(user, password, fullName string) error {
  128. userInfo := bson.M{}
  129. if len(password) > 0 && len(password) < 128 {
  130. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  131. if err != nil {
  132. return err
  133. }
  134. hashString := string(hash)
  135. userInfo["password"] = hashString
  136. }
  137. if len(fullName) > 0 && len(fullName) < 128 && utils.RegExpUtilsInstance().FullNameChecker.MatchString(fullName) {
  138. userInfo["fullName"] = fullName
  139. }
  140. if len(userInfo) > 0 {
  141. _, err := s.usersCollection.UpdateOne(context.Background(), bson.M{"user": user}, bson.M{"$set": userInfo})
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }
  148. func (s *Storage) AddEmail(user string, email string) error {
  149. return s.addEmail(user, email, false)
  150. }
  151. func (s *Storage) addEmail(user string, email string, upsert bool) error {
  152. result := struct {
  153. User string
  154. }{}
  155. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  156. if err != nil {
  157. return err
  158. }
  159. emails, err := s.GetAllEmails()
  160. if err != nil {
  161. return err
  162. }
  163. for _, existingEmail := range emails {
  164. if existingEmail == email {
  165. return errors.New("Email exists")
  166. }
  167. }
  168. emailParts := strings.Split(email, "@")
  169. if len(emailParts) != 2 {
  170. return errors.New("Invalid email format")
  171. }
  172. db, err := berkeleydb.NewDB()
  173. if err != nil {
  174. log.Fatal(err)
  175. }
  176. err = db.Open(config.ConfigInstance().VMailboxMaps, berkeleydb.DbHash, 0)
  177. if err != nil {
  178. log.Fatalf("Unable to open virtual mailbox maps %s %s\n", config.ConfigInstance().VMailboxMaps, err)
  179. }
  180. defer db.Close()
  181. err = db.Put(email, emailParts[1]+"/"+emailParts[0])
  182. if err != nil {
  183. return errors.New("Unable to add email to maps" + err.Error())
  184. }
  185. _, err = s.emailsCollection.UpdateOne(context.Background(),
  186. bson.M{"user": user},
  187. bson.M{"$addToSet": bson.M{"email": email}},
  188. options.Update().SetUpsert(upsert))
  189. return err
  190. }
  191. func (s *Storage) RemoveEmail(user string, email string) error {
  192. log.Printf("User %s removes email %s", user, email)
  193. result := s.emailsCollection.FindOne(context.Background(), bson.M{
  194. "user": user,
  195. "email": email,
  196. })
  197. if result.Err() != nil {
  198. return result.Err()
  199. }
  200. db, err := berkeleydb.NewDB()
  201. if err != nil {
  202. log.Fatal(err)
  203. }
  204. err = db.Open(config.ConfigInstance().VMailboxMaps, berkeleydb.DbHash, 0)
  205. if err != nil {
  206. log.Fatalf("Unable to open virtual mailbox maps %s %s\n", config.ConfigInstance().VMailboxMaps, err)
  207. }
  208. defer db.Close()
  209. err = db.Delete(email)
  210. if err != nil {
  211. return errors.New("Unable to remove email from maps" + err.Error())
  212. }
  213. err = s.cleanupAttachments(user, email)
  214. if err != nil {
  215. log.Printf("Unable to cleanup attachments for %s %s\n", email, err)
  216. }
  217. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  218. mailsCollection.DeleteMany(context.Background(), bson.M{"email": email})
  219. _, err = s.emailsCollection.UpdateOne(context.Background(),
  220. bson.M{"user": user},
  221. bson.M{"$pull": bson.M{"email": email}})
  222. return err
  223. }
  224. func (s *Storage) SaveMail(email, folder string, m *common.Mail, read bool) error {
  225. user := &struct {
  226. User string
  227. }{}
  228. s.emailsCollection.FindOne(context.Background(), bson.M{"email": email}).Decode(user)
  229. mailsCollection := s.db.Collection(qualifiedMailCollection(user.User))
  230. result, err := mailsCollection.InsertOne(context.Background(), &struct {
  231. Email string
  232. Mail *common.Mail
  233. Folder string
  234. Read bool
  235. Trash bool
  236. }{
  237. Email: email,
  238. Mail: m,
  239. Folder: folder,
  240. Read: read,
  241. Trash: false,
  242. }, options.InsertOne().SetBypassDocumentValidation(true))
  243. if err != nil {
  244. return err
  245. }
  246. mail := *m //deep copy for multithreading
  247. s.notifyNewMail(email, common.MailMetadata{
  248. Id: result.InsertedID.(primitive.ObjectID).Hex(),
  249. Read: false,
  250. Trash: false,
  251. Folder: folder,
  252. User: user.User,
  253. Mail: &mail,
  254. })
  255. stats, err := s.GetEmailStats(user.User, email, folder)
  256. if err == nil {
  257. s.notifyMailboxUpdate(email, []common.FolderStat{stats})
  258. }
  259. return nil
  260. }
  261. func (s *Storage) DeleteMail(user string, mailId string) error {
  262. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  263. oId, err := primitive.ObjectIDFromHex(mailId)
  264. if err != nil {
  265. return err
  266. }
  267. var result common.MailMetadata
  268. err = mailsCollection.FindOne(context.Background(), bson.M{"_id": oId}).Decode(&result)
  269. if err != nil {
  270. return err
  271. }
  272. for _, attachment := range result.Mail.Body.Attachments {
  273. removeAttachment(attachment.Id)
  274. }
  275. _, err = mailsCollection.DeleteOne(context.Background(), bson.M{"_id": oId})
  276. stats, errTemp := s.GetEmailStats(user, result.Email, common.Trash)
  277. if errTemp == nil {
  278. s.notifyMailboxUpdate(result.Email, []common.FolderStat{stats})
  279. }
  280. return err
  281. }
  282. func (s *Storage) GetMailList(user, email, folder string, frame common.Frame) ([]*common.MailMetadata, error) {
  283. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  284. matchFilter := bson.M{"email": email}
  285. if folder == common.Trash {
  286. matchFilter["$or"] = bson.A{
  287. bson.M{"trash": true},
  288. bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
  289. }
  290. } else {
  291. matchFilter["folder"] = folder
  292. matchFilter["$or"] = bson.A{
  293. bson.M{"trash": false},
  294. bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
  295. }
  296. }
  297. request := bson.A{
  298. bson.M{"$match": matchFilter},
  299. bson.M{"$sort": bson.M{"mail.header.date": -1}},
  300. }
  301. if frame.Skip > 0 {
  302. request = append(request, bson.M{"$skip": frame.Skip})
  303. }
  304. fmt.Printf("Trying limit number of mails: %v\n", frame)
  305. if frame.Limit > 0 {
  306. fmt.Printf("Limit number of mails: %v\n", frame)
  307. request = append(request, bson.M{"$limit": frame.Limit})
  308. }
  309. cur, err := mailsCollection.Aggregate(context.Background(), request)
  310. if err != nil {
  311. log.Println(err.Error())
  312. return nil, err
  313. }
  314. var headers []*common.MailMetadata
  315. for cur.Next(context.Background()) {
  316. result := &common.MailMetadata{}
  317. err = cur.Decode(result)
  318. if err != nil {
  319. log.Printf("Unable to read database mail record: %s", err)
  320. continue
  321. }
  322. // fmt.Printf("Add mail: %s", result.Id)
  323. headers = append(headers, result)
  324. }
  325. // fmt.Printf("Mails read from database: %v", headers)
  326. return headers, nil
  327. }
  328. func (s *Storage) GetUserInfo(user string) (*common.UserInfo, error) {
  329. result := &common.UserInfo{}
  330. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(result)
  331. return result, err
  332. }
  333. func (s *Storage) GetEmailStats(user string, email string, folder string) (stat common.FolderStat, err error) {
  334. stat = common.FolderStat{
  335. Folder: folder,
  336. }
  337. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  338. matchFilter := bson.M{"email": email}
  339. if folder == common.Trash {
  340. matchFilter["$or"] = bson.A{
  341. bson.M{"trash": true},
  342. bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
  343. }
  344. } else {
  345. matchFilter["folder"] = folder
  346. matchFilter["$or"] = bson.A{
  347. bson.M{"trash": false},
  348. bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
  349. }
  350. }
  351. cur, err := mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": matchFilter}, bson.M{"$count": "total"}})
  352. if err == nil && cur.Next(context.Background()) {
  353. cur.Decode(&stat)
  354. } else {
  355. return
  356. }
  357. matchFilter["read"] = false
  358. cur, err = mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": matchFilter}, bson.M{"$count": "unread"}})
  359. if err == nil && cur.Next(context.Background()) {
  360. cur.Decode(&stat)
  361. } else {
  362. return
  363. }
  364. return
  365. }
  366. func (s *Storage) GetMail(user string, id string) (metadata *common.MailMetadata, err error) {
  367. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  368. oId, err := primitive.ObjectIDFromHex(id)
  369. if err != nil {
  370. return nil, err
  371. }
  372. metadata = &common.MailMetadata{
  373. Mail: common.NewMail(),
  374. }
  375. err = mailsCollection.FindOne(context.Background(), bson.M{"_id": oId}).Decode(metadata)
  376. if err != nil {
  377. return nil, err
  378. }
  379. return metadata, nil
  380. }
  381. func (s *Storage) SetRead(user string, id string, read bool) error {
  382. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  383. oId, err := primitive.ObjectIDFromHex(id)
  384. if err != nil {
  385. return err
  386. }
  387. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"read": read}})
  388. s.notifyMailboxUpdateForMail(user, id)
  389. return err
  390. }
  391. func (s *Storage) UpdateMail(user string, id string, mailMap interface{}) error {
  392. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  393. oId, err := primitive.ObjectIDFromHex(id)
  394. if err != nil {
  395. return err
  396. }
  397. fromFolder := ""
  398. metadata, err := s.GetMail(user, id)
  399. if err == nil {
  400. if metadata.Trash {
  401. fromFolder = common.Trash
  402. } else {
  403. fromFolder = metadata.Folder
  404. }
  405. } else {
  406. log.Printf("Unable to get mail info to update folder statistics %s", err)
  407. }
  408. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": mailMap})
  409. s.notifyMailboxUpdateForMail(user, id, fromFolder)
  410. return err
  411. }
  412. func (s *Storage) GetUsers() (users []string, err error) {
  413. return nil, nil
  414. }
  415. func (s *Storage) GetEmails(user string) (emails []string, err error) {
  416. result := &struct {
  417. Email []string
  418. }{}
  419. err = s.emailsCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(result)
  420. if err != nil {
  421. return nil, err
  422. }
  423. return result.Email, nil
  424. }
  425. func (s *Storage) GetAllEmails() (emails []string, err error) {
  426. cur, err := s.allEmailsCollection.Find(context.Background(), bson.M{})
  427. if cur.Next(context.Background()) {
  428. result := struct {
  429. Emails []string
  430. }{}
  431. err = cur.Decode(&result)
  432. if err == nil {
  433. return result.Emails, nil
  434. }
  435. }
  436. return nil, err
  437. }
  438. func (s *Storage) CheckEmailExists(email string) bool {
  439. result := s.allEmailsCollection.FindOne(context.Background(), bson.M{"emails": email})
  440. return result.Err() == nil
  441. }
  442. func (s *Storage) GetFolders(email string) (folders []*common.Folder) {
  443. folders = []*common.Folder{
  444. {Name: common.Inbox, Custom: false},
  445. {Name: common.Sent, Custom: false},
  446. {Name: common.Trash, Custom: false},
  447. {Name: common.Spam, Custom: false},
  448. }
  449. return
  450. }
  451. func (s *Storage) ReadEmailMaps() (map[string]string, error) {
  452. registredEmails, err := s.GetAllEmails()
  453. if err != nil {
  454. return nil, err
  455. }
  456. mailPath := config.ConfigInstance().VMailboxBase
  457. mapsFile := config.ConfigInstance().VMailboxMaps
  458. if !utils.FileExists(mapsFile) {
  459. return nil, errors.New("Could not read virtual mailbox maps")
  460. }
  461. db, err := berkeleydb.NewDB()
  462. if err != nil {
  463. log.Fatal(err)
  464. }
  465. err = db.Open(config.ConfigInstance().VMailboxMaps, berkeleydb.DbHash, berkeleydb.DbRdOnly)
  466. if err != nil {
  467. return nil, errors.New("Unable to open virtual mailbox maps " + mapsFile + " " + err.Error())
  468. }
  469. defer db.Close()
  470. cursor, err := db.Cursor()
  471. if err != nil {
  472. return nil, errors.New("Unable to read virtual mailbox maps " + mapsFile + " " + err.Error())
  473. }
  474. emailMaps := make(map[string]string)
  475. for true {
  476. email, path, dberr := cursor.GetNext()
  477. if dberr != nil {
  478. break
  479. }
  480. found := false
  481. for _, registredEmail := range registredEmails {
  482. if email == registredEmail {
  483. found = true
  484. }
  485. }
  486. if !found {
  487. return nil, errors.New("Found non-registred mailbox <" + email + "> in mail maps. Database has inconsistancy")
  488. }
  489. emailMaps[email] = mailPath + "/" + path
  490. }
  491. for _, registredEmail := range registredEmails {
  492. if _, exists := emailMaps[registredEmail]; !exists {
  493. return nil, errors.New("Found existing mailbox <" + registredEmail + "> in database. Mail maps has inconsistancy")
  494. }
  495. }
  496. return emailMaps, nil
  497. }
  498. func removeAttachment(attachmentId string) error {
  499. attachmentPath := config.ConfigInstance().AttachmentsPath + "/" + attachmentId
  500. err := os.Remove(attachmentPath)
  501. if err != nil {
  502. log.Printf("Unable to remove attachment file: %s. Database inconsistency", attachmentPath)
  503. }
  504. return err
  505. }
  506. func (s *Storage) cleanupAttachments(user, email string) error {
  507. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  508. cur, err := mailsCollection.Aggregate(context.Background(), bson.A{
  509. bson.M{"$match": bson.M{"email": email}},
  510. bson.M{"$project": bson.M{"mail.body.attachments": 1}},
  511. bson.M{"$unwind": "$mail.body.attachments"},
  512. bson.M{"$replaceRoot": bson.M{"newRoot": "$mail.body.attachments"}},
  513. })
  514. if err != nil {
  515. return err
  516. }
  517. for cur.Next(context.Background()) {
  518. var attachment common.AttachmentHeader
  519. err = cur.Decode(&attachment)
  520. if err != nil {
  521. log.Printf("Unable to decode attachment")
  522. }
  523. removeAttachment(attachment.Id)
  524. }
  525. return nil
  526. }
  527. func (s *Storage) CheckAttachment(user, attachment string) bool {
  528. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  529. result := mailsCollection.FindOne(context.Background(), bson.M{"mail.body.attachments.id": attachment})
  530. return result.Err() == nil
  531. }
  532. func (s *Storage) RegisterNotifier(notifier common.Notifier) {
  533. if notifier != nil {
  534. notifiers.notifiersLock.Lock()
  535. defer notifiers.notifiersLock.Unlock()
  536. notifiers.notifiers = append(notifiers.notifiers, notifier)
  537. }
  538. }
  539. func (s *Storage) notifyNewMail(email string, mail common.MailMetadata) {
  540. notifiers.notifiersLock.Lock()
  541. defer notifiers.notifiersLock.Unlock()
  542. for _, notifier := range notifiers.notifiers {
  543. notifier.NotifyNewMail(email, mail)
  544. }
  545. }
  546. func (s *Storage) notifyMailboxUpdate(email string, stats []common.FolderStat) {
  547. notifiers.notifiersLock.Lock()
  548. defer notifiers.notifiersLock.Unlock()
  549. for _, notifier := range notifiers.notifiers {
  550. notifier.NotifyMaiboxUpdate(email, stats)
  551. }
  552. }
  553. func (s *Storage) notifyMailboxUpdateForMail(user, id string, folders ...string) {
  554. metadata, err := s.GetMail(user, id)
  555. if err != nil {
  556. log.Printf("Unable to get mail metadata to update mailbox stat %v\n", err)
  557. return
  558. }
  559. if metadata.Trash {
  560. folders = append(folders, common.Trash)
  561. }
  562. var stats []common.FolderStat
  563. stat, err := s.GetEmailStats(user, metadata.Email, metadata.Folder)
  564. stats = append(stats, stat)
  565. for _, folder := range folders {
  566. if folder == metadata.Folder {
  567. continue
  568. }
  569. stat, err = s.GetEmailStats(user, metadata.Email, folder)
  570. if err == nil {
  571. stats = append(stats, stat)
  572. } else {
  573. log.Printf("Unable to update mailbox stat %v\n", err)
  574. }
  575. }
  576. s.notifyMailboxUpdate(metadata.Email, stats)
  577. }