|
|
@ -16,49 +16,126 @@ package tools
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/tools/log"
|
|
|
|
"github.com/OpenIMSDK/tools/log"
|
|
|
|
"github.com/OpenIMSDK/tools/mcontext"
|
|
|
|
"github.com/OpenIMSDK/tools/mcontext"
|
|
|
|
"github.com/OpenIMSDK/tools/utils"
|
|
|
|
"github.com/OpenIMSDK/tools/utils"
|
|
|
|
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//func (c *MsgTool) ConversationsDestructMsgs() {
|
|
|
|
|
|
|
|
// log.ZInfo(context.Background(), "start msg destruct cron task")
|
|
|
|
|
|
|
|
// ctx := mcontext.NewCtx(utils.GetSelfFuncName())
|
|
|
|
|
|
|
|
// conversations, err := c.conversationDatabase.GetConversationIDsNeedDestruct(ctx)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// log.ZError(ctx, "get conversation id need destruct failed", err)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// log.ZDebug(context.Background(), "nums conversations need destruct", "nums", len(conversations))
|
|
|
|
|
|
|
|
// for _, conversation := range conversations {
|
|
|
|
|
|
|
|
// ctx = mcontext.NewCtx(utils.GetSelfFuncName() + "-" + utils.OperationIDGenerator() + "-" + conversation.ConversationID + "-" + conversation.OwnerUserID)
|
|
|
|
|
|
|
|
// log.ZDebug(
|
|
|
|
|
|
|
|
// ctx,
|
|
|
|
|
|
|
|
// "UserMsgsDestruct",
|
|
|
|
|
|
|
|
// "conversationID",
|
|
|
|
|
|
|
|
// conversation.ConversationID,
|
|
|
|
|
|
|
|
// "ownerUserID",
|
|
|
|
|
|
|
|
// conversation.OwnerUserID,
|
|
|
|
|
|
|
|
// "msgDestructTime",
|
|
|
|
|
|
|
|
// conversation.MsgDestructTime,
|
|
|
|
|
|
|
|
// "lastMsgDestructTime",
|
|
|
|
|
|
|
|
// conversation.LatestMsgDestructTime,
|
|
|
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
// now := time.Now()
|
|
|
|
|
|
|
|
// seqs, err := c.msgDatabase.UserMsgsDestruct(ctx, conversation.OwnerUserID, conversation.ConversationID, conversation.MsgDestructTime, conversation.LatestMsgDestructTime)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// log.ZError(ctx, "user msg destruct failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
|
|
|
|
// continue
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if len(seqs) > 0 {
|
|
|
|
|
|
|
|
// if err := c.conversationDatabase.UpdateUsersConversationFiled(ctx, []string{conversation.OwnerUserID}, conversation.ConversationID, map[string]interface{}{"latest_msg_destruct_time": now}); err != nil {
|
|
|
|
|
|
|
|
// log.ZError(ctx, "updateUsersConversationFiled failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
|
|
|
|
// continue
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if err := c.msgNotificationSender.UserDeleteMsgsNotification(ctx, conversation.OwnerUserID, conversation.ConversationID, seqs); err != nil {
|
|
|
|
|
|
|
|
// log.ZError(ctx, "userDeleteMsgsNotification failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *MsgTool) ConversationsDestructMsgs() {
|
|
|
|
func (c *MsgTool) ConversationsDestructMsgs() {
|
|
|
|
log.ZInfo(context.Background(), "start msg destruct cron task")
|
|
|
|
log.ZInfo(context.Background(), "start msg destruct cron task")
|
|
|
|
ctx := mcontext.NewCtx(utils.GetSelfFuncName())
|
|
|
|
ctx := mcontext.NewCtx(utils.GetSelfFuncName())
|
|
|
|
conversations, err := c.conversationDatabase.GetConversationIDsNeedDestruct(ctx)
|
|
|
|
num, err := c.conversationDatabase.GetAllConversationIDsNumber(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.ZError(ctx, "get conversation id need destruct failed", err)
|
|
|
|
log.ZError(ctx, "GetAllConversationIDsNumber failed", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.ZDebug(context.Background(), "nums conversations need destruct", "nums", len(conversations))
|
|
|
|
const batchNum = 50
|
|
|
|
for _, conversation := range conversations {
|
|
|
|
log.ZDebug(ctx, "GetAllConversationIDsNumber", "num", num)
|
|
|
|
ctx = mcontext.NewCtx(utils.GetSelfFuncName() + "-" + utils.OperationIDGenerator() + "-" + conversation.ConversationID + "-" + conversation.OwnerUserID)
|
|
|
|
if num == 0 {
|
|
|
|
log.ZDebug(
|
|
|
|
return
|
|
|
|
ctx,
|
|
|
|
}
|
|
|
|
"UserMsgsDestruct",
|
|
|
|
count := int(num/batchNum + num/batchNum/2)
|
|
|
|
"conversationID",
|
|
|
|
if count < 1 {
|
|
|
|
conversation.ConversationID,
|
|
|
|
count = 1
|
|
|
|
"ownerUserID",
|
|
|
|
}
|
|
|
|
conversation.OwnerUserID,
|
|
|
|
maxPage := 1 + num/batchNum
|
|
|
|
"msgDestructTime",
|
|
|
|
if num%batchNum != 0 {
|
|
|
|
conversation.MsgDestructTime,
|
|
|
|
maxPage++
|
|
|
|
"lastMsgDestructTime",
|
|
|
|
}
|
|
|
|
conversation.LatestMsgDestructTime,
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
)
|
|
|
|
pageNumber := rand.Int63() % maxPage
|
|
|
|
now := time.Now()
|
|
|
|
conversationIDs, err := c.conversationDatabase.PageConversationIDs(ctx, int32(pageNumber), batchNum)
|
|
|
|
seqs, err := c.msgDatabase.UserMsgsDestruct(ctx, conversation.OwnerUserID, conversation.ConversationID, conversation.MsgDestructTime, conversation.LatestMsgDestructTime)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.ZError(ctx, "user msg destruct failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(seqs) > 0 {
|
|
|
|
log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber, "conversationIDsNum", len(conversationIDs), "conversationIDs", conversationIDs)
|
|
|
|
if err := c.conversationDatabase.UpdateUsersConversationFiled(ctx, []string{conversation.OwnerUserID}, conversation.ConversationID, map[string]interface{}{"latest_msg_destruct_time": now}); err != nil {
|
|
|
|
if len(conversationIDs) == 0 {
|
|
|
|
log.ZError(ctx, "updateUsersConversationFiled failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
conversations, err := c.conversationDatabase.GetConversationsByConversationID(ctx, conversationIDs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.ZError(ctx, "GetConversationsByConversationID failed", err, "conversationIDs", conversationIDs)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
temp := make([]*relation.ConversationModel, 0, len(conversations))
|
|
|
|
|
|
|
|
for i, conversation := range conversations {
|
|
|
|
|
|
|
|
if conversation.IsMsgDestruct && conversation.MsgDestructTime != 0 && (time.Now().Unix() > (conversation.MsgDestructTime+conversation.LatestMsgDestructTime.Unix()+8*60*60)) || conversation.LatestMsgDestructTime.IsZero() {
|
|
|
|
|
|
|
|
temp = append(temp, conversations[i])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, conversation := range temp {
|
|
|
|
|
|
|
|
ctx = mcontext.NewCtx(utils.GetSelfFuncName() + "-" + utils.OperationIDGenerator() + "-" + conversation.ConversationID + "-" + conversation.OwnerUserID)
|
|
|
|
|
|
|
|
log.ZDebug(
|
|
|
|
|
|
|
|
ctx,
|
|
|
|
|
|
|
|
"UserMsgsDestruct",
|
|
|
|
|
|
|
|
"conversationID",
|
|
|
|
|
|
|
|
conversation.ConversationID,
|
|
|
|
|
|
|
|
"ownerUserID",
|
|
|
|
|
|
|
|
conversation.OwnerUserID,
|
|
|
|
|
|
|
|
"msgDestructTime",
|
|
|
|
|
|
|
|
conversation.MsgDestructTime,
|
|
|
|
|
|
|
|
"lastMsgDestructTime",
|
|
|
|
|
|
|
|
conversation.LatestMsgDestructTime,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
seqs, err := c.msgDatabase.UserMsgsDestruct(ctx, conversation.OwnerUserID, conversation.ConversationID, conversation.MsgDestructTime, conversation.LatestMsgDestructTime)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.ZError(ctx, "user msg destruct failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.msgNotificationSender.UserDeleteMsgsNotification(ctx, conversation.OwnerUserID, conversation.ConversationID, seqs); err != nil {
|
|
|
|
if len(seqs) > 0 {
|
|
|
|
log.ZError(ctx, "userDeleteMsgsNotification failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
if err := c.conversationDatabase.UpdateUsersConversationFiled(ctx, []string{conversation.OwnerUserID}, conversation.ConversationID, map[string]interface{}{"latest_msg_destruct_time": now}); err != nil {
|
|
|
|
|
|
|
|
log.ZError(ctx, "updateUsersConversationFiled failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.msgNotificationSender.UserDeleteMsgsNotification(ctx, conversation.OwnerUserID, conversation.ConversationID, seqs); err != nil {
|
|
|
|
|
|
|
|
log.ZError(ctx, "userDeleteMsgsNotification failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|