Added protocol register of ChatIdentity last published times per chat

This register is only used for publishing ChatIdentity to public chat
This commit is contained in:
Samuel Hawksby-Robinson 2020-10-28 13:18:24 +00:00 committed by Andrea Maria Piana
parent dcee7f8de7
commit af7117070f
2 changed files with 61 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"database/sql" "database/sql"
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -766,3 +767,49 @@ func (db sqlitePersistence) TransactionsToValidate() ([]*TransactionToValidate,
return transactions, nil return transactions, nil
} }
func (db sqlitePersistence) GetWhenChatIdentityLastPublished(chatId string) (*int64, error) {
rows, err := db.db.Query("SELECT clock_value FROM chat_identity_last_published WHERE chat_id = ?", chatId)
if err != nil {
return nil, err
}
defer rows.Close()
var t *int64
for rows.Next() {
err = rows.Scan(t)
if err != nil {
return nil, err
}
}
return t, nil
}
func (db sqlitePersistence) SaveWhenChatIdentityLastPublished(chatId string) error {
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()
stmt, err := tx.Prepare("INSERT INTO chat_identity_last_published (chat_id, clock_value) VALUES (?, ?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(chatId, time.Now().Unix())
if err != nil {
return err
}
return nil
}

View File

@ -639,7 +639,21 @@ func TestSaveMentions(t *testing.T) {
require.Len(t, retrievedMessages, 1) require.Len(t, retrievedMessages, 1)
require.Len(t, retrievedMessages[0].Mentions, 1) require.Len(t, retrievedMessages[0].Mentions, 1)
require.Equal(t, retrievedMessages[0].Mentions, message.Mentions) require.Equal(t, retrievedMessages[0].Mentions, message.Mentions)
}
func TestSqlitePersistence_GetWhenChatIdentityLastPublished(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := sqlitePersistence{db: db}
chatId := "abcd1234"
err = p.SaveWhenChatIdentityLastPublished(chatId)
require.NoError(t, err)
ts, err := p.GetWhenChatIdentityLastPublished(chatId)
require.NoError(t, err)
println(&ts)
} }
func TestSaveLinks(t *testing.T) { func TestSaveLinks(t *testing.T) {