aboutsummaryrefslogtreecommitdiff
path: root/services/mail/webmail.go
blob: 02831bc06cb659d8434303123bec5f2955e48705 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package mail

import (
	"fmt"
	"regexp"
	"strings"
	"time"

	mailModel "dove/models/mail"
	mailRepo "dove/repositories/mail"
	"dove/utils/email"
	"dove/utils/logger"
	"dove/utils/meta"
	"dove/utils/shortcuts"
	"dove/utils/storage"
)

type FolderWithCount struct {
	mailModel.Folder
	UnreadCount int64 `json:"unread_count"`
}

type WebMailResponse struct {
	Mailboxes     []mailModel.Mailbox `json:"mailboxes"`
	ActiveMailbox mailModel.Mailbox   `json:"active_mailbox"`
	Folders       []FolderWithCount   `json:"folders"`
	ActiveFolder  FolderWithCount     `json:"active_folder"`
	Emails        []mailModel.Email   `json:"emails"`
	TotalEmails   int64               `json:"total_emails"`
	ActiveEmail   *mailModel.Email    `json:"active_email"`
	IsStarredView bool                `json:"is_starred_view"`
	StarredCount  int64               `json:"starred_count"`
	Search        string              `json:"search"`
}

type EmailListResponse struct {
	Emails      []mailModel.Email `json:"emails"`
	TotalEmails int64             `json:"total_emails"`
	Search      string            `json:"search"`
}

type EmailPreviewResponse struct {
	Email         mailModel.Email    `json:"email"`
	EmailBody     string             `json:"email_body"`
	ActiveMailbox mailModel.Mailbox  `json:"active_mailbox"`
	Folders       []mailModel.Folder `json:"folders"`
}

type ComposeAddress struct {
	Address     string `json:"address"`
	DisplayName string `json:"display_name"`
	MailboxID   uint   `json:"mailbox_id"`
}

type ComposeResponse struct {
	Mailboxes     []mailModel.Mailbox `json:"mailboxes"`
	ActiveMailbox mailModel.Mailbox   `json:"active_mailbox"`
	FromAddresses []ComposeAddress    `json:"from_addresses"`
	AllRecipients []ComposeAddress    `json:"all_recipients"`
	DraftID       uint                `json:"draft_id"`
	ReplyTo       *mailModel.Email    `json:"reply_to"`
}

type SendEmailRequest struct {
	FromMailboxID uint   `form:"from_mailbox_id"`
	ToAddress     string `form:"to_address"`
	CcAddresses   string `form:"cc_addresses"`
	BccAddresses  string `form:"bcc_addresses"`
	Subject       string `form:"subject"`
	Body          string `form:"body"`
	DraftID       uint   `form:"draft_id"`
}

type MoveEmailRequest struct {
	FolderID uint `form:"folder_id"`
}

type BulkActionRequest struct {
	EmailIDs []uint `form:"email_ids"`
	Action   string `form:"action"`
	FolderID uint   `form:"folder_id"`
}

type CreateFolderRequest struct {
	Name      string `form:"name"`
	MailboxID uint
}

