Skip to content

Commit

Permalink
Allowing message action items to be already set.
Browse files Browse the repository at this point in the history
  • Loading branch information
splindsay-92 committed Oct 22, 2024
1 parent 10e13aa commit 58cdb05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"grunt": "grunt",
"test": "npm run test:node",
"test:node": "npm run build:node && npm run build:push && mocha",
"test:grep": "npm run build:node && npm run build:push && mocha --grep",
"test:node:skip-build": "mocha",
"test:webserver": "grunt test:webserver",
"test:playwright": "node test/support/runPlaywrightTests.js",
Expand Down
19 changes: 5 additions & 14 deletions src/common/lib/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,18 @@ const MessageActionArray: API.MessageAction[] = [
'message_meta_occupancy',
];

function toMessageActionString(actionNumber?: number): API.MessageAction {
if (actionNumber === undefined) {
// Allow for the case where the action is not set
return 'message_unset';
}
function toMessageActionString(actionNumber: number): API.MessageAction | undefined {
if (actionNumber in MessageActionArray) {
return MessageActionArray[actionNumber];
}
throw new ErrorInfo('Unknown message action number: ' + actionNumber, 40000, 400);
}

function toMessageActionNumber(messageAction?: API.MessageAction): number {
if (messageAction === undefined) {
// Allow for the case where the action is not set
return 0;
}
function toMessageActionNumber(messageAction?: API.MessageAction): number | undefined {
for (const [index, value] of MessageActionArray.entries()) {
if (value === messageAction) {
return index;
}
}
throw new ErrorInfo('Unknown message action: ' + messageAction, 40000, 400);
}

export type CipherOptions = {
Expand Down Expand Up @@ -337,7 +327,8 @@ export function fromValues(
): Message {
const stringifyAction = options?.stringifyAction;
if (stringifyAction) {
return Object.assign(new Message(), { ...values, action: toMessageActionString(values.action as number) });
const action = toMessageActionString(values.action as number) || values.action;
return Object.assign(new Message(), { ...values, action });
}
return Object.assign(new Message(), values);
}
Expand Down Expand Up @@ -410,7 +401,7 @@ class Message {
connectionKey: this.connectionKey,
extras: this.extras,
serial: this.serial,
action: toMessageActionNumber(this.action as API.MessageAction),
action: toMessageActionNumber(this.action as API.MessageAction) || this.action,
refSerial: this.refSerial,
refType: this.refType,
updatedAt: this.updatedAt,
Expand Down

0 comments on commit 58cdb05

Please sign in to comment.