Skip to content

Commit

Permalink
Merge branch 'feat/1.4.0/badge' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Sep 4, 2024
2 parents daab3ee + 052b78b commit 5c5300a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
18 changes: 2 additions & 16 deletions internal/service/notification/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ func (ns *NotificationService) countAllReviewAmount(ctx context.Context, req *sc
}

func (ns *NotificationService) ClearRedDot(ctx context.Context, req *schema.NotificationClearRequest) (*schema.RedDot, error) {
key := fmt.Sprintf(constant.RedDotCacheKey, req.NotificationType, req.UserID)
_ = ns.data.Cache.Del(ctx, key)

_ = ns.notificationCommon.DeleteRedDot(ctx, req.UserID, schema.NotificationType[req.NotificationType])
resp := &schema.GetRedDot{}
_ = copier.Copy(resp, req)
return ns.GetRedDot(ctx, resp)
Expand Down Expand Up @@ -203,19 +201,7 @@ func (ns *NotificationService) ClearIDUnRead(ctx context.Context, userID string,
log.Errorf("remove badge award alert cache failed: %v", err)
}

amount, err := ns.notificationRepo.CountNotificationByUser(ctx, &entity.Notification{
UserID: userID,
Type: notificationInfo.Type,
IsRead: schema.NotificationNotRead,
Status: schema.NotificationStatusNormal,
})
if err != nil {
log.Errorf("count notification failed: %v", err)
return nil
}
if amount == 0 {
_ = ns.notificationCommon.DeleteRedDot(ctx, userID, notificationInfo.Type)
}
_ = ns.notificationCommon.DecreaseRedDot(ctx, userID, notificationInfo.Type)
return nil
}

Expand Down
36 changes: 35 additions & 1 deletion internal/service/notification_common/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,44 @@ func (ns *NotificationCommon) addRedDot(ctx context.Context, userID string, noti
} else {
key = fmt.Sprintf(constant.RedDotCacheKey, constant.NotificationTypeAchievement, userID)
}
err := ns.data.Cache.SetInt64(ctx, key, 1, constant.RedDotCacheTime)
_, exist, err := ns.data.Cache.GetInt64(ctx, key)
if err != nil {
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
if exist {
if _, err := ns.data.Cache.Increase(ctx, key, 1); err != nil {
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
return nil
}
err = ns.data.Cache.SetInt64(ctx, key, 1, constant.RedDotCacheTime)
if err != nil {
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
return nil
}

func (ns *NotificationCommon) DecreaseRedDot(ctx context.Context, userID string, notificationType int) error {
var key string
if notificationType == schema.NotificationTypeInbox {
key = fmt.Sprintf(constant.RedDotCacheKey, constant.NotificationTypeInbox, userID)
} else {
key = fmt.Sprintf(constant.RedDotCacheKey, constant.NotificationTypeAchievement, userID)
}
_, exist, err := ns.data.Cache.GetInt64(ctx, key)
if err != nil {
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
if !exist {
return nil
}
res, err := ns.data.Cache.Decrease(ctx, key, 1)
if err != nil {
return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
if res <= 0 {
return ns.DeleteRedDot(ctx, userID, notificationType)
}
return nil
}

Expand Down

0 comments on commit 5c5300a

Please sign in to comment.