Skip to content

Commit

Permalink
feat(messaging): only create reaction hmac if doing encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
rstachof committed Dec 17, 2024
1 parent 5e322e8 commit 37a1d50
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
53 changes: 28 additions & 25 deletions packages/@webex/internal-plugin-conversation/src/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,33 +340,36 @@ const Conversation = WebexPlugin.extend({
* @param {String} recipientId,
* @returns {Promise<Activity>}
*/
addReaction(conversation, displayName, activity, recipientId) {
return this.createReactionHmac(displayName, activity).then((hmac) => {
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName,
hmac,
},
};
async addReaction(conversation, displayName, activity, recipientId) {
let hmac;
if (this.config.includeEncryptionTransforms) {
hmac = await this.createReactionHmac(displayName, activity);
}

if (recipientId) {
addReactionPayload.recipients = {items: [{id: recipientId, objectType: 'person'}]};
}
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName,
hmac,
},
};

return this.sendReaction(conversation, addReactionPayload);
});
if (recipientId) {
addReactionPayload.recipients = {items: [{id: recipientId, objectType: 'person'}]};
}

return this.sendReaction(conversation, addReactionPayload);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,27 @@ describe('plugin-conversation', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.config.includeEncryptionTransforms = true;
conversation.sendReaction = sinon.stub().returns(Promise.resolve());
conversation.createReactionHmac = sinon.stub().returns(Promise.resolve('hmac'));

return conversation.addReaction({}, 'example-display-name', {}, recipientId).then(() => {
assert.called(conversation.createReactionHmac);
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
});
});
});

describe('deleteReaction()', () => {
it('should add recipients to the payload if provided', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.sendReaction = sinon.stub().returns(Promise.resolve());

return conversation.deleteReaction({}, 'example-reaction-id', recipientId).then(() => {
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
});
});
});

describe('prepare()', () => {
it('should ammend activity recipients to the returned object', () => {
const {conversation} = webex.internal;
const activity = {recipients: 'example-recipients'};

return conversation.prepare(activity).then((results) => {
assert.deepEqual(results.recipients, activity.recipients);
});
});
});

describe('addReaction()', () => {
it('should add recipients to the payload if provided', () => {
it('will not call createReactionHmac if config prohibits', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.config.includeEncryptionTransforms = false;
conversation.sendReaction = sinon.stub().returns(Promise.resolve());
conversation.createReactionHmac = sinon.stub().returns(Promise.resolve('hmac'));
conversation.createReactionHmac = sinon.stub();

return conversation.addReaction({}, 'example-display-name', {}, recipientId).then(() => {
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
assert.notCalled(conversation.createReactionHmac);
});
});
});
Expand Down

0 comments on commit 37a1d50

Please sign in to comment.