Skip to content

Commit

Permalink
feat: modular database
Browse files Browse the repository at this point in the history
build with leveldb on default
  • Loading branch information
wdvxdr1123 committed Oct 17, 2021
1 parent 4e69ef4 commit 5e70c81
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 194 deletions.
14 changes: 0 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,6 @@ linters:
- revive
- forbidigo
- makezero
#- interfacer

# don't enable:
# - scopelint
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - interfacer
# - maligned
# - nestif
# - testpackage
# - wsl

run:
# default concurrency is a available CPU number.
Expand Down
17 changes: 7 additions & 10 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
ts.Add(time.Second)
if e.Get("data.id").Exists() {
i := e.Get("data.id").Int()
m, _ := bot.db.GetGroupMessageByGlobalID(int32(i))
m, _ := db.GetGroupMessageByGlobalID(int32(i))
if m != nil {
return &message.ForwardNode{
SenderId: m.Attribute.SenderUin,
Expand Down Expand Up @@ -781,7 +781,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
//
// https:// git.io/Jtz1y
func (bot *CQBot) CQDeleteMessage(messageID int32) global.MSG {
msg, err := bot.db.GetMessageByGlobalID(messageID)
msg, err := db.GetMessageByGlobalID(messageID)
if err != nil {
log.Warnf("撤回消息时出现错误: %v", err)
return Failed(100, "MESSAGE_NOT_FOUND", "消息不存在")
Expand Down Expand Up @@ -1115,7 +1115,7 @@ func (bot *CQBot) CQGetForwardMessage(resID string) global.MSG {
//
// https://git.io/Jtz1b
func (bot *CQBot) CQGetMessage(messageID int32) global.MSG {
msg, err := bot.db.GetMessageByGlobalID(messageID)
msg, err := db.GetMessageByGlobalID(messageID)
if err != nil {
log.Warnf("获取消息时出现错误: %v", err)
return Failed(100, "MSG_NOT_FOUND", "消息不存在")
Expand Down Expand Up @@ -1176,11 +1176,8 @@ func (bot *CQBot) CQGetGroupMessageHistory(groupID int64, seq int64) global.MSG
}
ms := make([]global.MSG, 0, len(msg))
for _, m := range msg {
id := m.Id
bot.checkMedia(m.Elements)
if bot.db != nil {
id = bot.InsertGroupMessage(m)
}
id := bot.InsertGroupMessage(m)
t := bot.formatGroupMessage(m)
t["message_id"] = id
ms = append(ms, t)
Expand Down Expand Up @@ -1302,7 +1299,7 @@ func (bot *CQBot) CQGetStatus() global.MSG {
//
// https://docs.go-cqhttp.org/api/#%E8%AE%BE%E7%BD%AE%E7%B2%BE%E5%8D%8E%E6%B6%88%E6%81%AF
func (bot *CQBot) CQSetEssenceMessage(messageID int32) global.MSG {
msg, err := bot.db.GetGroupMessageByGlobalID(messageID)
msg, err := db.GetGroupMessageByGlobalID(messageID)
if err != nil {
return Failed(100, "MESSAGE_NOT_FOUND", "消息不存在")
}
Expand All @@ -1317,7 +1314,7 @@ func (bot *CQBot) CQSetEssenceMessage(messageID int32) global.MSG {
//
// https://docs.go-cqhttp.org/api/#%E7%A7%BB%E5%87%BA%E7%B2%BE%E5%8D%8E%E6%B6%88%E6%81%AF
func (bot *CQBot) CQDeleteEssenceMessage(messageID int32) global.MSG {
msg, err := bot.db.GetGroupMessageByGlobalID(messageID)
msg, err := db.GetGroupMessageByGlobalID(messageID)
if err != nil {
return Failed(100, "MESSAGE_NOT_FOUND", "消息不存在")
}
Expand Down Expand Up @@ -1436,7 +1433,7 @@ func (bot *CQBot) CQSetModelShow(modelName string, modelShow string) global.MSG

// CQMarkMessageAsRead 标记消息已读
func (bot *CQBot) CQMarkMessageAsRead(msgID int32) global.MSG {
m, err := bot.db.GetMessageByGlobalID(msgID)
m, err := db.GetMessageByGlobalID(msgID)
if err != nil {
return Failed(100, "MSG_NOT_FOUND", "消息不存在")
}
Expand Down
30 changes: 2 additions & 28 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/modules/config"
)

// CQBot CQBot结构体,存储Bot实例相关配置
Expand All @@ -33,7 +32,6 @@ type CQBot struct {
lock sync.RWMutex
events []func(*Event)

db db.IDatabase
friendReqCache sync.Map
tempSessionCache sync.Map
}
Expand Down Expand Up @@ -71,30 +69,6 @@ func NewQQBot(cli *client.QQClient) *CQBot {
bot := &CQBot{
Client: cli,
}
levelNode, levelDB := base.Database["leveldb"]
mongoNode, mongoDB := base.Database["mongodb"]
multiDB := db.NewMultiDatabase()
if levelDB {
lconf := new(config.LevelDBConfig)
_ = levelNode.Decode(lconf)
if lconf.Enable {
multiDB.UseDB(db.UseLevelDB())
}
}
if mongoDB {
lconf := new(config.MongoDBConfig)
_ = mongoNode.Decode(lconf)
if lconf.Database == "" {
lconf.Database = "gocq-database"
}
if lconf.Enable {
multiDB.UseDB(db.UseMongoDB(lconf.URI, lconf.Database))
}
}
if err := multiDB.Open(); err != nil {
log.Fatalf("打开数据库失败: %v", err)
}
bot.db = multiDB
bot.Client.OnPrivateMessage(bot.privateMessageEvent)
bot.Client.OnGroupMessage(bot.groupMessageEvent)
if base.ReportSelfMessage {
Expand Down Expand Up @@ -384,7 +358,7 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
QuotedContent: ToMessageContent(reply.Elements),
}
}
if err := bot.db.InsertGroupMessage(msg); err != nil {
if err := db.InsertGroupMessage(msg); err != nil {
log.Warnf("记录聊天数据时出现错误: %v", err)
return -1
}
Expand Down Expand Up @@ -427,7 +401,7 @@ func (bot *CQBot) InsertPrivateMessage(m *message.PrivateMessage) int32 {
QuotedContent: ToMessageContent(m.Elements),
}
}
if err := bot.db.InsertPrivateMessage(msg); err != nil {
if err := db.InsertPrivateMessage(msg); err != nil {
log.Warnf("记录聊天数据时出现错误: %v", err)
return -1
}
Expand Down
12 changes: 6 additions & 6 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (bot *CQBot) ConvertStringMessage(raw string, isGroup bool) (r []message.IM
switch {
case customText != "":
var elem *message.ReplyElement
var org db.IStoredMessage
var org db.StoredMessage
sender, senderErr := strconv.ParseInt(d["qq"], 10, 64)
if senderErr != nil && err != nil {
log.Warnf("警告: 自定义 Reply 元素中必须包含 Uin 或 id")
Expand All @@ -491,7 +491,7 @@ func (bot *CQBot) ConvertStringMessage(raw string, isGroup bool) (r []message.IM
}
messageSeq, seqErr := strconv.ParseInt(d["seq"], 10, 64)
if err == nil {
org, _ = bot.db.GetMessageByGlobalID(int32(mid))
org, _ = db.GetMessageByGlobalID(int32(mid))
}
if org != nil {
elem = &message.ReplyElement{
Expand Down Expand Up @@ -519,7 +519,7 @@ func (bot *CQBot) ConvertStringMessage(raw string, isGroup bool) (r []message.IM
}
r = append([]message.IMessageElement{elem}, r...)
case err == nil:
org, err := bot.db.GetMessageByGlobalID(int32(mid))
org, err := db.GetMessageByGlobalID(int32(mid))
if err == nil {
r = append([]message.IMessageElement{
&message.ReplyElement{
Expand Down Expand Up @@ -651,7 +651,7 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
switch {
case customText != "":
var elem *message.ReplyElement
var org db.IStoredMessage
var org db.StoredMessage
sender, senderErr := strconv.ParseInt(e.Get("data.[user_id,qq]").String(), 10, 64)
if senderErr != nil && err != nil {
log.Warnf("警告: 自定义 Reply 元素中必须包含 user_id 或 id")
Expand All @@ -663,7 +663,7 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
}
messageSeq, seqErr := strconv.ParseInt(e.Get("data.seq").String(), 10, 64)
if err == nil {
org, _ = bot.db.GetMessageByGlobalID(int32(mid))
org, _ = db.GetMessageByGlobalID(int32(mid))
}
if org != nil {
elem = &message.ReplyElement{
Expand Down Expand Up @@ -691,7 +691,7 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
}
r = append([]message.IMessageElement{elem}, r...)
case err == nil:
org, err := bot.db.GetMessageByGlobalID(int32(mid))
org, err := db.GetMessageByGlobalID(int32(mid))
if err == nil {
r = append([]message.IMessageElement{
&message.ReplyElement{
Expand Down
18 changes: 6 additions & 12 deletions coolq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func ToFormattedMessage(e []message.IMessageElement, groupID int64, isRaw ...boo
func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMessage) {
bot.checkMedia(m.Elements)
cqm := ToStringMessage(m.Elements, 0, true)
id := m.Id
if bot.db != nil {
id = bot.InsertPrivateMessage(m)
}
id := bot.InsertPrivateMessage(m)
log.Infof("收到好友 %v(%v) 的消息: %v (%v)", m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
fm := global.MSG{
"post_type": func() string {
Expand Down Expand Up @@ -89,10 +86,7 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
}
}
cqm := ToStringMessage(m.Elements, m.GroupCode, true)
id := m.Id
if bot.db != nil {
id = bot.InsertGroupMessage(m)
}
id := bot.InsertGroupMessage(m)
log.Infof("收到群 %v(%v) 内 %v(%v) 的消息: %v (%v)", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm, id)
gm := bot.formatGroupMessage(m)
if gm == nil {
Expand All @@ -108,10 +102,10 @@ func (bot *CQBot) tempMessageEvent(c *client.QQClient, e *client.TempMessageEven
cqm := ToStringMessage(m.Elements, 0, true)
bot.tempSessionCache.Store(m.Sender.Uin, e.Session)
id := m.Id
if bot.db != nil { // nolint
// todo(Mrs4s)
// id = bot.InsertTempMessage(m.Sender.Uin, m)
}
// todo(Mrs4s)
// if bot.db != nil { // nolint
// id = bot.InsertTempMessage(m.Sender.Uin, m)
// }
log.Infof("收到来自群 %v(%v) 内 %v(%v) 的临时会话消息: %v", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm)
tm := global.MSG{
"post_type": "message",
Expand Down
8 changes: 4 additions & 4 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

type (
// IDatabase 数据库操作接口定义
IDatabase interface {
// Database 数据库操作接口定义
Database interface {
// Open 初始化数据库
Open() error

// GetMessageByGlobalID 通过 GlobalID 来获取消息
GetMessageByGlobalID(int32) (IStoredMessage, error)
GetMessageByGlobalID(int32) (StoredMessage, error)
// GetGroupMessageByGlobalID 通过 GlobalID 来获取群消息
GetGroupMessageByGlobalID(int32) (*StoredGroupMessage, error)
// GetPrivateMessageByGlobalID 通过 GlobalID 来获取私聊消息
Expand All @@ -26,7 +26,7 @@ type (
InsertPrivateMessage(*StoredPrivateMessage) error
}

IStoredMessage interface {
StoredMessage interface {
GetID() string
GetType() string
GetGlobalID() int32
Expand Down
Loading

0 comments on commit 5e70c81

Please sign in to comment.