chore!: remove query field on custom emojis listing (#33650)

pull/33665/head
Ricardo Garim 2 years ago committed by GitHub
parent f33c07ebb8
commit f4365b7dd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .changeset/perfect-wolves-impress.md
  2. 1
      apps/meteor/app/api/server/helpers/parseJsonQuery.ts
  3. 42
      apps/meteor/app/api/server/v1/emoji-custom.ts
  4. 2
      apps/meteor/client/views/admin/customEmoji/EditCustomEmojiWithData.tsx
  5. 4
      apps/meteor/tests/end-to-end/api/emoji-custom.ts
  6. 13
      packages/rest-typings/src/v1/emojiCustom.ts

@ -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.

@ -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';

@ -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';
@ -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,
@ -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: [],
},
});

@ -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');

@ -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) => {
@ -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) => {

@ -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',
@ -40,8 +37,14 @@ const emojiCustomListSchema = {
type: 'string',
nullable: true,
},
_updatedAt: {
type: 'string',
},
_id: {
type: 'string',
},
},
required: ['query'],
required: [],
additionalProperties: false,
};

Loading…
Cancel
Save