func WebMailData(mailboxID uint, folderSlug string, search string, pagination meta.Pagination, sorting meta.Sorting) (*WebMailResponse, *shortcuts.Error) {
	mailboxes := mailRepo.AllMailboxes()
	if len(mailboxes) == 0 {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, NoMailboxesExist)
	}

	var activeMailbox *mailModel.Mailbox
	if mailboxID > 0 {
		for index := range mailboxes {
			if mailboxes[index].ID == mailboxID {
				activeMailbox = &mailboxes[index]
				break
			}
		}
	}

	if activeMailbox == nil {
		activeMailbox = &mailboxes[0]
	}

	folders := mailRepo.FindFoldersByMailboxID(activeMailbox.ID)
	foldersWithCounts := buildFolderCounts(folders)

	isStarredView := folderSlug == "starred"

	var activeFolder *FolderWithCount
	if !isStarredView {
		for index := range foldersWithCounts {
			if foldersWithCounts[index].Slug == folderSlug {
				activeFolder = &foldersWithCounts[index]
				break
			}
		}

		if activeFolder == nil && len(foldersWithCounts) > 0 {
			activeFolder = &foldersWithCounts[0]
		}
	}

	var emails []mailModel.Email
	var totalEmails int64

	if isStarredView {
		emails, totalEmails = mailRepo.ListStarredEmails(activeMailbox.ID, pagination, sorting, search)
	} else if activeFolder != nil {
		emails, totalEmails = mailRepo.ListEmailsByFolder(activeFolder.ID, pagination, sorting, search)
	}

	starredCount := mailRepo.CountStarredByMailboxID(activeMailbox.ID)

	response := &WebMailResponse{
		Mailboxes:     mailboxes,
		ActiveMailbox: *activeMailbox,
		Folders:       foldersWithCounts,
		Emails:        emails,
		TotalEmails:   totalEmails,
		IsStarredView: isStarredView,
		StarredCount:  starredCount,
		Search:        search,
	}

	if activeFolder != nil {
		response.ActiveFolder = *activeFolder
	}

	return response, nil
}

func EmailListData(folderID uint, search string, pagination meta.Pagination, sorting meta.Sorting) EmailListResponse {
	emails, total := mailRepo.ListEmailsByFolder(folderID, pagination, sorting, search)
	return EmailListResponse{
		Emails:      emails,
		TotalEmails: total,
		Search:      search,
	}
}

func StarredEmailListData(mailboxID uint, search string, pagination meta.Pagination, sorting meta.Sorting) EmailListResponse {
	emails, total := mailRepo.ListStarredEmails(mailboxID, pagination, sorting, search)
	return EmailListResponse{
		Emails:      emails,
		TotalEmails: total,
		Search:      search,
	}
}

type FolderSidebarResponse struct {
	Folders       []FolderWithCount `json:"folders"`
	ActiveFolder  FolderWithCount   `json:"active_folder"`
	ActiveMailbox mailModel.Mailbox `json:"active_mailbox"`
	IsStarredView bool              `json:"is_starred_view"`
	StarredCount  int64             `json:"starred_count"`
}

func FolderSidebarData(mailboxID uint, folderSlug string) (*FolderSidebarResponse, *shortcuts.Error) {
	mailbox := mailRepo.FindMailboxByIDWithRelations(mailboxID)
	if mailbox == nil {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, MailboxNotFound)
	}

	folders := mailRepo.FindFoldersByMailboxID(mailboxID)
	foldersWithCounts := buildFolderCounts(folders)

	isStarredView := folderSlug == "starred"
	starredCount := mailRepo.CountStarredByMailboxID(mailboxID)

	response := &FolderSidebarResponse{
		Folders:       foldersWithCounts,
		ActiveMailbox: *mailbox,
		IsStarredView: isStarredView,
		StarredCount:  starredCount,
	}

	if !isStarredView {
		for index := range foldersWithCounts {
			if foldersWithCounts[index].Slug == folderSlug {
				response.ActiveFolder = foldersWithCounts[index]
				break
			}
		}
	}

	return response, nil
}

func IsEmailUnread(emailID uint) bool {
	email := mailRepo.FindEmailByID(emailID)
	return email != nil && !email.IsRead
}

func EmailPreviewData(emailID uint, mailboxID uint) (*EmailPreviewResponse, *shortcuts.Error) {
	email := mailRepo.FindEmailByIDWithRelations(emailID)
	if email == nil {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, EmailNotFound)
	}

	if !email.IsRead {
		mailRepo.MarkEmailAsRead(emailID)
		email.IsRead = true
	}

	mailbox := mailRepo.FindMailboxByIDWithRelations(mailboxID)
	if mailbox == nil {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, MailboxNotFound)
	}

	folders := mailRepo.FindFoldersByMailboxID(mailboxID)

	emailBody := readEmailBody(email.Mailbox.Address, email.Folder.Slug, email.Filename)

	return &EmailPreviewResponse{
		Email:         *email,
		EmailBody:     emailBody,
		ActiveMailbox: *mailbox,
		Folders:       folders,
	}, nil
}

