Skip to content

Commit

Permalink
feat(NOTIFY-1102): add account sync analytics callbacks (#4707)
Browse files Browse the repository at this point in the history
## Explanation

This PR adds callbacks that will be used for analytics in the account
sync context.

## References


[Notify-1102](https://consensyssoftware.atlassian.net/jira/software/projects/NOTIFY/boards/616?assignee=712020%3A5843b7e2-a7fe-4c45-9fbd-e1f2b2eb58c2&selectedIssue=NOTIFY-1102)

## Changelog

### `@metamask/profile-sync-controller`

- **ADDED**: `onAccountAdded` and `onAccountNameUpdated` account syncing
big sync callbacks


## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
mathieuartu authored Sep 16, 2024
1 parent 87cd863 commit d6be9f0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,59 @@ describe('user-storage/user-storage-controller - syncInternalAccountsWithUserSto
);
});

it('fires the onAccountAdded callback when adding an account', async () => {
const mockUserStorageAccountsResponse = async () => {
return {
status: 200,
body: await createMockUserStorageEntries(
MOCK_USER_STORAGE_ACCOUNTS.SAME_AS_INTERNAL_ALL,
),
};
};

const arrangeMocksForAccounts = async () => {
return {
messengerMocks: mockUserStorageMessenger({
accounts: {
accountsList: MOCK_INTERNAL_ACCOUNTS.ONE as InternalAccount[],
},
}),
mockAPI: {
mockEndpointGetUserStorage:
await mockEndpointGetUserStorageAllFeatureEntries(
'accounts',
await mockUserStorageAccountsResponse(),
),
},
};
};

const onAccountAdded = jest.fn();

const { messengerMocks, mockAPI } = await arrangeMocksForAccounts();
const controller = new UserStorageController({
messenger: messengerMocks.messenger,
config: {
accountSyncing: {
onAccountAdded,
},
},
env: {
isAccountSyncingEnabled: true,
},
getMetaMetricsState: () => true,
});

await controller.syncInternalAccountsWithUserStorage();

mockAPI.mockEndpointGetUserStorage.done();

expect(onAccountAdded).toHaveBeenCalledTimes(
MOCK_USER_STORAGE_ACCOUNTS.SAME_AS_INTERNAL_ALL.length -
MOCK_INTERNAL_ACCOUNTS.ONE.length,
);
});

it('does not create internal accounts if user storage has less accounts', async () => {
const mockUserStorageAccountsResponse = async () => {
return {
Expand Down Expand Up @@ -893,6 +946,48 @@ describe('user-storage/user-storage-controller - syncInternalAccountsWithUserSto
messengerMocks.mockAccountsUpdateAccountMetadata,
).not.toHaveBeenCalled();
});

it('fires the onAccountNameUpdated callback when renaming an internal account', async () => {
const arrangeMocksForAccounts = async () => {
return {
messengerMocks: mockUserStorageMessenger({
accounts: {
accountsList:
MOCK_INTERNAL_ACCOUNTS.ONE_DEFAULT_NAME as InternalAccount[],
},
}),
mockAPI: {
mockEndpointGetUserStorage:
await mockEndpointGetUserStorageAllFeatureEntries(
'accounts',
await mockUserStorageAccountsResponse(),
),
},
};
};

const onAccountNameUpdated = jest.fn();

const { messengerMocks, mockAPI } = await arrangeMocksForAccounts();
const controller = new UserStorageController({
messenger: messengerMocks.messenger,
config: {
accountSyncing: {
onAccountNameUpdated,
},
},
env: {
isAccountSyncingEnabled: true,
},
getMetaMetricsState: () => true,
});

await controller.syncInternalAccountsWithUserStorage();

mockAPI.mockEndpointGetUserStorage.done();

expect(onAccountNameUpdated).toHaveBeenCalledTimes(1);
});
});

describe('User storage name is a custom name with last updated', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ const metadata: StateMetadata<UserStorageControllerState> = {
},
};

type ControllerConfig = {
accountSyncing?: {
/**
* Callback that fires when account sync adds an account.
* This is used for analytics.
*/
onAccountAdded?: (profileId: string) => void;

/**
* Callback that fires when account sync updates the name of an account.
* This is used for analytics.
*/
onAccountNameUpdated?: (profileId: string) => void;
};
};

// Messenger Actions
type CreateActionsObj<Controller extends keyof UserStorageController> = {
[K in Controller]: {
Expand Down Expand Up @@ -347,6 +363,8 @@ export default class UserStorageController extends BaseController<
},
};

#config?: ControllerConfig;

#notificationServices = {
disableNotificationServices: async () => {
return await this.messagingSystem.call(
Expand Down Expand Up @@ -387,11 +405,13 @@ export default class UserStorageController extends BaseController<
messenger,
state,
env,
config,
getMetaMetricsState,
nativeScryptCrypto,
}: {
messenger: UserStorageControllerMessenger;
state?: UserStorageControllerState;
config?: ControllerConfig;
env?: {
isAccountSyncingEnabled?: boolean;
isNetworkSyncingEnabled?: boolean;
Expand All @@ -406,6 +426,8 @@ export default class UserStorageController extends BaseController<
state: { ...defaultState, ...state },
});

this.#config = config;

this.#accounts.isAccountSyncingEnabled = Boolean(
env?.isAccountSyncingEnabled,
);
Expand Down Expand Up @@ -722,6 +744,8 @@ export default class UserStorageController extends BaseController<
try {
this.#accounts.isAccountSyncingInProgress = true;

const profileId = await this.#auth.getProfileId();

const userStorageAccountsList =
await this.#accounts.getUserStorageAccountsList();

Expand Down Expand Up @@ -752,6 +776,7 @@ export default class UserStorageController extends BaseController<
length: numberOfAccountsToAdd,
}).map(async () => {
await this.messagingSystem.call('KeyringController:addNewAccount');
this.#config?.accountSyncing?.onAccountAdded?.(profileId);
});

await Promise.all(addNewAccountsPromises);
Expand Down Expand Up @@ -791,6 +816,8 @@ export default class UserStorageController extends BaseController<
name: userStorageAccount.n,
},
);

this.#config?.accountSyncing?.onAccountNameUpdated?.(profileId);
}
continue;
}
Expand Down Expand Up @@ -830,6 +857,8 @@ export default class UserStorageController extends BaseController<
},
);

this.#config?.accountSyncing?.onAccountNameUpdated?.(profileId);

continue;
} else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
await this.#accounts.saveInternalAccountToUserStorage(
Expand Down

0 comments on commit d6be9f0

Please sign in to comment.