Skip to content

Commit 27c98d0

Browse files
committed
json -> yaml
1 parent 006555b commit 27c98d0

File tree

5 files changed

+128
-11
lines changed

5 files changed

+128
-11
lines changed

db/database.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package db
2+
3+
import (
4+
"fmt"
5+
"hash/crc32"
6+
7+
"github.com/Mrs4s/go-cqhttp/global"
8+
)
9+
10+
type (
11+
// Database 数据库操作接口定义
12+
Database interface {
13+
// Open 初始化数据库
14+
Open() error
15+
16+
// GetMessageByGlobalID 通过 GlobalID 来获取消息
17+
GetMessageByGlobalID(int32) (StoredMessage, error)
18+
// GetGroupMessageByGlobalID 通过 GlobalID 来获取群消息
19+
GetGroupMessageByGlobalID(int32) (*StoredGroupMessage, error)
20+
// GetPrivateMessageByGlobalID 通过 GlobalID 来获取私聊消息
21+
GetPrivateMessageByGlobalID(int32) (*StoredPrivateMessage, error)
22+
// GetGuildChannelMessageByID 通过 ID 来获取频道消息
23+
GetGuildChannelMessageByID(string) (*StoredGuildChannelMessage, error)
24+
25+
// InsertGroupMessage 向数据库写入新的群消息
26+
InsertGroupMessage(*StoredGroupMessage) error
27+
// InsertPrivateMessage 向数据库写入新的私聊消息
28+
InsertPrivateMessage(*StoredPrivateMessage) error
29+
// InsertGuildChannelMessage 向数据库写入新的频道消息
30+
InsertGuildChannelMessage(*StoredGuildChannelMessage) error
31+
}
32+
33+
StoredMessage interface {
34+
GetID() string
35+
GetType() string
36+
GetGlobalID() int32
37+
GetAttribute() *StoredMessageAttribute
38+
GetContent() []global.MSG
39+
}
40+
41+
// StoredGroupMessage 持久化群消息
42+
StoredGroupMessage struct {
43+
ID string `bson:"_id" yaml:"-"`
44+
GlobalID int32 `bson:"globalId" yaml:"-"`
45+
Attribute *StoredMessageAttribute `bson:"attribute" yaml:"-"`
46+
SubType string `bson:"subType" yaml:"-"`
47+
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
48+
GroupCode int64 `bson:"groupCode" yaml:"-"`
49+
AnonymousID string `bson:"anonymousId" yaml:"-"`
50+
Content []global.MSG `bson:"content" yaml:"content"`
51+
}
52+
53+
// StoredPrivateMessage 持久化私聊消息
54+
StoredPrivateMessage struct {
55+
ID string `bson:"_id" yaml:"-"`
56+
GlobalID int32 `bson:"globalId" yaml:"-"`
57+
Attribute *StoredMessageAttribute `bson:"attribute" yaml:"-"`
58+
SubType string `bson:"subType" yaml:"-"`
59+
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
60+
SessionUin int64 `bson:"sessionUin" yaml:"-"`
61+
TargetUin int64 `bson:"targetUin" yaml:"-"`
62+
Content []global.MSG `bson:"content" yaml:"content"`
63+
}
64+
65+
// StoredGuildChannelMessage 持久化频道消息
66+
StoredGuildChannelMessage struct {
67+
ID string `bson:"_id" yaml:"-"`
68+
Attribute *StoredGuildMessageAttribute `bson:"attribute" yaml:"-"`
69+
GuildID uint64 `bson:"guildId" yaml:"-"`
70+
ChannelID uint64 `bson:"channelId" yaml:"-"`
71+
QuotedInfo *QuotedInfo `bson:"quotedInfo" yaml:"-"`
72+
Content []global.MSG `bson:"content" yaml:"content"`
73+
}
74+
75+
// StoredMessageAttribute 持久化消息属性
76+
StoredMessageAttribute struct {
77+
MessageSeq int32 `bson:"messageSeq" yaml:"-"`
78+
InternalID int32 `bson:"internalId" yaml:"-"`
79+
SenderUin int64 `bson:"senderUin" yaml:"-"`
80+
SenderName string `bson:"senderName" yaml:"-"`
81+
Timestamp int64 `bson:"timestamp" yaml:"-"`
82+
}
83+
84+
// StoredGuildMessageAttribute 持久化频道消息属性
85+
StoredGuildMessageAttribute struct {
86+
MessageSeq uint64 `bson:"messageSeq" yaml:"-"`
87+
InternalID uint64 `bson:"internalId" yaml:"-"`
88+
SenderTinyID uint64 `bson:"senderTinyId" yaml:"-"`
89+
SenderName string `bson:"senderName" yaml:"-"`
90+
Timestamp int64 `bson:"timestamp" yaml:"-"`
91+
}
92+
93+
// QuotedInfo 引用回复
94+
QuotedInfo struct {
95+
PrevID string `bson:"prevId" yaml:"-"`
96+
PrevGlobalID int32 `bson:"prevGlobalId" yaml:"-"`
97+
QuotedContent []global.MSG `bson:"quotedContent" yaml:"quoted_content"`
98+
}
99+
)
100+
101+
// ToGlobalID 构建`code`-`msgID`的字符串并返回其CRC32 Checksum的值
102+
func ToGlobalID(code int64, msgID int32) int32 {
103+
return int32(crc32.ChecksumIEEE([]byte(fmt.Sprintf("%d-%d", code, msgID))))
104+
}
105+
106+
func (m *StoredGroupMessage) GetID() string { return m.ID }
107+
func (m *StoredGroupMessage) GetType() string { return "group" }
108+
func (m *StoredGroupMessage) GetGlobalID() int32 { return m.GlobalID }
109+
func (m *StoredGroupMessage) GetAttribute() *StoredMessageAttribute { return m.Attribute }
110+
func (m *StoredGroupMessage) GetContent() []global.MSG { return m.Content }
111+
112+
func (m *StoredPrivateMessage) GetID() string { return m.ID }
113+
func (m *StoredPrivateMessage) GetType() string { return "private" }
114+
func (m *StoredPrivateMessage) GetGlobalID() int32 { return m.GlobalID }
115+
func (m *StoredPrivateMessage) GetAttribute() *StoredMessageAttribute { return m.Attribute }
116+
func (m *StoredPrivateMessage) GetContent() []global.MSG { return m.Content }

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/Mrs4s/go-cqhttp v1.0.0-rc3.0.20221109121432-fa267b6a2d47
99
github.com/pkg/errors v0.9.1
1010
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
11+
gopkg.in/yaml.v3 v3.0.1
1112
)
1213