func ComposeData(mailboxID uint, replyToEmailID uint) (*ComposeResponse, *shortcuts.Error) {
	mailboxes := mailRepo.AllMailboxes()
	if len(mailboxes) == 0 {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, NoMailboxesExist)
	}

	var activeMailbox *mailModel.Mailbox
	for index := range mailboxes {
		if mailboxes[index].ID == mailboxID {
			activeMailbox = &mailboxes[index]
			break
		}
	}

	if activeMailbox == nil {
		activeMailbox = &mailboxes[0]
	}

	fromAddresses := buildFromAddresses(activeMailbox, mailboxes)
	allRecipients := buildAllRecipients(mailboxes)

	response := &ComposeResponse{
		Mailboxes:     mailboxes,
		ActiveMailbox: *activeMailbox,
		FromAddresses: fromAddresses,
		AllRecipients: allRecipients,
	}

	if replyToEmailID > 0 {
		replyEmail := mailRepo.FindEmailByID(replyToEmailID)
		if replyEmail != nil {
			response.ReplyTo = replyEmail
		}
	}

	return response, nil
}

func buildFromAddresses(activeMailbox *mailModel.Mailbox, allMailboxes []mailModel.Mailbox) []ComposeAddress {
	var fromAddresses []ComposeAddress

	for _, mailbox := range allMailboxes {
		if mailbox.UserID != activeMailbox.UserID {
			continue
		}

		fromAddresses = append(fromAddresses, ComposeAddress{
			Address:     mailbox.Address,
			DisplayName: mailbox.User.DisplayName,
			MailboxID:   mailbox.ID,
		})

		for _, alias := range mailbox.Aliases {
			fromAddresses = append(fromAddresses, ComposeAddress{
				Address:     alias.SourceAddress,
				DisplayName: mailbox.User.DisplayName,
				MailboxID:   mailbox.ID,
			})
		}
	}

	return fromAddresses
}

func buildAllRecipients(allMailboxes []mailModel.Mailbox) []ComposeAddress {
	var recipients []ComposeAddress

	for _, mailbox := range allMailboxes {
		recipients = append(recipients, ComposeAddress{
			Address:     mailbox.Address,
			DisplayName: mailbox.User.DisplayName,
			MailboxID:   mailbox.ID,
		})

		for _, alias := range mailbox.Aliases {
			recipients = append(recipients, ComposeAddress{
				Address:     alias.SourceAddress,
				DisplayName: mailbox.User.DisplayName,
				MailboxID:   mailbox.ID,
			})
		}
	}

	return recipients
}

func SendEmail(request SendEmailRequest) *shortcuts.Error {
	toAddress := strings.TrimSpace(request.ToAddress)
	ccAddresses := strings.TrimSpace(request.CcAddresses)
	bccAddresses := strings.TrimSpace(request.BccAddresses)
	subject := strings.TrimSpace(request.Subject)
	body := strings.TrimSpace(request.Body)

	switch {
	case request.FromMailboxID == 0:
		return shortcuts.ServiceError(shortcuts.BadRequest, SenderRequired)
	case toAddress == "":
		return shortcuts.ServiceError(shortcuts.BadRequest, RecipientRequired)
	case subject == "":
		return shortcuts.ServiceError(shortcuts.BadRequest, SubjectRequired)
	}

	senderMailbox := mailRepo.FindMailboxByIDWithRelations(request.FromMailboxID)
	if senderMailbox == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, MailboxNotFound)
	}

	messageID := generateMessageID(senderMailbox.Address)

	rawMessage := email.Compose(email.ComposeRequest{
		MessageID:   messageID,
		FromAddress: senderMailbox.Address,
		FromName:    senderMailbox.User.DisplayName,
		ToAddresses: toAddress,
		CcAddresses: ccAddresses,
		Subject:     subject,
		HTMLBody:    body,
	})

	recipients := email.EnvelopeRecipients(toAddress, ccAddresses, bccAddresses)

	submitError := email.Submit(senderMailbox.Address, recipients, rawMessage)
	if submitError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailSendFailed)
	}

	storeSentCopy(senderMailbox, messageID, toAddress, ccAddresses, subject, body, rawMessage)

	if request.DraftID > 0 {
		deleteDraftWithFile(request.DraftID, senderMailbox.Address)
	}

	return nil
}

