@ -17,39 +17,48 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, {
return RocketChat . API . v1 . failure ( 'The room id provided does not match where the message is from.' ) ;
}
if ( this . bodyParams . asUser && msg . u . _id !== this . userId && ! RocketChat . authz . hasPermission ( Meteor . userId ( ) , 'force-delete-message' , msg . rid ) ) {
return RocketChat . API . v1 . failure ( 'Unauthorized. You must have the permission "force-delete-message" to delete other\'s message as them.' ) ;
}
Meteor . runAsUser ( this . bodyParams . asUser ? msg . u . _id : this . userId , ( ) => {
Meteor . call ( 'deleteMessage' , { _id : msg . _id } ) ;
} ) ;
return RocketChat . API . v1 . success ( {
_id : msg . _id ,
ts : Date . now ( )
ts : Date . now ( ) ,
message : msg
} ) ;
}
} ) ;
RocketChat . API . v1 . addRoute ( 'chat.syncMessages' , { authRequired : true } , {
get ( ) {
const { rid } = this . queryParams ;
let lastUpdate = this . queryParams ;
lastUpdate = lastUpdate ? new Date ( lastUpdate ) : lastUpdate ;
if ( ! rid ) {
return RocketChat . API . v1 . failure ( 'The "rid" query parameter must be provided.' ) ;
const { roomId , lastUpdate } = this . queryParams ;
if ( ! roomId ) {
throw new Meteor . Error ( 'error-roomId-param-not-provided' , 'The required "roomId" query param is missing.' ) ;
}
if ( ! lastUpdate ) {
return RocketChat . API . v1 . failure ( 'The "lastUpdate" query parameter must be provided.' ) ;
throw new Meteor . Error ( 'error-lastUpdate-param-not-provided' , 'The required "lastUpdate" query param is missing.' ) ;
} else if ( isNaN ( Date . parse ( lastUpdate ) ) ) {
throw new Meteor . Error ( 'error-roomId-param-invalid' , 'The "lastUpdate" query parameter must be a valid date.' ) ;
}
let result ;
Meteor . runAsUser ( this . userId , ( ) => {
result = Meteor . call ( 'messages/get' , ri d , { lastUpdate } ) ;
result = Meteor . call ( 'messages/get' , roomI d , { lastUpdate : new Date ( lastUpdate ) } ) ;
} ) ;
if ( ! result ) {
return RocketChat . API . v1 . failure ( ) ;
}
return RocketChat . API . v1 . success ( { result } ) ;
return RocketChat . API . v1 . success ( {
result
} ) ;
}
} ) ;
@ -59,7 +68,6 @@ RocketChat.API.v1.addRoute('chat.getMessage', { authRequired: true }, {
return RocketChat . API . v1 . failure ( 'The "msgId" query parameter must be provided.' ) ;
}
let msg ;
Meteor . runAsUser ( this . userId , ( ) => {
msg = Meteor . call ( 'getSingleMessage' , this . queryParams . msgId ) ;
@ -78,7 +86,7 @@ RocketChat.API.v1.addRoute('chat.getMessage', { authRequired: true }, {
RocketChat . API . v1 . addRoute ( 'chat.pinMessage' , { authRequired : true } , {
post ( ) {
if ( ! this . bodyParams . messageId || ! this . bodyParams . messageId . trim ( ) ) {
throw new Meteor . Error ( 'error-messageid-param-not-provided' , 'The required "messageId" param is required .' ) ;
throw new Meteor . Error ( 'error-messageid-param-not-provided' , 'The required "messageId" param is missing .' ) ;
}
const msg = RocketChat . models . Messages . findOneById ( this . bodyParams . messageId ) ;
@ -112,6 +120,49 @@ RocketChat.API.v1.addRoute('chat.postMessage', { authRequired: true }, {
}
} ) ;
RocketChat . API . v1 . addRoute ( 'chat.search' , { authRequired : true } , {
get ( ) {
const { roomId , searchText , limit } = this . queryParams ;
if ( ! roomId ) {
throw new Meteor . Error ( 'error-roomId-param-not-provided' , 'The required "roomId" query param is missing.' ) ;
}
if ( ! searchText ) {
throw new Meteor . Error ( 'error-searchText-param-not-provided' , 'The required "searchText" query param is missing.' ) ;
}
if ( limit && ( typeof limit !== 'number' || isNaN ( limit ) || limit <= 0 ) ) {
throw new Meteor . Error ( 'error-limit-param-invalid' , 'The "limit" query parameter must be a valid number and be greater than 0.' ) ;
}
let result ;
Meteor . runAsUser ( this . userId , ( ) => result = Meteor . call ( 'messageSearch' , searchText , roomId , limit ) ) ;
return RocketChat . API . v1 . success ( {
messages : result . messages
} ) ;
}
} ) ;
// The difference between `chat.postMessage` and `chat.sendMessage` is that `chat.sendMessage` allows
// for passing a value for `_id` and the other one doesn't. Also, `chat.sendMessage` only sends it to
// one channel whereas the other one allows for sending to more than one channel at a time.
RocketChat . API . v1 . addRoute ( 'chat.sendMessage' , { authRequired : true } , {
post ( ) {
if ( ! this . bodyParams . message ) {
throw new Meteor . Error ( 'error-invalid-params' , 'The "message" parameter must be provided.' ) ;
}
let message ;
Meteor . runAsUser ( this . userId , ( ) => message = Meteor . call ( 'sendMessage' , this . bodyParams . message ) ) ;
return RocketChat . API . v1 . success ( {
message
} ) ;
}
} ) ;
RocketChat . API . v1 . addRoute ( 'chat.starMessage' , { authRequired : true } , {
post ( ) {
if ( ! this . bodyParams . messageId || ! this . bodyParams . messageId . trim ( ) ) {