From 8620e0e2968fb73729d9fc274d3d81d6c2363d55 Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:24:03 +0800 Subject: [PATCH] fix: remove repeat platform on online status. --- pkg/common/db/cache/user.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 5fb0fdde62..b821b4a524 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 +}