Skip to content

Commit

Permalink
Beta455 (#455)
Browse files Browse the repository at this point in the history
* beta447

* beta448

* beta449

* beta450

* beta451

* beta452

* beta453

* beta454

* beta455

* btea455
  • Loading branch information
Hoshinonyaruko authored Jul 9, 2024
1 parent 03f2aac commit a417243
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 53 deletions.
31 changes: 23 additions & 8 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
//收到私聊信息调用的具体还原步骤
//1,idmap还原真实userid,
//发信息使用的是userid

messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}

messageID := int(messageID64)
if config.GetAutoBind() {
if len(data.Attachments) > 0 && data.Attachments[0].URL != "" {
Expand Down Expand Up @@ -205,10 +213,17 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
//框架内指令
p.HandleFrameworkCommand(messageText, data, "group_private")
//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
//todo 判断array模式 然后对Message处理成array格式
Expand Down
29 changes: 22 additions & 7 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
//3,通过idmap用channelid获取guildid,
//发信息使用的是guildid
//todo 优化数据库读写次数
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
//转换at
Expand Down Expand Up @@ -344,10 +352,17 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)

//userid := int(userid64)
//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
// 如果在Array模式下, 则处理Message为Segment格式
Expand Down
16 changes: 12 additions & 4 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Processor

import (
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -78,10 +79,17 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
//框架内指令
p.HandleFrameworkCommand(messageText, data, "group")
//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
if config.GetAutoBind() {
Expand Down
16 changes: 12 additions & 4 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Processor

import (
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -195,10 +196,17 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis)

//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
// 如果在Array模式下, 则处理Message为Segment格式
Expand Down
16 changes: 12 additions & 4 deletions Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Processor

import (
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -191,10 +192,17 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
// 构造echostr,包括AppID,原始的s变量和当前时间戳
echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis)
//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
// 如果在Array模式下, 则处理Message为Segment格式
Expand Down
16 changes: 12 additions & 4 deletions Processor/ProcessThreadMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package Processor
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -312,10 +313,17 @@ func (p *Processors) ProcessThreadMessage(data *dto.WSThreadData) error {
// 构造echostr,包括AppID,原始的s变量和当前时间戳
echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis)
//映射str的messageID到int
messageID64, err := idmap.StoreCachev2(data.ID)
if err != nil {
mylog.Printf("Error storing ID: %v", err)
return nil
var messageID64 int64
if config.GetMemoryMsgid() {
messageID64, err = echo.StoreCacheInMemory(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
} else {
messageID64, err = idmap.StoreCachev2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
}
messageID := int(messageID64)
// 如果在Array模式下, 则处理Message为Segment格式
Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2352,3 +2352,15 @@ func GetDoNotReplaceAppid() bool {
}
return instance.Settings.DoNotReplaceAppid
}

// 获取GetMemoryMsgid的值
func GetMemoryMsgid() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to MemoryMsgid value.")
return false
}
return instance.Settings.MemoryMsgid
}
90 changes: 90 additions & 0 deletions echo/echo.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package echo

import (
"fmt"
"math/rand"
"strconv"
"sync"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/tencent-connect/botgo/dto"
)

Expand Down Expand Up @@ -53,6 +55,14 @@ type globalMessageGroup struct {
stack []MessageGroupPair
}

// 使用 sync.Map 作为内存存储
var (
globalSyncMapMsgid sync.Map
globalReverseMapMsgid sync.Map // 用于存储反向键值对
cleanupTicker *time.Ticker
onceMsgid sync.Once
)

// 初始化一个全局栈实例
var globalMessageGroupStack = &globalMessageGroup{
stack: make([]MessageGroupPair, 0),
Expand Down Expand Up @@ -273,3 +283,83 @@ func RemoveFromGlobalStack(index int) {

globalMessageGroupStack.stack = append(globalMessageGroupStack.stack[:index], globalMessageGroupStack.stack[index+1:]...)
}

// StoreCacheInMemory 根据 ID 将映射存储在内存中的 sync.Map 中
func StoreCacheInMemory(id string) (int64, error) {
var newRow int64

// 检查是否已存在映射
if value, ok := globalSyncMapMsgid.Load(id); ok {
newRow = value.(int64)
return newRow, nil
}

// 生成新的行号
var err error
maxDigits := 18 // int64 的位数上限-1
for digits := 9; digits <= maxDigits; digits++ {
newRow, err = idmap.GenerateRowID(id, digits)
if err != nil {
return 0, err
}

// 检查新生成的行号是否重复
if _, exists := globalSyncMapMsgid.LoadOrStore(id, newRow); !exists {
// 存储反向键值对
globalReverseMapMsgid.Store(newRow, id)
// 找到了一个唯一的行号,可以跳出循环
break
}

// 如果到达了最大尝试次数还没有找到唯一的行号,则返回错误
if digits == maxDigits {
return 0, fmt.Errorf("unable to find a unique row ID after %d attempts", maxDigits-8)
}
}

return newRow, nil
}

// GetIDFromRowID 根据行号获取原始 ID
func GetCacheIDFromMemoryByRowID(rowID string) (string, bool) {
introwID, _ := strconv.ParseInt(rowID, 10, 64)
if value, ok := globalReverseMapMsgid.Load(introwID); ok {
return value.(string), true
}
return "", false
}

// StartCleanupRoutine 启动定时清理函数,每5分钟清空 globalSyncMapMsgid 和 globalReverseMapMsgid
func StartCleanupRoutine() {
onceMsgid.Do(func() {
cleanupTicker = time.NewTicker(5 * time.Minute)

// 启动一个协程执行清理操作
go func() {
for range cleanupTicker.C {
fmt.Println("Starting cleanup...")

// 清空 sync.Map
globalSyncMapMsgid.Range(func(key, value interface{}) bool {
globalSyncMapMsgid.Delete(key)
return true
})

// 清空反向映射 sync.Map
globalReverseMapMsgid.Range(func(key, value interface{}) bool {
globalReverseMapMsgid.Delete(key)
return true
})

fmt.Println("Cleanup completed.")
}
}()
})
}

// StopCleanupRoutine 停止定时清理函数
func StopCleanupRoutine() {
if cleanupTicker != nil {
cleanupTicker.Stop()
}
}
21 changes: 16 additions & 5 deletions handlers/delete_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"

"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/tencent-connect/botgo/openapi"
Expand All @@ -16,12 +18,21 @@ func init() {
}

func DeleteMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) (string, error) {

//还原msgid
RealMsgID, err := idmap.RetrieveRowByCachev2(message.Params.MessageID.(string))
if err != nil {
mylog.Printf("error retrieving real RChannelID: %v", err)
var RealMsgID string
var err error

// 如果从内存取
if config.GetMemoryMsgid() {
//还原msgid
RealMsgID, _ = echo.GetCacheIDFromMemoryByRowID(message.Params.MessageID.(string))
} else {
//还原msgid
RealMsgID, err = idmap.RetrieveRowByCachev2(message.Params.MessageID.(string))
if err != nil {
mylog.Printf("error retrieving real RChannelID: %v", err)
}
}

//重新赋值
message.Params.MessageID = RealMsgID
//撤回频道信息
Expand Down
Loading

0 comments on commit a417243

Please sign in to comment.