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

import (
	"fmt"
	"net/url"
	"strings"

	"github.com/SayaAndy/saya-today-web/locale"
	"github.com/gofiber/fiber/v2"
)

func init() {
	assert(0, tm.Add("personal-page-status", "views/partials/personal-page-status.html"))
}

func Api_V1_Email_IsInVerification(l map[string]*locale.LocaleConfig) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)

		id := c.IP()
		lang := "en"

		var referer, path string
		var pathParts []string
		var urlStruct *url.URL
		var err error

		referer = c.Get("Referer", "")
		if referer == "" {
			goto skipFetchingLang
		}

		urlStruct, err = url.ParseRequestURI(referer)
		if err != nil {
			goto skipFetchingLang
		}

		path = urlStruct.EscapedPath()
		pathParts = strings.Split(strings.Trim(path, "/"), "/")
		if len(pathParts) == 0 {
			goto skipFetchingLang
		}

		lang = pathParts[0]

	skipFetchingLang:
		isAllowed, whenAllowed, codeExpiry := Mailer.IsAllowedToRetryVerification(id)
		if !isAllowed {
			return api_v1_email_sendStatusHtml(c, "email-message", l, "Neutral", fiber.StatusOK, lang, strings.ReplaceAll(l[lang].UserProfile.DelayTilVerification, "{}", whenAllowed.Format("2006-01-02 15:04:05 MST")), map[string]string{
				"striked-end-time": fmt.Sprint(whenAllowed.UnixMilli()),
				"code-expiry-time": fmt.Sprint(codeExpiry.UnixMilli()),
			})
		}
		return api_v1_email_sendStatusHtml(c, "email-message", l, "OK", fiber.StatusOK, lang, "", map[string]string{
			"code-expiry-time": fmt.Sprint(codeExpiry.UnixMilli()),
		})
	}
}