diff options
| author | 2025-10-25 23:25:31 +0700 | |
|---|---|---|
| committer | 2025-10-25 23:25:31 +0700 | |
| commit | dd69b45d3a41be2b5c38892c3b107770a0f859f2 (patch) | |
| tree | d2b6ed2bd645eef448f987e57b97ff918909ea48 /internal/router/api-v1-subs.go | |
| parent | 69b868a7655a25c82ce4a2bd444812be69c8b998 (diff) | |
| parent | 629e275e5270ed146f9cd6dba25383cf80022909 (diff) | |
| download | web-0.11.0.tar.gz web-0.11.0.zip | |
v0.11.0 (#17)v0.11.0
- [feat: send new blog posts to
subscribers](https://github.com/SayaAndy/saya-today-web/commit/adcd8cf82f7ec4027701643c545b1f27a7cfc221)
- [feat: email
linking](https://github.com/SayaAndy/saya-today-web/commit/0d261d2f7d080b610f50102576073e334a89e8cb)
- [feat: subscription
settings](https://github.com/SayaAndy/saya-today-web/commit/4b2da2abbc7376ce0de71a00b18b7091b00dbcd1)
- [feat: allow to unsubscribe from an
email](https://github.com/SayaAndy/saya-today-web/commit/df7a0ae1c0fc177cba2eae8cc73fe21d2afdd229)
Diffstat (limited to 'internal/router/api-v1-subs.go')
| -rw-r--r-- | internal/router/api-v1-subs.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/internal/router/api-v1-subs.go b/internal/router/api-v1-subs.go new file mode 100644 index 0000000..72f16e0 --- /dev/null +++ b/internal/router/api-v1-subs.go @@ -0,0 +1,67 @@ +package router + +import ( + "net/url" + "strings" + + "github.com/SayaAndy/saya-today-web/internal/mailer" + "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_Subs_Put(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: + subscriptionType := c.FormValue("tags") + var subscriptionTypeEnum mailer.SubscriptionType + switch subscriptionType { + case "all": + subscriptionTypeEnum = mailer.All + case "none": + subscriptionTypeEnum = mailer.None + case "specific": + subscriptionTypeEnum = mailer.Specific + default: + return api_v1_email_sendStatusHtml(c, "subs-message", l, "Failed", fiber.StatusUnprocessableEntity, lang, l[lang].UserProfile.SubscribeInvalidType, map[string]string{}) + } + + specificTags := c.FormValue("tags_picked") + if err = Mailer.Subscribe(Mailer.GetHash(id), subscriptionTypeEnum, specificTags); err != nil { + return api_v1_email_sendStatusHtml(c, "subs-message", l, "Failed", fiber.StatusUnprocessableEntity, lang, l[lang].UserProfile.FailedToSubscribe, map[string]string{}) + } + + return api_v1_email_sendStatusHtml(c, "subs-message", l, "OK", fiber.StatusOK, lang, l[lang].UserProfile.SubscribedSuccessfully, map[string]string{}) + } +} |