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

fix: processWebhookMessage & chat.postMessage types #30652

Merged
merged 12 commits into from
Oct 26, 2023
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
import type { IMessage, IUser, RequiredField, MessageAttachment } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';

import { ensureArray } from '../../../../lib/utils/arrayUtils';
import { trim } from '../../../../lib/utils/stringUtils';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { validateRoomMessagePermissionsAsync } from '../../../authorization/server/functions/canSendMessage';
import { getRoomByNameOrIdWithOptionToJoin } from './getRoomByNameOrIdWithOptionToJoin';
import { sendMessage } from './sendMessage';

export const processWebhookMessage = async function (messageObj, user, defaultValues = { channel: '', alias: '', avatar: '', emoji: '' }) {
type Payload = {
channel?: string | string[];
roomId?: string | string[];
text?: IMessage['msg'];
msg?: IMessage['msg']; // overridden if text is present
username?: IMessage['alias'];
alias?: IMessage['alias']; // overridden if username is present
icon_emoji?: IMessage['emoji'];
emoji?: IMessage['emoji']; // overridden if icon_emoji is present
icon_url?: IMessage['avatar'];
avatar?: IMessage['avatar']; // overridden if icon_url is present
attachments?: IMessage['attachments'];
parseUrls?: boolean;
bot?: IMessage['bot'];
groupable?: IMessage['groupable'];
tmid?: IMessage['tmid'];
};

type DefaultValues = {
channel: string | string[];
alias: string;
avatar: string;
emoji: string;
};

export const processWebhookMessage = async function (
messageObj: Payload,
user: IUser & { username: RequiredField<IUser, 'username'> },
defaultValues: DefaultValues = { channel: '', alias: '', avatar: '', emoji: '' },
) {
const sentData = [];
const channels = [].concat(messageObj.channel || messageObj.roomId || defaultValues.channel);

const channels: Array<string> = [...new Set(ensureArray(messageObj.channel || messageObj.roomId || defaultValues.channel))];

for await (const channel of channels) {
const channelType = channel[0];
Expand Down Expand Up @@ -69,7 +101,7 @@ export const processWebhookMessage = async function (messageObj, user, defaultVa
messageObj.attachments = undefined;
}

const message = {
const message: Partial<IMessage> & { parseUrls?: boolean } = {
alias: messageObj.username || messageObj.alias || defaultValues.alias,
msg: trim(messageObj.text || messageObj.msg || ''),
attachments: messageObj.attachments || [],
Expand All @@ -91,7 +123,7 @@ export const processWebhookMessage = async function (messageObj, user, defaultVa

if (Array.isArray(message.attachments)) {
for (let i = 0; i < message.attachments.length; i++) {
const attachment = message.attachments[i];
const attachment = message.attachments[i] as MessageAttachment & { msg?: string };
if (attachment.msg) {
attachment.text = trim(attachment.msg);
delete attachment.msg;
Expand Down
24 changes: 20 additions & 4 deletions packages/rest-typings/src/v1/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,24 @@ const ChatGetDeletedMessagesSchema = {
export const isChatGetDeletedMessagesProps = ajv.compile<ChatGetDeletedMessages>(ChatGetDeletedMessagesSchema);

type ChatPostMessage =
| { roomId: string; text?: string; alias?: string; emoji?: string; avatar?: string; attachments?: MessageAttachment[] }
| { channel: string; text?: string; alias?: string; emoji?: string; avatar?: string; attachments?: MessageAttachment[] };
| { roomId: string | string[]; text?: string; alias?: string; emoji?: string; avatar?: string; attachments?: MessageAttachment[] }
| { channel: string | string[]; text?: string; alias?: string; emoji?: string; avatar?: string; attachments?: MessageAttachment[] };

const ChatPostMessageSchema = {
oneOf: [
{
type: 'object',
properties: {
roomId: {
type: 'string',
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
Expand Down Expand Up @@ -739,7 +747,15 @@ const ChatPostMessageSchema = {
type: 'object',
properties: {
channel: {
type: 'string',
oneOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string',
},
},
],
},
text: {
type: 'string',
Expand Down
Loading