Skip to content

Commit

Permalink
fix: remove repeat platform on online status.
Browse files Browse the repository at this point in the history
  • Loading branch information
FGadvancer committed Oct 13, 2023
1 parent 2f1595e commit 8620e0e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pkg/common/db/cache/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 8620e0e

Please sign in to comment.