Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/update_msc4461_support_to_v3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: minor
---

Update MSC4461 (Persona) support to v3
3 changes: 1 addition & 2 deletions src/app/features/room/composerMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const mx = {
const profile = (id: string, displayname: string): PerMessageProfileMsc4461 => ({
id,
displayname,
trigger: { prefix: [] },
});

/** Mirrors a command selected from autocomplete in the engine-neutral document. */
Expand Down Expand Up @@ -179,7 +178,7 @@ describe('buildOutgoingMessage', () => {
});

it('strips a pluralkit proxy wrapper and lets its profile win', async () => {
const proxied = { ...profile('proxy', 'Proxied'), trigger: { prefix: ['A: '] } };
const proxied = { ...profile('proxy', 'Proxied'), triggers: [{ prefix: 'A: ' }] };
profiles.account = proxied;

const result = await build('A: hello there', {
Expand Down
8 changes: 4 additions & 4 deletions src/app/features/room/persona-picker/PersonaPicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ function deferred<T>() {
}

const profiles: PerMessageProfileMsc4461[] = [
{ id: 'first', displayname: 'First', trigger: { prefix: [] } },
{ id: 'second', displayname: 'Second', trigger: { prefix: [] } },
{ id: 'first', displayname: 'First' },
{ id: 'second', displayname: 'Second' },
];

type TestMatrixClient = MatrixClient & { emitAccountData: (type: string) => void };
Expand Down Expand Up @@ -198,12 +198,12 @@ describe('PersonaPicker async flows', () => {
view.rerender({ mx: secondClient });

await act(async () => {
secondFetch.resolve([{ id: 'new', displayname: 'New', trigger: { prefix: [] } }]);
secondFetch.resolve([{ id: 'new', displayname: 'New' }]);
await secondFetch.promise;
});
await waitFor(() => expect(view.result.current.profiles?.[0]?.id).toBe('new'));
await act(async () => {
firstFetch.resolve([{ id: 'old', displayname: 'Old', trigger: { prefix: [] } }]);
firstFetch.resolve([{ id: 'old', displayname: 'Old' }]);
await firstFetch.promise;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('PerMessageProfileEditor', () => {
mx={{} as MatrixClient}
profileId="old-id"
displayName="New Profile"
shorthands={{ prefix: [] }}
shorthands={[]}
/>
);

Expand Down
60 changes: 14 additions & 46 deletions src/app/features/settings/Persona/PerMessageProfileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ import { SettingTile } from '$components/setting-tile';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { NameColorEditor } from '../account/NameColorEditor';
import {
MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_CIRCUMFIX_PROPERTY_NAME,
MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_SUFFIX_PROPERTY_NAME,
MATRIX_UNSTABLE_COLORS,
MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME,
} from '$unstable/prefixes';
import { accessibleColor } from '$plugins/color';
import { ThemeKind } from '$hooks/useTheme';

type Shorthand = { prefix?: string; suffix?: string };
type ShorthandRow = Shorthand & { id: string };
type ShorthandRow = ProfileTrigger & { id: string };

type ShorthandListItemProps = ShorthandRow & {
onDelete: (shorthandId: string) => void;
onChange: (shorthandId: string, shorthand: Shorthand) => void;
onChange: (shorthandId: string, shorthand: ProfileTrigger) => void;
};
function ShorthandListItem({ id, prefix, suffix, onDelete, onChange }: ShorthandListItemProps) {
const [newPrefix, setNewPrefix] = useState(prefix);
Expand Down Expand Up @@ -136,46 +133,17 @@ function ShorthandListItem({ id, prefix, suffix, onDelete, onChange }: Shorthand
);
}

function triggersToShorthandRows(trigger: ProfileTrigger): ShorthandRow[] {
const prefixes: ShorthandRow[] = trigger.prefix.map((str) => {
return { prefix: str, id: nanoid() };
});
const suffixes: ShorthandRow[] | undefined = trigger['net.f0rest.suffix']?.map((str) => {
return { suffix: str, id: nanoid() };
});
const circumfixes: ShorthandRow[] | undefined = trigger['net.f0rest.circumfix']?.map(
({ prefix, suffix }) => {
return { prefix: prefix, suffix: suffix, id: nanoid() };
}
);

return prefixes.concat(suffixes ?? [], circumfixes ?? []);
function triggersToShorthandRows(triggers: ProfileTrigger[]): ShorthandRow[] {
return triggers.map(({ prefix, suffix, keep_trigger }) => ({
prefix,
suffix,
keep_trigger,
id: nanoid(),
}));
}

function shorthandRowsToTriggers(rows: ShorthandRow[]): ProfileTrigger {
const prefix: string[] = [];
const suffix: string[] = [];
const circumfix: { prefix: string; suffix: string }[] = [];

rows.forEach((row) => {
if (row.prefix && row.suffix) {
circumfix.push({ prefix: row.prefix, suffix: row.suffix });
} else if (row.prefix) {
prefix.push(row.prefix);
} else if (row.suffix) {
suffix.push(row.suffix);
}
});

return {
prefix,
...(suffix.length > 0
? { [MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_SUFFIX_PROPERTY_NAME]: suffix }
: {}),
...(circumfix.length > 0
? { [MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_CIRCUMFIX_PROPERTY_NAME]: circumfix }
: {}),
};
function shorthandRowsToTriggers(rows: ShorthandRow[]): ProfileTrigger[] {
return rows.map(({ prefix, suffix, keep_trigger }) => ({ prefix, suffix, keep_trigger }));
}
/**
* the props we use for the per-message profile editor, which is used to edit a per-message profile. This is used in the settings page when the user wants to edit a profile.
Expand All @@ -188,7 +156,7 @@ export type PerMessageProfileEditorProps = {
pronouns?: PronounSet[];
nameColorLightTheme?: string;
nameColorDarkTheme?: string;
shorthands?: ProfileTrigger;
shorthands?: ProfileTrigger[];
onDelete?: (profileId: string) => void;
};

Expand Down Expand Up @@ -255,7 +223,7 @@ export function PerMessageProfileEditor({
setNewShorthands((s) => s?.filter((shorthand) => shorthand.id !== id));
};

const handleSaveShorthand = (oldId: string, shorthand: Shorthand) => {
const handleSaveShorthand = (oldId: string, shorthand: ProfileTrigger) => {
setNewShorthands((rows = []) => {
const index = rows.findIndex((row) => row.id === oldId);
if (index === -1) return rows;
Expand Down Expand Up @@ -358,7 +326,7 @@ export function PerMessageProfileEditor({
displayname: newDisplayName,
avatar_url: avatarMxc,
[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]: newPronouns,
trigger: shorthandRowsToTriggers(newShorthands ?? []),
triggers: shorthandRowsToTriggers(newShorthands ?? []),
[MATRIX_UNSTABLE_COLORS]: {
on_light: newNameColorLight ?? undefined,
on_dark: newNameColorDark ?? undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function PerMessageProfileOverview({
const newProfile: PerMessageProfileMsc4461 = {
id: generateShortId(5),
displayname: 'New Profile',
trigger: { prefix: [] },
triggers: [],
};
await addOrUpdatePerMessageProfile(mx, newProfile);
onCreateProfile(newProfile);
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/settings/Persona/ProfilesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function PerMessageProfilePage({ requestBack, requestClose }: PerMessageP
pronouns={editingProfile['io.fsky.nyx.pronouns']}
nameColorLightTheme={editingProfile['eu.she-a.color']?.on_light}
nameColorDarkTheme={editingProfile['eu.she-a.color']?.on_dark}
shorthands={editingProfile.trigger}
shorthands={editingProfile.triggers ?? []}
requestClose={handleEditorClose}
/>
);
Expand Down
1 change: 0 additions & 1 deletion src/app/hooks/commands/pmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const createPmpCommands = (ctx: CommandContext): Partial<CommandRecord> =
id: profileId,
displayname: name || '',
avatar_url,
trigger: { prefix: [] },
};
await addOrUpdatePerMessageProfile(mx, pmp)
.then(() => {
Expand Down
82 changes: 65 additions & 17 deletions src/app/hooks/usePerMessageProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { MatrixClient } from '$types/matrix-sdk';
import {
MATRIX_SABLE_UNSTABLE_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME,
MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME,
MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME_V2,
} from '$unstable/prefixes';
import {
addOrUpdatePerMessageProfile,
Expand All @@ -12,7 +13,7 @@ import {
renamePerMessageProfile,
type PerMessageProfileMsc4461,
} from './usePerMessageProfile';
import type { PersonaCatalogContent } from '$app/persona/catalog';
import type { PersonaCatalogContent, PersonaV2 } from '$app/persona/catalog';
import { projectPersona } from '$app/persona/projection';
import { resolvePersonaProxy } from '$app/persona/proxy';
import { resolvePersona } from '$app/persona/selection';
Expand Down Expand Up @@ -47,15 +48,24 @@ function createMatrixClient(profiles: PerMessageProfileMsc4461[] = []) {
const profile = (id: string): PerMessageProfileMsc4461 => ({
id,
displayname: `Profile ${id}`,
trigger: { prefix: [] },
triggers: [],
});

const profileV2 = (id: string): PersonaV2 => ({
id,
displayname: `Profile ${id}`,
trigger: {
prefix: [],
},
});

describe('profile persistence', () => {
it('normalizes the previously nested MSC4461 account-data payload', async () => {
const { accountData, mx } = createMatrixClient();
accountData.set(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME, {
accountData.delete(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME);
accountData.set(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME_V2, {
type: 'm.per_message_profiles',
content: { profiles: [profile('first')] },
content: { profiles: [profileV2('first')] },
});

await expect(getAllPerMessageProfiles(mx)).resolves.toEqual([profile('first')]);
Expand Down Expand Up @@ -83,11 +93,7 @@ describe('profile persistence', () => {
id: 'legacy',
displayname: 'Legacy',
avatar_url: 'mxc://example.org/avatar',
trigger: {
prefix: [],
'net.f0rest.suffix': [],
'net.f0rest.circumfix': [],
},
triggers: [],
},
]);
expect(
Expand All @@ -98,6 +104,44 @@ describe('profile persistence', () => {
).toBeUndefined();
});

it('migrates MSC4461 catalog from v2 to v3', async () => {
const { accountData, mx } = createMatrixClient();
accountData.delete(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME);
accountData.set(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME_V2, {
profiles: [
{
id: 'v2id',
displayname: 'V2 Persona',
'com.example.unknown-third-party': 'foobar',
trigger: {
prefix: ['a: '],
'net.f0rest.suffix': [' :b'],
'net.f0rest.circumfix': [{ prefix: '[', suffix: ']' }],
},
},
],
});

const catalog = {
profiles: [
{
id: 'v2id',
displayname: 'V2 Persona',
'com.example.unknown-third-party': 'foobar',
triggers: [{ prefix: '[', suffix: ']' }, { prefix: 'a: ' }, { suffix: ' :b' }],
},
],
};

await expect(getAllPerMessageProfiles(mx)).resolves.toEqual(catalog.profiles);
expect(
accountData.get(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME)
).toEqual(catalog);
expect(
accountData.get(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME_V2)
).toBeUndefined();
});

it('cleans up an empty legacy index', async () => {
const { accountData, mx } = createMatrixClient();
accountData.delete(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME);
Expand All @@ -116,13 +160,17 @@ describe('profile persistence', () => {
});
});

it('filters malformed catalog and legacy profile entries', async () => {
it('filters malformed legacy profile entries', async () => {
const { accountData, mx } = createMatrixClient();
accountData.set(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME, {
profiles: [profile('valid'), { id: 'missing-trigger', displayname: 'Invalid' }],
accountData.delete(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME);
accountData.set(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME_V2, {
profiles: [profileV2('valid'), { id: 'missing-trigger', displayname: 'Invalid' }],
});
await expect(getAllPerMessageProfiles(mx)).resolves.toEqual([profile('valid')]);
});

it('filters malformed catalog v2 profile entries', async () => {
const { accountData, mx } = createMatrixClient();
accountData.delete(MATRIX_UNSTABLE_MSC4461_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME);
accountData.set(`${MATRIX_SABLE_UNSTABLE_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME}.index`, {
profileIds: ['valid', 'invalid', 1],
Expand All @@ -138,7 +186,7 @@ describe('profile persistence', () => {
{
id: 'valid',
displayname: 'Valid',
trigger: { prefix: [], 'net.f0rest.suffix': [], 'net.f0rest.circumfix': [] },
triggers: [],
},
]);
});
Expand Down Expand Up @@ -226,8 +274,8 @@ describe('profile persistence', () => {

describe('persona resolution', () => {
const personas = [
{ ...profile('first'), trigger: { prefix: ['first: '] } },
{ ...profile('second'), trigger: { prefix: ['second: '] } },
{ ...profile('first'), triggers: [{ prefix: 'first: ' }] },
{ ...profile('second'), triggers: [{ prefix: 'second: ' }] },
];

it('applies precedence and ignores expired selections', () => {
Expand Down Expand Up @@ -258,10 +306,10 @@ describe('persona resolution', () => {
});

it('strips suffix and circumfix triggers', () => {
const suffix = { ...personas[0]!, trigger: { prefix: [], 'net.f0rest.suffix': [' -a'] } };
const suffix = { ...personas[0]!, triggers: [{ suffix: ' -a' }] };
const circumfix = {
...personas[1]!,
trigger: { prefix: [], 'net.f0rest.circumfix': [{ prefix: '[', suffix: ']' }] },
triggers: [{ prefix: '[', suffix: ']' }],
};

expect(resolvePersonaProxy([suffix], 'hello -a')).toEqual({ persona: suffix, body: 'hello' });
Expand Down
Loading
Loading