1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: subscription expiry notification mails

This commit is contained in:
Ferdinand Mütsch
2022-12-29 17:12:34 +01:00
parent dc0bcbe65d
commit 50c54685ec
11 changed files with 269 additions and 27 deletions

View File

@@ -19,10 +19,12 @@ const (
tplNameImportNotification = "import_finished"
tplNameWakatimeFailureNotification = "wakatime_connection_failure"
tplNameReport = "report"
tplNameSubscriptionNotification = "subscription_expiring"
subjectPasswordReset = "Wakapi - Password Reset"
subjectImportNotification = "Wakapi - Data Import Finished"
subjectWakatimeFailureNotification = "Wakapi - WakaTime Connection Failure"
subjectReport = "Wakapi - Report from %s"
subjectSubscriptionNotification = "Wakapi - Subscription expiring / expired"
)
type SendingService interface {
@@ -122,6 +124,24 @@ func (m *MailService) SendReport(recipient *models.User, report *models.Report)
return m.sendingService.Send(mail)
}
func (m *MailService) SendSubscriptionNotification(recipient *models.User, hasExpired bool) error {
tpl, err := m.getSubscriptionNotificationTemplate(SubscriptionNotificationTplData{
PublicUrl: m.config.Server.PublicUrl,
DataRetentionMonths: m.config.App.DataRetentionMonths,
HasExpired: hasExpired,
})
if err != nil {
return err
}
mail := &models.Mail{
From: models.MailAddress(m.config.Mail.Sender),
To: models.MailAddresses([]models.MailAddress{models.MailAddress(recipient.Email)}),
Subject: subjectSubscriptionNotification,
}
mail.WithHTML(tpl.String())
return m.sendingService.Send(mail)
}
func (m *MailService) getPasswordResetTemplate(data PasswordResetTplData) (*bytes.Buffer, error) {
var rendered bytes.Buffer
if err := m.templates[m.fmtName(tplNamePasswordReset)].Execute(&rendered, data); err != nil {
@@ -154,6 +174,14 @@ func (m *MailService) getReportTemplate(data ReportTplData) (*bytes.Buffer, erro
return &rendered, nil
}
func (m *MailService) getSubscriptionNotificationTemplate(data SubscriptionNotificationTplData) (*bytes.Buffer, error) {
var rendered bytes.Buffer
if err := m.templates[m.fmtName(tplNameSubscriptionNotification)].Execute(&rendered, data); err != nil {
return nil, err
}
return &rendered, nil
}
func (m *MailService) fmtName(name string) string {
return fmt.Sprintf("%s.tpl.html", name)
}

View File

@@ -20,3 +20,9 @@ type WakatimeFailureNotificationNotificationTplData struct {
type ReportTplData struct {
Report *models.Report
}
type SubscriptionNotificationTplData struct {
PublicUrl string
HasExpired bool
DataRetentionMonths int
}

View File

@@ -2,12 +2,14 @@ package services
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
"github.com/emvi/logbuch"
"github.com/muety/artifex/v2"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/utils"
"go.uber.org/atomic"
"strconv"
"strings"
"sync"
"time"
@@ -15,8 +17,13 @@ import (
)
const (
countUsersEvery = 1 * time.Hour
computeOldestDataEvery = 6 * time.Hour
countUsersEvery = 1 * time.Hour
computeOldestDataEvery = 6 * time.Hour
notifyExpiringSubscriptionsEvery = 12 * time.Hour
)
const (
notifyBeforeSubscriptionExpiry = 7 * 24 * time.Hour
)
var countLock = sync.Mutex{}
@@ -28,19 +35,23 @@ type MiscService struct {
heartbeatService IHeartbeatService
summaryService ISummaryService
keyValueService IKeyValueService
mailService IMailService
queueDefault *artifex.Dispatcher
queueWorkers *artifex.Dispatcher
queueMails *artifex.Dispatcher
}
func NewMiscService(userService IUserService, heartbeatService IHeartbeatService, summaryService ISummaryService, keyValueService IKeyValueService) *MiscService {
func NewMiscService(userService IUserService, heartbeatService IHeartbeatService, summaryService ISummaryService, keyValueService IKeyValueService, mailService IMailService) *MiscService {
return &MiscService{
config: config.Get(),
userService: userService,
heartbeatService: heartbeatService,
summaryService: summaryService,
keyValueService: keyValueService,
mailService: mailService,
queueDefault: config.GetDefaultQueue(),
queueWorkers: config.GetQueue(config.QueueProcessing),
queueMails: config.GetQueue(config.QueueMails),
}
}
@@ -55,6 +66,13 @@ func (srv *MiscService) Schedule() {
config.Log().Error("failed to schedule first data computing jobs, %v", err)
}
if srv.config.Subscriptions.Enabled && srv.config.Subscriptions.ExpiryNotifications && srv.config.App.DataRetentionMonths > 0 {
logbuch.Info("scheduling subscription notifications")
if _, err := srv.queueDefault.DispatchEvery(srv.ComputeOldestHeartbeats, notifyExpiringSubscriptionsEvery); err != nil {
config.Log().Error("failed to schedule subscription notification jobs, %v", err)
}
}
// run once initially for a fresh instance
if !srv.existsUsersTotalTime() {
if err := srv.queueDefault.Dispatch(srv.CountTotalTime); err != nil {
@@ -66,6 +84,11 @@ func (srv *MiscService) Schedule() {
config.Log().Error("failed to dispatch first data computing jobs, %v", err)
}
}
if !srv.existsSubscriptionNotifications() && srv.config.Subscriptions.Enabled && srv.config.Subscriptions.ExpiryNotifications && srv.config.App.DataRetentionMonths > 0 {
if err := srv.queueDefault.Dispatch(srv.NotifyExpiringSubscription); err != nil {
config.Log().Error("failed to schedule subscription notification jobs, %v", err)
}
}
}
func (srv *MiscService) CountTotalTime() {
@@ -78,6 +101,7 @@ func (srv *MiscService) CountTotalTime() {
users, err := srv.userService.GetAll()
if err != nil {
config.Log().Error("failed to fetch users for time counting, %v", err)
return
}
var totalTime = atomic.NewDuration(0)
@@ -130,6 +154,7 @@ func (srv *MiscService) ComputeOldestHeartbeats() {
results, err := srv.heartbeatService.GetFirstByUsers()
if err != nil {
config.Log().Error("failed to compute users' first data, %v", err)
return
}
for _, entry := range results {
@@ -150,6 +175,55 @@ func (srv *MiscService) ComputeOldestHeartbeats() {
}
}
// NotifyExpiringSubscription sends a reminder e-mail to all users, notifying them if their subscription has expired or is about to, given these conditions:
// - Data cleanup is enabled on the server (non-zero retention time)
// - Subscriptions are enabled on the server (aka. users can do something about their old data getting cleaned up)
// - User has an e-mail address configured
// - User's subscription has expired or is about to expire soon
// - The user has gotten no such e-mail before recently
// Note: only one mail will be sent for either "expired" or "about to expire" state.
func (srv *MiscService) NotifyExpiringSubscription() {
if srv.config.App.DataRetentionMonths <= 0 || !srv.config.Subscriptions.Enabled {
return
}
logbuch.Info("notifying users about soon to expire subscriptions")
users, err := srv.userService.GetAll()
if err != nil {
config.Log().Error("failed to fetch users for subscription notifications, %v", err)
return
}
var subscriptionReminders map[string][]*models.KeyStringValue
if result, err := srv.keyValueService.GetByPrefix(config.KeySubscriptionNotificationSent); err == nil {
subscriptionReminders = slice.GroupWith[*models.KeyStringValue, string](result, func(kv *models.KeyStringValue) string {
return strings.TrimPrefix(kv.Key, config.KeySubscriptionNotificationSent+"_")
})
} else {
config.Log().Error("failed to fetch key-values for subscription notifications, %v", err)
return
}
for _, u := range users {
if u.HasActiveSubscription() && u.Email == "" {
config.Log().Warn("invalid state: user '%s' has active subscription but no e-mail address set")
}
// skip users without e-mail address
// skip users who already received a notification before
// skip users who either never had a subscription before or intentionally deleted it
if _, ok := subscriptionReminders[u.ID]; ok || u.Email == "" || u.SubscribedUntil == nil {
continue
}
expired, expiredSince := u.SubscriptionExpiredSince()
if expired || (expiredSince < 0 && expiredSince*-1 <= notifyBeforeSubscriptionExpiry) {
srv.sendSubscriptionNotificationScheduled(u, expired)
}
}
}
func (srv *MiscService) countUserTotalTime(userId string) time.Duration {
result, err := srv.summaryService.Aliased(time.Time{}, time.Now(), &models.User{ID: userId}, srv.summaryService.Retrieve, nil, false)
if err != nil {
@@ -159,6 +233,26 @@ func (srv *MiscService) countUserTotalTime(userId string) time.Duration {
return result.TotalTime()
}
func (srv *MiscService) sendSubscriptionNotificationScheduled(user *models.User, hasExpired bool) {
u := *user
srv.queueMails.Dispatch(func() {
logbuch.Info("sending subscription expiry notification mail to %s (expired: %v)", u.ID, hasExpired)
defer time.Sleep(10 * time.Second)
if err := srv.mailService.SendSubscriptionNotification(&u, hasExpired); err != nil {
config.Log().Error("failed to send subscription notification mail to user '%s', %v", u.ID, err)
return
}
if err := srv.keyValueService.PutString(&models.KeyStringValue{
Key: fmt.Sprintf("%s_%s", config.KeySubscriptionNotificationSent, u.ID),
Value: time.Now().Format(time.RFC822Z),
}); err != nil {
config.Log().Error("failed to update subscription notification status key-value for user %s, %v", u.ID, err)
}
})
}
func (srv *MiscService) existsUsersTotalTime() bool {
results, _ := srv.keyValueService.GetByPrefix(config.KeyLatestTotalTime)
return len(results) > 0
@@ -168,3 +262,8 @@ func (srv *MiscService) existsUsersFirstData() bool {
results, _ := srv.keyValueService.GetByPrefix(config.KeyFirstHeartbeat)
return len(results) > 0
}
func (srv *MiscService) existsSubscriptionNotifications() bool {
results, _ := srv.keyValueService.GetByPrefix(config.KeySubscriptionNotificationSent)
return len(results) > 0
}

View File

@@ -80,6 +80,7 @@ type IMailService interface {
SendWakatimeFailureNotification(*models.User, int) error
SendImportNotification(*models.User, time.Duration, int) error
SendReport(*models.User, *models.Report) error
SendSubscriptionNotification(*models.User, bool) error
}
type IDurationService interface {