Skip to content

Commit

Permalink
fix: possible id == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Nov 9, 2022
1 parent c84d583 commit 5c78174
Showing 1 changed file with 90 additions and 60 deletions.
150 changes: 90 additions & 60 deletions db/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,33 @@ func (s *database) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessage,
ret.GroupCode = grpmsg.GroupCode
ret.AnonymousID = grpmsg.AnonymousID
_ = json.Unmarshal(utils.S2B(grpmsg.Content), &ret.Content)
var attr StoredMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3MessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(grpmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredMessageAttribute{
MessageSeq: attr.MessageSeq,
InternalID: attr.InternalID,
SenderUin: attr.SenderUin,
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
if grpmsg.AttributeID != 0 {
var attr StoredMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3MessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(grpmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredMessageAttribute{
MessageSeq: attr.MessageSeq,
InternalID: attr.InternalID,
SenderUin: attr.SenderUin,
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
}
}
}
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(grpmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
if grpmsg.QuotedInfoID != 0 {
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(grpmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
return &ret, nil
}
Expand All @@ -115,29 +119,33 @@ func (s *database) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMessa
ret.SessionUin = privmsg.SessionUin
ret.TargetUin = privmsg.TargetUin
_ = json.Unmarshal(utils.S2B(privmsg.Content), &ret.Content)
var attr StoredMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3MessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(privmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredMessageAttribute{
MessageSeq: attr.MessageSeq,
InternalID: attr.InternalID,
SenderUin: attr.SenderUin,
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
if privmsg.AttributeID != 0 {
var attr StoredMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3MessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(privmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredMessageAttribute{
MessageSeq: attr.MessageSeq,
InternalID: attr.InternalID,
SenderUin: attr.SenderUin,
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
}
}
}
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(privmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
if privmsg.QuotedInfoID != 0 {
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(privmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
return &ret, nil
}
Expand All @@ -155,29 +163,33 @@ func (s *database) GetGuildChannelMessageByID(id string) (*db.StoredGuildChannel
ret.GuildID = uint64(guildmsg.GuildID)
ret.ChannelID = uint64(guildmsg.ChannelID)
_ = json.Unmarshal(utils.S2B(guildmsg.Content), &ret.Content)
var attr StoredGuildMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3GuildMessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(guildmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredGuildMessageAttribute{
MessageSeq: uint64(attr.MessageSeq),
InternalID: uint64(attr.InternalID),
SenderTinyID: uint64(attr.SenderTinyID),
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
if guildmsg.AttributeID != 0 {
var attr StoredGuildMessageAttribute
s.RLock()
err = s.db.Find(Sqlite3GuildMessageAttributeTableName, &attr, "WHERE ID="+strconv.FormatInt(guildmsg.AttributeID, 10))
s.RUnlock()
if err == nil {
ret.Attribute = &db.StoredGuildMessageAttribute{
MessageSeq: uint64(attr.MessageSeq),
InternalID: uint64(attr.InternalID),
SenderTinyID: uint64(attr.SenderTinyID),
SenderName: attr.SenderName,
Timestamp: attr.Timestamp,
}
}
}
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(guildmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
if guildmsg.QuotedInfoID != 0 {
var quoinf QuotedInfo
s.RLock()
err = s.db.Find(Sqlite3QuotedInfoTableName, &quoinf, "WHERE ID="+strconv.FormatInt(guildmsg.QuotedInfoID, 10))
s.RUnlock()
if err == nil {
ret.QuotedInfo = &db.QuotedInfo{
PrevID: quoinf.PrevID,
PrevGlobalID: quoinf.PrevGlobalID,
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
_ = json.Unmarshal(utils.S2B(quoinf.QuotedContent), &ret.QuotedInfo.QuotedContent)
}
return &ret, nil
}
Expand All @@ -200,6 +212,9 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
}))
h.Write(utils.S2B(msg.Attribute.SenderName))
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err := s.db.Insert(Sqlite3MessageAttributeTableName, &StoredMessageAttribute{
ID: id,
Expand All @@ -226,6 +241,9 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
}
h.Write(content)
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err = s.db.Insert(Sqlite3QuotedInfoTableName, &QuotedInfo{
ID: id,
Expand Down Expand Up @@ -270,6 +288,9 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
}))
h.Write(utils.S2B(msg.Attribute.SenderName))
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err := s.db.Insert(Sqlite3MessageAttributeTableName, &StoredMessageAttribute{
ID: id,
Expand All @@ -296,6 +317,9 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
}
h.Write(content)
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err = s.db.Insert(Sqlite3QuotedInfoTableName, &QuotedInfo{
ID: id,
Expand Down Expand Up @@ -338,6 +362,9 @@ func (s *database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
}))
h.Write(utils.S2B(msg.Attribute.SenderName))
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err := s.db.Insert(Sqlite3MessageAttributeTableName, &StoredGuildMessageAttribute{
ID: id,
Expand All @@ -364,6 +391,9 @@ func (s *database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage)
}
h.Write(content)
id := int64(h.Sum64())
if id == 0 {
id++
}
s.Lock()
err = s.db.Insert(Sqlite3QuotedInfoTableName, &QuotedInfo{
ID: id,
Expand Down

0 comments on commit 5c78174

Please sign in to comment.