Skip to content

Commit fcff9ef

Browse files
authored
Fix PMP ID handling and generation (#583)
### Description This pull request addresses issues with ID handling and generation for Personas by standardizing the way short IDs are generated and used throughout the codebase. It replaces the use of `crypto.randomUUID()` with a new utility function, `generateShortId`, ensuring IDs are shorter and more consistent. Additionally, it improves command parsing for Persona-related commands. #### ID Generation Improvements - Introduced a new utility function `generateShortId` in `src/app/utils/shortIdGen.ts` to generate short, consistent IDs by truncating UUIDs. - Replaced all instances of `crypto.randomUUID()` with `generateShortId(5)` when creating new Persona or member IDs in `PerMessageProfileOverview.tsx` and `PKitCommandMessageHandler.ts`, ensuring all IDs are now short and uniform. #### Command Parsing Enhancements - Updated regular expressions in `useCommands.ts` to use `\S+` instead of `\w+` for Persona IDs, allowing IDs with a broader set of characters and improving robustness of command parsing. Fixes #582 #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. --> No AI was used for this PR
2 parents a8ddeb6 + dae8e6a commit fcff9ef

5 files changed

Lines changed: 15 additions & 4 deletions

File tree

.changeset/fix_pmp_id_handling.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
fix id handling and id generation for Personas

src/app/features/settings/Persona/PerMessageProfileOverview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '$hooks/usePerMessageProfile';
77
import { useEffect, useState } from 'react';
88
import { Box, Button, Text } from 'folds';
9+
import { generateShortId } from '$utils/shortIdGen';
910
import { PerMessageProfileEditor } from './PerMessageProfileEditor';
1011

1112
/**
@@ -36,7 +37,7 @@ export function PerMessageProfileOverview() {
3637
<Button
3738
onClick={() => {
3839
const newProfile: PerMessageProfile = {
39-
id: crypto.randomUUID(),
40+
id: generateShortId(5),
4041
name: 'New Profile',
4142
};
4243
addOrUpdatePerMessageProfile(mx, newProfile).then(() => {

src/app/hooks/useCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const FLAG_PAT = String.raw`(?:^|\s)-(\w+)\b`;
5151
const FLAG_REG = new RegExp(FLAG_PAT);
5252
const FLAG_REG_G = new RegExp(FLAG_PAT, 'g');
5353

54-
const ADDPMP_REGEX = /(\w+) (name=)?"?([\w\s]*)"? (avatar=)?([\w.:/]+)/;
55-
const USEPMP_REGEX = /^(\w+)\s*(-g)?(-o)?(-u)?\s*(\d+)?$/;
54+
const ADDPMP_REGEX = /(\S+) (name=)?"?([\w\s]*)"? (avatar=)?([\w.:/]+)/;
55+
const USEPMP_REGEX = /^(\S+)\s*(-g)?(-o)?(-u)?\s*(\d+)?$/;
5656

5757
export const splitPayloadContentAndFlags = (payload: string): [string, string | undefined] => {
5858
const flagMatch = new RegExp(FLAG_REG).exec(payload);

src/app/plugins/pluralkit-handler/PKitCommandMessageHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '$hooks/usePerMessageProfile';
99
import { sendFeedback } from '$utils/sendFeedbackToUser';
1010
import { MatrixClient, Room } from 'matrix-js-sdk';
11+
import { generateShortId } from '$utils/shortIdGen';
1112

1213
const pkMemberRenameRegex = /^(pk;member)\s+"?([\w\s]+)"?\s*rename\s+"?([\w\s]+)"?$/;
1314
const pkMemberNewRegex = /^(pk;member)\s+new\s+"?([\w\s]+)"?$/;
@@ -90,7 +91,7 @@ export class PKitCommandMessageHandler {
9091
return;
9192
}
9293
const memberName = cmdParts[2];
93-
const generatedID = crypto.randomUUID();
94+
const generatedID = generateShortId(5);
9495
sendFeedback(
9596
`adding new member has been created with id: ${generatedID} and name ${memberName}`,
9697
this.room,

src/app/utils/shortIdGen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function generateShortId(length: number): string {
2+
const uuid = crypto.randomUUID();
3+
return uuid.replaceAll('-', '').substring(0, length);
4+
}

0 commit comments

Comments
 (0)