mirror of https://gitee.com/answerdev/answer.git
feat(user): set default user notification config
This commit is contained in:
parent
758309d77b
commit
a9540bf330
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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}
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -42,6 +42,7 @@ type UserService struct {
|
|||
userRoleService *role.UserRoleRelService
|
||||
userExternalLoginService *user_external_login.UserExternalLoginService
|
||||
userNotificationConfigRepo user_notification_config.UserNotificationConfigRepo
|
||||
userNotificationConfigService *user_notification_config.UserNotificationConfigService
|
||||
}
|
||||
|
||||
func NewUserService(userRepo usercommon.UserRepo,
|
||||
|
@ -54,6 +55,7 @@ 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,
|
||||
|
@ -66,6 +68,7 @@ func NewUserService(userRepo usercommon.UserRepo,
|
|||
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{
|
||||
|
|
Loading…
Reference in New Issue