func storeSentCopy(senderMailbox *mailModel.Mailbox, messageID string, toAddresses string, ccAddresses string, subject string, body string, rawMessage []byte) {
	sentFolder := mailRepo.FindFolderBySlug(senderMailbox.ID, "sent")
	if sentFolder == nil {
		return
	}

	filename := generateFilename() + "-sent"

	sentEmail := &mailModel.Email{
		MailboxID:   senderMailbox.ID,
		FolderID:    sentFolder.ID,
		MessageID:   messageID,
		Filename:    filename,
		FromAddress: senderMailbox.Address,
		FromName:    senderMailbox.User.DisplayName,
		ToAddresses: toAddresses,
		CcAddresses: ccAddresses,
		Subject:     subject,
		Snippet:     truncateSnippet(body),
		Size:        int64(len(rawMessage)),
		IsRead:      true,
	}

	if createError := mailRepo.CreateEmail(sentEmail); createError != nil {
		logger.Warnf(LogPrefix, EmailStorageFailed, createError)
		return
	}

	if writeError := storage.WriteMailFile(senderMailbox.Address, "sent", filename, rawMessage); writeError != nil {
		logger.Warnf(LogPrefix, EmailStorageFailed, writeError)
	}
}

func SaveDraft(request SendEmailRequest) (*mailModel.Email, *shortcuts.Error) {
	if request.FromMailboxID == 0 {
		return nil, shortcuts.ServiceError(shortcuts.BadRequest, SenderRequired)
	}

	senderMailbox := mailRepo.FindMailboxByIDWithRelations(request.FromMailboxID)
	if senderMailbox == nil {
		return nil, shortcuts.ServiceError(shortcuts.NotFound, MailboxNotFound)
	}

	draftsFolder := mailRepo.FindFolderBySlug(senderMailbox.ID, "drafts")
	if draftsFolder == nil {
		return nil, shortcuts.ServiceError(shortcuts.Internal, FolderNotFound)
	}

	body := strings.TrimSpace(request.Body)

	if request.DraftID > 0 {
		existingDraft := mailRepo.FindEmailByID(request.DraftID)
		if existingDraft != nil {
			existingDraft.ToAddresses = strings.TrimSpace(request.ToAddress)
			existingDraft.CcAddresses = strings.TrimSpace(request.CcAddresses)
			existingDraft.Subject = strings.TrimSpace(request.Subject)
			existingDraft.Snippet = truncateSnippet(body)
			existingDraft.Size = int64(len(body))

			if updateError := mailRepo.UpdateEmail(existingDraft); updateError != nil {
				return nil, shortcuts.ServiceError(shortcuts.Internal, EmailDraftFailed)
			}

			if writeError := storage.WriteMailFile(senderMailbox.Address, "drafts", existingDraft.Filename, []byte(body)); writeError != nil {
				logger.Warnf(LogPrefix, EmailStorageFailed, writeError)
			}

			return existingDraft, nil
		}
	}

	draftFilename := generateFilename() + "-draft"
	draft := &mailModel.Email{
		MailboxID:   senderMailbox.ID,
		FolderID:    draftsFolder.ID,
		MessageID:   generateMessageID(senderMailbox.Address),
		Filename:    draftFilename,
		FromAddress: senderMailbox.Address,
		FromName:    senderMailbox.User.DisplayName,
		ToAddresses: strings.TrimSpace(request.ToAddress),
		CcAddresses: strings.TrimSpace(request.CcAddresses),
		Subject:     strings.TrimSpace(request.Subject),
		Snippet:     truncateSnippet(body),
		Size:        int64(len(body)),
		IsRead:      true,
	}

	if createError := mailRepo.CreateEmail(draft); createError != nil {
		return nil, shortcuts.ServiceError(shortcuts.Internal, EmailDraftFailed)
	}

	if writeError := storage.WriteMailFile(senderMailbox.Address, "drafts", draftFilename, []byte(body)); writeError != nil {
		logger.Warnf(LogPrefix, EmailStorageFailed, writeError)
	}

	return draft, nil
}

