2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-11-30 19:45:10 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrUpdatingWrongAccount raised if caller tries to update any other account except one used for login.
|
2019-12-30 09:43:51 +00:00
|
|
|
ErrUpdatingWrongAccount = errors.New("failed to update wrong account. Please login with that account first")
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
2022-01-24 09:29:18 +00:00
|
|
|
func NewMultiAccountsAPI(db *multiaccounts.Database) *MultiAccountsAPI {
|
|
|
|
return &MultiAccountsAPI{db: db}
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MultiAccountsAPI is class with methods available over RPC.
|
|
|
|
type MultiAccountsAPI struct {
|
2022-01-24 09:29:18 +00:00
|
|
|
db *multiaccounts.Database
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *MultiAccountsAPI) UpdateAccount(account multiaccounts.Account) error {
|
|
|
|
return api.db.UpdateAccount(account)
|
|
|
|
}
|
2020-11-30 15:41:45 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Profile Images
|
|
|
|
//
|
|
|
|
|
|
|
|
// GetIdentityImages returns an array of json marshalled IdentityImages assigned to the user's identity
|
|
|
|
func (api *MultiAccountsAPI) GetIdentityImages(keyUID string) ([]*images.IdentityImage, error) {
|
|
|
|
return api.db.GetIdentityImages(keyUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentityImage returns a json object representing the image with the given name
|
|
|
|
func (api *MultiAccountsAPI) GetIdentityImage(keyUID, name string) (*images.IdentityImage, error) {
|
|
|
|
return api.db.GetIdentityImage(keyUID, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreIdentityImage takes the filepath of an image, crops it as per the rect coords and finally resizes the image.
|
|
|
|
// The resulting image(s) will be stored in the DB along with other user account information.
|
|
|
|
// aX and aY represent the pixel coordinates of the upper left corner of the image's cropping area
|
|
|
|
// bX and bY represent the pixel coordinates of the lower right corner of the image's cropping area
|
|
|
|
func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY, bX, bY int) ([]*images.IdentityImage, error) {
|
|
|
|
iis, err := images.GenerateIdentityImages(filepath, aX, aY, bX, bY)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.db.StoreIdentityImages(keyUID, iis)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return iis, err
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:00:44 +00:00
|
|
|
func (api *MultiAccountsAPI) StoreIdentityImageFromURL(keyUID, url string) ([]*images.IdentityImage, error) {
|
|
|
|
iis, err := images.GenerateIdentityImagesFromURL(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.db.StoreIdentityImages(keyUID, iis)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return iis, err
|
|
|
|
}
|
|
|
|
|
2020-11-30 15:41:45 +00:00
|
|
|
// DeleteIdentityImage deletes an IdentityImage from the db with the given name
|
|
|
|
func (api *MultiAccountsAPI) DeleteIdentityImage(keyUID string) error {
|
|
|
|
return api.db.DeleteIdentityImage(keyUID)
|
|
|
|
}
|