1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

fix issue of repeatedly trigger for PrivateMessage event.

This commit is contained in:
Mrs4s 2020-08-01 10:28:34 +08:00
parent 23ff9b7269
commit 8f1c0c053a
3 changed files with 73 additions and 6 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/Mrs4s/MiraiGo/client/pb/structmsg"
"github.com/Mrs4s/MiraiGo/utils"
"github.com/golang/protobuf/proto"
"math"
"sync"
"sync/atomic"
"time"
@ -229,12 +228,17 @@ func decodeMessageSvcPacket(c *QQClient, _ uint16, payload []byte) (interface{},
if message.Body.RichText == nil || message.Body.RichText.Elems == nil {
continue
}
if c.lastMessageSeq >= message.Head.MsgSeq {
if math.Abs(float64(c.lastMessageSeq-message.Head.MsgSeq)) < 1000 {
continue
}
friend := c.FindFriend(message.Head.FromUin)
if friend == nil {
return nil, nil
}
c.lastMessageSeq = message.Head.MsgSeq
if friend.msgSeqList == nil {
friend.msgSeqList = utils.NewTTList(60)
}
if friend.msgSeqList.Any(func(i interface{}) bool { return i.(int32) == message.Head.MsgSeq }) {
continue
}
friend.msgSeqList.Add(message.Head.MsgSeq)
c.dispatchFriendMessage(c.parsePrivateMessage(message))
case 187:
_, pkt := c.buildSystemMsgNewFriendPacket()

View File

@ -2,6 +2,7 @@ package client
import (
"errors"
"github.com/Mrs4s/MiraiGo/utils"
"strings"
"sync"
)
@ -35,6 +36,8 @@ type (
Nickname string
Remark string
FaceId int16
msgSeqList *utils.TTList
}
FriendListResponse struct {

60
utils/ttlmap.go Normal file
View File

@ -0,0 +1,60 @@
package utils
import (
"sync"
"time"
)
type TTList struct {
list []*item
lock *sync.Mutex
}
type item struct {
i interface{}
lastAccess int64
}
func NewTTList(ttl int64) *TTList {
l := &TTList{
lock: new(sync.Mutex),
}
go func() {
for now := range time.Tick(time.Second * 5) {
l.lock.Lock()
pos := 0
for _, i := range l.list {
if now.Unix()-i.lastAccess > ttl {
l.list = append(l.list[:pos], l.list[pos+1:]...)
if pos > 0 {
pos++
}
}
pos++
}
l.lock.Unlock()
}
}()
return l
}
func (l *TTList) Add(i interface{}) {
l.lock.Lock()
l.lock.Unlock()
l.list = append(l.list, &item{
i: i,
lastAccess: time.Now().Unix(),
})
}
func (l *TTList) Any(filter func(i interface{}) bool) bool {
l.lock.Lock()
l.lock.Unlock()
for _, it := range l.list {
if filter(it.i) {
it.lastAccess = time.Now().Unix()
return true
}
}
return false
}