summaryrefslogtreecommitdiff
path: root/internal/router/handlers/lang-user-unsubscribe.go
blob: e22d8547fdf8d594c688502b59bc8ca81555546e (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
package handlers

import (
	"log/slog"

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

type UnsubscribeHandler struct {
	router.BasicHandler
}

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

func (r *UnsubscribeHandler) Filter() (method string, path string) {
	return "GET", "/:lang/user/unsubscribe"
}

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

func (r *UnsubscribeHandler) TemplatesToInject() []string {
	return []string{"views/pages/unsubscribe-page.html"}
}

func (r *UnsubscribeHandler) ToValidateLang() router.LangSetting {
	return router.InPath
}

func (r *UnsubscribeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
	var statusEmoji, statusText, statusColor string
	var status int

	unsubscribeCode := c.FormValue("code")
	if unsubscribeCode == "" {
		statusColor = "0, 0, 255"
		statusEmoji = "(╭ರ_•́)"
		statusText = l10n.T.GetPath(lang, "UnsubscribePage", "UnsetCode").(string)
		status = fiber.ErrBadRequest.Code
	} else if clientError, serverError := supplements.Mailer.Unsubscribe(unsubscribeCode); clientError != nil {
		slog.Info("got a client error when unsubscribing", slog.String("error", clientError.Error()))
		statusColor = "255, 0, 0"
		statusEmoji = "(͠≖~≖  ͡ )"
		statusText = l10n.T.GetPath(lang, "UnsubscribePage", "InvalidCode").(string)
		status = fiber.ErrBadRequest.Code
	} else if serverError != nil {
		slog.Error("got a server error when unsubscribing", slog.String("error", serverError.Error()))
		statusColor = "255, 128, 0"
		statusEmoji = "( ˶°ㅁ°) !!"
		statusText = l10n.T.GetPath(lang, "UnsubscribePage", "OnServerError").(string)
		status = fiber.ErrInternalServerError.Code
	} else {
		statusColor = "0, 255, 0"
		statusEmoji = "♡⸜(˶˃ ᵕ ˂˶)⸝♡"
		statusText = l10n.T.GetPath(lang, "UnsubscribePage", "Success").(string)
		status = fiber.StatusOK
	}

	templateMap["StatusColor"] = statusColor
	templateMap["StatusEmoji"] = statusEmoji
	templateMap["StatusText"] = statusText

	return status, nil
}