From e34b7cbb873086c3cef99f283ba84e7d78eabcdb Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Mon, 29 Jun 2020 14:57:39 -0300 Subject: [PATCH] [CHORE] Add wrapper to make Meteor methods calls over REST (#2104) * [WIP] Use rest instead methodCall * [WIP] Some method calls using wrapper * [WIP] Wrap all necessary methodCalls * fix Co-authored-by: Diego Mello --- app/constants/settings.js | 3 ++ app/lib/methods/callJitsi.js | 2 +- app/lib/rocketchat.js | 77 ++++++++++++++++++++---------------- app/views/RoomView/index.js | 2 +- app/views/SetUsernameView.js | 2 +- 5 files changed, 48 insertions(+), 38 deletions(-) diff --git a/app/constants/settings.js b/app/constants/settings.js index 83a269df..4c2030c0 100644 --- a/app/constants/settings.js +++ b/app/constants/settings.js @@ -50,6 +50,9 @@ export default { Accounts_ManuallyApproveNewUsers: { type: 'valueAsBoolean' }, + API_Use_REST_For_DDP_Calls: { + type: 'valueAsBoolean' + }, Accounts_iframe_enabled: { type: 'valueAsBoolean' }, diff --git a/app/lib/methods/callJitsi.js b/app/lib/methods/callJitsi.js index 7e5d4cde..92f7bc44 100644 --- a/app/lib/methods/callJitsi.js +++ b/app/lib/methods/callJitsi.js @@ -25,7 +25,7 @@ async function callJitsi(rid, onlyAudio = false) { if (Jitsi_Enabled_TokenAuth) { try { - accessToken = await this.sdk.methodCall('jitsi:generateAccessToken', rid); + accessToken = await this.methodCallWrapper('jitsi:generateAccessToken', rid); } catch (e) { // do nothing } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index fa207372..831d7579 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -77,7 +77,7 @@ const RocketChat = { name, users, type, readOnly, broadcast }) { // RC 0.51.0 - return this.methodCall(type ? 'createPrivateGroup' : 'createChannel', name, users, readOnly, {}, { broadcast }); + return this.methodCallWrapper(type ? 'createPrivateGroup' : 'createChannel', name, users, readOnly, {}, { broadcast }); }, async getUserToken() { try { @@ -318,8 +318,9 @@ const RocketChat = { reduxStore.dispatch(shareSetUser({})); }, - updateJitsiTimeout(rid) { - return this.methodCall('jitsi:updateTimeout', rid); + updateJitsiTimeout(roomId) { + // RC 0.74.0 + return this.post('jitsi.updateTimeout', { roomId }); }, register(credentials) { @@ -327,11 +328,6 @@ const RocketChat = { return this.post('users.register', credentials, false); }, - setUsername(username) { - // RC 0.51.0 - return this.methodCall('setUsername', username); - }, - forgotPassword(email) { // RC 0.64.0 return this.post('users.forgotPassword', { email }, false); @@ -567,7 +563,7 @@ const RocketChat = { spotlight(search, usernames, type) { // RC 0.51.0 - return this.methodCall('spotlight', search, usernames, type); + return this.methodCallWrapper('spotlight', search, usernames, type); }, createDirectMessage(username) { @@ -596,7 +592,7 @@ const RocketChat = { // TODO: join code // RC 0.48.0 if (type === 'p') { - return this.methodCall('joinRoom', roomId); + return this.methodCallWrapper('joinRoom', roomId); } return this.post('channels.join', { roomId }); }, @@ -738,11 +734,22 @@ const RocketChat = { }, getRoomMembers(rid, allUsers, skip = 0, limit = 10) { // RC 0.42.0 - return this.methodCall('getUsersOfRoom', rid, allUsers, { skip, limit }); + return this.methodCallWrapper('getUsersOfRoom', rid, allUsers, { skip, limit }); }, + + async methodCallWrapper(method, ...params) { + const { API_Use_REST_For_DDP_Calls } = reduxStore.getState().settings; + if (API_Use_REST_For_DDP_Calls) { + const data = await this.post(`method.call/${ method }`, { message: JSON.stringify({ method, params }) }); + const { result } = JSON.parse(data.message); + return result; + } + return this.methodCall(method, ...params); + }, + getUserRoles() { // RC 0.27.0 - return this.methodCall('getUserRoles'); + return this.methodCallWrapper('getUserRoles'); }, getRoomCounters(roomId, t) { // RC 0.65.0 @@ -767,19 +774,19 @@ const RocketChat = { }, closeLivechat(rid, comment) { // RC 0.29.0 - return this.methodCall('livechat:closeRoom', rid, comment, { clientAction: true }); + return this.methodCallWrapper('livechat:closeRoom', rid, comment, { clientAction: true }); }, editLivechat(userData, roomData) { // RC 0.55.0 - return this.methodCall('livechat:saveInfo', userData, roomData); + return this.methodCallWrapper('livechat:saveInfo', userData, roomData); }, returnLivechat(rid) { // RC 0.72.0 - return this.methodCall('livechat:returnAsInquiry', rid); + return this.methodCallWrapper('livechat:returnAsInquiry', rid); }, forwardLivechat(transferData) { // RC 0.36.0 - return this.methodCall('livechat:transfer', transferData); + return this.methodCallWrapper('livechat:transfer', transferData); }, getPagesLivechat(rid, offset) { // RC 2.3.0 @@ -799,11 +806,11 @@ const RocketChat = { }, getRoutingConfig() { // RC 2.0.0 - return this.methodCall('livechat:getRoutingConfig'); + return this.methodCallWrapper('livechat:getRoutingConfig'); }, getTagsList() { // RC 2.0.0 - return this.methodCall('livechat:getTagsList'); + return this.methodCallWrapper('livechat:getTagsList'); }, getAgentDepartments(uid) { // RC 2.4.0 @@ -815,7 +822,7 @@ const RocketChat = { }, changeLivechatStatus() { // RC 0.26.0 - return this.methodCall('livechat:changeLivechatStatus'); + return this.methodCallWrapper('livechat:changeLivechatStatus'); }, getUidDirectMessage(room) { @@ -843,10 +850,10 @@ const RocketChat = { toggleBlockUser(rid, blocked, block) { if (block) { // RC 0.49.0 - return this.methodCall('blockUser', { rid, blocked }); + return this.methodCallWrapper('blockUser', { rid, blocked }); } // RC 0.49.0 - return this.methodCall('unblockUser', { rid, blocked }); + return this.methodCallWrapper('unblockUser', { rid, blocked }); }, leaveRoom(roomId, t) { // RC 0.48.0 @@ -859,10 +866,10 @@ const RocketChat = { toggleMuteUserInRoom(rid, username, mute) { if (mute) { // RC 0.51.0 - return this.methodCall('muteUserInRoom', { rid, username }); + return this.methodCallWrapper('muteUserInRoom', { rid, username }); } // RC 0.51.0 - return this.methodCall('unmuteUserInRoom', { rid, username }); + return this.methodCallWrapper('unmuteUserInRoom', { rid, username }); }, toggleArchiveRoom(roomId, t, archive) { if (archive) { @@ -877,7 +884,7 @@ const RocketChat = { }, saveRoomSettings(rid, params) { // RC 0.55.0 - return this.methodCall('saveRoomSettings', rid, params); + return this.methodCallWrapper('saveRoomSettings', rid, params); }, post(...args) { return new Promise(async(resolve, reject) => { @@ -930,9 +937,9 @@ const RocketChat = { // RC 0.62.2 return this.post('users.updateOwnBasicInfo', { data, customFields }); }, - saveUserPreferences(params) { - // RC 0.51.0 - return this.methodCall('saveUserPreferences', params); + saveUserPreferences(data) { + // RC 0.62.0 + return this.post('users.setPreferences', { data }); }, saveNotificationSettings(roomId, notifications) { // RC 0.63.0 @@ -942,11 +949,11 @@ const RocketChat = { let { users } = reduxStore.getState().selectedUsers; users = users.map(u => u.name); // RC 0.51.0 - return this.methodCall('addUsersToRoom', { rid, users }); + return this.methodCallWrapper('addUsersToRoom', { rid, users }); }, getSingleMessage(msgId) { - // RC 0.57.0 - return this.methodCall('getSingleMessage', msgId); + // RC 0.47.0 + return this.sdk.get('chat.getMessage', { msgId }); }, async hasPermission(permissions, rid) { const db = database.active; @@ -991,7 +998,7 @@ const RocketChat = { }, getAvatarSuggestion() { // RC 0.51.0 - return this.methodCall('getAvatarSuggestion'); + return this.methodCallWrapper('getAvatarSuggestion'); }, resetAvatar(userId) { // RC 0.55.0 @@ -999,7 +1006,7 @@ const RocketChat = { }, setAvatarFromService({ data, contentType = '', service = null }) { // RC 0.51.0 - return this.methodCall('setAvatarFromService', data, contentType, service); + return this.methodCallWrapper('setAvatarFromService', data, contentType, service); }, async getAllowCrashReport() { const allowCrashReport = await AsyncStorage.getItem(CRASH_REPORT_KEY); @@ -1211,13 +1218,13 @@ const RocketChat = { saveAutoTranslate({ rid, field, value, options }) { - return this.methodCall('autoTranslate.saveSettings', rid, field, value, options); + return this.methodCallWrapper('autoTranslate.saveSettings', rid, field, value, options); }, getSupportedLanguagesAutoTranslate() { - return this.methodCall('autoTranslate.getSupportedLanguages', 'en'); + return this.methodCallWrapper('autoTranslate.getSupportedLanguages', 'en'); }, translateMessage(message, targetLanguage) { - return this.methodCall('autoTranslate.translateMessage', message, targetLanguage); + return this.methodCallWrapper('autoTranslate.translateMessage', message, targetLanguage); }, getRoomTitle(room) { const { UI_Use_Real_Name: useRealName, UI_Allow_room_names_with_special_chars: allowSpecialChars } = reduxStore.getState().settings; diff --git a/app/views/RoomView/index.js b/app/views/RoomView/index.js index 52607449..62553d36 100644 --- a/app/views/RoomView/index.js +++ b/app/views/RoomView/index.js @@ -698,7 +698,7 @@ class RoomView extends React.Component { }); }); } else { - const thread = await RocketChat.getSingleMessage(tmid); + const { message: thread } = await RocketChat.getSingleMessage(tmid); await db.action(async() => { await db.batch( threadCollection.prepareCreate((t) => { diff --git a/app/views/SetUsernameView.js b/app/views/SetUsernameView.js index a6b7bc4c..2025bc91 100644 --- a/app/views/SetUsernameView.js +++ b/app/views/SetUsernameView.js @@ -88,7 +88,7 @@ class SetUsernameView extends React.Component { this.setState({ saving: true }); try { - await RocketChat.setUsername(username); + await RocketChat.saveUserProfile({ username }); await loginRequest({ resume: token }); } catch (e) { showErrorAlert(e.message, I18n.t('Oops'));