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:
parent
dc0bcbe65d
commit
50c54685ec
@ -1,4 +1,6 @@
|
|||||||
env: production
|
env: production
|
||||||
|
quick_start: false # whether to skip initial tasks on application startup, like summary generation
|
||||||
|
skip_migrations: false # whether to intentionally not run database migrations, only use for dev purposes
|
||||||
|
|
||||||
server:
|
server:
|
||||||
listen_ipv4: 127.0.0.1 # leave blank to disable ipv4
|
listen_ipv4: 127.0.0.1 # leave blank to disable ipv4
|
||||||
@ -44,7 +46,7 @@ db:
|
|||||||
charset: utf8mb4 # only used for mysql connections
|
charset: utf8mb4 # only used for mysql connections
|
||||||
max_conn: 2 # maximum number of concurrent connections to maintain
|
max_conn: 2 # maximum number of concurrent connections to maintain
|
||||||
ssl: false # whether to use tls for db connection (must be true for cockroachdb) (ignored for mysql and sqlite)
|
ssl: false # whether to use tls for db connection (must be true for cockroachdb) (ignored for mysql and sqlite)
|
||||||
automgirate_fail_silently: false # whether to ignore schema auto-migration failures when starting up
|
automigrate_fail_silently: false # whether to ignore schema auto-migration failures when starting up
|
||||||
|
|
||||||
security:
|
security:
|
||||||
password_salt: # change this
|
password_salt: # change this
|
||||||
@ -60,6 +62,14 @@ sentry:
|
|||||||
sample_rate: 0.75 # probability of tracing a request
|
sample_rate: 0.75 # probability of tracing a request
|
||||||
sample_rate_heartbeats: 0.1 # probability of tracing a heartbeat request
|
sample_rate_heartbeats: 0.1 # probability of tracing a heartbeat request
|
||||||
|
|
||||||
|
# only relevant for running wakapi as a hosted service with paid subscriptions and stripe payments
|
||||||
|
subscriptions:
|
||||||
|
enabled: false
|
||||||
|
stripe_api_key:
|
||||||
|
stripe_secret_key:
|
||||||
|
stripe_endpoint_secret:
|
||||||
|
standard_price_id:
|
||||||
|
|
||||||
mail:
|
mail:
|
||||||
enabled: true # whether to enable mails (used for password resets, reports, etc.)
|
enabled: true # whether to enable mails (used for password resets, reports, etc.)
|
||||||
provider: smtp # method for sending mails, currently one of ['smtp', 'mailwhale']
|
provider: smtp # method for sending mails, currently one of ['smtp', 'mailwhale']
|
||||||
@ -78,6 +88,3 @@ mail:
|
|||||||
url:
|
url:
|
||||||
client_id:
|
client_id:
|
||||||
client_secret:
|
client_secret:
|
||||||
|
|
||||||
quick_start: false # whether to skip initial tasks on application startup, like summary generation
|
|
||||||
skip_migrations: false # whether to intentionally not run database migrations, only use for dev purposes
|
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -11,13 +12,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/muety/wakapi/utils"
|
|
||||||
"github.com/robfig/cron/v3"
|
|
||||||
|
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/gorilla/securecookie"
|
"github.com/gorilla/securecookie"
|
||||||
"github.com/jinzhu/configor"
|
"github.com/jinzhu/configor"
|
||||||
"github.com/muety/wakapi/data"
|
"github.com/muety/wakapi/data"
|
||||||
|
"github.com/muety/wakapi/utils"
|
||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,6 +31,7 @@ const (
|
|||||||
KeyLatestTotalUsers = "latest_total_users"
|
KeyLatestTotalUsers = "latest_total_users"
|
||||||
KeyLastImportImport = "last_import"
|
KeyLastImportImport = "last_import"
|
||||||
KeyFirstHeartbeat = "first_heartbeat"
|
KeyFirstHeartbeat = "first_heartbeat"
|
||||||
|
KeySubscriptionNotificationSent = "sub_reminder"
|
||||||
KeyNewsbox = "newsbox"
|
KeyNewsbox = "newsbox"
|
||||||
|
|
||||||
SimpleDateFormat = "2006-01-02"
|
SimpleDateFormat = "2006-01-02"
|
||||||
@ -124,6 +124,7 @@ type serverConfig struct {
|
|||||||
|
|
||||||
type subscriptionsConfig struct {
|
type subscriptionsConfig struct {
|
||||||
Enabled bool `yaml:"enabled" default:"false" env:"WAKAPI_SUBSCRIPTIONS_ENABLED"`
|
Enabled bool `yaml:"enabled" default:"false" env:"WAKAPI_SUBSCRIPTIONS_ENABLED"`
|
||||||
|
ExpiryNotifications bool `yaml:"expiry_notifications" default:"true" env:"WAKAPI_SUBSCRIPTIONS_EXPIRY_NOTIFICATIONS"`
|
||||||
StripeApiKey string `yaml:"stripe_api_key" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_API_KEY"`
|
StripeApiKey string `yaml:"stripe_api_key" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_API_KEY"`
|
||||||
StripeSecretKey string `yaml:"stripe_secret_key" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_SECRET_KEY"`
|
StripeSecretKey string `yaml:"stripe_secret_key" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_SECRET_KEY"`
|
||||||
StripeEndpointSecret string `yaml:"stripe_endpoint_secret" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_ENDPOINT_SECRET"`
|
StripeEndpointSecret string `yaml:"stripe_endpoint_secret" env:"WAKAPI_SUBSCRIPTIONS_STRIPE_ENDPOINT_SECRET"`
|
||||||
@ -412,7 +413,11 @@ func Load(version string) *Config {
|
|||||||
if config.App.DataRetentionMonths <= 0 {
|
if config.App.DataRetentionMonths <= 0 {
|
||||||
logbuch.Info("disabling data retention policy, keeping data forever")
|
logbuch.Info("disabling data retention policy, keeping data forever")
|
||||||
} else {
|
} else {
|
||||||
logbuch.Info("data retention policy set to keep data for %d months at max", config.App.DataRetentionMonths)
|
dataRetentionWarning := fmt.Sprintf("⚠️ data retention policy will cause user data older than %d months to be deleted", config.App.DataRetentionMonths)
|
||||||
|
if config.Subscriptions.Enabled {
|
||||||
|
dataRetentionWarning += " (except for users with active subscriptions)"
|
||||||
|
}
|
||||||
|
logbuch.Warn(dataRetentionWarning)
|
||||||
}
|
}
|
||||||
|
|
||||||
// some validation checks
|
// some validation checks
|
||||||
|
@ -15,6 +15,7 @@ const (
|
|||||||
QueueDefault = "wakapi.default"
|
QueueDefault = "wakapi.default"
|
||||||
QueueProcessing = "wakapi.processing"
|
QueueProcessing = "wakapi.processing"
|
||||||
QueueReports = "wakapi.reports"
|
QueueReports = "wakapi.reports"
|
||||||
|
QueueMails = "wakapi.mail"
|
||||||
QueueImports = "wakapi.imports"
|
QueueImports = "wakapi.imports"
|
||||||
QueueHousekeeping = "wakapi.housekeeping"
|
QueueHousekeeping = "wakapi.housekeeping"
|
||||||
)
|
)
|
||||||
@ -31,6 +32,7 @@ func init() {
|
|||||||
InitQueue(QueueDefault, 1)
|
InitQueue(QueueDefault, 1)
|
||||||
InitQueue(QueueProcessing, halfCPUs())
|
InitQueue(QueueProcessing, halfCPUs())
|
||||||
InitQueue(QueueReports, 1)
|
InitQueue(QueueReports, 1)
|
||||||
|
InitQueue(QueueMails, 1)
|
||||||
InitQueue(QueueImports, 1)
|
InitQueue(QueueImports, 1)
|
||||||
InitQueue(QueueHousekeeping, halfCPUs())
|
InitQueue(QueueHousekeeping, halfCPUs())
|
||||||
}
|
}
|
||||||
|
4
main.go
4
main.go
@ -183,7 +183,7 @@ func main() {
|
|||||||
reportService = services.NewReportService(summaryService, userService, mailService)
|
reportService = services.NewReportService(summaryService, userService, mailService)
|
||||||
diagnosticsService = services.NewDiagnosticsService(diagnosticsRepository)
|
diagnosticsService = services.NewDiagnosticsService(diagnosticsRepository)
|
||||||
housekeepingService = services.NewHousekeepingService(userService, heartbeatService, summaryService)
|
housekeepingService = services.NewHousekeepingService(userService, heartbeatService, summaryService)
|
||||||
miscService = services.NewMiscService(userService, heartbeatService, summaryService, keyValueService)
|
miscService = services.NewMiscService(userService, heartbeatService, summaryService, keyValueService, mailService)
|
||||||
|
|
||||||
// Schedule background tasks
|
// Schedule background tasks
|
||||||
go aggregationService.Schedule()
|
go aggregationService.Schedule()
|
||||||
@ -216,7 +216,7 @@ func main() {
|
|||||||
// MVC Handlers
|
// MVC Handlers
|
||||||
summaryHandler := routes.NewSummaryHandler(summaryService, userService)
|
summaryHandler := routes.NewSummaryHandler(summaryService, userService)
|
||||||
settingsHandler := routes.NewSettingsHandler(userService, heartbeatService, summaryService, aliasService, aggregationService, languageMappingService, projectLabelService, keyValueService, mailService)
|
settingsHandler := routes.NewSettingsHandler(userService, heartbeatService, summaryService, aliasService, aggregationService, languageMappingService, projectLabelService, keyValueService, mailService)
|
||||||
subscriptionHandler := routes.NewSubscriptionHandler(userService, mailService)
|
subscriptionHandler := routes.NewSubscriptionHandler(userService, mailService, keyValueService)
|
||||||
leaderboardHandler := routes.NewLeaderboardHandler(userService, leaderboardService)
|
leaderboardHandler := routes.NewLeaderboardHandler(userService, leaderboardService)
|
||||||
homeHandler := routes.NewHomeHandler(keyValueService)
|
homeHandler := routes.NewHomeHandler(keyValueService)
|
||||||
loginHandler := routes.NewLoginHandler(userService, mailService)
|
loginHandler := routes.NewLoginHandler(userService, mailService)
|
||||||
|
@ -135,6 +135,22 @@ func (u *User) HasActiveSubscriptionStrict() bool {
|
|||||||
return u.SubscribedUntil != nil && u.SubscribedUntil.T().After(time.Now())
|
return u.SubscribedUntil != nil && u.SubscribedUntil.T().After(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SubscriptionExpiredSince returns if a user's subscription has expiration and the duration since when that happened.
|
||||||
|
// Returns (false, <negative duration>), if subscription hasn't expired, yet.
|
||||||
|
// Returns (false, 0), if subscriptions are not enabled in the first place.
|
||||||
|
// Returns (true, <very long duration>), if the user has never had a subscription.
|
||||||
|
func (u *User) SubscriptionExpiredSince() (bool, time.Duration) {
|
||||||
|
cfg := conf.Get()
|
||||||
|
if !cfg.Subscriptions.Enabled {
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
if u.SubscribedUntil == nil {
|
||||||
|
return true, 99 * 365 * 24 * time.Hour
|
||||||
|
}
|
||||||
|
diff := time.Now().Sub(u.SubscribedUntil.T())
|
||||||
|
return diff >= 0, diff
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) MinDataAge() time.Time {
|
func (u *User) MinDataAge() time.Time {
|
||||||
retentionMonths := conf.Get().App.DataRetentionMonths
|
retentionMonths := conf.Get().App.DataRetentionMonths
|
||||||
if retentionMonths <= 0 || u.HasActiveSubscription() {
|
if retentionMonths <= 0 || u.HasActiveSubscription() {
|
||||||
|
@ -26,12 +26,14 @@ type SubscriptionHandler struct {
|
|||||||
config *conf.Config
|
config *conf.Config
|
||||||
userSrvc services.IUserService
|
userSrvc services.IUserService
|
||||||
mailSrvc services.IMailService
|
mailSrvc services.IMailService
|
||||||
|
keyValueSrvc services.IKeyValueService
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionHandler(
|
func NewSubscriptionHandler(
|
||||||
userService services.IUserService,
|
userService services.IUserService,
|
||||||
mailService services.IMailService,
|
mailService services.IMailService,
|
||||||
|
keyValueService services.IKeyValueService,
|
||||||
) *SubscriptionHandler {
|
) *SubscriptionHandler {
|
||||||
config := conf.Get()
|
config := conf.Get()
|
||||||
|
|
||||||
@ -51,6 +53,7 @@ func NewSubscriptionHandler(
|
|||||||
config: config,
|
config: config,
|
||||||
userSrvc: userService,
|
userSrvc: userService,
|
||||||
mailSrvc: mailService,
|
mailSrvc: mailService,
|
||||||
|
keyValueSrvc: keyValueService,
|
||||||
httpClient: &http.Client{Timeout: 10 * time.Second},
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,11 +207,14 @@ func (h *SubscriptionHandler) GetCheckoutCancel(w http.ResponseWriter, r *http.R
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *SubscriptionHandler) handleSubscriptionEvent(subscription *stripe.Subscription, user *models.User) error {
|
func (h *SubscriptionHandler) handleSubscriptionEvent(subscription *stripe.Subscription, user *models.User) error {
|
||||||
|
var hasSubscribed bool
|
||||||
|
|
||||||
switch subscription.Status {
|
switch subscription.Status {
|
||||||
case "active":
|
case "active":
|
||||||
until := models.CustomTime(time.Unix(subscription.CurrentPeriodEnd, 0))
|
until := models.CustomTime(time.Unix(subscription.CurrentPeriodEnd, 0))
|
||||||
|
|
||||||
if user.SubscribedUntil == nil || !user.SubscribedUntil.T().Equal(until.T()) {
|
if user.SubscribedUntil == nil || !user.SubscribedUntil.T().Equal(until.T()) {
|
||||||
|
hasSubscribed = true
|
||||||
user.SubscribedUntil = &until
|
user.SubscribedUntil = &until
|
||||||
logbuch.Info("user %s got active subscription %s until %v", user.ID, subscription.ID, user.SubscribedUntil)
|
logbuch.Info("user %s got active subscription %s until %v", user.ID, subscription.ID, user.SubscribedUntil)
|
||||||
}
|
}
|
||||||
@ -224,6 +230,9 @@ func (h *SubscriptionHandler) handleSubscriptionEvent(subscription *stripe.Subsc
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err := h.userSrvc.Update(user)
|
_, err := h.userSrvc.Update(user)
|
||||||
|
if err == nil && hasSubscribed {
|
||||||
|
go h.clearSubscriptionNotificationStatus(user.ID)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,3 +274,10 @@ func (h *SubscriptionHandler) findStripeCustomerByEmail(email string) (*stripe.C
|
|||||||
return nil, errors.New("no customer found with given criteria")
|
return nil, errors.New("no customer found with given criteria")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *SubscriptionHandler) clearSubscriptionNotificationStatus(userId string) {
|
||||||
|
key := fmt.Sprintf("%s_%s", conf.KeySubscriptionNotificationSent, userId)
|
||||||
|
if err := h.keyValueSrvc.DeleteString(key); err != nil {
|
||||||
|
conf.Log().Error("failed to delete '%s', %v", key, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,10 +19,12 @@ const (
|
|||||||
tplNameImportNotification = "import_finished"
|
tplNameImportNotification = "import_finished"
|
||||||
tplNameWakatimeFailureNotification = "wakatime_connection_failure"
|
tplNameWakatimeFailureNotification = "wakatime_connection_failure"
|
||||||
tplNameReport = "report"
|
tplNameReport = "report"
|
||||||
|
tplNameSubscriptionNotification = "subscription_expiring"
|
||||||
subjectPasswordReset = "Wakapi - Password Reset"
|
subjectPasswordReset = "Wakapi - Password Reset"
|
||||||
subjectImportNotification = "Wakapi - Data Import Finished"
|
subjectImportNotification = "Wakapi - Data Import Finished"
|
||||||
subjectWakatimeFailureNotification = "Wakapi - WakaTime Connection Failure"
|
subjectWakatimeFailureNotification = "Wakapi - WakaTime Connection Failure"
|
||||||
subjectReport = "Wakapi - Report from %s"
|
subjectReport = "Wakapi - Report from %s"
|
||||||
|
subjectSubscriptionNotification = "Wakapi - Subscription expiring / expired"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SendingService interface {
|
type SendingService interface {
|
||||||
@ -122,6 +124,24 @@ func (m *MailService) SendReport(recipient *models.User, report *models.Report)
|
|||||||
return m.sendingService.Send(mail)
|
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) {
|
func (m *MailService) getPasswordResetTemplate(data PasswordResetTplData) (*bytes.Buffer, error) {
|
||||||
var rendered bytes.Buffer
|
var rendered bytes.Buffer
|
||||||
if err := m.templates[m.fmtName(tplNamePasswordReset)].Execute(&rendered, data); err != nil {
|
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
|
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 {
|
func (m *MailService) fmtName(name string) string {
|
||||||
return fmt.Sprintf("%s.tpl.html", name)
|
return fmt.Sprintf("%s.tpl.html", name)
|
||||||
}
|
}
|
||||||
|
@ -20,3 +20,9 @@ type WakatimeFailureNotificationNotificationTplData struct {
|
|||||||
type ReportTplData struct {
|
type ReportTplData struct {
|
||||||
Report *models.Report
|
Report *models.Report
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SubscriptionNotificationTplData struct {
|
||||||
|
PublicUrl string
|
||||||
|
HasExpired bool
|
||||||
|
DataRetentionMonths int
|
||||||
|
}
|
||||||
|
101
services/misc.go
101
services/misc.go
@ -2,12 +2,14 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
"github.com/emvi/logbuch"
|
"github.com/emvi/logbuch"
|
||||||
"github.com/muety/artifex/v2"
|
"github.com/muety/artifex/v2"
|
||||||
"github.com/muety/wakapi/config"
|
"github.com/muety/wakapi/config"
|
||||||
"github.com/muety/wakapi/utils"
|
"github.com/muety/wakapi/utils"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -17,6 +19,11 @@ import (
|
|||||||
const (
|
const (
|
||||||
countUsersEvery = 1 * time.Hour
|
countUsersEvery = 1 * time.Hour
|
||||||
computeOldestDataEvery = 6 * time.Hour
|
computeOldestDataEvery = 6 * time.Hour
|
||||||
|
notifyExpiringSubscriptionsEvery = 12 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
notifyBeforeSubscriptionExpiry = 7 * 24 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
var countLock = sync.Mutex{}
|
var countLock = sync.Mutex{}
|
||||||
@ -28,19 +35,23 @@ type MiscService struct {
|
|||||||
heartbeatService IHeartbeatService
|
heartbeatService IHeartbeatService
|
||||||
summaryService ISummaryService
|
summaryService ISummaryService
|
||||||
keyValueService IKeyValueService
|
keyValueService IKeyValueService
|
||||||
|
mailService IMailService
|
||||||
queueDefault *artifex.Dispatcher
|
queueDefault *artifex.Dispatcher
|
||||||
queueWorkers *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{
|
return &MiscService{
|
||||||
config: config.Get(),
|
config: config.Get(),
|
||||||
userService: userService,
|
userService: userService,
|
||||||
heartbeatService: heartbeatService,
|
heartbeatService: heartbeatService,
|
||||||
summaryService: summaryService,
|
summaryService: summaryService,
|
||||||
keyValueService: keyValueService,
|
keyValueService: keyValueService,
|
||||||
|
mailService: mailService,
|
||||||
queueDefault: config.GetDefaultQueue(),
|
queueDefault: config.GetDefaultQueue(),
|
||||||
queueWorkers: config.GetQueue(config.QueueProcessing),
|
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)
|
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
|
// run once initially for a fresh instance
|
||||||
if !srv.existsUsersTotalTime() {
|
if !srv.existsUsersTotalTime() {
|
||||||
if err := srv.queueDefault.Dispatch(srv.CountTotalTime); err != nil {
|
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)
|
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() {
|
func (srv *MiscService) CountTotalTime() {
|
||||||
@ -78,6 +101,7 @@ func (srv *MiscService) CountTotalTime() {
|
|||||||
users, err := srv.userService.GetAll()
|
users, err := srv.userService.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.Log().Error("failed to fetch users for time counting, %v", err)
|
config.Log().Error("failed to fetch users for time counting, %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var totalTime = atomic.NewDuration(0)
|
var totalTime = atomic.NewDuration(0)
|
||||||
@ -130,6 +154,7 @@ func (srv *MiscService) ComputeOldestHeartbeats() {
|
|||||||
results, err := srv.heartbeatService.GetFirstByUsers()
|
results, err := srv.heartbeatService.GetFirstByUsers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.Log().Error("failed to compute users' first data, %v", err)
|
config.Log().Error("failed to compute users' first data, %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entry := range results {
|
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 {
|
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)
|
result, err := srv.summaryService.Aliased(time.Time{}, time.Now(), &models.User{ID: userId}, srv.summaryService.Retrieve, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -159,6 +233,26 @@ func (srv *MiscService) countUserTotalTime(userId string) time.Duration {
|
|||||||
return result.TotalTime()
|
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 {
|
func (srv *MiscService) existsUsersTotalTime() bool {
|
||||||
results, _ := srv.keyValueService.GetByPrefix(config.KeyLatestTotalTime)
|
results, _ := srv.keyValueService.GetByPrefix(config.KeyLatestTotalTime)
|
||||||
return len(results) > 0
|
return len(results) > 0
|
||||||
@ -168,3 +262,8 @@ func (srv *MiscService) existsUsersFirstData() bool {
|
|||||||
results, _ := srv.keyValueService.GetByPrefix(config.KeyFirstHeartbeat)
|
results, _ := srv.keyValueService.GetByPrefix(config.KeyFirstHeartbeat)
|
||||||
return len(results) > 0
|
return len(results) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *MiscService) existsSubscriptionNotifications() bool {
|
||||||
|
results, _ := srv.keyValueService.GetByPrefix(config.KeySubscriptionNotificationSent)
|
||||||
|
return len(results) > 0
|
||||||
|
}
|
||||||
|
@ -80,6 +80,7 @@ type IMailService interface {
|
|||||||
SendWakatimeFailureNotification(*models.User, int) error
|
SendWakatimeFailureNotification(*models.User, int) error
|
||||||
SendImportNotification(*models.User, time.Duration, int) error
|
SendImportNotification(*models.User, time.Duration, int) error
|
||||||
SendReport(*models.User, *models.Report) error
|
SendReport(*models.User, *models.Report) error
|
||||||
|
SendSubscriptionNotification(*models.User, bool) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type IDurationService interface {
|
type IDurationService interface {
|
||||||
|
62
views/mail/subscription_expiring.tpl.html
Normal file
62
views/mail/subscription_expiring.tpl.html
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
{{ template "head.tpl.html" . }}
|
||||||
|
|
||||||
|
<body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
|
||||||
|
<table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">
|
||||||
|
<tr>
|
||||||
|
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;"> </td>
|
||||||
|
<td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">
|
||||||
|
{{ template "theader.tpl.html" . }}
|
||||||
|
|
||||||
|
<div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">
|
||||||
|
<table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">
|
||||||
|
<tr>
|
||||||
|
<td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">
|
||||||
|
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">
|
||||||
|
<tr>
|
||||||
|
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;">
|
||||||
|
{{ if .HasExpired }}
|
||||||
|
<p style="font-family: sans-serif; font-size: 18px; font-weight: 500; margin: 0; Margin-bottom: 15px;">Subscription expired</p>
|
||||||
|
{{ else }}
|
||||||
|
<p style="font-family: sans-serif; font-size: 18px; font-weight: 500; margin: 0; Margin-bottom: 15px;">Subscription about to expire</p>
|
||||||
|
{{ end }}
|
||||||
|
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;">
|
||||||
|
{{ if .HasExpired }}
|
||||||
|
Your Wakapi subscription has expired.
|
||||||
|
{{ else }}
|
||||||
|
Your Wakapi subscription will expire soon.
|
||||||
|
{{ end }}
|
||||||
|
All coding activity older than {{ .DataRetentionMonths }} months will be deleted soon. Please refer to <a href="https://github.com/muety/wakapi/discussions/447" target="_blank">this article</a> for further details on subscriptions.
|
||||||
|
</p>
|
||||||
|
<table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td align="left" style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding-bottom: 15px;">
|
||||||
|
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #2F855A; border-radius: 5px; text-align: center;"> <a href="{{ .PublicUrl }}/settings#subscription" target="_blank" style="display: inline-block; color: #ffffff; background-color: #2F855A; border: solid 1px #2F855A; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none; font-size: 14px; font-weight: bold; margin: 0; padding: 12px 25px; text-transform: capitalize; border-color: #2F855A;">Go to settings</a> </td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
{{ template "tfooter.tpl.html" . }}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;"> </td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user