11import {
22 addOrUpdatePerMessageProfile ,
33 associateProxyWithProfile ,
4+ dropProxyAssociationForPMP ,
45 getAllPerMessageProfiles ,
56 getPerMessageProfileById ,
67 PerMessageProfile ,
@@ -10,11 +11,15 @@ import { MatrixClient, Room } from 'matrix-js-sdk';
1011
1112const pkMemberRenameRegex = / ^ ( p k ; m e m b e r ) \s + " ? ( [ \w \s ] + ) " ? \s * r e n a m e \s + " ? ( [ \w \s ] + ) " ? $ / ;
1213const pkMemberNewRegex = / ^ ( p k ; m e m b e r ) \s + n e w \s + " ? ( [ \w \s ] + ) " ? $ / ;
13- const pkMemberNewProxy = / ^ ( p k ; m e m b e r ) \s + " ? ( [ \w \s ] + ) " ? \s + p r o x y \s + ( .* t e x t .* ) $ / ;
14+ const pkMemberNewProxy = / ^ ( p k ; m e m b e r ) \s + " ? ( [ \w \s ] + ) " ? \s + p r o x y ( \s + a d d ) ? \s + ( .* t e x t .* ) $ / ;
15+ const pkMemberRemoveProxy = / ^ ( p k ; m e m b e r ) \s + " ? ( [ \w \s ] + ) " ? \s + p r o x y \s + r e m o v e \s + ( .* t e x t .* ) $ / ;
1416
1517const helpTextPkMemberNew = 'To create a new persona: pk;member new Yumi' ;
1618const helpTextPkMemberRename = 'To rename a persona: pk;member "Rain Deer" rename "Micky Mouse"' ;
17- const helpTextPkMemberNewProxy = 'To create a persona: pk;member Yumi proxy [text]' ;
19+ const helpTextPkMemberNewProxy = 'To add a shorthand to a persona: pk;member Yumi proxy [text]' ;
20+ const helpTextPkMemberRemoveProxy =
21+ 'To remove a shorthand from a persona: pk;member Yumi proxy remove [text]' ;
22+ const helpTextPkMember = `Available 'pk;member' commands:\n${ helpTextPkMemberNew } \n${ helpTextPkMemberRename } \n${ helpTextPkMemberNewProxy } \n${ helpTextPkMemberRemoveProxy } ` ;
1823
1924/**
2025 * build a regex to recognize proxies
@@ -37,15 +42,32 @@ function buildRegex(template: string): RegExp {
3742 * @author Rye
3843 */
3944export class PKitCommandMessageHandler {
45+ /**
46+ * the matrix client we use, is set in the constructor
47+ *
48+ * @private
49+ * @type {MatrixClient }
50+ * @memberof PKitCommandMessageHandler
51+ */
4052 private readonly mx : MatrixClient ;
4153
4254 private message = '' ;
4355
4456 private readonly room : Room ;
4557
58+ /**
59+ * flag to interpret the name given as id
60+ *
61+ * @private
62+ * @type {boolean }
63+ * @memberof PKitCommandMessageHandler
64+ */
65+ private useIdInsteadOfNameWherePossible : boolean ;
66+
4667 public constructor ( mx : MatrixClient , room : Room ) {
4768 this . mx = mx ;
4869 this . room = room ;
70+ this . useIdInsteadOfNameWherePossible = false ;
4971 }
5072
5173 /**
@@ -127,15 +149,41 @@ export class PKitCommandMessageHandler {
127149 this . mx . getSafeUserId ( )
128150 ) ;
129151 addOrUpdatePerMessageProfile ( this . mx , pmp ) ;
152+ } else if ( pkMemberRemoveProxy . test ( this . message ) ) {
153+ const cmdParts = pkMemberRemoveProxy . exec ( this . message ) ;
154+ if ( ! cmdParts ) return ;
155+ const name = cmdParts [ 2 ] ;
156+ const matchAgainst = cmdParts [ 3 ] ;
157+ const pmpId = this . useIdInsteadOfNameWherePossible
158+ ? name
159+ : ( await getAllPerMessageProfiles ( this . mx ) ) . find ( ( pmp ) => pmp . name === name ) ?. id ;
160+ if ( ! pmpId ) {
161+ sendFeedback (
162+ `Persona with ${ this . useIdInsteadOfNameWherePossible ? 'id' : 'name' } "${ name } " doesn't exist in your records, ${ helpTextPkMemberNew } ` ,
163+ this . room ,
164+ this . mx . getSafeUserId ( )
165+ ) ;
166+ return ;
167+ }
168+
169+ dropProxyAssociationForPMP ( this . mx , matchAgainst ) ;
170+
171+ sendFeedback (
172+ `Persona with ${ this . useIdInsteadOfNameWherePossible ? 'id' : 'name' } "${ name } " (${ pmpId } ) is now no longer associated with ${ matchAgainst } ` ,
173+ this . room ,
174+ this . mx . getSafeUserId ( )
175+ ) ;
130176 } else if ( pkMemberNewProxy . test ( this . message ) ) {
131177 const cmdParts = pkMemberNewProxy . exec ( this . message ) ;
132178 if ( ! cmdParts ) return ;
133179 const name = cmdParts [ 2 ] ;
134- const matchAgainst = cmdParts [ 3 ] ;
135- const pmpId = ( await getAllPerMessageProfiles ( this . mx ) ) . find ( ( pmp ) => pmp . name === name ) ?. id ;
180+ const matchAgainst = cmdParts [ 4 ] ;
181+ const pmpId = this . useIdInsteadOfNameWherePossible
182+ ? name
183+ : ( await getAllPerMessageProfiles ( this . mx ) ) . find ( ( pmp ) => pmp . name === name ) ?. id ;
136184 if ( ! pmpId ) {
137185 sendFeedback (
138- `Persona with name "${ name } " doesn't exist in your records, ${ helpTextPkMemberNew } ` ,
186+ `Persona with ${ this . useIdInsteadOfNameWherePossible ? 'id' : ' name' } "${ name } " doesn't exist in your records, ${ helpTextPkMemberNew } ` ,
139187 this . room ,
140188 this . mx . getSafeUserId ( )
141189 ) ;
@@ -144,7 +192,7 @@ export class PKitCommandMessageHandler {
144192 const matchAgainstRegExp = buildRegex ( matchAgainst ) ;
145193 associateProxyWithProfile ( this . mx , pmpId , matchAgainst , matchAgainstRegExp , false ) ;
146194 sendFeedback (
147- `Persona with name "${ name } " (${ pmpId } ) is now associated with ${ matchAgainst } ` ,
195+ `Persona with ${ this . useIdInsteadOfNameWherePossible ? 'id' : ' name' } "${ name } " (${ pmpId } ) is now associated with ${ matchAgainst } ` ,
148196 this . room ,
149197 this . mx . getSafeUserId ( )
150198 ) ;
@@ -155,7 +203,7 @@ export class PKitCommandMessageHandler {
155203 . map ( ( pmp : PerMessageProfile ) => `${ pmp . id } : ${ pmp . name ? pmp . name : '(empty name)' } ` )
156204 . join ( '\n' ) ;
157205 sendFeedback (
158- `You currently have the following persona set up:\n${ stringListOfProfiles } \n\n${ helpTextPkMemberNew } \n ${ helpTextPkMemberRename } \n ${ helpTextPkMemberNewProxy } ` ,
206+ `If you see this, you have messed up a command\n\nYou currently have the following persona set up:\n${ stringListOfProfiles } \n\n${ helpTextPkMember } ` ,
159207 this . room ,
160208 this . mx . getSafeUserId ( )
161209 ) ;
@@ -176,9 +224,18 @@ export class PKitCommandMessageHandler {
176224 * @param message the message we want to handle
177225 * @returns void
178226 */
179- public async handleMessage ( message : string ) : Promise < void > {
227+ public async handleMessage ( message : string , useId ?: boolean ) : Promise < void > {
180228 this . message = message ;
181- if ( ! this . message . startsWith ( 'pk' ) ) return ;
182- if ( this . message . startsWith ( 'pk;member' ) ) await this . memberHandler ( ) ;
229+ this . useIdInsteadOfNameWherePossible = useId ?? false ;
230+ if ( ! this . message . startsWith ( 'pk;' ) ) return ;
231+ if ( this . message . startsWith ( 'pk;member' ) ) {
232+ await this . memberHandler ( ) ;
233+ return ;
234+ }
235+ sendFeedback (
236+ `Command currently not supported, right now compatibility is limited and only a subset of pk;member is supported\n\n${ helpTextPkMember } ` ,
237+ this . room ,
238+ this . mx . getSafeUserId ( )
239+ ) ;
183240 }
184241}
0 commit comments