|
| 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 } |
0 commit comments