github.com/status-im/status-go@v1.1.0/services/accounts/multiaccounts.go (about)

     1  package accounts
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/status-im/status-go/timesource"
     7  
     8  	"github.com/status-im/status-go/server"
     9  
    10  	"github.com/status-im/status-go/images"
    11  	"github.com/status-im/status-go/multiaccounts"
    12  )
    13  
    14  var (
    15  	// ErrUpdatingWrongAccount raised if caller tries to update any other account except one used for login.
    16  	ErrUpdatingWrongAccount = errors.New("failed to update wrong account. Please login with that account first")
    17  )
    18  
    19  func NewMultiAccountsAPI(db *multiaccounts.Database, mediaServer *server.MediaServer) *MultiAccountsAPI {
    20  	return &MultiAccountsAPI{db: db, mediaServer: mediaServer}
    21  }
    22  
    23  // MultiAccountsAPI is class with methods available over RPC.
    24  type MultiAccountsAPI struct {
    25  	db          *multiaccounts.Database
    26  	mediaServer *server.MediaServer
    27  }
    28  
    29  func (api *MultiAccountsAPI) UpdateAccount(account multiaccounts.Account) error {
    30  	oldAcc, err := api.db.GetAccount(account.KeyUID)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	if oldAcc == nil {
    35  		return errors.New("UpdateAccount but account not found")
    36  	}
    37  	if oldAcc.CustomizationColor != account.CustomizationColor {
    38  		updatedAt := timesource.GetCurrentTimeInMillis()
    39  		account.CustomizationColorClock = updatedAt
    40  	}
    41  	return api.db.UpdateAccount(account)
    42  }
    43  
    44  //
    45  // Profile Images
    46  //
    47  
    48  // GetIdentityImages returns an array of json marshalled IdentityImages assigned to the user's identity
    49  func (api *MultiAccountsAPI) GetIdentityImages(keyUID string) ([]*images.IdentityImage, error) {
    50  	return api.db.GetIdentityImages(keyUID)
    51  }
    52  
    53  // GetIdentityImage returns a json object representing the image with the given name
    54  func (api *MultiAccountsAPI) GetIdentityImage(keyUID, name string) (*images.IdentityImage, error) {
    55  	return api.db.GetIdentityImage(keyUID, name)
    56  }
    57  
    58  // StoreIdentityImage takes the filepath of an image, crops it as per the rect coords and finally resizes the image.
    59  // The resulting image(s) will be stored in the DB along with other user account information.
    60  // aX and aY represent the pixel coordinates of the upper left corner of the image's cropping area
    61  // bX and bY represent the pixel coordinates of the lower right corner of the image's cropping area
    62  func (api *MultiAccountsAPI) StoreIdentityImage(keyUID, filepath string, aX, aY, bX, bY int) ([]images.IdentityImage, error) {
    63  	iis, err := images.GenerateIdentityImages(filepath, aX, aY, bX, bY)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	err = api.db.StoreIdentityImages(keyUID, iis, true)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return iis, err
    74  }
    75  
    76  func (api *MultiAccountsAPI) StoreIdentityImageFromURL(keyUID, url string) ([]images.IdentityImage, error) {
    77  	iis, err := images.GenerateIdentityImagesFromURL(url)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	err = api.db.StoreIdentityImages(keyUID, iis, true)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return iis, err
    88  }
    89  
    90  // DeleteIdentityImage deletes an IdentityImage from the db with the given name
    91  func (api *MultiAccountsAPI) DeleteIdentityImage(keyUID string) error {
    92  	return api.db.DeleteIdentityImage(keyUID)
    93  }