Added Identity Images to multi-accounts GetAccounts()

This commit is contained in:
Samuel Hawksby-Robinson 2020-11-30 19:45:10 +00:00 committed by Andrea Maria Piana
parent 79f2b0bcc7
commit 094ea2ce85
3 changed files with 66 additions and 16 deletions

View File

@ -11,11 +11,11 @@ import (
// Account stores public information about account.
type Account struct {
Name string `json:"name"`
Timestamp int64 `json:"timestamp"`
KeycardPairing string `json:"keycard-pairing"`
KeyUID string `json:"key-uid"`
ImageURIs []string `json:"image-uris"`
Name string `json:"name"`
Timestamp int64 `json:"timestamp"`
KeycardPairing string `json:"keycard-pairing"`
KeyUID string `json:"key-uid"`
Images []images.IdentityImage `json:"images"`
}
type Database struct {
@ -40,22 +40,71 @@ func (db *Database) Close() error {
}
func (db *Database) GetAccounts() ([]Account, error) {
rows, err := db.db.Query("SELECT name, loginTimestamp, keycardPairing, keyUid from accounts ORDER BY loginTimestamp DESC")
rows, err := db.db.Query("SELECT a.name, a.loginTimestamp, a.keycardPairing, a.keyUid, ii.name, ii.image_payload, ii.width, ii.height, ii.file_size, ii.resize_target FROM accounts AS a LEFT JOIN identity_images AS ii ON ii.key_uid = a.keyUid ORDER BY loginTimestamp DESC")
if err != nil {
return nil, err
}
defer rows.Close()
var rst []Account
inthelper := sql.NullInt64{}
accs := map[string]*Account{}
accLoginTimestamp := sql.NullInt64{}
for rows.Next() {
acc := Account{}
err = rows.Scan(&acc.Name, &inthelper, &acc.KeycardPairing, &acc.KeyUID)
ii := &images.IdentityImage{}
iiName := sql.NullString{}
iiWidth := sql.NullInt64{}
iiHeight := sql.NullInt64{}
iiFileSize := sql.NullInt64{}
iiResizeTarget := sql.NullInt64{}
err = rows.Scan(
&acc.Name,
&accLoginTimestamp,
&acc.KeycardPairing,
&acc.KeyUID,
&iiName,
&ii.Payload,
&iiWidth,
&iiHeight,
&iiFileSize,
&iiResizeTarget,
)
if err != nil {
return nil, err
}
acc.Timestamp = inthelper.Int64
rst = append(rst, acc)
acc.Timestamp = accLoginTimestamp.Int64
ii.KeyUID = acc.KeyUID
ii.Name = iiName.String
ii.Width = int(iiWidth.Int64)
ii.Height = int(iiHeight.Int64)
ii.FileSize = int(iiFileSize.Int64)
ii.ResizeTarget = int(iiResizeTarget.Int64)
if ii.Name == "" && len(ii.Payload) == 0 && ii.Width == 0 && ii.Height == 0 && ii.FileSize == 0 && ii.ResizeTarget == 0 {
ii = nil
}
if ii != nil {
a, ok := accs[acc.Name]
if ok {
a.Images = append(a.Images, *ii)
} else {
acc.Images = append(acc.Images, *ii)
}
}
accs[acc.Name] = &acc
}
// Yes, I know, I'm converting a map into a slice, this is to maintain the function signature and API behaviour and
// not need to loop through the slice searching for an account with a given keyUID
for _, a := range accs {
rst = append(rst, *a)
}
return rst, nil
}
@ -140,9 +189,10 @@ func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityIma
continue
}
ii.KeyUID = keyUID
_, err := tx.Exec(
"INSERT INTO identity_images (key_uid, name, image_payload, width, height, file_size, resize_target) VALUES (?, ?, ?, ?, ?, ?, ?)",
keyUID,
ii.KeyUID,
ii.Name,
ii.Payload,
ii.Width,

View File

@ -80,7 +80,7 @@ func TestDatabase_GetIdentityImages(t *testing.T) {
defer stop()
seedTestDB(t, db)
expected := `[{"key_uid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240},{"key_uid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}]`
expected := `[{"keyUid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"fileSize":1024,"resizeTarget":240},{"keyUid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"fileSize":256,"resizeTarget":80}]`
oiis, err := db.GetIdentityImages(keyUID)
require.NoError(t, err)
@ -108,17 +108,17 @@ func TestDatabase_GetIdentityImage(t *testing.T) {
{
keyUID,
images.SmallDimName,
`{"key_uid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`,
`{"keyUid":"0xdeadbeef","type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"fileSize":256,"resizeTarget":80}`,
},
{
keyUID,
images.LargeDimName,
`{"key_uid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240}`,
`{"keyUid":"0xdeadbeef","type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"fileSize":1024,"resizeTarget":240}`,
},
{
keyUID2,
images.LargeDimName,
`{"key_uid":"","type":"","uri":"","width":0,"height":0,"file_size":0,"resize_target":0}`,
`{"keyUid":"","type":"","uri":"","width":0,"height":0,"fileSize":0,"resizeTarget":0}`,
},
}

View File

@ -2,9 +2,9 @@ package accounts
import (
"errors"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts"
)