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

feat(message): Support Message Custom Fields on upload API #32272

Merged
merged 9 commits into from
Apr 20, 2024
5 changes: 5 additions & 0 deletions .changeset/tough-boats-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": minor
---

Support Message Custom Fields on upload API via field `customField` and JSON value
2 changes: 2 additions & 0 deletions apps/meteor/app/file-upload/server/methods/sendFileMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export const sendFileMessage = async (
groupable: Match.Optional(Boolean),
msg: Match.Optional(String),
tmid: Match.Optional(String),
customFields: Match.Optional(String),
t: Match.Optional(String),
e2e: Match.Optional(String),
}),
Expand All @@ -199,6 +200,7 @@ export const sendFileMessage = async (
files,
attachments,
...(msgData as Partial<IMessage>),
...(msgData?.customFields && { customFields: JSON.parse(msgData.customFields) }),
msg: msgData?.msg ?? '',
groupable: msgData?.groupable ?? false,
});
Expand Down
225 changes: 73 additions & 152 deletions apps/meteor/tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { after, before, beforeEach, describe, it } from 'mocha';

import { getCredentials, api, request, credentials, message } from '../../data/api-data.js';
import { sendSimpleMessage, deleteMessage, pinMessage } from '../../data/chat.helper.js';
import { imgURL } from '../../data/interactions';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { createRoom, deleteRoom } from '../../data/rooms.helper.js';
import { password } from '../../data/user';
Expand Down Expand Up @@ -1103,43 +1104,55 @@ describe('[Chat]', function () {
});

describe('customFields', () => {
async function testMessageSending({ customFields, testCb, statusCode }) {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
customFields,
},
})
.expect('Content-Type', 'application/json')
.expect(statusCode)
.expect(testCb);

await (customFields
? request.post(api(`rooms.upload/${testChannel._id}`)).field('customFields', JSON.stringify(customFields))
: request.post(api(`rooms.upload/${testChannel._id}`))
)
.set(credentials)
.attach('file', imgURL)
.expect('Content-Type', 'application/json')
.expect(statusCode)
.expect(testCb);

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
customFields,
})
.expect('Content-Type', 'application/json')
.expect(statusCode)
.expect(testCb);
}
describe('when disabled', () => {
it('should not allow sending custom fields', async () => {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
customFields: {
field1: 'value1',
},
},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'Custom fields not enabled');
});

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
customFields: {
field1: 'value1',
},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
await testMessageSending({
customFields: {
field1: 'value1',
},
statusCode: 400,
testCb: (res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error', 'Custom fields not enabled');
});
},
});
});

it('should not allow update custom fields', async () => {
Expand Down Expand Up @@ -1189,139 +1202,47 @@ describe('[Chat]', function () {
});

it('should allow not sending custom fields', async () => {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
await testMessageSending({
statusCode: 200,
testCb: (res) => {
expect(res.body).to.have.property('success', true);
});
},
});
});

it('should not allow sending empty custom fields', async () => {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
customFields: {},
},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
});

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
customFields: {},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
await testMessageSending({
customFields: {},
statusCode: 400,
testCb: (res) => {
expect(res.body).to.have.property('success', false);
});
},
});
});

it('should not allow sending wrong custom fields', async () => {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
customFields: {
field1: 'value1',
},
},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
});

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
customFields: {
field1: 'value1',
},
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
await testMessageSending({
customFields: {
field1: 'value1',
},
statusCode: 400,
testCb: (res) => {
expect(res.body).to.have.property('success', false);
});
},
});
});

it('should allow sending correct custom fields', async () => {
await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({
message: {
rid: testChannel._id,
msg: 'Sample message',
customFields: {
priority: 'low',
},
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.message).to.have.property('customFields').to.deep.equal({ priority: 'low' });
});

await request
.post(api('chat.postMessage'))
.set(credentials)
.send({
roomId: testChannel._id,
msg: 'Sample message',
customFields: {
priority: 'low',
},
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
await testMessageSending({
customFields: {
priority: 'low',
},
statusCode: 200,
testCb: (res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.message).to.have.property('customFields').to.deep.equal({ priority: 'low' });
});
},
});
});

it('should allow not sending custom fields on update', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/rest-typings/src/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ export type RoomsEndpoints = {
groupable?: boolean;
msg?: string;
tmid?: string;
customFields?: string;
t?: IMessage['t'];
e2e?: IMessage['e2e'];
}) => { message: IMessage | null };
Expand Down
Loading