Browse Source

Add legacy folder check to restore email

Alexey Edelev 5 years ago
parent
commit
5e29678d27
2 changed files with 14 additions and 7 deletions
  1. 11 5
      db/db.go
  2. 3 2
      web/mail.go

+ 11 - 5
db/db.go

@@ -342,7 +342,7 @@ func (s *Storage) MoveMail(user string, mailId string, folder string) error {
 	return err
 }
 
-func (s *Storage) RestoreMail(user string, mailId string, folder string) error {
+func (s *Storage) RestoreMail(user string, mailId string) error {
 	mailsCollection := s.db.Collection(qualifiedMailCollection(user))
 
 	oId, err := primitive.ObjectIDFromHex(mailId)
@@ -350,6 +350,12 @@ func (s *Storage) RestoreMail(user string, mailId string, folder string) error {
 		return err
 	}
 
+	//TODO: Legacy for old databases remove soon
+	metadata, err := s.GetMail(user, mailId)
+	if metadata.Folder == common.Trash {
+		_, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"folder": common.Inbox}})
+	}
+
 	_, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"trash": false}})
 	return err
 }
@@ -373,13 +379,13 @@ func (s *Storage) GetMailList(user, email, folder string, frame common.Frame) ([
 	if folder == common.Trash {
 		matchFilter["$or"] = bson.A{
 			bson.M{"trash": true},
-			bson.M{"folder": folder}, //legacy support
+			bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
 		}
 	} else {
 		matchFilter["folder"] = folder
 		matchFilter["$or"] = bson.A{
 			bson.M{"trash": false},
-			bson.M{"trash": bson.M{"$exists": false}}, //legacy support
+			bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
 		}
 	}
 
@@ -438,13 +444,13 @@ func (s *Storage) GetEmailStats(user string, email string, folder string) (unrea
 	if folder == common.Trash {
 		matchFilter["$or"] = bson.A{
 			bson.M{"trash": true},
-			bson.M{"folder": folder}, //legacy support
+			bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
 		}
 	} else {
 		matchFilter["folder"] = folder
 		matchFilter["$or"] = bson.A{
 			bson.M{"trash": false},
-			bson.M{"trash": bson.M{"$exists": false}}, //legacy support
+			bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
 		}
 	}
 

+ 3 - 2
web/mail.go

@@ -91,7 +91,8 @@ func (s *Server) handleMailDetails(w http.ResponseWriter, user, mailId string) {
 		Text:    template.HTML(text),
 		MailId:  mailId,
 		Read:    false,
-		Trash:   mail.Trash,
+		Trash: mail.Trash ||
+			mail.Folder == common.Trash, //TODO: Legacy for old databases remove soon
 	}))
 }
 
@@ -110,7 +111,7 @@ func (s *Server) handleRemove(w http.ResponseWriter, user, mailId string) {
 }
 
 func (s *Server) handleRestore(w http.ResponseWriter, user, mailId string) {
-	err := s.storage.RestoreMail(user, mailId, common.Trash)
+	err := s.storage.RestoreMail(user, mailId)
 	if err != nil {
 		s.error(http.StatusInternalServerError, "Could not move email to trash", w)
 	}