Skip to content

Commit 119c7dd

Browse files
committed
Merge branch 'feat/1.1.2/notification' into test
2 parents 907f5ca + fce6e1d commit 119c7dd

26 files changed

Lines changed: 177 additions & 102 deletions

cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package answercmd
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"time"
@@ -39,7 +40,7 @@ var (
3940
// @name Authorization
4041
func Main() {
4142
log.SetLogger(zap.NewLogger(
42-
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
43+
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath)))
4344
Execute()
4445
}
4546

@@ -59,7 +60,7 @@ func runApp() {
5960
fmt.Println("answer Version:", constant.Version, " Revision:", constant.Revision)
6061

6162
defer cleanup()
62-
if err := app.Run(); err != nil {
63+
if err := app.Run(context.Background()); err != nil {
6364
panic(err)
6465
}
6566
}

cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ require (
2828
github.com/ory/dockertest/v3 v3.9.1
2929
github.com/robfig/cron/v3 v3.0.1
3030
github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405
31-
github.com/segmentfault/pacman v1.0.4
32-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0
31+
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f
32+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822083413-c0075a2d401f
3333
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05
3434
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150
3535
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05

go.sum

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZw
9191
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
9292
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
9393
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
94+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
9495
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
9596
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
9697
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
@@ -640,10 +641,14 @@ github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405 h1:2i
640641
github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405/go.mod h1:rIxVzVLKlBwLxO+lC+k/I4HJfRQcemg/f/76Xmmzsec=
641642
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
642643
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
643-
github.com/segmentfault/pacman v1.0.4 h1:6UIXuMHUeYMWe5toflV9SXZQizRny1RczjZJLj9kul0=
644-
github.com/segmentfault/pacman v1.0.4/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
645-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs=
646-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
644+
github.com/segmentfault/pacman v1.0.5-0.20230822075009-309985fb8700 h1:VqxiuNGQg86GEKxnzmJegZR2Ufr7EVXo68mdKaf+/MQ=
645+
github.com/segmentfault/pacman v1.0.5-0.20230822075009-309985fb8700/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
646+
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f h1:9f2Bjf6bdMvNyUop32wAGJCdp+Jdm/d6nKBYvFvkRo0=
647+
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
648+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822075009-309985fb8700 h1:VZpexPTcr7sOxxYUGa/9cneyCESfisAhCNbaEuTpcwI=
649+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822075009-309985fb8700/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
650+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822083413-c0075a2d401f h1:1KHe0uN6p798E7XJZPhZkgm/hXk5CTjisCvFMqaZSKI=
651+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822083413-c0075a2d401f/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
647652
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk=
648653
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=
649654
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 h1:OEuW1D7RGDE0CZDr0oGMw9Eiq7fAbD9C4WMrvSixamk=
@@ -772,6 +777,7 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
772777
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
773778
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
774779
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
780+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
775781
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
776782
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
777783
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=

internal/base/constant/cache_key.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package constant
33
import "time"
44

55
const (
6-
UserStatusChangedCacheKey = "answer:user:status:"
7-
UserStatusChangedCacheTime = 7 * 24 * time.Hour
8-
UserTokenCacheKey = "answer:user:token:"
9-
UserTokenCacheTime = 7 * 24 * time.Hour
10-
AdminTokenCacheKey = "answer:admin:token:"
11-
AdminTokenCacheTime = 7 * 24 * time.Hour
12-
UserTokenMappingCacheKey = "answer:user-token:mapping:"
13-
SiteInfoCacheKey = "answer:site-info:"
14-
SiteInfoCacheTime = 1 * time.Hour
15-
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
16-
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
17-
ConnectorUserExternalInfoCacheKey = "answer:connector:"
18-
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
19-
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"
20-
SiteMapQuestionCacheTime = time.Hour
21-
SitemapMaxSize = 50000
6+
UserStatusChangedCacheKey = "answer:user:status:"
7+
UserStatusChangedCacheTime = 7 * 24 * time.Hour
8+
UserTokenCacheKey = "answer:user:token:"
9+
UserTokenCacheTime = 7 * 24 * time.Hour
10+
AdminTokenCacheKey = "answer:admin:token:"
11+
AdminTokenCacheTime = 7 * 24 * time.Hour
12+
UserTokenMappingCacheKey = "answer:user-token:mapping:"
13+
SiteInfoCacheKey = "answer:site-info:"
14+
SiteInfoCacheTime = 1 * time.Hour
15+
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
16+
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
17+
ConnectorUserExternalInfoCacheKey = "answer:connector:"
18+
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
19+
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"
20+
SiteMapQuestionCacheTime = time.Hour
21+
SitemapMaxSize = 50000
22+
NewQuestionNotificationLimitCacheKeyPrefix = "answer:new-question-notification-limit:"
23+
NewQuestionNotificationLimitCacheTime = 7 * 24 * time.Hour
24+
NewQuestionNotificationLimitMax = 50
2225
)

internal/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var migrations = []Migration{
7171
NewMigration("v1.1.0-beta.2", "update question post time", updateQuestionPostTime, true),
7272
NewMigration("v1.1.0", "add gravatar base url", updateCount, true),
7373
NewMigration("v1.1.1", "update the length of revision content", updateTheLengthOfRevisionContent, false),
74-
NewMigration("v1.1.2", "add notification config", addNoticeConfig, false),
74+
NewMigration("v1.1.2", "add notification config", addNoticeConfig, true),
7575
}
7676

7777
func GetMigrations() []Migration {

internal/repo/auth/auth.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func NewAuthRepo(data *data.Data) auth.AuthRepo {
2727

2828
// GetUserCacheInfo get user cache info
2929
func (ar *authRepo) GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
30-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserTokenCacheKey+accessToken)
30+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.UserTokenCacheKey+accessToken)
3131
if err != nil {
3232
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
3333
}
34-
userInfo = &entity.UserCacheInfo{}
35-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
36-
if err != nil {
37-
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
34+
if !exist {
35+
return nil, nil
3836
}
37+
userInfo = &entity.UserCacheInfo{}
38+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
3939
return userInfo, nil
4040
}
4141

@@ -81,15 +81,15 @@ func (ar *authRepo) SetUserStatus(ctx context.Context, userID string, userInfo *
8181

8282
// GetUserStatus get user status
8383
func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) {
84-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
84+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
8585
if err != nil {
8686
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
8787
}
88-
userInfo = &entity.UserCacheInfo{}
89-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
90-
if err != nil {
91-
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
88+
if !exist {
89+
return nil, nil
9290
}
91+
userInfo = &entity.UserCacheInfo{}
92+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
9393
return userInfo, nil
9494
}
9595

@@ -104,16 +104,16 @@ func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err er
104104

105105
// GetAdminUserCacheInfo get admin user cache info
106106
func (ar *authRepo) GetAdminUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
107-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.AdminTokenCacheKey+accessToken)
107+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.AdminTokenCacheKey+accessToken)
108108
if err != nil {
109109
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
110110
return
111111
}
112-
userInfo = &entity.UserCacheInfo{}
113-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
114-
if err != nil {
115-
return nil, err
112+
if !exist {
113+
return nil, nil
116114
}
115+
userInfo = &entity.UserCacheInfo{}
116+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
117117
return userInfo, nil
118118
}
119119

@@ -144,7 +144,10 @@ func (ar *authRepo) RemoveAdminUserCacheInfo(ctx context.Context, accessToken st
144144
// AddUserTokenMapping add user token mapping
145145
func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error) {
146146
key := constant.UserTokenMappingCacheKey + userID
147-
resp, _ := ar.data.Cache.GetString(ctx, key)
147+
resp, _, err := ar.data.Cache.GetString(ctx, key)
148+
if err != nil {
149+
return err
150+
}
148151
mapping := make(map[string]bool, 0)
149152
if len(resp) > 0 {
150153
_ = json.Unmarshal([]byte(resp), &mapping)
@@ -157,7 +160,10 @@ func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken
157160
// RemoveUserTokens Log out all users under this user id
158161
func (ar *authRepo) RemoveUserTokens(ctx context.Context, userID string, remainToken string) {
159162
key := constant.UserTokenMappingCacheKey + userID
160-
resp, _ := ar.data.Cache.GetString(ctx, key)
163+
resp, _, err := ar.data.Cache.GetString(ctx, key)
164+
if err != nil {
165+
return
166+
}
161167
mapping := make(map[string]bool, 0)
162168
if len(resp) > 0 {
163169
_ = json.Unmarshal([]byte(resp), &mapping)

internal/repo/captcha/captcha.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType, conf
4343
return
4444
}
4545

46-
func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error) {
46+
func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actionInfo *entity.ActionRecordInfo, err error) {
4747
now := time.Now()
4848
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
49-
actioninfo = &entity.ActionRecordInfo{}
50-
res, err := cr.data.Cache.GetString(ctx, cacheKey)
49+
res, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
5150
if err != nil {
52-
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
51+
return nil, err
5352
}
54-
err = json.Unmarshal([]byte(res), actioninfo)
55-
if err != nil {
56-
return actioninfo, nil
53+
if !exist {
54+
return nil, nil
5755
}
58-
return actioninfo, nil
56+
actionInfo = &entity.ActionRecordInfo{}
57+
_ = json.Unmarshal([]byte(res), actionInfo)
58+
return actionInfo, nil
5959
}
6060

6161
func (cr *captchaRepo) DelActionType(ctx context.Context, unit, actionType string) (err error) {
@@ -79,9 +79,12 @@ func (cr *captchaRepo) SetCaptcha(ctx context.Context, key, captcha string) (err
7979

8080
// GetCaptcha get captcha from cache
8181
func (cr *captchaRepo) GetCaptcha(ctx context.Context, key string) (captcha string, err error) {
82-
captcha, err = cr.data.Cache.GetString(ctx, key)
82+
captcha, exist, err := cr.data.Cache.GetString(ctx, key)
8383
if err != nil {
84-
log.Debug(err)
84+
return "", err
85+
}
86+
if !exist {
87+
return "", fmt.Errorf("captcha not exist")
8588
}
8689
return captcha, nil
8790
}

internal/repo/config/config_repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewConfigRepo(data *data.Data) config.ConfigRepo {
2828

2929
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
3030
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
31-
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
31+
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
3232
c = &entity.Config{}
3333
c.BuildByJSON([]byte(cacheData))
3434
if c.ID > 0 {
@@ -54,7 +54,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
5454

5555
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
5656
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
57-
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
57+
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
5858
c = &entity.Config{}
5959
c.BuildByJSON([]byte(cacheData))
6060
if c.ID > 0 {

internal/repo/export/email_repo.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ func (e *emailRepo) SetCode(ctx context.Context, code, content string, duration
3333

3434
// VerifyCode verify the code if out of date
3535
func (e *emailRepo) VerifyCode(ctx context.Context, code string) (content string, err error) {
36-
content, err = e.data.Cache.GetString(ctx, code)
36+
content, exist, err := e.data.Cache.GetString(ctx, code)
3737
if err != nil {
38-
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
38+
return "", err
3939
}
40-
return
40+
if !exist {
41+
return "", nil
42+
}
43+
return content, nil
4144
}

0 commit comments

Comments
 (0)