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 c5876a6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
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
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 c5876a6

Please sign in to comment.