summaryrefslogtreecommitdiff
path: root/controllers/mail.go
blob: 36166ce3f47fa01a6031aafcf6c4cf156c74951e (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
package controllers

import (
	"lain/models"
	"lain/repository"
	"lain/session"
	"lain/utils/meta"
	"lain/utils/shortcuts"

	"github.com/gofiber/fiber/v2"
)

func Mailbox(context *fiber.Ctx) error {
	folderPath := context.Params("*", "inbox")
	if folderPath == "" {
		folderPath = "inbox"
	}

	email, err := session.GetSessionEmail(context)
	if err != nil {
		return InternalServerError(context, err)
	}

	prefs := context.Locals("Preferences").(*models.Preferences)

	folders := repository.GetFolders(email, folderPath)
	displayName := repository.GetFolderDisplayName(email, folderPath)

	page := context.QueryInt("page", 1)
	if page < 1 {
		page = 1
	}

	limit := prefs.EmailsPerPage
	offset := (page - 1) * limit

	emails, err := repository.GetEmails(email, folderPath, limit, offset)
	if err != nil {
		emails = []fiber.Map{}
	}

	meta.SetPageTitle(context, displayName)

	return shortcuts.Render(context, "mail/folder", fiber.Map{
		"Folders": folders,
		"Emails":  emails,
		"Email":   nil,
		"Page":    page,
	})
}