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, "