mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-01-07 12:17:02 +08:00
For online status, account type conversion issues
This commit is contained in:
parent
86e09f0d0e
commit
80cb0b4605
34
pkg/common/db/cache/user.go
vendored
34
pkg/common/db/cache/user.go
vendored
@ -17,7 +17,10 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
"github.com/OpenIMSDK/protocol/user"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
"hash/crc32"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -165,11 +168,9 @@ func (u *UserCacheRedis) getOnlineStatusKey(userID string) string {
|
||||
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||||
var res []*user.OnlineStatus
|
||||
for _, userID := range userIDs {
|
||||
UserIDNum, err := strconv.Atoi(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var modKey = strconv.Itoa(UserIDNum % statusMod)
|
||||
//UserIDNum, err := strconv.Atoi(userID)
|
||||
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
||||
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
|
||||
var onlineStatus user.OnlineStatus
|
||||
key := olineStatusKey + modKey
|
||||
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||||
@ -177,18 +178,17 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
||||
if err == redis.Nil {
|
||||
// key or field does not exist
|
||||
res = append(res, &user.OnlineStatus{
|
||||
UserID: userID,
|
||||
Status: 0,
|
||||
PlatformID: -1,
|
||||
UserID: userID,
|
||||
Status: constant.Offline,
|
||||
})
|
||||
continue
|
||||
} else {
|
||||
return nil, err
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
}
|
||||
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
onlineStatus.UserID = userID
|
||||
res = append(res, &onlineStatus)
|
||||
@ -200,23 +200,21 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
||||
func (u *UserCacheRedis) SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error {
|
||||
for _, status := range list {
|
||||
var isNewKey int64
|
||||
UserIDNum, err := strconv.Atoi(status.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var modKey = strconv.Itoa(UserIDNum % statusMod)
|
||||
//UserIDNum, err := strconv.Atoi(status.UserID)
|
||||
UserIDNum := crc32.ChecksumIEEE([]byte(status.UserID))
|
||||
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
|
||||
key := olineStatusKey + modKey
|
||||
jsonData, err := json.Marshal(status)
|
||||
if err != nil {
|
||||
return err
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
isNewKey, err = u.rdb.Exists(ctx, key).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
_, err = u.rdb.HSet(ctx, key, status.UserID, string(jsonData)).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
if isNewKey > 0 {
|
||||
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user