11import { Media } from '@rocket.chat/core-services' ;
22import type { IEmojiCustom } from '@rocket.chat/core-typings' ;
33import { EmojiCustom } from '@rocket.chat/models' ;
4- import { ajv , isEmojiCustomList } from '@rocket.chat/rest-typings' ;
4+ import { ajv , isEmojiCustomList , validateBadRequestErrorResponse , validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings' ;
55import { escapeRegExp } from '@rocket.chat/string-helpers' ;
66import { Meteor } from 'meteor/meteor' ;
77
@@ -30,11 +30,43 @@ function validateDateParam(paramName: string, paramValue: string | undefined): D
3030 return date ;
3131}
3232
33- API . v1 . addRoute (
34- 'emoji-custom.list' ,
35- { authRequired : true , validateParams : isEmojiCustomList } ,
36- {
37- async get ( ) {
33+ const emojiListResponseSchema = ajv . compile ( {
34+ type : 'object' ,
35+ properties : {
36+ emojis : {
37+ type : 'object' ,
38+ properties : {
39+ update : { type : 'array' , items : { type : 'object' } } ,
40+ remove : { type : 'array' , items : { type : 'object' } } ,
41+ } ,
42+ required : [ 'update' , 'remove' ] ,
43+ } ,
44+ success : { type : 'boolean' , enum : [ true ] } ,
45+ } ,
46+ required : [ 'emojis' , 'success' ] ,
47+ additionalProperties : false ,
48+ } ) ;
49+
50+ const emojiDeleteBodySchema = ajv . compile ( {
51+ type : 'object' ,
52+ properties : { emojiId : { type : 'string' } } ,
53+ required : [ 'emojiId' ] ,
54+ additionalProperties : false ,
55+ } ) ;
56+
57+ const emojiCustomCreateEndpoints = API . v1
58+ . get (
59+ 'emoji-custom.list' ,
60+ {
61+ authRequired : true ,
62+ query : isEmojiCustomList ,
63+ response : {
64+ 200 : emojiListResponseSchema ,
65+ 400 : validateBadRequestErrorResponse ,
66+ 401 : validateUnauthorizedErrorResponse ,
67+ } ,
68+ } ,
69+ async function action ( ) {
3870 const { query } = await this . parseJsonQuery ( ) ;
3971 const { updatedSince, _updatedAt, _id } = this . queryParams ;
4072
@@ -71,14 +103,28 @@ API.v1.addRoute(
71103 } ,
72104 } ) ;
73105 } ,
74- } ,
75- ) ;
76-
77- API . v1 . addRoute (
78- 'emoji-custom.all' ,
79- { authRequired : true } ,
80- {
81- async get ( ) {
106+ )
107+ . get (
108+ 'emoji-custom.all' ,
109+ {
110+ authRequired : true ,
111+ response : {
112+ 200 : ajv . compile ( {
113+ type : 'object' ,
114+ properties : {
115+ emojis : { type : 'array' , items : { type : 'object' } } ,
116+ total : { type : 'number' } ,
117+ count : { type : 'number' } ,
118+ offset : { type : 'number' } ,
119+ success : { type : 'boolean' , enum : [ true ] } ,
120+ } ,
121+ required : [ 'emojis' , 'total' , 'count' , 'offset' , 'success' ] ,
122+ additionalProperties : false ,
123+ } ) ,
124+ 401 : validateUnauthorizedErrorResponse ,
125+ } ,
126+ } ,
127+ async function action ( ) {
82128 const { offset, count } = await getPaginationItems ( this . queryParams ) ;
83129 const { sort, query } = await this . parseJsonQuery ( ) ;
84130 const { name } = this . queryParams ;
@@ -101,84 +147,92 @@ API.v1.addRoute(
101147 } ) ,
102148 ) ;
103149 } ,
104- } ,
105- ) ;
106-
107- const emojiCustomCreateEndpoints = API . v1 . post (
108- 'emoji-custom.create' ,
109- {
110- authRequired : true ,
111- response : {
112- 400 : ajv . compile ( {
113- type : 'object' ,
114- properties : {
115- success : { type : 'boolean' , enum : [ false ] } ,
116- stack : { type : 'string' } ,
117- error : { type : 'string' } ,
118- errorType : { type : 'string' } ,
119- details : { type : 'string' } ,
120- } ,
121- required : [ 'success' ] ,
122- additionalProperties : false ,
123- } ) ,
124- 200 : ajv . compile < void > ( {
125- type : 'object' ,
126- properties : {
127- success : {
128- type : 'boolean' ,
129- enum : [ true ] ,
150+ )
151+ . post (
152+ 'emoji-custom.create' ,
153+ {
154+ authRequired : true ,
155+ response : {
156+ 400 : ajv . compile ( {
157+ type : 'object' ,
158+ properties : {
159+ success : { type : 'boolean' , enum : [ false ] } ,
160+ stack : { type : 'string' } ,
161+ error : { type : 'string' } ,
162+ errorType : { type : 'string' } ,
163+ details : { type : 'string' } ,
130164 } ,
131- } ,
132- required : [ 'success' ] ,
133- additionalProperties : false ,
134- } ) ,
135- } ,
136- } ,
137- async function action ( ) {
138- const emoji = await getUploadFormData (
139- {
140- request : this . request ,
141- } ,
142- {
143- field : 'emoji' ,
144- sizeLimit : settings . get ( 'FileUpload_MaxFileSize' ) ,
165+ required : [ 'success' ] ,
166+ additionalProperties : false ,
167+ } ) ,
168+ 200 : ajv . compile < void > ( {
169+ type : 'object' ,
170+ properties : {
171+ success : {
172+ type : 'boolean' ,
173+ enum : [ true ] ,
174+ } ,
175+ } ,
176+ required : [ 'success' ] ,
177+ additionalProperties : false ,
178+ } ) ,
145179 } ,
146- ) ;
147-
148- const { fields, fileBuffer, mimetype } = emoji ;
180+ } ,
181+ async function action ( ) {
182+ const emoji = await getUploadFormData (
183+ {
184+ request : this . request ,
185+ } ,
186+ {
187+ field : 'emoji' ,
188+ sizeLimit : settings . get ( 'FileUpload_MaxFileSize' ) ,
189+ } ,
190+ ) ;
149191
150- const isUploadable = await Media . isImage ( fileBuffer ) ;
151- if ( ! isUploadable ) {
152- throw new Meteor . Error ( 'emoji-is-not-image' , "Emoji file provided cannot be uploaded since it's not an image" ) ;
153- }
192+ const { fields, fileBuffer, mimetype } = emoji ;
154193
155- const [ , extension ] = mimetype . split ( '/' ) ;
156- fields . extension = extension ;
194+ const isUploadable = await Media . isImage ( fileBuffer ) ;
195+ if ( ! isUploadable ) {
196+ throw new Meteor . Error ( 'emoji-is-not-image' , "Emoji file provided cannot be uploaded since it's not an image" ) ;
197+ }
157198
158- try {
159- const emojiData = await insertOrUpdateEmoji ( this . userId , {
160- ...fields ,
161- newFile : true ,
162- aliases : fields . aliases || '' ,
163- name : fields . name ,
164- extension : fields . extension ,
165- } ) ;
199+ const [ , extension ] = mimetype . split ( '/' ) ;
200+ fields . extension = extension ;
166201
167- await uploadEmojiCustomWithBuffer ( this . userId , fileBuffer , mimetype , emojiData ) ;
168- } catch ( err ) {
169- SystemLogger . error ( { err } ) ;
170- return API . v1 . failure ( ) ;
171- }
202+ try {
203+ const emojiData = await insertOrUpdateEmoji ( this . userId , {
204+ ...fields ,
205+ newFile : true ,
206+ aliases : fields . aliases || '' ,
207+ name : fields . name ,
208+ extension : fields . extension ,
209+ } ) ;
172210
173- return API . v1 . success ( ) ;
174- } ,
175- ) ;
211+ await uploadEmojiCustomWithBuffer ( this . userId , fileBuffer , mimetype , emojiData ) ;
212+ } catch ( err ) {
213+ SystemLogger . error ( { err } ) ;
214+ return API . v1 . failure ( ) ;
215+ }
176216
177- API . v1 . addRoute (
178- 'emoji-custom.update' ,
179- { authRequired : true } ,
180- {
181- async post ( ) {
217+ return API . v1 . success ( ) ;
218+ } ,
219+ )
220+ . post (
221+ 'emoji-custom.update' ,
222+ {
223+ authRequired : true ,
224+ response : {
225+ 200 : ajv . compile ( {
226+ type : 'object' ,
227+ properties : { success : { type : 'boolean' , enum : [ true ] } } ,
228+ required : [ 'success' ] ,
229+ additionalProperties : false ,
230+ } ) ,
231+ 400 : validateBadRequestErrorResponse ,
232+ 401 : validateUnauthorizedErrorResponse ,
233+ } ,
234+ } ,
235+ async function action ( ) {
182236 const emoji = await getUploadFormData (
183237 {
184238 request : this . request ,
@@ -229,14 +283,24 @@ API.v1.addRoute(
229283 }
230284 return API . v1 . success ( ) ;
231285 } ,
232- } ,
233- ) ;
234-
235- API . v1 . addRoute (
236- 'emoji-custom.delete' ,
237- { authRequired : true } ,
238- {
239- async post ( ) {
286+ )
287+ . post (
288+ 'emoji-custom.delete' ,
289+ {
290+ authRequired : true ,
291+ body : emojiDeleteBodySchema ,
292+ response : {
293+ 200 : ajv . compile ( {
294+ type : 'object' ,
295+ properties : { success : { type : 'boolean' , enum : [ true ] } } ,
296+ required : [ 'success' ] ,
297+ additionalProperties : false ,
298+ } ) ,
299+ 400 : validateBadRequestErrorResponse ,
300+ 401 : validateUnauthorizedErrorResponse ,
301+ } ,
302+ } ,
303+ async function action ( ) {
240304 const { emojiId } = this . bodyParams ;
241305 if ( ! emojiId ) {
242306 return API . v1 . failure ( 'The "emojiId" params is required!' ) ;
@@ -246,8 +310,7 @@ API.v1.addRoute(
246310
247311 return API . v1 . success ( ) ;
248312 } ,
249- } ,
250- ) ;
313+ ) ;
251314
252315type EmojiCustomCreateEndpoints = ExtractRoutesFromAPI < typeof emojiCustomCreateEndpoints > ;
253316
0 commit comments