diff --git a/protocol/persistence.go b/protocol/persistence.go index 7205d1b03..7132ed452 100644 --- a/protocol/persistence.go +++ b/protocol/persistence.go @@ -6,6 +6,7 @@ import ( "database/sql" "encoding/gob" "encoding/json" + "time" "github.com/pkg/errors" @@ -766,3 +767,49 @@ func (db sqlitePersistence) TransactionsToValidate() ([]*TransactionToValidate, 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 +} diff --git a/protocol/persistence_test.go b/protocol/persistence_test.go index 1a4d3bb67..d41325683 100644 --- a/protocol/persistence_test.go +++ b/protocol/persistence_test.go @@ -639,7 +639,21 @@ func TestSaveMentions(t *testing.T) { require.Len(t, retrievedMessages, 1) require.Len(t, retrievedMessages[0].Mentions, 1) 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) {