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.

feat(badge): delete red dot when no unread notification

LinkinStars41d63bd245cd
diff --git a/internal/repo/notification/notification_repo.go b/internal/repo/notification/notification_repo.go
index bd325ef2..5a2a6cc7 100644
--- a/internal/repo/notification/notification_repo.go
+++ b/internal/repo/notification/notification_repo.go
@@ -129,3 +129,11 @@ func (nr *notificationRepo) GetNotificationPage(ctx context.Context, searchCond
}
return
}
+
+func (nr *notificationRepo) CountNotificationByUser(ctx context.Context, cond *entity.Notification) (int64, error) {
+ count, err := nr.data.DB.Context(ctx).Count(cond)
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return count, err
+}
diff --git a/internal/service/notification/notification_service.go b/internal/service/notification/notification_service.go
index b73d4fda..be2963fe 100644
--- a/internal/service/notification/notification_service.go
+++ b/internal/service/notification/notification_service.go
@@ -23,12 +23,6 @@ import (
"context"
"encoding/json"
"fmt"
- "github.com/apache/incubator-answer/internal/service/badge"
- "github.com/apache/incubator-answer/internal/service/report_common"
- "github.com/apache/incubator-answer/internal/service/review"
- usercommon "github.com/apache/incubator-answer/internal/service/user_common"
- "github.com/apache/incubator-answer/pkg/converter"
-
"github.com/apache/incubator-answer/internal/base/constant"
"github.com/apache/incubator-answer/internal/base/data"
"github.com/apache/incubator-answer/internal/base/handler"
@@ -36,8 +30,13 @@ import (
"github.com/apache/incubator-answer/internal/base/translator"
"github.com/apache/incubator-answer/internal/entity"
"github.com/apache/incubator-answer/internal/schema"
+ "github.com/apache/incubator-answer/internal/service/badge"
notficationcommon "github.com/apache/incubator-answer/internal/service/notification_common"
+ "github.com/apache/incubator-answer/internal/service/report_common"
+ "github.com/apache/incubator-answer/internal/service/review"
"github.com/apache/incubator-answer/internal/service/revision_common"
+ usercommon "github.com/apache/incubator-answer/internal/service/user_common"
+ "github.com/apache/incubator-answer/pkg/converter"
"github.com/apache/incubator-answer/pkg/uid"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/log"
@@ -203,6 +202,20 @@ func (ns *NotificationService) ClearIDUnRead(ctx context.Context, userID string,
if err != nil {
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)
+ }
return nil
}
diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go
index a3129b3a..f3732fcf 100644
--- a/internal/service/notification_common/notification.go
+++ b/internal/service/notification_common/notification.go
@@ -54,6 +54,7 @@ type NotificationRepo interface {
GetByUserIdObjectIdTypeId(ctx context.Context, userID, objectID string, notificationType int) (*entity.Notification, bool, error)
UpdateNotificationContent(ctx context.Context, notification *entity.Notification) (err error)
GetById(ctx context.Context, id string) (*entity.Notification, bool, error)
+ CountNotificationByUser(ctx context.Context, cond *entity.Notification) (int64, error)
}
type NotificationCommon struct {
@@ -226,6 +227,20 @@ func (ns *NotificationCommon) addRedDot(ctx context.Context, userID string, noti
return nil
}
+func (ns *NotificationCommon) DeleteRedDot(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)
+ }
+ err := ns.data.Cache.Del(ctx, key)
+ if err != nil {
+ return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
+ }
+ return nil
+}
+
// AddBadgeAwardAlertCache add badge award alert cache
func (ns *NotificationCommon) AddBadgeAwardAlertCache(ctx context.Context, userID, notificationID, badgeID string) (err error) {
key := fmt.Sprintf(constant.RedDotCacheKey, constant.NotificationTypeBadgeAchievement, userID)