func ToggleStar(emailID uint) *shortcuts.Error {
	email := mailRepo.FindEmailByID(emailID)
	if email == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, EmailNotFound)
	}

	email.IsStarred = !email.IsStarred
	if updateError := mailRepo.UpdateEmail(email); updateError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailUpdateFailed)
	}

	return nil
}

func MarkRead(emailID uint) *shortcuts.Error {
	if markError := mailRepo.MarkEmailAsRead(emailID); markError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailUpdateFailed)
	}
	return nil
}

func MarkUnread(emailID uint) *shortcuts.Error {
	if markError := mailRepo.MarkEmailAsUnread(emailID); markError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailUpdateFailed)
	}
	return nil
}

func MoveEmail(emailID uint, request MoveEmailRequest) *shortcuts.Error {
	email := mailRepo.FindEmailByIDWithRelations(emailID)
	if email == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, EmailNotFound)
	}

	targetFolder := mailRepo.FindFolderByID(request.FolderID)
	if targetFolder == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, FolderNotFound)
	}

	sourceFolderSlug := email.Folder.Slug

	if moveError := mailRepo.MoveEmailToFolder(emailID, request.FolderID); moveError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailMoveFailed)
	}

	if moveFileError := storage.MoveMailFile(email.Mailbox.Address, sourceFolderSlug, targetFolder.Slug, email.Filename); moveFileError != nil {
		logger.Warnf(LogPrefix, EmailStorageFailed, moveFileError)
	}

	return nil
}

func TrashEmail(emailID uint, mailboxID uint) *shortcuts.Error {
	email := mailRepo.FindEmailByIDWithRelations(emailID)
	if email == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, EmailNotFound)
	}

	trashFolder := mailRepo.FindFolderBySlug(mailboxID, "trash")
	if trashFolder == nil {
		return shortcuts.ServiceError(shortcuts.Internal, FolderNotFound)
	}

	sourceFolderSlug := email.Folder.Slug

	if email.FolderID == trashFolder.ID {
		if deleteError := mailRepo.DeleteEmail(email); deleteError != nil {
			return shortcuts.ServiceError(shortcuts.Internal, EmailDeleteFailed)
		}
		storage.DeleteMailFile(email.Mailbox.Address, sourceFolderSlug, email.Filename)
		return nil
	}

	if moveError := mailRepo.MoveEmailToFolder(emailID, trashFolder.ID); moveError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, EmailMoveFailed)
	}

	if moveFileError := storage.MoveMailFile(email.Mailbox.Address, sourceFolderSlug, "trash", email.Filename); moveFileError != nil {
		logger.Warnf(LogPrefix, EmailStorageFailed, moveFileError)
	}

	return nil
}

func BulkAction(request BulkActionRequest, mailboxID uint) *shortcuts.Error {
	if len(request.EmailIDs) == 0 {
		return nil
	}

	switch request.Action {
	case "read":
		mailRepo.BulkMarkAsRead(request.EmailIDs)
	case "unread":
		mailRepo.BulkMarkAsUnread(request.EmailIDs)
	case "move":
		if request.FolderID == 0 {
			return shortcuts.ServiceError(shortcuts.BadRequest, FolderNotFound)
		}
		mailRepo.BulkMoveEmails(request.EmailIDs, request.FolderID)
	case "trash":
		trashFolder := mailRepo.FindFolderBySlug(mailboxID, "trash")
		if trashFolder == nil {
			return shortcuts.ServiceError(shortcuts.Internal, FolderNotFound)
		}
		mailRepo.BulkMoveEmails(request.EmailIDs, trashFolder.ID)
	case "delete":
		mailRepo.BulkDeleteEmails(request.EmailIDs)
	}

	return nil
}

