/
messageidmap.go
144 lines (128 loc) · 3.91 KB
/
messageidmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package echo
import (
"math/rand"
"strconv"
"strings"
"sync"
"time"
"github.com/hoshinonyaruko/gensokyo-wxmp/config"
"github.com/hoshinonyaruko/gensokyo-wxmp/idmap"
"github.com/hoshinonyaruko/gensokyo-wxmp/mylog"
)
type messageRecord struct {
messageID string
timestamp time.Time
}
type messageStore struct {
mu sync.RWMutex
records map[string][]messageRecord
}
var instance *messageStore
var once sync.Once
// 惰性初始化
func initInstance() *messageStore {
once.Do(func() {
instance = &messageStore{
records: make(map[string][]messageRecord),
}
})
return instance
}
// AddLazyMessageId 添加 message_id 和它的时间戳到指定群号
func AddLazyMessageId(groupID, messageID string, timestamp time.Time) {
store := initInstance()
store.mu.Lock()
defer store.mu.Unlock()
store.records[groupID] = append(store.records[groupID], messageRecord{messageID: messageID, timestamp: timestamp})
}
// AddLazyMessageId 添加 message_id 和它的时间戳到指定群号
func AddLazyMessageIdv2(groupID, userID, messageID string, timestamp time.Time) {
store := initInstance()
store.mu.Lock()
defer store.mu.Unlock()
key := groupID + "." + userID
store.records[key] = append(store.records[key], messageRecord{messageID: messageID, timestamp: timestamp})
}
// GetRecentMessages 获取指定群号中最近5分钟内的 message_id~
func GetLazyMessagesId(groupID string) string {
store := initInstance()
store.mu.RLock()
defer store.mu.RUnlock()
fiveMinutesAgo := time.Now().Add(-5 * time.Minute)
var recentMessages []string
for _, record := range store.records[groupID] {
if record.timestamp.After(fiveMinutesAgo) {
recentMessages = append(recentMessages, record.messageID)
}
}
var randomMessageID string
if len(recentMessages) > 0 {
randomIndex := rand.Intn(len(recentMessages))
randomMessageID = recentMessages[randomIndex]
} else {
groupIDint64, err := idmap.StoreIDv2(groupID)
if err != nil {
mylog.Printf("Error storing ID 75: %v", err)
return "2000" //主动信息(不知道消息类型,按2000,纯主动信息处理)
}
msgType := GetMessageTypeByGroupidv2(config.GetWxAppId(), groupIDint64)
if strings.HasPrefix(msgType, "guild") {
randomMessageID = "1000" // 频道主动信息
} else {
randomMessageID = "2000" //群主动信息
}
}
return randomMessageID
}
// GetLazyMessagesIdv2 获取指定群号和用户ID中最近5分钟内的 message_id
func GetLazyMessagesIdv2(groupID, userID string) string { //1
store := initInstance()
store.mu.RLock()
defer store.mu.RUnlock()
// 构建复合键
key := groupID + "." + userID
fiveMinutesAgo := time.Now().Add(-5 * time.Minute)
var recentMessages []string
for _, record := range store.records[key] {
if record.timestamp.After(fiveMinutesAgo) {
recentMessages = append(recentMessages, record.messageID)
}
}
var randomMessageID string
if len(recentMessages) > 0 {
randomIndex := rand.Intn(len(recentMessages))
randomMessageID = recentMessages[randomIndex]
} else {
// 如果没有找到最近消息,处理默认行为
groupIDint64, err := idmap.StoreIDv2(groupID)
if err != nil {
mylog.Printf("Error storing ID 75: %v", err)
return "2000" //主动信息(不知道消息类型,按2000,纯主动信息处理)
}
msgType := GetMessageTypeByGroupidv2(config.GetWxAppId(), groupIDint64)
if strings.HasPrefix(msgType, "guild") {
randomMessageID = "1000" // 频道主动信息
} else {
randomMessageID = "2000" //群主动信息
}
}
return randomMessageID
}
// 通过group_id获取类型
func GetMessageTypeByGroupidv2(appID string, GroupID interface{}) string { //2
// 从appID和userID生成key
var GroupIDStr string
switch u := GroupID.(type) {
case int:
GroupIDStr = strconv.Itoa(u)
case int64:
GroupIDStr = strconv.FormatInt(u, 10)
case string:
GroupIDStr = u
default:
// 可能需要处理其他类型或报错
return ""
}
key := appID + "_" + GroupIDStr
return GetMsgTypeByKey(key)
}