Skip to content

Commit 126de9f

Browse files
author
Irae Hueck Costa
committed
Fix SMTP integration and clean up unused code
1 parent 8c65f32 commit 126de9f

File tree

4 files changed

+28
-56
lines changed

4 files changed

+28
-56
lines changed

backend/endpoints/recover.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
ctx.NoAutoCleanup()
2525
go func(){
2626
defer ctx.Cleanup()
27-
err = claimedUser.PasswordRecovery(ctx.App.Config.MailgunSecretApiKey)
27+
err = claimedUser.PasswordRecovery(ctx.App.Config.SMTPSecret)
2828
if err != nil {
2929
ctx.App.Logger.Printf("password recovery: %s\n", err)
3030
}

backend/endpoints/register.go

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func init() {
3535
ctx.CatchError(err)
3636
}
3737

38-
user.SendSurvey(ctx.App.Config.SMTPSecret)
3938
ctx.SetSessionUser(userId)
4039
ctx.ReturnUser()
4140

backend/models/user.go

+18-54
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ import (
66
"encoding/base64"
77
"fmt"
88
"strings"
9-
"time"
10-
"context"
119

1210
"github.com/gomodule/redigo/redis"
11+
uuidLib "github.com/google/uuid"
1312
"github.com/ihucos/counter.dev/utils"
13+
"gopkg.in/gomail.v2"
1414
"gorm.io/gorm"
15-
uuidLib "github.com/google/uuid"
16-
"github.com/mailgun/mailgun-go/v4"
1715
)
1816

19-
2017
var surveySender string = "hey@counter.dev"
2118
var surveySubject string = "Is counter.dev useful?"
2219
var surveyText string = `Hello %s,
@@ -30,9 +27,6 @@ Thank you. Your feedback is appreciated!
3027
3128
Your counter.dev team`
3229

33-
34-
35-
3630
var passwordRecoverySender string = "hey@counter.dev"
3731
var passwordRecoverSubject string = "Forgot your password?"
3832
var passwordRecoveryContent string = `Hello %s,
@@ -51,13 +45,12 @@ Cheers,
5145
5246
The counter.dev team`
5347

54-
5548
var uuid2id = map[string]string{}
5649

5750
type User struct {
58-
redis redis.Conn
59-
db *gorm.DB
60-
Id string
51+
redis redis.Conn
52+
db *gorm.DB
53+
Id string
6154
passwordSalt string
6255
}
6356

@@ -93,13 +86,13 @@ func NewUser(conn redis.Conn, userId string, db *gorm.DB, passwordSalt []byte) U
9386
return User{redis: conn, Id: truncate(userId), passwordSalt: string(passwordSalt), db: db}
9487
}
9588

