From 8ea0dd1610b456b507351f25ae0139340d070447 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 26 Mar 2026 11:19:04 +0100 Subject: [PATCH] feat: add API token expiry notification types --- pkg/models/api_tokens_expiry_notification.go | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 pkg/models/api_tokens_expiry_notification.go diff --git a/pkg/models/api_tokens_expiry_notification.go b/pkg/models/api_tokens_expiry_notification.go new file mode 100644 index 000000000..656ffe279 --- /dev/null +++ b/pkg/models/api_tokens_expiry_notification.go @@ -0,0 +1,78 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package models + +import ( + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/i18n" + "code.vikunja.io/api/pkg/notifications" + "code.vikunja.io/api/pkg/user" +) + +// APITokenExpiringWeekNotification is sent 7 days before an API token expires. +type APITokenExpiringWeekNotification struct { + User *user.User `json:"user"` + Token *APIToken `json:"api_token"` +} + +func (n *APITokenExpiringWeekNotification) ToMail(lang string) *notifications.Mail { + return notifications.NewMail(). + Subject(i18n.T(lang, "notifications.api_token.expiring.week.subject", n.Token.Title)). + Greeting(i18n.T(lang, "notifications.greeting", n.User.GetName())). + Line(i18n.T(lang, "notifications.api_token.expiring.week.message", n.Token.Title, n.Token.ExpiresAt.Format("January 2, 2006"))). + Action(i18n.T(lang, "notifications.api_token.expiring.action"), config.ServicePublicURL.GetString()+"user/settings/api-tokens"). + Line(i18n.T(lang, "notifications.common.have_nice_day")) +} + +func (n *APITokenExpiringWeekNotification) ToDB() any { + return n +} + +func (n *APITokenExpiringWeekNotification) Name() string { + return "api_token.expiring.week" +} + +func (n *APITokenExpiringWeekNotification) SubjectID() int64 { + return n.Token.ID +} + +// APITokenExpiringDayNotification is sent 1 day before an API token expires. +type APITokenExpiringDayNotification struct { + User *user.User `json:"user"` + Token *APIToken `json:"api_token"` +} + +func (n *APITokenExpiringDayNotification) ToMail(lang string) *notifications.Mail { + return notifications.NewMail(). + Subject(i18n.T(lang, "notifications.api_token.expiring.day.subject", n.Token.Title)). + Greeting(i18n.T(lang, "notifications.greeting", n.User.GetName())). + Line(i18n.T(lang, "notifications.api_token.expiring.day.message", n.Token.Title, n.Token.ExpiresAt.Format("January 2, 2006"))). + Action(i18n.T(lang, "notifications.api_token.expiring.action"), config.ServicePublicURL.GetString()+"user/settings/api-tokens"). + Line(i18n.T(lang, "notifications.common.have_nice_day")) +} + +func (n *APITokenExpiringDayNotification) ToDB() any { + return n +} + +func (n *APITokenExpiringDayNotification) Name() string { + return "api_token.expiring.day" +} + +func (n *APITokenExpiringDayNotification) SubjectID() int64 { + return n.Token.ID +}