You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
515 lines
21 KiB
515 lines
21 KiB
2 years ago
|
package cache
|
||
2 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
2 years ago
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||
2 years ago
|
"strconv"
|
||
|
"time"
|
||
|
|
||
2 years ago
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||
2 years ago
|
"github.com/gogo/protobuf/jsonpb"
|
||
|
|
||
|
"google.golang.org/protobuf/proto"
|
||
2 years ago
|
|
||
2 years ago
|
"github.com/go-redis/redis/v8"
|
||
2 years ago
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const (
|
||
2 years ago
|
NotificationUserIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
|
||
|
NotificationUserMinSeq = "REDIS_USER_MIN_SEQ:"
|
||
|
NotificationGetuiToken = "GETUI_TOKEN"
|
||
|
NotificationGetuiTaskID = "GETUI_TASK_ID"
|
||
|
NotificationMessageCache = "MESSAGE_CACHE:"
|
||
|
NotificationSignalCache = "SIGNAL_CACHE:"
|
||
|
NotificationSignalListCache = "SIGNAL_LIST_CACHE:"
|
||
|
NotificationFcmToken = "FCM_TOKEN:"
|
||
|
NotificationGroupUserMinSeq = "GROUP_USER_MIN_SEQ:"
|
||
|
NotificationGroupMaxSeq = "GROUP_MAX_SEQ:"
|
||
|
NotificationGroupMinSeq = "GROUP_MIN_SEQ:"
|
||
|
NotificationSendMsgFailedFlag = "SEND_MSG_FAILED_FLAG:"
|
||
|
NotificationUserBadgeUnreadCountSum = "USER_BADGE_UNREAD_COUNT_SUM:"
|
||
|
NotificationExTypeKeyLocker = "EX_LOCK:"
|
||
|
NotificationUidPidToken = "UID_PID_TOKEN_STATUS:"
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
type NotificationModel interface {
|
||
2 years ago
|
IncrUserSeq(ctx context.Context, userID string) (int64, error)
|
||
|
GetUserMaxSeq(ctx context.Context, userID string) (int64, error)
|
||
|
SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error
|
||
|
SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error)
|
||
|
GetUserMinSeq(ctx context.Context, userID string) (int64, error)
|
||
|
SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error)
|
||
|
GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error)
|
||
|
GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||
2 years ago
|
GetGroupMinSeq(ctx context.Context, groupID string) (int64, error)
|
||
2 years ago
|
IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||
|
SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error
|
||
|
SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error
|
||
2 years ago
|
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
|
||
2 years ago
|
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
|
||
2 years ago
|
SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error
|
||
|
DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error
|
||
2 years ago
|
GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||
2 years ago
|
SetMessageToCache(ctx context.Context, userID string, msgList []*sdkws.MsgData) (int, error)
|
||
|
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*sdkws.MsgData) error
|
||
2 years ago
|
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
|
||
2 years ago
|
HandleSignalInvite(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error)
|
||
2 years ago
|
GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *sdkws.SignalInviteReq, err error)
|
||
|
GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error)
|
||
2 years ago
|
DelUserSignalList(ctx context.Context, userID string) error
|
||
2 years ago
|
DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error
|
||
2 years ago
|
SetGetuiToken(ctx context.Context, token string, expireTime int64) error
|
||
|
GetGetuiToken(ctx context.Context) (string, error)
|
||
|
SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error
|
||
|
GetGetuiTaskID(ctx context.Context) (string, error)
|
||
2 years ago
|
SetSendMsgStatus(ctx context.Context, id string, status int32) error
|
||
|
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
|
||
2 years ago
|
SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error)
|
||
|
GetFcmToken(ctx context.Context, account string, platformID int) (string, error)
|
||
|
DelFcmToken(ctx context.Context, account string, platformID int) error
|
||
|
IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error)
|
||
|
SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error
|
||
|
GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error)
|
||
2 years ago
|
JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error)
|
||
2 years ago
|
GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error)
|
||
|
DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error
|
||
|
SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error)
|
||
|
GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error)
|
||
|
SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error
|
||
|
LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||
|
UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func NewNotificationCacheModel(client redis.UniversalClient) NotificationModel {
|
||
|
return ¬ificationCache{rdb: client}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
type notificationCache struct {
|
||
2 years ago
|
rdb redis.UniversalClient
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// 兼容老版本调用
|
||
2 years ago
|
func (c *notificationCache) DelKeys() {
|
||
2 years ago
|
for _, key := range []string{"GROUP_CACHE:", "FRIEND_RELATION_CACHE:", "BLACK_LIST_CACHE:", "USER_INFO_CACHE:", "GROUP_INFO_CACHE:", "JOINED_GROUP_LIST_CACHE:",
|
||
|
"GROUP_MEMBER_INFO_CACHE:", "GROUP_ALL_MEMBER_INFO_CACHE:", "ALL_FRIEND_INFO_CACHE:"} {
|
||
|
fName := utils.GetSelfFuncName()
|
||
|
var cursor uint64
|
||
|
var n int
|
||
|
for {
|
||
|
var keys []string
|
||
|
var err error
|
||
|
keys, cursor, err = c.rdb.Scan(context.Background(), cursor, key+"*", scanCount).Result()
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
n += len(keys)
|
||
|
// for each for redis cluster
|
||
|
for _, key := range keys {
|
||
|
if err = c.rdb.Del(context.Background(), key).Err(); err != nil {
|
||
|
log.NewError("", fName, key, err.Error())
|
||
|
err = c.rdb.Del(context.Background(), key).Err()
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if cursor == 0 {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationUserIncrSeq+userID).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationUserIncrSeq+userID).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationUserIncrSeq+userID, maxSeq, 0).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationUserMinSeq+userID, minSeq, 0).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationUserMinSeq+userID).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||
|
key := NotificationGroupUserMinSeq + "g:" + groupID + "u:" + userID
|
||
2 years ago
|
return errs.Wrap(c.rdb.Set(ctx, key, minSeq, 0).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||
|
key := NotificationGroupUserMinSeq + "g:" + groupID + "u:" + userID
|
||
2 years ago
|
return utils.Wrap2(c.rdb.Get(ctx, key).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationGroupMaxSeq+groupID).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationGroupMinSeq+groupID).Int64())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||
|
key := NotificationGroupMaxSeq + groupID
|
||
2 years ago
|
seq, err := c.rdb.Incr(ctx, key).Uint64()
|
||
2 years ago
|
return int64(seq), errs.Wrap(err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||
|
key := NotificationGroupMaxSeq + groupID
|
||
2 years ago
|
return errs.Wrap(c.rdb.Set(ctx, key, maxSeq, 0).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||
|
key := NotificationGroupMinSeq + groupID
|
||
2 years ago
|
return errs.Wrap(c.rdb.Set(ctx, key, minSeq, 0).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||
|
key := NotificationUidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||
2 years ago
|
return errs.Wrap(c.rdb.HSet(ctx, key, token, flag).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||
|
key := NotificationUidPidToken + userID + ":" + platformID
|
||
2 years ago
|
m, err := c.rdb.HGetAll(ctx, key).Result()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
|
mm := make(map[string]int)
|
||
|
for k, v := range m {
|
||
|
mm[k] = utils.StringToInt(v)
|
||
|
}
|
||
2 years ago
|
return mm, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
|
||
|
key := NotificationUidPidToken + userID + ":" + platform
|
||
2 years ago
|
mm := make(map[string]interface{})
|
||
|
for k, v := range m {
|
||
|
mm[k] = v
|
||
|
}
|
||
2 years ago
|
return errs.Wrap(c.rdb.HSet(ctx, key, mm).Err())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
|
||
|
key := NotificationUidPidToken + userID + ":" + platform
|
||
2 years ago
|
return errs.Wrap(c.rdb.HDel(ctx, key, fields...).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) getMessageCacheKey(sourceID string, seq int64) string {
|
||
|
return NotificationMessageCache + sourceID + "_" + strconv.Itoa(int(seq))
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) allMessageCacheKey(sourceID string) string {
|
||
|
return NotificationMessageCache + sourceID + "_*"
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetMessagesBySeq(ctx context.Context, userID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
||
2 years ago
|
pipe := c.rdb.Pipeline()
|
||
|
for _, v := range seqs {
|
||
2 years ago
|
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
||
2 years ago
|
key := c.getMessageCacheKey(userID, v)
|
||
2 years ago
|
if err := pipe.Get(ctx, key).Err(); err != nil && err != redis.Nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
}
|
||
|
result, err := pipe.Exec(ctx)
|
||
|
for i, v := range result {
|
||
|
if v.Err() != nil {
|
||
|
failedSeqs = append(failedSeqs, seqs[i])
|
||
2 years ago
|
} else {
|
||
2 years ago
|
msg := sdkws.MsgData{}
|
||
2 years ago
|
err = jsonpb.UnmarshalString(v.String(), &msg)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
failedSeqs = append(failedSeqs, seqs[i])
|
||
2 years ago
|
} else {
|
||
2 years ago
|
seqMsgs = append(seqMsgs, &msg)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
return seqMsgs, failedSeqs, err
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) SetMessageToCache(ctx context.Context, userID string, msgList []*sdkws.MsgData) (int, error) {
|
||
2 years ago
|
pipe := c.rdb.Pipeline()
|
||
2 years ago
|
var failedMsgs []sdkws.MsgData
|
||
2 years ago
|
for _, msg := range msgList {
|
||
2 years ago
|
key := c.getMessageCacheKey(userID, msg.Seq)
|
||
|
s, err := utils.Pb2String(msg)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return 0, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return 0, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
if len(failedMsgs) != 0 {
|
||
2 years ago
|
return len(failedMsgs), fmt.Errorf("set msg to notificationCache failed, failed lists: %v, %s", failedMsgs, userID)
|
||
2 years ago
|
}
|
||
2 years ago
|
_, err := pipe.Exec(ctx)
|
||
2 years ago
|
return 0, err
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*sdkws.MsgData) error {
|
||
2 years ago
|
pipe := c.rdb.Pipeline()
|
||
2 years ago
|
for _, v := range msgList {
|
||
2 years ago
|
if err := pipe.Del(ctx, c.getMessageCacheKey(userID, v.Seq)).Err(); err != nil {
|
||
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
_, err := pipe.Exec(ctx)
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||
2 years ago
|
vals, err := c.rdb.Keys(ctx, c.allMessageCacheKey(userID)).Result()
|
||
2 years ago
|
if err == redis.Nil {
|
||
2 years ago
|
return nil
|
||
|
}
|
||
|
if err != nil {
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
pipe := c.rdb.Pipeline()
|
||
2 years ago
|
for _, v := range vals {
|
||
2 years ago
|
if err := pipe.Del(ctx, v).Err(); err != nil {
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
_, err = pipe.Exec(ctx)
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) HandleSignalInvite(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||
2 years ago
|
req := &sdkws.SignalReq{}
|
||
2 years ago
|
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
var inviteeUserIDs []string
|
||
2 years ago
|
var isInviteSignal bool
|
||
|
switch signalInfo := req.Payload.(type) {
|
||
2 years ago
|
case *sdkws.SignalReq_Invite:
|
||
|
inviteeUserIDs = signalInfo.Invite.Invitation.InviteeUserIDList
|
||
2 years ago
|
isInviteSignal = true
|
||
2 years ago
|
case *sdkws.SignalReq_InviteInGroup:
|
||
|
inviteeUserIDs = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
|
||
2 years ago
|
isInviteSignal = true
|
||
2 years ago
|
if !utils.Contain(pushToUserID, inviteeUserIDs...) {
|
||
2 years ago
|
return false, nil
|
||
|
}
|
||
2 years ago
|
case *sdkws.SignalReq_HungUp, *sdkws.SignalReq_Cancel, *sdkws.SignalReq_Reject, *sdkws.SignalReq_Accept:
|
||
2 years ago
|
return false, errs.Wrap(errors.New("signalInfo do not need offlinePush"))
|
||
2 years ago
|
default:
|
||
2 years ago
|
return false, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
if isInviteSignal {
|
||
2 years ago
|
pipe := c.rdb.Pipeline()
|
||
2 years ago
|
for _, userID := range inviteeUserIDs {
|
||
2 years ago
|
timeout, err := strconv.Atoi(config.Config.Rtc.SignalTimeout)
|
||
|
if err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
keys := NotificationSignalListCache + userID
|
||
2 years ago
|
err = pipe.LPush(ctx, keys, msg.ClientMsgID).Err()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
err = pipe.Expire(ctx, keys, time.Duration(timeout)*time.Second).Err()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
key := NotificationSignalCache + msg.ClientMsgID
|
||
2 years ago
|
err = pipe.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
_, err := pipe.Exec(ctx)
|
||
|
if err != nil {
|
||
2 years ago
|
return false, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
return true, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (signalInviteReq *sdkws.SignalInviteReq, err error) {
|
||
|
bytes, err := c.rdb.Get(ctx, NotificationSignalCache+clientMsgID).Bytes()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
signalReq := &sdkws.SignalReq{}
|
||
|
if err = proto.Unmarshal(bytes, signalReq); err != nil {
|
||
2 years ago
|
return nil, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
signalInviteReq = &sdkws.SignalInviteReq{}
|
||
|
switch req := signalReq.Payload.(type) {
|
||
|
case *sdkws.SignalReq_Invite:
|
||
|
signalInviteReq.Invitation = req.Invite.Invitation
|
||
|
signalInviteReq.OpUserID = req.Invite.OpUserID
|
||
|
case *sdkws.SignalReq_InviteInGroup:
|
||
|
signalInviteReq.Invitation = req.InviteInGroup.Invitation
|
||
|
signalInviteReq.OpUserID = req.InviteInGroup.OpUserID
|
||
2 years ago
|
}
|
||
2 years ago
|
return signalInviteReq, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error) {
|
||
|
key, err := c.rdb.LPop(ctx, NotificationSignalListCache+userID).Result()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
invitationInfo, err = c.GetSignalInvitationInfoByClientMsgID(ctx, key)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, err
|
||
2 years ago
|
}
|
||
2 years ago
|
return invitationInfo, errs.Wrap(c.DelUserSignalList(ctx, userID))
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) DelUserSignalList(ctx context.Context, userID string) error {
|
||
|
return errs.Wrap(c.rdb.Del(ctx, NotificationSignalListCache+userID).Err())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) DelMsgFromCache(ctx context.Context, userID string, seqs []int64) error {
|
||
2 years ago
|
for _, seq := range seqs {
|
||
2 years ago
|
key := c.getMessageCacheKey(userID, seq)
|
||
2 years ago
|
result, err := c.rdb.Get(ctx, key).Result()
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
if err == redis.Nil {
|
||
2 years ago
|
continue
|
||
2 years ago
|
}
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
var msg sdkws.MsgData
|
||
2 years ago
|
if err := jsonpb.UnmarshalString(result, &msg); err != nil {
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
msg.Status = constant.MsgDeleted
|
||
|
s, err := utils.Pb2String(&msg)
|
||
|
if err != nil {
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
if err := c.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||
2 years ago
|
return errs.Wrap(err)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
return nil
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationGetuiToken, token, time.Duration(expireTime)*time.Second).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetGetuiToken(ctx context.Context) (string, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationGetuiToken).Result())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationGetuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationGetuiTaskID).Result())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationSendMsgFailedFlag+id, status, time.Hour*24).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||
|
result, err := c.rdb.Get(ctx, NotificationSendMsgFailedFlag+id).Int()
|
||
2 years ago
|
return int32(result), errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationFcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationFcmToken+account+":"+strconv.Itoa(platformID)).Result())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||
|
return errs.Wrap(c.rdb.Del(ctx, NotificationFcmToken+account+":"+strconv.Itoa(platformID)).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||
|
seq, err := c.rdb.Incr(ctx, NotificationUserBadgeUnreadCountSum+userID).Result()
|
||
2 years ago
|
return int(seq), errs.Wrap(err)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
|
||
|
return errs.Wrap(c.rdb.Set(ctx, NotificationUserBadgeUnreadCountSum+userID, value, 0).Err())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||
|
return utils.Wrap2(c.rdb.Get(ctx, NotificationUserBadgeUnreadCountSum+userID).Int())
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||
|
key := NotificationExTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||
2 years ago
|
return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||
|
key := NotificationExTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||
2 years ago
|
return errs.Wrap(c.rdb.Del(ctx, key).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||
2 years ago
|
switch sessionType {
|
||
|
case constant.SingleChatType:
|
||
|
return "EX_SINGLE_" + clientMsgID
|
||
|
case constant.GroupChatType:
|
||
|
return "EX_GROUP_" + clientMsgID
|
||
|
case constant.SuperGroupChatType:
|
||
|
return "EX_SUPER_GROUP_" + clientMsgID
|
||
|
case constant.NotificationChatType:
|
||
|
return "EX_NOTIFICATION" + clientMsgID
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (c *notificationCache) JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||
2 years ago
|
n, err := c.rdb.Exists(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||
2 years ago
|
if err != nil {
|
||
|
return false, utils.Wrap(err, "")
|
||
|
}
|
||
|
return n > 0, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||
2 years ago
|
return errs.Wrap(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||
2 years ago
|
return utils.Wrap2(c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||
2 years ago
|
return utils.Wrap2(c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||
2 years ago
|
return utils.Wrap2(c.rdb.HGetAll(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (c *notificationCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||
2 years ago
|
return errs.Wrap(c.rdb.HDel(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||
2 years ago
|
}
|