diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 5fb0fdde6..b821b4a52 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -287,10 +287,12 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu if err != nil { return errs.Wrap(err) } + onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID)) + } else { + onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) } onlineStatus.Status = constant.Online onlineStatus.UserID = userID - onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID) newjsonData, err := json.Marshal(&onlineStatus) if err != nil { return errs.Wrap(err) @@ -304,3 +306,20 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu return nil } + +type Comparable interface { + ~int | ~string | ~float64 | ~int32 +} + +func RemoveRepeatedElementsInList[T Comparable](slc []T) []T { + var result []T + tempMap := map[T]struct{}{} + for _, e := range slc { + if _, found := tempMap[e]; !found { + tempMap[e] = struct{}{} + result = append(result, e) + } + } + + return result +}