Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(badge): remove red dot only when the badge notification has been read

LinkinStars052b78be2862
diff --git a/internal/service/notification/notification_service.go b/internal/service/notification/notification_service.go
index be2963fe..0860c894 100644
--- a/internal/service/notification/notification_service.go
+++ b/internal/service/notification/notification_service.go
@@ -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)
@@ -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
}
diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go
index f3732fcf..9d08e258 100644
--- a/internal/service/notification_common/notification.go
+++ b/internal/service/notification_common/notification.go
@@ -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
}