feat(user): set default user notification config

This commit is contained in:
LinkinStars 2023-08-30 16:24:33 +08:00
parent 758309d77b
commit a9540bf330
6 changed files with 93 additions and 22 deletions

View File

@ -130,10 +130,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
userExternalLoginRepo := user_external_login.NewUserExternalLoginRepo(dataData)
userExternalLoginService := user_external_login2.NewUserExternalLoginService(userRepo, userCommon, userExternalLoginRepo, emailService, siteInfoCommonService, userActiveActivityRepo)
userNotificationConfigRepo := user_notification_config.NewUserNotificationConfigRepo(dataData)
userService := service.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, siteInfoCommonService, userRoleRelService, userCommon, userExternalLoginService, userNotificationConfigRepo)
userNotificationConfigService := user_notification_config2.NewUserNotificationConfigService(userRepo, userNotificationConfigRepo)
userService := service.NewUserService(userRepo, userActiveActivityRepo, activityRepo, emailService, authService, siteInfoCommonService, userRoleRelService, userCommon, userExternalLoginService, userNotificationConfigRepo, userNotificationConfigService)
captchaRepo := captcha.NewCaptchaRepo(dataData)
captchaService := action.NewCaptchaService(captchaRepo)
userNotificationConfigService := user_notification_config2.NewUserNotificationConfigService(userRepo, userNotificationConfigRepo)
userController := controller.NewUserController(authService, userService, captchaService, emailService, siteInfoCommonService, userNotificationConfigService)
commentRepo := comment.NewCommentRepo(dataData, uniqueIDRepo)
commentCommonRepo := comment.NewCommentCommonRepo(dataData, uniqueIDRepo)

View File

