1 parent
99988bdebc
commit
e9981cff70
16 files changed
+649
-256
No files matched your search
@@ -6,7 +6,6 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"log/slog"
|
||||
"xorm.io/xorm"
|
||||
@@ -80,14 +79,14 @@ func DelByUID(ctx *context.Context, ids []int) error {
|
||||
}
|
||||
for _, id := range ids {
|
||||
var ue models.UserEmail
|
||||
found, err := session.Table("user_email").Where(Eq{"id": id, "user_id": ctx.UserID}).Get(&ue)
|
||||
found, err := session.Table("user_email").Where(Eq{"id": id, "user_id": ctx.UserID, "is_deleted": 1}).Get(&ue)
|
||||
if err != nil {
|
||||
rollbackDeleteSession(session)
|
||||
return err
|
||||
}
|
||||
if !found || ue.ID == 0 {
|
||||
log.WithContext(ctx).Warn("no user email found")
|
||||
continue
|
||||
rollbackDeleteSession(session)
|
||||
return fmt.Errorf("email relation %d is missing or not marked deleted", id)
|
||||
}
|
||||
emailId := ue.EmailID
|
||||
|
||||
|
||||
@@ -100,3 +100,54 @@ func TestForcedDeleteRemovesLastOwnedEmail(t *testing.T) {
|
||||
t.Fatal("unreferenced owned email was not deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelByUIDRequiresDeletedFlag(t *testing.T) {
|
||||
initDeleteTestDB(t)
|
||||
email := insertDeleteTestEmail(t)
|
||||
relation := models.UserEmail{UserID: 1, EmailID: email.Id}
|
||||
if _, err := db.Instance.Insert(&relation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := DelByUID(&context.Context{UserID: 1}, []int{relation.ID}); err == nil {
|
||||
t.Fatal("unmarked relation was accepted for expunge")
|
||||
}
|
||||
|
||||
found, err := db.Instance.ID(relation.ID).Get(&models.UserEmail{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("unmarked relation was deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelByUIDRollsBackBatchWhenRelationIsNotMarked(t *testing.T) {
|
||||
initDeleteTestDB(t)
|
||||
firstEmail := insertDeleteTestEmail(t)
|
||||
secondEmail := insertDeleteTestEmail(t)
|
||||
firstRelation := models.UserEmail{UserID: 1, EmailID: firstEmail.Id, IsDeleted: 1}
|
||||
if _, err := db.Instance.Insert(&firstRelation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondRelation := models.UserEmail{UserID: 1, EmailID: secondEmail.Id}
|
||||
if _, err := db.Instance.Insert(&secondRelation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relations := []models.UserEmail{firstRelation, secondRelation}
|
||||
|
||||
err := DelByUID(&context.Context{UserID: 1}, []int{relations[0].ID, relations[1].ID})
|
||||
if err == nil {
|
||||
t.Fatal("mixed marked and unmarked batch was accepted")
|
||||
}
|
||||
|
||||
for _, relation := range relations {
|
||||
found, loadErr := db.Instance.ID(relation.ID).Get(&models.UserEmail{})
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("relation %d was not rolled back", relation.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
+130
-170
@@ -2,6 +2,8 @@ package list
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/consts"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto"
|
||||
@@ -9,8 +11,8 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/query"
|
||||
. "xorm.io/builder"
|
||||
)
|
||||
import . "xorm.io/builder"
|
||||
|
||||
func GetEmailList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64, err error) {
|
||||
return getList(ctx, tagInfo, keyword, pop3List, offset, limit)
|
||||
@@ -127,153 +129,122 @@ type ImapListReq struct {
|
||||
End int
|
||||
}
|
||||
|
||||
func GetUEListByUID(ctx *context.Context, groupName string, star, end int, uidList []int) ([]*response.UserEmailUIDData, error) {
|
||||
var ue []*response.UserEmailUIDData
|
||||
sql := "SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE user_id = ? "
|
||||
|
||||
func buildIMAPMailboxRelationQuery(ctx *context.Context, groupName string) (string, []any, error) {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
return "", nil, fmt.Errorf("invalid mailbox user")
|
||||
}
|
||||
inner := "SELECT id,email_id,is_read,is_deleted,ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM user_email WHERE user_id = ?"
|
||||
params := []any{ctx.UserID}
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 0)
|
||||
case "Sent Messages":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 1)
|
||||
case "Drafts":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 4)
|
||||
case "Deleted Messages":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 3)
|
||||
case "Junk":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 5)
|
||||
default:
|
||||
var mailbox models.Group
|
||||
found, err := db.Instance.Table("group").Where("user_id=? and full_path=?", ctx.UserID, groupName).Get(&mailbox)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("query mailbox: %w", err)
|
||||
}
|
||||
if !found || mailbox.ID == 0 {
|
||||
return "", nil, fmt.Errorf("mailbox not found")
|
||||
}
|
||||
inner += " and group_id = ?"
|
||||
params = append(params, mailbox.ID)
|
||||
}
|
||||
return inner, params, nil
|
||||
}
|
||||
|
||||
func GetUEListByUID(ctx *context.Context, groupName string, star, end int, uidList []int) ([]*response.UserEmailUIDData, error) {
|
||||
if star < 0 || end < 0 || (end > 0 && star > end) {
|
||||
return nil, fmt.Errorf("invalid UID range")
|
||||
}
|
||||
inner, params, err := buildIMAPMailboxRelationQuery(ctx, groupName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filters := make([]string, 0, 3)
|
||||
if len(uidList) > 0 {
|
||||
placeholders, uidArgs, ok := query.IntList(uidList, 10000)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid UID list")
|
||||
}
|
||||
sql += fmt.Sprintf(" and id in (%s)", placeholders)
|
||||
filters = append(filters, fmt.Sprintf("id in (%s)", placeholders))
|
||||
params = append(params, uidArgs...)
|
||||
}
|
||||
if star > 0 {
|
||||
sql += " and id >=?"
|
||||
filters = append(filters, "id >= ?")
|
||||
params = append(params, star)
|
||||
}
|
||||
if end > 0 {
|
||||
sql += " and id <=?"
|
||||
filters = append(filters, "id <= ?")
|
||||
params = append(params, end)
|
||||
}
|
||||
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
sql += " and status =? and group_id = 0"
|
||||
params = append(params, 0)
|
||||
case "Sent Messages":
|
||||
sql += " and status =? and group_id = 0"
|
||||
params = append(params, 1)
|
||||
case "Drafts":
|
||||
sql += " and status =? and group_id = 0"
|
||||
params = append(params, 4)
|
||||
case "Deleted Messages":
|
||||
sql += " and status =? and group_id = 0"
|
||||
params = append(params, 3)
|
||||
case "Junk":
|
||||
sql += " and status =? and group_id = 0"
|
||||
params = append(params, 5)
|
||||
default:
|
||||
var group models.Group
|
||||
found, err := db.Instance.Table("group").Where("user_id=? and full_path=?", ctx.UserID, groupName).Get(&group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !found || group.ID == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
sql += " and group_id = ?"
|
||||
params = append(params, group.ID)
|
||||
sql := "SELECT * FROM (" + inner + ") mailbox"
|
||||
if len(filters) > 0 {
|
||||
sql += " WHERE " + strings.Join(filters, " AND ")
|
||||
}
|
||||
|
||||
if err := db.Instance.SQL(sql, params...).Find(&ue); err != nil {
|
||||
return nil, err
|
||||
sql += " ORDER BY id"
|
||||
var relations []*response.UserEmailUIDData
|
||||
if err := db.Instance.SQL(sql, params...).Find(&relations); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox UID relations: %w", err)
|
||||
}
|
||||
return ue, nil
|
||||
return relations, nil
|
||||
}
|
||||
|
||||
func GetMaxUIDByMailbox(ctx *context.Context, groupName string) (int, error) {
|
||||
inner, params, err := buildIMAPMailboxRelationQuery(ctx, groupName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var maxUID int
|
||||
if _, err := db.Instance.SQL("SELECT COALESCE(MAX(id), 0) FROM ("+inner+") mailbox", params...).Get(&maxUID); err != nil {
|
||||
return 0, fmt.Errorf("query mailbox maximum UID: %w", err)
|
||||
}
|
||||
return maxUID, nil
|
||||
}
|
||||
|
||||
func getEmailListByUidList(ctx *context.Context, groupName string, req ImapListReq) ([]*response.EmailResponseData, error) {
|
||||
var ret []*response.EmailResponseData
|
||||
var ue []*response.UserEmailUIDData
|
||||
if req.Star < 0 || req.End < 0 || (req.End > 0 && req.Star > req.End) {
|
||||
return nil, fmt.Errorf("invalid UID range")
|
||||
}
|
||||
|
||||
buildQuery := func(filterColumn string, baseParams []any) (string, []any, bool) {
|
||||
sql := "SELECT id,email_id,is_read,ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE user_id = ?"
|
||||
params := append([]any{}, baseParams...)
|
||||
if filterColumn != "" {
|
||||
sql += " and " + filterColumn + " = ?"
|
||||
if filterColumn == "status" {
|
||||
sql += " and group_id = 0"
|
||||
}
|
||||
}
|
||||
if req.Star > 0 {
|
||||
sql += " and id >= ?"
|
||||
params = append(params, req.Star)
|
||||
if req.End > 0 {
|
||||
sql += " and id <= ?"
|
||||
params = append(params, req.End)
|
||||
}
|
||||
return sql, params, true
|
||||
inner, params, err := buildIMAPMailboxRelationQuery(ctx, groupName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sql := "SELECT * FROM (" + inner + ") mailbox WHERE "
|
||||
if req.Star > 0 {
|
||||
sql += "id >= ?"
|
||||
params = append(params, req.Star)
|
||||
if req.End > 0 {
|
||||
sql += " and id <= ?"
|
||||
params = append(params, req.End)
|
||||
}
|
||||
} else {
|
||||
placeholders, uidArgs, ok := query.IntList(req.UidList, 10000)
|
||||
if !ok {
|
||||
return "", nil, false
|
||||
return nil, fmt.Errorf("invalid UID list")
|
||||
}
|
||||
sql += fmt.Sprintf(" and id in (%s)", placeholders)
|
||||
sql += fmt.Sprintf("id in (%s)", placeholders)
|
||||
params = append(params, uidArgs...)
|
||||
return sql, params, true
|
||||
}
|
||||
|
||||
var sql string
|
||||
var params []any
|
||||
var ok bool
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
sql, params, ok = buildQuery("status", []any{ctx.UserID, 0})
|
||||
case "Sent Messages":
|
||||
sql, params, ok = buildQuery("status", []any{ctx.UserID, 1})
|
||||
case "Drafts":
|
||||
sql, params, ok = buildQuery("status", []any{ctx.UserID, 4})
|
||||
case "Deleted Messages":
|
||||
sql, params, ok = buildQuery("status", []any{ctx.UserID, 3})
|
||||
case "Junk":
|
||||
sql, params, ok = buildQuery("status", []any{ctx.UserID, 5})
|
||||
default:
|
||||
var group models.Group
|
||||
found, err := db.Instance.Table("group").Where("user_id=? and full_path=?", ctx.UserID, groupName).Get(&group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query mailbox: %w", err)
|
||||
}
|
||||
if !found || group.ID == 0 {
|
||||
return nil, fmt.Errorf("mailbox not found")
|
||||
}
|
||||
sql, params, ok = buildQuery("group_id", []any{ctx.UserID, group.ID})
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid UID list")
|
||||
}
|
||||
if err := db.Instance.SQL(sql, params...).Find(&ue); err != nil {
|
||||
sql += " ORDER BY id"
|
||||
var relations []*response.UserEmailUIDData
|
||||
if err := db.Instance.SQL(sql, params...).Find(&relations); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox UID list: %w", err)
|
||||
}
|
||||
|
||||
ueMap := map[int]*response.UserEmailUIDData{}
|
||||
var emailIds []int
|
||||
for _, email := range ue {
|
||||
ueMap[email.EmailID] = email
|
||||
emailIds = append(emailIds, email.EmailID)
|
||||
}
|
||||
if len(emailIds) == 0 {
|
||||
return ret, nil
|
||||
}
|
||||
if err := db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox messages: %w", err)
|
||||
}
|
||||
for i, data := range ret {
|
||||
item := ueMap[data.Id]
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
ret[i].IsRead = item.IsRead
|
||||
ret[i].SerialNumber = item.SerialNumber
|
||||
ret[i].UeId = item.ID
|
||||
}
|
||||
return ret, nil
|
||||
return hydrateIMAPEmailResponses(relations)
|
||||
}
|
||||
|
||||
func GetEmailListByGroup(ctx *context.Context, groupName string, req ImapListReq, uid bool) ([]*response.EmailResponseData, error) {
|
||||
@@ -286,41 +257,11 @@ func GetEmailListByGroup(ctx *context.Context, groupName string, req ImapListReq
|
||||
if req.Star < 0 || req.End < 0 || (req.End > 0 && req.Star > req.End) {
|
||||
return nil, fmt.Errorf("invalid sequence range")
|
||||
}
|
||||
|
||||
var ret []*response.EmailResponseData
|
||||
var ue []*response.UserEmailUIDData
|
||||
inner := "SELECT id,email_id,is_read,ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE user_id = ?"
|
||||
params := []any{ctx.UserID}
|
||||
switch groupName {
|
||||
case "INBOX":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 0)
|
||||
case "Sent Messages":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 1)
|
||||
case "Drafts":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 4)
|
||||
case "Deleted Messages":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 3)
|
||||
case "Junk":
|
||||
inner += " and status = ? and group_id = 0"
|
||||
params = append(params, 5)
|
||||
default:
|
||||
var group models.Group
|
||||
found, err := db.Instance.Table("group").Where("user_id=? and full_path=?", ctx.UserID, groupName).Get(&group)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query mailbox: %w", err)
|
||||
}
|
||||
if !found || group.ID == 0 {
|
||||
return nil, fmt.Errorf("mailbox not found")
|
||||
}
|
||||
inner += " and group_id = ?"
|
||||
params = append(params, group.ID)
|
||||
inner, params, err := buildIMAPMailboxRelationQuery(ctx, groupName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sql := "SELECT * FROM (" + inner + ") a WHERE "
|
||||
sql := "SELECT * FROM (" + inner + ") mailbox WHERE "
|
||||
if req.Star > 0 {
|
||||
sql += "serial_number >= ?"
|
||||
params = append(params, req.Star)
|
||||
@@ -336,30 +277,49 @@ func GetEmailListByGroup(ctx *context.Context, groupName string, req ImapListReq
|
||||
sql += fmt.Sprintf("serial_number in (%s)", placeholders)
|
||||
params = append(params, sequenceArgs...)
|
||||
}
|
||||
if err := db.Instance.SQL(sql, params...).Find(&ue); err != nil {
|
||||
sql += " ORDER BY serial_number"
|
||||
var relations []*response.UserEmailUIDData
|
||||
if err := db.Instance.SQL(sql, params...).Find(&relations); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox sequence list: %w", err)
|
||||
}
|
||||
return hydrateIMAPEmailResponses(relations)
|
||||
}
|
||||
|
||||
ueMap := map[int]*response.UserEmailUIDData{}
|
||||
var emailIds []int
|
||||
for _, email := range ue {
|
||||
ueMap[email.EmailID] = email
|
||||
emailIds = append(emailIds, email.EmailID)
|
||||
func hydrateIMAPEmailResponses(relations []*response.UserEmailUIDData) ([]*response.EmailResponseData, error) {
|
||||
result := make([]*response.EmailResponseData, 0, len(relations))
|
||||
if len(relations) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
if len(emailIds) == 0 {
|
||||
return ret, nil
|
||||
}
|
||||
if err := db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox messages: %w", err)
|
||||
}
|
||||
for i, data := range ret {
|
||||
item := ueMap[data.Id]
|
||||
if item == nil {
|
||||
emailIDs := make([]int, 0, len(relations))
|
||||
for _, relation := range relations {
|
||||
if relation == nil || relation.EmailID <= 0 {
|
||||
continue
|
||||
}
|
||||
ret[i].IsRead = item.IsRead
|
||||
ret[i].SerialNumber = item.SerialNumber
|
||||
ret[i].UeId = item.ID
|
||||
emailIDs = append(emailIDs, relation.EmailID)
|
||||
}
|
||||
return ret, nil
|
||||
if len(emailIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
var stored []*response.EmailResponseData
|
||||
if err := db.Instance.Table("email").Select("*").Where(Eq{"id": emailIDs}).Find(&stored); err != nil {
|
||||
return nil, fmt.Errorf("query mailbox messages: %w", err)
|
||||
}
|
||||
messageByID := make(map[int]*response.EmailResponseData, len(stored))
|
||||
for _, message := range stored {
|
||||
if message != nil {
|
||||
messageByID[message.Id] = message
|
||||
}
|
||||
}
|
||||
for _, relation := range relations {
|
||||
message := messageByID[relation.EmailID]
|
||||
if message == nil {
|
||||
continue
|
||||
}
|
||||
message.IsRead = relation.IsRead
|
||||
message.IsDeleted = relation.IsDeleted
|
||||
message.SerialNumber = relation.SerialNumber
|
||||
message.UeId = relation.ID
|
||||
result = append(result, message)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in new issue
Block a user