Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/nice-mice-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': minor
---

Adds `separateResponse` param to incoming webhooks to return per-channel statuses; when false or omitted, validates all targets and aborts sending to any channel if one fails.
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ API.v1.addRoute(

const messageReturn = (await applyAirGappedRestrictionsValidation(() => processWebhookMessage(this.bodyParams, this.user)))[0];

if (!messageReturn) {
if (!messageReturn?.message) {
return API.v1.failure('unknown-error');
}

Expand Down
29 changes: 24 additions & 5 deletions apps/meteor/app/integrations/server/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { APIClass } from '../../../api/server/ApiClass';
import type { RateLimiterOptions } from '../../../api/server/api';
import { API, defaultRateLimiterOptions } from '../../../api/server/api';
import type { FailureResult, PartialThis, SuccessResult, UnavailableResult } from '../../../api/server/definition';
import type { WebhookResponseItem } from '../../../lib/server/functions/processWebhookMessage';
import { processWebhookMessage } from '../../../lib/server/functions/processWebhookMessage';
import { settings } from '../../../settings/server';
import { IsolatedVMScriptEngine } from '../lib/isolated-vm/isolated-vm';
Expand Down Expand Up @@ -115,7 +116,12 @@ async function removeIntegration(options: { target_url: string }, user: IUser):

async function executeIntegrationRest(
this: IntegrationThis,
): Promise<SuccessResult<Record<string, string> | undefined | void> | FailureResult<string> | UnavailableResult<string>> {
): Promise<
| SuccessResult<Record<string, string> | { responses: WebhookResponseItem[] } | undefined | void>
| FailureResult<string>
| FailureResult<{ responses: WebhookResponseItem[] }>
| UnavailableResult<string>
> {
incomingLogger.info({ msg: 'Post integration:', integration: this.request.integration.name });
incomingLogger.debug({ urlParams: this.urlParams, bodyParams: this.bodyParams });

Expand All @@ -133,6 +139,7 @@ async function executeIntegrationRest(
const scriptEngine = getEngine(this.request.integration);

let { bodyParams } = this;
const separateResponse = this.bodyParams?.separateResponse === true;
let scriptResponse: Record<string, any> | undefined;

if (scriptEngine.integrationHasValidScript(this.request.integration) && this.request.body) {
Expand Down Expand Up @@ -185,6 +192,11 @@ async function executeIntegrationRest(
}

bodyParams = result && result.content;

if (!('separateResponse' in bodyParams)) {
bodyParams.separateResponse = separateResponse;
}

scriptResponse = result.response;
if (result.user) {
this.user = result.user;
Expand Down Expand Up @@ -217,16 +229,23 @@ async function executeIntegrationRest(
bodyParams.bot = { i: this.request.integration._id };

try {
const message = await processWebhookMessage(bodyParams, this.user, defaultValues);
if (_.isEmpty(message)) {
const messageResponse = await processWebhookMessage(bodyParams, this.user, defaultValues);
if (_.isEmpty(messageResponse)) {
return API.v1.failure('unknown-error');
}

if (scriptResponse) {
incomingLogger.debug({ msg: 'response', response: scriptResponse });
return API.v1.success(scriptResponse);
}

return API.v1.success(scriptResponse);
if (bodyParams.separateResponse) {
const allFailed = messageResponse.every((response) => 'error' in response && response.error);
if (allFailed) {
return API.v1.failure({ responses: messageResponse });
}
return API.v1.success({ responses: messageResponse });
}
return API.v1.success();
} catch ({ error, message }: any) {
return API.v1.failure(error || message);
}
Expand Down
15 changes: 5 additions & 10 deletions apps/meteor/app/integrations/server/lib/triggerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class RocketChatIntegrationHandler {
room?: IRoom;
message: { channel: string; bot?: Record<string, any>; message: Partial<IMessage> };
data: IntegrationData;
}): Promise<{ channel: string; message: Partial<IMessage> } | undefined> {
}): Promise<{ channel: string; message: Partial<IMessage> }[] | undefined> {
let user: IUser | null = null;
// Try to find the user who we are impersonating
if (trigger.impersonateUser) {
Expand Down Expand Up @@ -180,12 +180,7 @@ class RocketChatIntegrationHandler {
channel: tmpRoom.t === 'd' ? `@${tmpRoom._id}` : `#${tmpRoom._id}`,
};

message = (await processWebhookMessage(
message as any,
user as IUser & { username: RequiredField<IUser, 'username'> },
defaultValues,
)) as unknown as { channel: string; message: Partial<IMessage> };
return message;
return processWebhookMessage(message, user as IUser & { username: RequiredField<IUser, 'username'> }, defaultValues);
}

eventNameArgumentsToObject(...args: unknown[]) {
Expand Down Expand Up @@ -575,7 +570,7 @@ class RocketChatIntegrationHandler {
await updateHistory({
historyId,
step: 'after-prepare-send-message',
prepareSentMessage: [prepareMessage],
prepareSentMessage: prepareMessage,
});
}

Expand Down Expand Up @@ -669,7 +664,7 @@ class RocketChatIntegrationHandler {
await updateHistory({
historyId,
step: 'after-process-send-message',
processSentMessage: [resultMessage],
processSentMessage: resultMessage,
finished: true,
});
return;
Expand Down Expand Up @@ -765,7 +760,7 @@ class RocketChatIntegrationHandler {
await updateHistory({
historyId,
step: 'url-response-sent-message',
resultMessage: [resultMsg],
resultMessage: resultMsg,
finished: true,
});
}
Expand Down
Loading
Loading