Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
[#1545] Increase resolution of persisted chat messages
Browse files Browse the repository at this point in the history
Messages are stored with seconds which cause messages received within
the same second to have non-deterministic ordering. This increased
resolution will allow these messages to maintain proper order.
  • Loading branch information
placer14 committed Apr 18, 2019
1 parent 1000f97 commit 693b7f0
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions repo/db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *ChatDB) Put(messageId string, peerId string, subject string, message st
subject,
message,
readInt,
int(timestamp.Unix()),
int(timestamp.UnixNano()),
outgoingInt,
)
if err != nil {
Expand Down Expand Up @@ -79,21 +79,23 @@ func (c *ChatDB) GetConversations() []repo.ChatConversation {
}
defer rows.Close()
for _, peerId := range ids {
stm := "select Count(*) from chat where peerID='" + peerId + "' and read=0 and subject='' and outgoing=0;"
row := c.db.QueryRow(stm)
var count int
var (
count int
m string
ts int64
outInt int
stm = "select Count(*) from chat where peerID='" + peerId + "' and read=0 and subject='' and outgoing=0;"
row = c.db.QueryRow(stm)
)
row.Scan(&count)
stm = "select max(timestamp), message, outgoing from chat where peerID='" + peerId + "' and subject=''"
row = c.db.QueryRow(stm)
var m string
var ts int
var outInt int
row.Scan(&ts, &m, &outInt)
outgoing := false
if outInt > 0 {
outgoing = true
}
timestamp := time.Unix(int64(ts), 0)
timestamp := time.Unix(0, ts)
convo := repo.ChatConversation{
PeerId: peerId,
Unread: count,
Expand Down Expand Up @@ -128,12 +130,15 @@ func (c *ChatDB) GetMessages(peerID string, subject string, offsetId string, lim
return ret
}
for rows.Next() {
var msgID string
var pid string
var message string
var readInt int
var timestampInt int
var outgoingInt int
var (
msgID string
pid string
message string
readInt int
timestampInt int64
timestamp time.Time
outgoingInt int
)
if err := rows.Scan(&msgID, &pid, &message, &readInt, &timestampInt, &outgoingInt); err != nil {
continue
}
Expand All @@ -145,7 +150,7 @@ func (c *ChatDB) GetMessages(peerID string, subject string, offsetId string, lim
if outgoingInt == 1 {
outgoing = true
}
timestamp := time.Unix(int64(timestampInt), 0)
timestamp = time.Unix(0, timestampInt)
chatMessage := repo.ChatMessage{
PeerId: pid,
MessageId: msgID,
Expand Down

0 comments on commit 693b7f0

Please sign in to comment.