summaryrefslogtreecommitdiffci
path: root/internal/router/handlers/api-v1-email-is-in-verification.go
blob: b8ae162d84990ee26dc570e709ece36bda304fc0 (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
package handlers

import (
	"fmt"
	"html/template"
	"strings"

	"github.com/SayaAndy/saya-today-web/internal/router"
	"github.com/SayaAndy/saya-today-web/l10n"
	"github.com/gofiber/fiber/v2"
)

type OngoingVerificationHandler struct {
	router.BasicHandler
}

func init() {
	router.Routes = append(router.Routes, &OngoingVerificationHandler{})
}

func (r *OngoingVerificationHandler) Filter() (method string, path string) {
	return "GET", "/api/v1/email/is-in-verification"
}

func (r *OngoingVerificationHandler) IsTemplated() bool {
	return false
}

func (r *OngoingVerificationHandler) TemplatesToInject() []string {
	return []string{"views/partials/personal-page-status.html"}
}

func (r *OngoingVerificationHandler) ToCache() router.CacheSetting {
	return router.Disabled
}

func (r *OngoingVerificationHandler) ToValidateLang() router.LangSetting {
	return router.InReferer
}

func (r *OngoingVerificationHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
	isAllowed, whenAllowed, codeExpiry := supplements.Mailer.IsAllowedToRetryVerification(c.IP())

	sterileDataset := make(map[string]any)
	sterileDataset["code-expiry-time"] = template.HTMLAttr(fmt.Sprintf("data-code-expiry-time=\"%d\"", codeExpiry.UnixMilli()))

	templateMap["StatusId"] = "email-message"
	templateMap["DataAttributes"] = sterileDataset

	if !isAllowed {
		sterileDataset["striked-end-time"] = template.HTMLAttr(fmt.Sprintf("data-striked-end-time=\"%d\"", whenAllowed.UnixMilli()))
		templateMap["Status"] = "Neutral"
		templateMap["Message"] = strings.ReplaceAll(l10n.T.GetPath(lang, "UserProfile", "DelayTilVerification").(string), "{}", whenAllowed.Format("2006-01-02 15:04:05 MST"))
	} else {
		templateMap["Status"] = "OK"
		templateMap["Message"] = ""
	}

	return fiber.StatusOK, nil
}