@ -72,6 +72,7 @@ var migrations = []Migration{
NewMigration("v1.1.0", "add gravatar base url", updateCount, true),
NewMigration("v1.1.1", "update the length of revision content", updateTheLengthOfRevisionContent, false),
NewMigration("v1.1.2", "add notification config", addNoticeConfig, true),
NewMigration("v1.1.3", "set default user notification config", setDefaultUserNotificationConfig, false),
}
func GetMigrations() []Migration {

View File

@ -0,0 +1,38 @@
package migrations
import (
"context"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/entity"
"github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)
func setDefaultUserNotificationConfig(ctx context.Context, x *xorm.Engine) error {
userIDs := make([]string, 0)
err := x.Context(ctx).Table("user").Select("id").Find(&userIDs)
if err != nil {
return err
}
for _, id := range userIDs {
bean := entity.UserNotificationConfig{UserID: id, Source: string(constant.InboxSource)}
exist, err := x.Context(ctx).Get(&bean)
if err != nil {
log.Error(err)
}
if exist {
continue
}
_, err = x.Context(ctx).Insert(&entity.UserNotificationConfig{
UserID: id,
Source: string(constant.InboxSource),
Channels: `[{"key":"email","enable":true}]`,
Enabled: true,
})
if err != nil {
log.Error(err)
}
}
return nil
}

View File

@ -22,6 +22,24 @@ func NewUserNotificationConfigRepo(data *data.Data) user_notification_config.Use
}
}
// Add add notification config
func (ur *userNotificationConfigRepo) Add(ctx context.Context, userIDs []string, source, channels string) (err error) {
var configs []*entity.UserNotificationConfig
for _, userID := range userIDs {
configs = append(configs, &entity.UserNotificationConfig{
UserID: userID,
Source: source,
Channels: channels,
Enabled: true,
})
}
_, err = ur.data.DB.Context(ctx).Insert(configs)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return nil
}
// Save save notification config, if existed, update, if not exist, insert
func (ur *userNotificationConfigRepo) Save(ctx context.Context, uc *entity.UserNotificationConfig) (err error) {
old := &entity.UserNotificationConfig{UserID: uc.UserID, Source: uc.Source}

View File

@ -9,6 +9,7 @@ import (
)
type UserNotificationConfigRepo interface {
Add(ctx context.Context, userIDs []string, source, channels string) (err error)
Save(ctx context.Context, uc *entity.UserNotificationConfig) (err error)
GetByUserID(ctx context.Context, userID string) ([]*entity.UserNotificationConfig, error)
GetBySource(ctx context.Context, source constant.NotificationSource) ([]*entity.UserNotificationConfig, error)
@ -68,6 +69,13 @@ func (us *UserNotificationConfigService) UpdateUserNotificationConfig(
return nil
}
// SetDefaultUserNotificationConfig set default user notification config for user register
func (us *UserNotificationConfigService) SetDefaultUserNotificationConfig(ctx context.Context, userIDs []string) (
err error) {
return us.userNotificationConfigRepo.Add(ctx, userIDs,
string(constant.InboxSource), `[{"key":"email","enable":true}]`)
}
func (us *UserNotificationConfigService) convertToEntity(ctx context.Context, userID string,
source constant.NotificationSource, channels schema.NotificationChannels) (c *entity.UserNotificationConfig) {
c = &entity.UserNotificationConfig{

View File

@ -32,16 +32,17 @@ import (
// UserService user service
type UserService struct {
userCommonService *usercommon.UserCommon
userRepo usercommon.UserRepo
userActivity activity.UserActiveActivityRepo
activityRepo activity_common.ActivityRepo
emailService *export.EmailService
authService *auth.AuthService
siteInfoService siteinfo_common.SiteInfoCommonService
userRoleService *role.UserRoleRelService
userExternalLoginService *user_external_login.UserExternalLoginService
userNotificationConfigRepo user_notification_config.UserNotificationConfigRepo
userCommonService *usercommon.UserCommon
userRepo usercommon.UserRepo
userActivity activity.UserActiveActivityRepo
activityRepo activity_common.ActivityRepo
emailService *export.EmailService
authService *auth.AuthService
siteInfoService siteinfo_common.SiteInfoCommonService
userRoleService *role.UserRoleRelService
userExternalLoginService *user_external_login.UserExternalLoginService
userNotificationConfigRepo user_notification_config.UserNotificationConfigRepo
userNotificationConfigService *user_notification_config.UserNotificationConfigService
}
func NewUserService(userRepo usercommon.UserRepo,
@ -54,18 +55,20 @@ func NewUserService(userRepo usercommon.UserRepo,
userCommonService *usercommon.UserCommon,
userExternalLoginService *user_external_login.UserExternalLoginService,
userNotificationConfigRepo user_notification_config.UserNotificationConfigRepo,
userNotificationConfigService *user_notification_config.UserNotificationConfigService,
) *UserService {
return &UserService{
userCommonService: userCommonService,
userRepo: userRepo,
userActivity: userActivity,
activityRepo: activityRepo,
emailService: emailService,
authService: authService,
siteInfoService: siteInfoService,
userRoleService: userRoleService,
userExternalLoginService: userExternalLoginService,
userNotificationConfigRepo: userNotificationConfigRepo,
userCommonService: userCommonService,
userRepo: userRepo,
userActivity: userActivity,
activityRepo: activityRepo,
emailService: emailService,
authService: authService,
siteInfoService: siteInfoService,
userRoleService: userRoleService,
userExternalLoginService: userExternalLoginService,
userNotificationConfigRepo: userNotificationConfigRepo,
userNotificationConfigService: userNotificationConfigService,
}
}
@ -398,6 +401,9 @@ func (us *UserService) UserRegisterByEmail(ctx context.Context, registerUserInfo
if err != nil {
return nil, nil, err
}
if err := us.userNotificationConfigService.SetDefaultUserNotificationConfig(ctx, []string{userInfo.ID}); err != nil {
log.Errorf("set default user notification config failed, err: %v", err)
}
// send email
data := &schema.EmailCodeContent{