Diffstat (limited to 'internal/mailer')
| -rw-r--r-- | internal/mailer/mailer.go | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/internal/mailer/mailer.go b/internal/mailer/mailer.go index ffa7b30..847ebde 100644 --- a/internal/mailer/mailer.go +++ b/internal/mailer/mailer.go @@ -14,7 +14,7 @@ import ( "sync" "time" - "github.com/SayaAndy/saya-today-web/internal/blog" + "github.com/SayaAndy/saya-today-web/internal/b2" "github.com/SayaAndy/saya-today-web/internal/templatemanager" "github.com/SayaAndy/saya-today-web/locale" "github.com/dgraph-io/ristretto/v2" @@ -349,8 +349,8 @@ func (m *Mailer) GetSubscriptions(userId string) (subscriptionType SubscriptionT defer func(tx *sql.Tx) { if err = tx.Commit(); err != nil { tx.Rollback() + slog.Debug("ended db transaction", slog.String("method", "GetSubscriptions")) } - slog.Debug("ended db transaction", slog.String("method", "GetSubscriptions")) }(tx) hash := m.GetHash(userId) @@ -418,7 +418,7 @@ func (m *Mailer) Subscribe(userIdHash []byte, subscriptionType SubscriptionType, return nil } -func (m *Mailer) NewPost(post *blog.Page) error { +func (m *Mailer) NewPost(post *b2.BlogPage) error { tx, err := m.db.Begin() if err != nil { return fmt.Errorf("failed to initialize transaction with db: %s", err) @@ -452,11 +452,21 @@ rowLoop: continue } + email, lang, err := m.GetInfo(userId) + if err != nil { + slog.Warn("failed to get info about the user", slog.String("error", err.Error()), slog.Int("index", i), slog.String("user_id", base64.RawStdEncoding.EncodeToString(userId))) + continue + } + + if lang != post.Lang { + continue + } + if tagsString == "_all" { usersToSend = append(usersToSend, struct { userId []byte email string - }{userId, ""}) + }{userId, email}) continue } @@ -466,7 +476,7 @@ rowLoop: usersToSend = append(usersToSend, struct { userId []byte email string - }{userId, ""}) + }{userId, email}) continue rowLoop } } @@ -476,25 +486,8 @@ rowLoop: rows.Close() slog.Debug("ended db transaction", slog.String("method", "NewPost")) - for i := range usersToSend { - email, lang, err := m.GetInfo(usersToSend[i].userId) - if err != nil { - slog.Warn("failed to get info about the user", slog.String("error", err.Error()), slog.Int("index", i), slog.String("user_id", base64.RawStdEncoding.EncodeToString(userId))) - continue - } - - if lang != post.Lang { - continue - } - - usersToSend[i].email = email - } - messages := make([]*mail.Msg, 0, len(usersToSend)) for _, user := range usersToSend { - if user.email == "" { - continue - } unsubscribeCodeBytes := make([]byte, 8) rand.Read(unsubscribeCodeBytes) unsubscribeCode := binary.LittleEndian.Uint64(unsubscribeCodeBytes) |