func CreateFolder(request CreateFolderRequest) *shortcuts.Error {
	folderName := strings.TrimSpace(request.Name)
	if folderName == "" {
		return shortcuts.ServiceError(shortcuts.BadRequest, FolderNameRequired)
	}

	slug := strings.ToLower(strings.ReplaceAll(folderName, " ", "-"))

	existing := mailRepo.FindFolderBySlug(request.MailboxID, slug)
	if existing != nil {
		return shortcuts.ServiceError(shortcuts.Unprocessable, "A folder with this name already exists.")
	}

	nextPosition := mailRepo.MaxFolderPosition(request.MailboxID) + 1

	folder := &mailModel.Folder{
		Name:      folderName,
		Slug:      slug,
		MailboxID: request.MailboxID,
		IsSystem:  false,
		Position:  nextPosition,
	}

	if createError := mailRepo.CreateFolder(folder); createError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, FolderCreationFailed)
	}

	return nil
}

func DeleteFolder(folderID uint) *shortcuts.Error {
	folder := mailRepo.FindFolderByID(folderID)
	if folder == nil {
		return shortcuts.ServiceError(shortcuts.NotFound, FolderNotFound)
	}

	if folder.IsSystem {
		return shortcuts.ServiceError(shortcuts.Unprocessable, FolderIsSystem)
	}

	emailCount := mailRepo.CountEmailsByFolderID(folderID)
	if emailCount > 0 {
		return shortcuts.ServiceError(shortcuts.Unprocessable, FolderHasEmails)
	}

	if deleteError := mailRepo.DeleteFolder(folder); deleteError != nil {
		return shortcuts.ServiceError(shortcuts.Internal, FolderDeletionFailed)
	}

	return nil
}

func buildFolderCounts(folders []mailModel.Folder) []FolderWithCount {
	foldersWithCounts := make([]FolderWithCount, len(folders))
	for index, folder := range folders {
		foldersWithCounts[index] = FolderWithCount{
			Folder:      folder,
			UnreadCount: mailRepo.CountUnreadByFolderID(folder.ID),
		}
	}
	return foldersWithCounts
}

func generateMessageID(senderAddress string) string {
	parts := strings.Split(senderAddress, "@")
	domainPart := "localhost"
	if len(parts) > 1 {
		domainPart = parts[1]
	}
	return fmt.Sprintf("<%d.%s@%s>", time.Now().UnixNano(), parts[0], domainPart)
}

func generateFilename() string {
	return fmt.Sprintf("%d", time.Now().UnixNano())
}

func readEmailBody(mailboxAddress string, folderSlug string, filename string) string {
	content, readError := storage.ReadMailFile(mailboxAddress, folderSlug, filename)
	if readError != nil {
		logger.Warnf(LogPrefix, EmailFileReadFailed, readError)
		return ""
	}
	return string(content)
}

func deleteDraftWithFile(draftID uint, senderAddress string) {
	draft := mailRepo.FindEmailByIDWithRelations(draftID)
	if draft == nil {
		return
	}
	storage.DeleteMailFile(senderAddress, "drafts", draft.Filename)
	mailRepo.DeleteEmail(draft)
}

var htmlTagPattern = regexp.MustCompile(`<[^>]*>`)

func truncateSnippet(body string) string {
	plainText := htmlTagPattern.ReplaceAllString(body, "")
	plainText = strings.Join(strings.Fields(plainText), " ")
	plainText = strings.TrimSpace(plainText)
	if len(plainText) <= 200 {
		return plainText
	}
	return plainText[:200]
}