package wxtask import ( "regexp" "strings" "sync" "time" "xiawan/wx/clientsdk/baseinfo" "xiawan/wx/db" "xiawan/wx/protobuf/wechat" "xiawan/wx/srv/wxface" ) // WXRevokeTask 消息防撤回管理器 type WXRevokeTask struct { wxConn wxface.IWXConnect // 防撤回开关 bAvoidRevoke bool // 读写数据的读写锁 revokeLock sync.RWMutex } // NewWXRevokeTask 新建消息防撤回任务管理器 func NewWXRevokeTask(wxConn wxface.IWXConnect) *WXRevokeTask { return &WXRevokeTask{ wxConn: wxConn, bAvoidRevoke: false, } } // Start 开启防消息撤回任务 func (wxrt *WXRevokeTask) Start() { } // Stop 停止防消息撤回任务 func (wxrt *WXRevokeTask) Stop() { } // AddNewMsg 新增缓存消息 func (wxrt *WXRevokeTask) AddNewMsg(addMsg *wechat.AddMsg, uuid string) { wxrt.revokeLock.Lock() defer wxrt.revokeLock.Unlock() _ = db.SaveMsg(*addMsg, uuid) } // OnRevokeMsg 某人移除了消息 func (wxrt *WXRevokeTask) OnRevokeMsg(revokeMsg baseinfo.RevokeMsg, groupWXID string, toGroupWXID string, uuid string) { wxrt.revokeLock.Lock() defer wxrt.revokeLock.Unlock() // fmt.Printf("撤回消息:groupWXID %s\n", groupWXID) currentReqInvoker := wxrt.wxConn.GetWXReqInvoker() now := time.Now() timeStr := now.Format("2006-01-02 15:04:05") groupName := "" if groupWXID != "" { groupInfo, _, _ := currentReqInvoker.SendGetContactRequestForCache(groupWXID, true) if groupInfo != nil { groupName = groupInfo.GetNickName().GetStr() } } if groupName != "" { groupName = "【" + groupName + "】\n" } groupName = timeStr + "\n" + groupName currentWXFileHelperMgr := wxrt.wxConn.GetWXFileHelperMgr() tmpAddMsg := db.GetAndDeleteMsg(revokeMsg.NewMsgID, uuid) if tmpAddMsg == nil { return } strMsg := tmpAddMsg.GetContent().GetStr() // fmt.Println("撤回消息 MsgType:" + fmt.Sprintf("%d", tmpAddMsg.GetMsgType())) if tmpAddMsg.GetMsgType() == 1 { if groupWXID != "" && toGroupWXID == "" && strings.Contains(strMsg, ":") { // 群聊,去掉第一个冒号之前的内容 strMsg = strings.Split(strMsg, ":")[1] } showMsg := groupName + revokeMsg.ReplaceMsg + ":" + strMsg currentWXFileHelperMgr.AddNewTipMsg(showMsg) } if tmpAddMsg.GetMsgType() == baseinfo.MMAddMsgTypeRefer && strings.Contains(strMsg, "57") { // 引用消息,只可能是文本 // 了解一下 titleRegex := regexp.MustCompile(`([\s\S]*?)<\/title>`) match := titleRegex.FindStringSubmatch(strMsg) if len(match) > 1 { strMsg = match[1] showMsg := groupName + revokeMsg.ReplaceMsg + ":" + strMsg currentWXFileHelperMgr.AddNewTipMsg(showMsg) } } // 图片消息 if tmpAddMsg.GetMsgType() == baseinfo.MMAddMsgTypeImage { strMsgByte := []byte(strMsg) showMsg := groupName + revokeMsg.ReplaceMsg + ":[图片]" currentWXFileHelperMgr.AddNewTipMsg(showMsg) currentWXFileHelperMgr.ForwardImageMsg(strMsgByte) } // 表情消息 if tmpAddMsg.GetMsgType() == baseinfo.MMAddMsgTypePic { strMsgByte := []byte(strMsg) showMsg := groupName + revokeMsg.ReplaceMsg + ":[表情]" currentWXFileHelperMgr.AddNewTipMsg(showMsg) currentWXFileHelperMgr.ForwardEmoticonMsg(strMsgByte) } } // SetAvoidRevoke 设置是否防消息撤回 func (wxrt *WXRevokeTask) SetAvoidRevoke(bFlag bool) { wxrt.bAvoidRevoke = bFlag } // IsAvoidRevoke 是否防消息撤回 func (wxrt *WXRevokeTask) IsAvoidRevoke() bool { return wxrt.bAvoidRevoke }