Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add a setting to not run outgoing integrations on message edits #6615

Merged
Merged
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
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@
"Integration_Retry_Count_Description": "How many times should the integration be tried if the call to the url fails?",
"Integration_Retry_Delay": "Retry Delay",
"Integration_Retry_Delay_Description": "Which delay algorithm should the retrying use? <code class=\"inline\">10^x</code> or <code class=\"inline\">2^x</code> or <code class=\"inline\">x*2</code>",
"Integration_Run_When_Message_Is_Edited": "Run On Edits",
"Integration_Run_When_Message_Is_Edited_Description": "Should the integration run when the message is edited? Setting this to false will cause the integration to only run on <strong>new</strong> messages.",
"Integration_Word_Trigger_Placement": "Word Placement Anywhere",
"Integration_Word_Trigger_Placement_Description": "Should the Word be Triggered when placed anywhere in the sentence other than the beginning?",
"Integration_updated": "Integration has been updated.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ <h2>
<div class="settings-description">{{_ "Integration_Word_Trigger_Placement_Description"}}</div>
</div>
</div>
<div class="input-line double-col">
<label>{{_ "Integration_Run_When_Message_Is_Edited"}}</label>
<div>
<label><input type="radio" name="runOnEdits" value="1" checked="{{$eq data.runOnEdits true}}" /> {{_ "True"}}</label>
<label><input type="radio" name="runOnEdits" value="0" checked="{{$neq data.runOnEdits true}}" /> {{_ "False"}}</label>
<div class="settings-description">{{{_ "Integration_Run_When_Message_Is_Edited_Description"}}}</div>
</div>
</div>
{{/if}}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Template.integrationsOutgoing.onCreated(function _integrationsOutgoingOnCreated(
token: Random.id(24),
retryFailedCalls: true,
retryCount: 6,
retryDelay: 'powers-of-ten'
retryDelay: 'powers-of-ten',
runOnEdits: true
});

this.updateRecord = () => {
Expand All @@ -31,7 +32,8 @@ Template.integrationsOutgoing.onCreated(function _integrationsOutgoingOnCreated(
triggerWordAnywhere: $('[name=triggerWordAnywhere]').val() ? $('[name=triggerWordAnywhere]').val().trim() : undefined,
retryFailedCalls: $('[name=retryFailedCalls]:checked').val().trim() === '1',
retryCount: $('[name=retryCount]').val() ? $('[name=retryCount]').val().trim() : 6,
retryDelay: $('[name=retryDelay]').val() ? $('[name=retryDelay]').val().trim() : 'powers-of-ten'
retryDelay: $('[name=retryDelay]').val() ? $('[name=retryDelay]').val().trim() : 'powers-of-ten',
runOnEdits: $('[name=runOnEdits]:checked').val().trim() === '1'
});
};

Expand Down Expand Up @@ -287,11 +289,13 @@ Template.integrationsOutgoing.events({

let triggerWords;
let triggerWordAnywhere;
let runOnEdits;
if (RocketChat.integrations.outgoingEvents[event].use.triggerWords) {
triggerWords = $('[name=triggerWords]').val().trim();
triggerWords = triggerWords.split(',').filter((word) => word.trim() !== '');

triggerWordAnywhere = $('[name=triggerWordAnywhere]').val().trim();
runOnEdits = $('[name=runOnEdits]:checked').val().trim();
}

let channel;
Expand Down Expand Up @@ -338,7 +342,8 @@ Template.integrationsOutgoing.events({
retryFailedCalls: retryFailedCalls === '1',
retryCount: retryCount ? retryCount : 6,
retryDelay: retryDelay ? retryDelay : 'powers-of-ten',
triggerWordAnywhere: triggerWordAnywhere === '1'
triggerWordAnywhere: triggerWordAnywhere === '1',
runOnEdits: runOnEdits === '1'
};

const params = Template.instance().data.params? Template.instance().data.params() : undefined;
Expand Down
9 changes: 9 additions & 0 deletions packages/rocketchat-integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ RocketChat.integrations.triggerHandler = new class RocketChatIntegrationHandler
if (message.bot) {
data.bot = message.bot;
}

if (message.editedAt) {
data.isEdited = true;
}
break;
case 'fileUploaded':
data.channel_id = room._id;
Expand Down Expand Up @@ -585,6 +589,11 @@ RocketChat.integrations.triggerHandler = new class RocketChatIntegrationHandler
}
}

if (message && message.editedAt && !trigger.runOnEdits) {
logger.outgoing.debug(`The trigger "${ trigger.name }"'s run on edits is disabled and the message was edited.`);
return;
}

const historyId = this.updateHistory({ step: 'start-execute-trigger-url', integration: trigger, event });

const data = {
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-integrations/server/lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ RocketChat.integrations.validateOutgoing = function _validateOutgoing(integratio
}
}

if (typeof integration.runOnEdits !== 'undefined') {
// Verify this value is only true/false
integration.runOnEdits = integration.runOnEdits === true;
}

_verifyUserHasPermissionForChannels(integration, userId, channels);
_verifyRetryInformation(integration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Meteor.methods({
retryCount: integration.retryCount,
retryDelay: integration.retryDelay,
triggerWordAnywhere: integration.triggerWordAnywhere,
runOnEdits: integration.runOnEdits,
_updatedAt: new Date(),
_updatedBy: RocketChat.models.Users.findOne(this.userId, {fields: {username: 1}})
}
Expand Down
10 changes: 10 additions & 0 deletions server/startup/migrations/v092.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RocketChat.Migrations.add({
version: 92,
up() {
const outgoingIntegrations = RocketChat.models.Integrations.find({ type: 'webhook-outgoing', 'event': 'sendMessage' }, { fields: { name: 1 }}).fetch();

outgoingIntegrations.forEach((i) => {
RocketChat.models.Integrations.update(i._id, { $set: { runOnEdits: true }});
});
}
});