96-
func NewUserByCachedUUID(conn redis.Conn, uuid string, db *gorm.DB, passwordSalt []byte) (User, error){
89+
func NewUserByCachedUUID(conn redis.Conn, uuid string, db *gorm.DB, passwordSalt []byte) (User, error) {
9790
var err error
9891
// Basically we must 'id' here so it can be set inside the if clause
9992
var id string
10093
var ok bool
10194
id, ok = uuid2id[uuid]
102-
if ! ok {
95+
if !ok {
10396
// hit the redis db
10497
id, err = redis.String(conn.Do("HGET", "uuid2id", uuid))
10598
if err == redis.ErrNil {
@@ -150,10 +143,9 @@ func (user User) ReadToken() (string, error) {
150143
if err != nil {
151144
return "", err
152145
}
153-
return base64.URLEncoding.EncodeToString([]byte(token)), nil
146+
return base64.URLEncoding.EncodeToString([]byte(token)), nil
154147
}
155148

156-
157149
func (user User) ReadUUID() (string, error) {
158150
return redis.String(user.redis.Do("HGET", "id2uuid", user.Id))
159151
}
@@ -253,7 +245,6 @@ func (user User) VerifyTmpPassword(tmpPassword string) (bool, error) {
253245
return hashedTmpPassword != "" && hashedTmpPassword == user.hashPassword(tmpPassword), nil
254246
}
255247

256-
257248
func (user User) VerifyPasswordOrTmpPassword(password string) (bool, error) {
258249

259250
// validate as password
@@ -277,7 +268,7 @@ func (user User) VerifyPasswordOrTmpPassword(password string) (bool, error) {
277268
}
278269

279270
func (user User) NewTmpPassword() (string, error) {
280-
expire := 60 * 15 // 15 minutes
271+
expire := 60 * 15 // 15 minutes
281272
tmpPassword := base64.URLEncoding.EncodeToString([]byte(randToken()[:8]))
282273
_, err := user.redis.Do("SETEX",
283274
fmt.Sprintf("tmppwd:%s", user.Id), expire, user.hashPassword(tmpPassword))
@@ -400,8 +391,7 @@ func (user User) HandleSignals(conn redis.Conn, cb func(error)) {
400391
}
401392
}
402393

403-
404-
func (user User) PasswordRecovery(mailgunSecretApiKey string) error {
394+
func (user User) PasswordRecovery(SMTPSecret string) error {
405395
mail, err := user.GetPref("mail")
406396
if err != nil {
407397
return err
@@ -410,57 +400,31 @@ func (user User) PasswordRecovery(mailgunSecretApiKey string) error {
410400
if err != nil {
411401
return err
412402
}
413-
mg := mailgun.NewMailgun("counter.dev", mailgunSecretApiKey)
414403

415404
body := fmt.Sprintf(passwordRecoveryContent, user.Id, user.Id, tmppwd)
416-
message := mg.NewMessage(passwordRecoverySender, passwordRecoverSubject, body, mail)
417-
418-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
419-
defer cancel()
420405

421-
_, _, err = mg.Send(ctx, message)
406+
m := gomail.NewMessage()
407+
m.SetHeader("From", passwordRecoverySender)
408+
m.SetHeader("To", mail)
409+
m.SetHeader("Subject", passwordRecoverSubject)
410+
m.SetBody("text/plain", body)
422411

412+
d := gomail.NewDialer("smtp.protonmail.ch", 587, "hey@counter.dev", SMTPSecret)
413+
err = d.DialAndSend(m)
423414
if err != nil {
424415
return err
425416
}
426417
return nil
427418
}
428419

429-
func (user User) SendSurvey(mailgunSecretApiKey string) error {
430-
mail, err := user.GetPref("mail")
431-
if err != nil {
432-
return err
433-
}
434-
if mail == "" {
435-
return nil
436-
}
437-
mg := mailgun.NewMailgun("counter.dev", mailgunSecretApiKey)
438-
439-
body := fmt.Sprintf(surveyText, user.Id)
440-
message := mg.NewMessage(surveySender, surveySubject, body, mail)
441-
message.SetDeliveryTime(time.Now().Add(24 * 2 * time.Second))
442-
443-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
444-
defer cancel()
445-
446-
_, _, err = mg.Send(ctx, message)
447-
448-
if err != nil {
449-
return err
450-
}
451-
return nil
452-
}
453-
454-
455-
func (user User) RegisterSubscriptionID(subscriptionID string) error{
420+
func (user User) RegisterSubscriptionID(subscriptionID string) error {
456421
_, err := user.redis.Do("HSET", "subscription", user.Id, subscriptionID)
457422
if err != nil {
458423
return err
459424
}
460425
return nil
461426
}
462427

463-
464428
func (user User) ReadSubscriptionID() (string, error) {
465429
val, err := redis.String(user.redis.Do("HGET", "subscription", user.Id))
466430
if err == redis.ErrNil {

static/js/dashboard.js

+9
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ function dGroupDates(dates) {
353353
return acc;
354354
}, {});
355355

356+
357+
let groupedByYear = Object.entries(dates).reduce((acc, val) => {
358+
let group = moment(val[0]).format("YYYY");
359+
acc[group] = (acc[group] || 0) + val[1];
360+
return acc;
361+
}, {});
362+
356363
var groupedDates = dates;
357364
if (Object.keys(groupedDates).length > 31) {
358365
groupedDates = groupedByWeek;
@@ -362,6 +369,8 @@ function dGroupDates(dates) {
362369
}
363370
}
364371

372+
groupedDates = groupedByYear
373+
365374
return [Object.keys(groupedDates), Object.values(groupedDates)];
366375
}
367376

0 commit comments

Comments
 (0)