Skip to content

Commit

Permalink
chore!: remove query field on custom emojis listing (#33650)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogarim authored Oct 19, 2024
1 parent f33c07e commit f4365b7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changeset/perfect-wolves-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/rest-typings': major
'@rocket.chat/meteor': major
---

Changes custom emoji listing endpoint by moving query params from the 'query' attribute to standard query parameters.
1 change: 1 addition & 0 deletions apps/meteor/app/api/server/helpers/parseJsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export async function parseJsonQuery(api: PartialThis): Promise<{
'/api/v1/custom-sounds.list',
'/api/v1/channels.list',
'/api/v1/channels.online',
'/api/v1/emoji-custom.list',
].includes(route);

const isUnsafeQueryParamsAllowed = process.env.ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS?.toUpperCase() === 'TRUE';
Expand Down
42 changes: 33 additions & 9 deletions apps/meteor/app/api/server/v1/emoji-custom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Media } from '@rocket.chat/core-services';
import { EmojiCustom } from '@rocket.chat/models';
import { isEmojiCustomList } from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';

import { SystemLogger } from '../../../../server/lib/logger/system';
Expand All @@ -11,22 +12,41 @@ import { getPaginationItems } from '../helpers/getPaginationItems';
import { findEmojisCustom } from '../lib/emoji-custom';
import { getUploadFormData } from '../lib/getUploadFormData';

function validateDateParam(paramName: string, paramValue: string | undefined): Date | undefined {
if (!paramValue) {
return undefined;
}

const date = new Date(paramValue);
if (isNaN(date.getTime())) {
throw new Meteor.Error('error-roomId-param-invalid', `The "${paramName}" query parameter must be a valid date.`);
}

return date;
}

API.v1.addRoute(
'emoji-custom.list',
{ authRequired: true },
{ authRequired: true, validateParams: isEmojiCustomList },
{
async get() {
const { query } = await this.parseJsonQuery();
const { updatedSince } = this.queryParams;
if (updatedSince) {
const updatedSinceDate = new Date(updatedSince);
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-roomId-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
}
const { updatedSince, _updatedAt, _id } = this.queryParams;

const updatedSinceDate = validateDateParam('updatedSince', updatedSince);
const _updatedAtDate = validateDateParam('_updatedAt', _updatedAt);

if (updatedSinceDate) {
const [update, remove] = await Promise.all([
EmojiCustom.find({ ...query, _updatedAt: { $gt: updatedSinceDate } }).toArray(),
EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
_updatedAt: { $gt: updatedSinceDate },
}).toArray(),
EmojiCustom.trashFindDeletedAfter(updatedSinceDate).toArray(),
]);

return API.v1.success({
emojis: {
update,
Expand All @@ -37,7 +57,11 @@ API.v1.addRoute(

return API.v1.success({
emojis: {
update: await EmojiCustom.find(query).toArray(),
update: await EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
}).toArray(),
remove: [],
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type EditCustomEmojiWithDataProps = {

const EditCustomEmojiWithData = ({ _id, onChange, close, ...props }: EditCustomEmojiWithDataProps) => {
const t = useTranslation();
const query = useMemo(() => ({ query: JSON.stringify({ _id }) }), [_id]);
const query = useMemo(() => ({ _id }), [_id]);

const getEmojis = useEndpoint('GET', '/v1/emoji-custom.list');

Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/tests/end-to-end/api/emoji-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('[EmojiCustom]', () => {
it('should return emojis when use "query" query parameter', (done) => {
void request
.get(api('emoji-custom.list'))
.query({ query: `{ "_updatedAt": { "$gt": { "$date": "${new Date().toISOString()}" } } }` })
.query({ _updatedAt: new Date().toISOString() })
.set(credentials)
.expect(200)
.expect((res) => {
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('[EmojiCustom]', () => {
it('should return emojis when use both, "updateSince" and "query" query parameter', (done) => {
void request
.get(api('emoji-custom.list'))
.query({ query: `{"_updatedAt": {"$gt": { "$date": "${new Date().toISOString()}" } }}`, updatedSince: new Date().toISOString() })
.query({ _updatedAt: new Date().toISOString(), updatedSince: new Date().toISOString() })
.set(credentials)
.expect(200)
.expect((res) => {
Expand Down
13 changes: 8 additions & 5 deletions packages/rest-typings/src/v1/emojiCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const emojiCustomDeletePropsSchema = {

export const isEmojiCustomDelete = ajv.compile<emojiCustomDeleteProps>(emojiCustomDeletePropsSchema);

type emojiCustomList = {
query: string;
updatedSince?: string;
};
type emojiCustomList = { query?: string; updatedSince?: string; _updatedAt?: string; _id?: string };

const emojiCustomListSchema = {
type: 'object',
Expand All @@ -40,8 +37,14 @@ const emojiCustomListSchema = {
type: 'string',
nullable: true,
},
_updatedAt: {
type: 'string',
},
_id: {
type: 'string',
},
},
required: ['query'],
required: [],
additionalProperties: false,
};

Expand Down

0 comments on commit f4365b7

Please sign in to comment.