1314
require (
@@ -28,7 +29,6 @@ require (
2829
github.com/tidwall/pretty v1.2.0 // indirect
2930
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
3031
golang.org/x/text v0.3.7 // indirect
31-
gopkg.in/yaml.v3 v3.0.1 // indirect
3232
modernc.org/libc v1.14.6 // indirect
3333
modernc.org/mathutil v1.4.1 // indirect
3434
modernc.org/memory v1.0.5 // indirect

leveldb/structs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package leveldb
22

3-
import "github.com/Mrs4s/go-cqhttp/db"
3+
import "github.com/RomiChan/gocq-sqlite3-migrate/db"
44

55
func (r *reader) readStoredGroupMessage() *db.StoredGroupMessage {
66
coder := r.coder()

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/Mrs4s/go-cqhttp/db"
8+
"github.com/RomiChan/gocq-sqlite3-migrate/db"
99
"github.com/RomiChan/gocq-sqlite3-migrate/leveldb"
1010
"github.com/RomiChan/gocq-sqlite3-migrate/sqlite3"
1111
)

sqlite3/sqlite3.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
package sqlite3
55

66
import (
7-
"encoding/json"
87
"hash/crc64"
98
"sync"
109
"time"
1110

1211
sql "github.com/FloatTech/sqlite"
1312
"github.com/pkg/errors"
13+
"gopkg.in/yaml.v3"
1414

1515
"github.com/Mrs4s/MiraiGo/binary"
1616
"github.com/Mrs4s/MiraiGo/utils"
17-
"github.com/Mrs4s/go-cqhttp/db"
17+
18+
"github.com/RomiChan/gocq-sqlite3-migrate/db"
1819
)
1920

2021
type Database struct {
@@ -152,7 +153,7 @@ func (s *Database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
152153
h.Write(binary.NewWriterF(func(w *binary.Writer) {
153154
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
154155
}))
155-
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
156+
content, err := yaml.Marshal(&msg.QuotedInfo)
156157
if err != nil {
157158
return errors.Wrap(err, "insert marshal QuotedContent error")
158159
}
@@ -173,7 +174,7 @@ func (s *Database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
173174
grpmsg.QuotedInfoID = id
174175
}
175176
}
176-
content, err := json.Marshal(&msg.Content)
177+
content, err := yaml.Marshal(&msg)
177178
if err != nil {
178179
return errors.Wrap(err, "insert marshal Content error")
179180
}
@@ -233,7 +234,7 @@ func (s *Database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
233234
h.Write(binary.NewWriterF(func(w *binary.Writer) {
234235
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
235236
}))
236-
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
237+
content, err := yaml.Marshal(&msg.QuotedInfo)
237238
if err != nil {
238239
return errors.Wrap(err, "insert marshal QuotedContent error")
239240
}
@@ -254,7 +255,7 @@ func (s *Database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
254255
privmsg.QuotedInfoID = id
255256
}
256257
}
257-
content, err := json.Marshal(&msg.Content)
258+
content, err := yaml.Marshal(&msg)
258259
if err != nil {
259260
return errors.Wrap(err, "insert marshal Content error")
260261
}
@@ -312,7 +313,7 @@ func (s *Database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
312313
h.Write(binary.NewWriterF(func(w *binary.Writer) {
313314
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
314315
}))
315-
content, err := json.Marshal(&msg.QuotedInfo.QuotedContent)
316+
content, err := yaml.Marshal(&msg.QuotedInfo)
316317
if err != nil {
317318
return errors.Wrap(err, "insert marshal QuotedContent error")
318319
}
@@ -333,7 +334,7 @@ func (s *Database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
333334
guildmsg.QuotedInfoID = id
334335
}
335336
}
336-
content, err := json.Marshal(&msg.Content)
337+
content, err := yaml.Marshal(&msg)
337338
if err != nil {
338339
return errors.Wrap(err, "insert marshal Content error")
339340
}

0 commit comments

Comments
 (0)