mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
chore: notify users about failing wakatime connection
This commit is contained in:
@ -16,12 +16,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tplNamePasswordReset = "reset_password"
|
||||
tplNameImportNotification = "import_finished"
|
||||
tplNameReport = "report"
|
||||
subjectPasswordReset = "Wakapi - Password Reset"
|
||||
subjectImportNotification = "Wakapi - Data Import Finished"
|
||||
subjectReport = "Wakapi - Report from %s"
|
||||
tplNamePasswordReset = "reset_password"
|
||||
tplNameImportNotification = "import_finished"
|
||||
tplNameWakatimeFailureNotification = "wakatime_connection_failure"
|
||||
tplNameReport = "report"
|
||||
subjectPasswordReset = "Wakapi - Password Reset"
|
||||
subjectImportNotification = "Wakapi - Data Import Finished"
|
||||
subjectWakatimeFailureNotification = "Wakapi - WakaTime Connection Failure"
|
||||
subjectReport = "Wakapi - Report from %s"
|
||||
)
|
||||
|
||||
type SendingService interface {
|
||||
@ -64,6 +66,23 @@ func (m *MailService) SendPasswordReset(recipient *models.User, resetLink string
|
||||
return m.sendingService.Send(mail)
|
||||
}
|
||||
|
||||
func (m *MailService) SendWakatimeFailureNotification(recipient *models.User, numFailures int) error {
|
||||
tpl, err := getWakatimeFailureNotificationTemplate(WakatimeFailureNotificationNotificationTplData{
|
||||
PublicUrl: m.config.Server.PublicUrl,
|
||||
NumFailures: numFailures,
|
||||
})
|
||||
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: subjectWakatimeFailureNotification,
|
||||
}
|
||||
mail.WithHTML(tpl.String())
|
||||
return m.sendingService.Send(mail)
|
||||
}
|
||||
|
||||
func (m *MailService) SendImportNotification(recipient *models.User, duration time.Duration, numHeartbeats int) error {
|
||||
tpl, err := getImportNotificationTemplate(ImportNotificationTplData{
|
||||
PublicUrl: m.config.Server.PublicUrl,
|
||||
@ -108,6 +127,18 @@ func getPasswordResetTemplate(data PasswordResetTplData) (*bytes.Buffer, error)
|
||||
return &rendered, nil
|
||||
}
|
||||
|
||||
func getWakatimeFailureNotificationTemplate(data WakatimeFailureNotificationNotificationTplData) (*bytes.Buffer, error) {
|
||||
tpl, err := loadTemplate(tplNameWakatimeFailureNotification)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rendered bytes.Buffer
|
||||
if err := tpl.Execute(&rendered, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rendered, nil
|
||||
}
|
||||
|
||||
func getImportNotificationTemplate(data ImportNotificationTplData) (*bytes.Buffer, error) {
|
||||
tpl, err := loadTemplate(tplNameImportNotification)
|
||||
if err != nil {
|
||||
|
@ -12,6 +12,11 @@ type ImportNotificationTplData struct {
|
||||
NumHeartbeats int
|
||||
}
|
||||
|
||||
type WakatimeFailureNotificationNotificationTplData struct {
|
||||
PublicUrl string
|
||||
NumFailures int
|
||||
}
|
||||
|
||||
type ReportTplData struct {
|
||||
Report *models.Report
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ type IProjectLabelService interface {
|
||||
|
||||
type IMailService interface {
|
||||
SendPasswordReset(*models.User, string) error
|
||||
SendWakatimeFailureNotification(*models.User, int) error
|
||||
SendImportNotification(*models.User, time.Duration, int) error
|
||||
SendReport(*models.User, *models.Report) error
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/leandro-lugaresi/hub"
|
||||
"github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/models"
|
||||
@ -13,19 +14,45 @@ import (
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
config *config.Config
|
||||
cache *cache.Cache
|
||||
eventBus *hub.Hub
|
||||
repository repositories.IUserRepository
|
||||
config *config.Config
|
||||
cache *cache.Cache
|
||||
eventBus *hub.Hub
|
||||
mailService IMailService
|
||||
repository repositories.IUserRepository
|
||||
}
|
||||
|
||||
func NewUserService(userRepo repositories.IUserRepository) *UserService {
|
||||
return &UserService{
|
||||
config: config.Get(),
|
||||
eventBus: config.EventBus(),
|
||||
cache: cache.New(1*time.Hour, 2*time.Hour),
|
||||
repository: userRepo,
|
||||
func NewUserService(mailService IMailService, userRepo repositories.IUserRepository) *UserService {
|
||||
srv := &UserService{
|
||||
config: config.Get(),
|
||||
eventBus: config.EventBus(),
|
||||
cache: cache.New(1*time.Hour, 2*time.Hour),
|
||||
mailService: mailService,
|
||||
repository: userRepo,
|
||||
}
|
||||
|
||||
sub1 := srv.eventBus.Subscribe(0, config.EventWakatimeFailure)
|
||||
go func(sub *hub.Subscription) {
|
||||
for m := range sub.Receiver {
|
||||
user := m.Fields[config.FieldUser].(*models.User)
|
||||
n := m.Fields[config.FieldPayload].(int)
|
||||
|
||||
logbuch.Warn("resetting wakatime api key for user %s, because of too many failures (%d)", user.ID, n)
|
||||
|
||||
if _, err := srv.SetWakatimeApiKey(user, ""); err != nil {
|
||||
logbuch.Error("failed to set wakatime api key for user %s", user.ID)
|
||||
}
|
||||
|
||||
if user.Email != "" {
|
||||
if err := mailService.SendWakatimeFailureNotification(user, n); err != nil {
|
||||
logbuch.Error("failed to send wakatime failure notification mail to user %s", user.ID)
|
||||
} else {
|
||||
logbuch.Info("sent wakatime connection failure mail to %s", user.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}(&sub1)
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func (srv *UserService) GetUserById(userId string) (*models.User, error) {
|
||||
|
Reference in New Issue
Block a user