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.
Open-IM-Server/internal/msgtransfer/modify_msg_handler.go

114 lines
4.9 KiB

2 years ago
package msgtransfer
2 years ago
import (
2 years ago
"context"
2 years ago
"encoding/json"
2 years ago
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/db/controller"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
kfk "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
pbMsg "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/Shopify/sarama"
2 years ago
"google.golang.org/protobuf/proto"
2 years ago
)
type ModifyMsgConsumerHandler struct {
modifyMsgConsumerGroup *kfk.MConsumerGroup
2 years ago
2 years ago
extendMsgDatabase controller.ExtendMsgDatabase
2 years ago
extendSetMsgModel unRelationTb.ExtendMsgSetModel
2 years ago
}
2 years ago
func NewModifyMsgConsumerHandler(database controller.ExtendMsgDatabase) *ModifyMsgConsumerHandler {
return &ModifyMsgConsumerHandler{
modifyMsgConsumerGroup: kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.MsgToModify.Topic},
config.Config.Kafka.MsgToModify.Addr, config.Config.Kafka.ConsumerGroupID.MsgToModify),
extendMsgDatabase: database,
}
2 years ago
}
func (ModifyMsgConsumerHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (ModifyMsgConsumerHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
func (mmc *ModifyMsgConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
2 years ago
ctx := mmc.modifyMsgConsumerGroup.GetContextFromMsg(msg)
log.ZDebug(ctx, "kafka get info to mysql", "ModifyMsgConsumerHandler", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value), "key", string(msg.Key))
2 years ago
if len(msg.Value) != 0 {
2 years ago
mmc.ModifyMsg(ctx, msg, string(msg.Key), sess)
2 years ago
} else {
2 years ago
log.ZError(ctx, "msg get from kafka but is nil", nil, "key", msg.Key)
2 years ago
}
sess.MarkMessage(msg, "")
}
return nil
}
2 years ago
func (mmc *ModifyMsgConsumerHandler) ModifyMsg(ctx context.Context, cMsg *sarama.ConsumerMessage, msgKey string, _ sarama.ConsumerGroupSession) {
2 years ago
msgFromMQ := pbMsg.MsgDataToModifyByMQ{}
2 years ago
operationID := mcontext.GetOperationID(ctx)
2 years ago
err := proto.Unmarshal(cMsg.Value, &msgFromMQ)
if err != nil {
2 years ago
log.ZError(ctx, "msg_transfer Unmarshal msg err", err, "msg", string(cMsg.Value))
2 years ago
return
}
2 years ago
log.ZDebug(ctx, "proto.Unmarshal MsgDataToMQ", "msgs", msgFromMQ.String())
2 years ago
for _, msg := range msgFromMQ.Messages {
isReactionFromCache := utils.GetSwitchFromOptions(msg.Options, constant.IsReactionFromCache)
2 years ago
if !isReactionFromCache {
continue
}
2 years ago
ctx = mcontext.SetOperationID(ctx, operationID)
2 years ago
if msg.ContentType == constant.ReactionMessageModifier {
2 years ago
notification := &sdkws.ReactionMessageModifierNotification{}
2 years ago
if err := json.Unmarshal(msg.Content, notification); err != nil {
2 years ago
continue
}
if notification.IsExternalExtensions {
continue
}
if !notification.IsReact {
// first time to modify
2 years ago
var reactionExtensionList = make(map[string]unRelationTb.KeyValueModel)
extendMsg := unRelationTb.ExtendMsgModel{
2 years ago
ReactionExtensionList: reactionExtensionList,
ClientMsgID: notification.ClientMsgID,
MsgFirstModifyTime: notification.MsgFirstModifyTime,
}
2 years ago
for _, v := range notification.SuccessReactionExtensions {
2 years ago
reactionExtensionList[v.TypeKey] = unRelationTb.KeyValueModel{
2 years ago
TypeKey: v.TypeKey,
Value: v.Value,
LatestUpdateTime: v.LatestUpdateTime,
}
}
if err := mmc.extendMsgDatabase.InsertExtendMsg(ctx, notification.ConversationID, notification.SessionType, &extendMsg); err != nil {
2 years ago
// log.ZError(ctx, "MsgFirstModify InsertExtendMsg failed", notification.ConversationID, notification.SessionType, extendMsg, err.Error())
2 years ago
continue
}
} else {
if err := mmc.extendMsgDatabase.InsertOrUpdateReactionExtendMsgSet(ctx, notification.ConversationID, notification.SessionType, notification.ClientMsgID, notification.MsgFirstModifyTime, mmc.extendSetMsgModel.Pb2Model(notification.SuccessReactionExtensions)); err != nil {
2 years ago
// log.NewError(operationID, "InsertOrUpdateReactionExtendMsgSet failed")
2 years ago
}
}
2 years ago
} else if msg.ContentType == constant.ReactionMessageDeleter {
2 years ago
notification := &sdkws.ReactionMessageDeleteNotification{}
2 years ago
if err := json.Unmarshal(msg.Content, notification); err != nil {
2 years ago
continue
}
if err := mmc.extendMsgDatabase.DeleteReactionExtendMsgSet(ctx, notification.ConversationID, notification.SessionType, notification.ClientMsgID, notification.MsgFirstModifyTime, mmc.extendSetMsgModel.Pb2Model(notification.SuccessReactionExtensions)); err != nil {
2 years ago
// log.NewError(operationID, "InsertOrUpdateReactionExtendMsgSet failed")
2 years ago
}
}
}
}