Skip to content

Commit

Permalink
Migrate realms config from old account data key to new one
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemelia committed Dec 19, 2024
1 parent 55db1a6 commit c46c43b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/host/app/services/matrix-sdk-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export type ExtendedClient = Pick<
| 'setRoomName'
| 'startClient'
| 'getAccountDataFromServer'
| 'setAccountData'
> & {
requestEmailToken(
type: 'registration' | 'threepid',
Expand Down
29 changes: 29 additions & 0 deletions packages/host/app/services/matrix-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
APP_BOXEL_MESSAGE_MSGTYPE,
APP_BOXEL_REALM_SERVER_EVENT_MSGTYPE,
APP_BOXEL_REALMS_EVENT_TYPE,
LEGACY_APP_BOXEL_REALMS_EVENT_TYPE,
} from '@cardstack/runtime-common/matrix-constants';

import {
Expand Down Expand Up @@ -411,6 +412,34 @@ export default class MatrixService extends Service {
let accountDataContent = await this._client.getAccountDataFromServer<{
realms: string[];
}>(APP_BOXEL_REALMS_EVENT_TYPE);
// TODO: remove this once we've migrated all users
// TEMPORARY MIGRATION CODE
if (!accountDataContent?.realms?.length) {
console.log(
'You currently have no realms set, checking your old realms',
);
try {
accountDataContent = await this._client.getAccountDataFromServer<{
realms: string[];
}>(LEGACY_APP_BOXEL_REALMS_EVENT_TYPE);
} catch (e) {
// throws if nothing at this key
}
if (accountDataContent?.realms) {
console.log('Migrating your old realms to the new format');
await this._client.setAccountData(APP_BOXEL_REALMS_EVENT_TYPE, {
realms: accountDataContent.realms,
});
console.log('Removing your old realms data');
await this._client.setAccountData(
LEGACY_APP_BOXEL_REALMS_EVENT_TYPE,
{},
);
} else {
console.log('No old realms found');
}
}
// END OF TEMPORARY MIGRATION CODE
await this.realmServer.setAvailableRealmURLs(
accountDataContent?.realms ?? [],
);
Expand Down
14 changes: 12 additions & 2 deletions packages/host/tests/helpers/mock-matrix/_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { baseRealm, unixTime } from '@cardstack/runtime-common';
import {
APP_BOXEL_ROOM_SKILLS_EVENT_TYPE,
APP_BOXEL_REALMS_EVENT_TYPE,
LEGACY_APP_BOXEL_REALMS_EVENT_TYPE,
} from '@cardstack/runtime-common/matrix-constants';

import type { ExtendedClient } from '@cardstack/host/services/matrix-sdk-loader';
Expand Down Expand Up @@ -114,8 +115,17 @@ export class MockClient implements ExtendedClient {
throw new Error('Method not implemented.');
}

setAccountData<T>(_type: string, _data: T): Promise<{}> {
throw new Error('Method not implemented.');
setAccountData<T>(type: string, data: T): Promise<{}> {
if (type === APP_BOXEL_REALMS_EVENT_TYPE) {
this.sdkOpts.activeRealms = (data as any).realms;
} else if (type === LEGACY_APP_BOXEL_REALMS_EVENT_TYPE) {
// nothing to do
} else {
throw new Error(
'Support for updating this event type in account data is not yet implemented in this mock.',
);
}
return Promise.resolve({});
}

getAccountData<T>(_type: string): Promise<T> {
Expand Down
1 change: 1 addition & 0 deletions packages/matrix/helpers/matrix-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const APP_BOXEL_COMMAND_RESULT_MSGTYPE = 'app.boxel.commandResult';
export const APP_BOXEL_REALM_SERVER_EVENT_MSGTYPE =
'app.boxel.realm-server-event';
export const APP_BOXEL_REALMS_EVENT_TYPE = 'app.boxel.realms';
export const LEGACY_APP_BOXEL_REALMS_EVENT_TYPE = 'com.cardstack.boxel.realms';
48 changes: 46 additions & 2 deletions packages/matrix/tests/realm-urls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import {
synapseStop,
type SynapseInstance,
registerUser,
getAccountData,
updateAccountData,
updateUser,
} from '../docker/synapse';
import { smtpStart, smtpStop } from '../docker/smtp4dev';
import { login, registerRealmUsers, setupUserSubscribed } from '../helpers';
import { APP_BOXEL_REALMS_EVENT_TYPE } from '@cardstack/runtime-common/matrix-constants';
import {
login,
logout,
registerRealmUsers,
setupUserSubscribed,
} from '../helpers';

import {
appURL,
startServer as startRealmServer,
type IsolatedRealmServer,
} from '../helpers/isolated-realm-server';
import {
APP_BOXEL_REALMS_EVENT_TYPE,
LEGACY_APP_BOXEL_REALMS_EVENT_TYPE,
} from '../helpers/matrix-constants';

test.describe('Realm URLs in Matrix account data', () => {
let synapse: SynapseInstance;
Expand Down Expand Up @@ -90,4 +100,38 @@ test.describe('Realm URLs in Matrix account data', () => {
),
).toHaveText('private');
});

test('deprecated account data key is supported by auto-migrating user to new key', async ({
page,
}) => {
await login(page, 'user1', 'pass', { url: appURL });
await updateAccountData(
'@user1:localhost',
user.accessToken,
LEGACY_APP_BOXEL_REALMS_EVENT_TYPE,
JSON.stringify({
realms: ['http://localhost:4205/user1/personal/'],
}),
);
await logout(page);
await login(page, 'user1', 'pass', { url: appURL });

await page.locator('[data-test-workspace-chooser-toggle]').click();

await page
.locator('[data-test-workspace-chooser]')
.waitFor({ state: 'visible' });

expect(
page.locator('[data-test-workspace-list] [data-test-workspace]'),
).toHaveCount(1);
let realms = await getAccountData<{ realms: string[] } | undefined>(
'@user1:localhost',
user.accessToken,
APP_BOXEL_REALMS_EVENT_TYPE,
);
expect(realms).toEqual({
realms: ['http://localhost:4205/user1/personal/'],
});
});
});
1 change: 1 addition & 0 deletions packages/runtime-common/matrix-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const APP_BOXEL_REALM_SERVER_EVENT_MSGTYPE =
'app.boxel.realm-server-event';
export const APP_BOXEL_ROOM_SKILLS_EVENT_TYPE = 'app.boxel.room.skills';
export const APP_BOXEL_REALMS_EVENT_TYPE = 'app.boxel.realms';
export const LEGACY_APP_BOXEL_REALMS_EVENT_TYPE = 'com.cardstack.boxel.realms';

0 comments on commit c46c43b

Please sign in to comment.