mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
fix issue of repeatedly trigger for PrivateMessage event.
This commit is contained in:
parent
23ff9b7269
commit
8f1c0c053a
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/Mrs4s/MiraiGo/client/pb/structmsg"
|
"github.com/Mrs4s/MiraiGo/client/pb/structmsg"
|
||||||
"github.com/Mrs4s/MiraiGo/utils"
|
"github.com/Mrs4s/MiraiGo/utils"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"math"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -229,12 +228,17 @@ func decodeMessageSvcPacket(c *QQClient, _ uint16, payload []byte) (interface{},
|
|||||||
if message.Body.RichText == nil || message.Body.RichText.Elems == nil {
|
if message.Body.RichText == nil || message.Body.RichText.Elems == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if c.lastMessageSeq >= message.Head.MsgSeq {
|
friend := c.FindFriend(message.Head.FromUin)
|
||||||
if math.Abs(float64(c.lastMessageSeq-message.Head.MsgSeq)) < 1000 {
|
if friend == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if friend.msgSeqList == nil {
|
||||||
|
friend.msgSeqList = utils.NewTTList(60)
|
||||||
|
}
|
||||||
|
if friend.msgSeqList.Any(func(i interface{}) bool { return i.(int32) == message.Head.MsgSeq }) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
friend.msgSeqList.Add(message.Head.MsgSeq)
|
||||||
c.lastMessageSeq = message.Head.MsgSeq
|
|
||||||
c.dispatchFriendMessage(c.parsePrivateMessage(message))
|
c.dispatchFriendMessage(c.parsePrivateMessage(message))
|
||||||
case 187:
|
case 187:
|
||||||
_, pkt := c.buildSystemMsgNewFriendPacket()
|
_, pkt := c.buildSystemMsgNewFriendPacket()
|
||||||
|
@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/Mrs4s/MiraiGo/utils"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -35,6 +36,8 @@ type (
|
|||||||
Nickname string
|
Nickname string
|
||||||
Remark string
|
Remark string
|
||||||
FaceId int16
|
FaceId int16
|
||||||
|
|
||||||
|
msgSeqList *utils.TTList
|
||||||
}
|
}
|
||||||
|
|
||||||
FriendListResponse struct {
|
FriendListResponse struct {
|
||||||
|
60
utils/ttlmap.go
Normal file
60
utils/ttlmap.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user