Skip to content

Commit

Permalink
Remove dependency of RC namespace in rc-api (#13278)
Browse files Browse the repository at this point in the history
* Move RestAPI client to rc-api package

* Remove dependency of RC namespace in rc-api/helpers and api.js

* Remove dependency of RC namespace on half of api files

* Partial Remove dependency of RC namespace in rc-api

* import API where it was being used by the RC namespace

* Move processWebhookMessage function to rc-lib package

* Remove API from RC namespace and import missing function that was being used with namespace

* Remove namespace in the new livechat endpoint
  • Loading branch information
MarcosSpessatto authored and rodrigok committed Feb 6, 2019
1 parent db160bf commit c977598
Show file tree
Hide file tree
Showing 46 changed files with 369 additions and 332 deletions.
1 change: 0 additions & 1 deletion packages/rocketchat-api/client/lib/RestApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,3 @@ export const API = {
},
},
};
RocketChat.API = API;
1 change: 0 additions & 1 deletion packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Package.onUse(function(api) {
'rocketchat:utils',
'rocketchat:metrics',
'rocketchat:authorization',
'rocketchat:integrations',
'rocketchat:file-upload',
]);

Expand Down
3 changes: 0 additions & 3 deletions packages/rocketchat-api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Meteor } from 'meteor/meteor';
import { DDPCommon } from 'meteor/ddp-common';
import { DDP } from 'meteor/ddp';
import { Accounts } from 'meteor/accounts-base';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Restivus } from 'meteor/nimble:restivus';
import { Logger } from 'meteor/rocketchat:logger';
import { settings } from 'meteor/rocketchat:settings';
Expand Down Expand Up @@ -448,8 +447,6 @@ API = {
ApiClass: APIClass,
};

RocketChat.API = API;

const defaultOptionsEndpoint = function _defaultOptionsEndpoint() {
if (this.request.method === 'OPTIONS' && this.request.headers['access-control-request-method']) {
if (settings.get('API_Enable_CORS') === true) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { processWebhookMessage } from 'meteor/rocketchat:integrations';
import { Messages } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { composeMessageObjectWithUser } from 'meteor/rocketchat:utils';
import { processWebhookMessage } from 'meteor/rocketchat:lib';
import { API } from '../api';

API.v1.addRoute('chat.delete', { authRequired: true }, {
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-api/server/v1/im.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import { getRoomByNameOrIdWithOptionToJoin } from 'meteor/rocketchat:lib';
import { Subscriptions, Uploads, Users, Messages, Rooms } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { composeMessageObjectWithUser } from 'meteor/rocketchat:utils';
Expand All @@ -11,7 +11,7 @@ function findDirectMessageRoom(params, user) {
throw new Meteor.Error('error-room-param-not-provided', 'Body param "roomId" or "username" is required');
}

const room = RocketChat.getRoomByNameOrIdWithOptionToJoin({
const room = getRoomByNameOrIdWithOptionToJoin({
currentUserId: user._id,
nameOrId: params.username || params.roomId,
type: 'd',
Expand Down
26 changes: 17 additions & 9 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { Users, Subscriptions } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { settings } from 'meteor/rocketchat:settings';
import { getURL } from 'meteor/rocketchat:utils';
import {
validateCustomFields,
saveUser,
saveCustomFieldsWithoutValidation,
checkUsernameAvailability,
setUserAvatar,
saveCustomFields,
} from 'meteor/rocketchat:lib';
import { API } from '../api';
import _ from 'underscore';
import Busboy from 'busboy';
Expand All @@ -31,13 +39,13 @@ API.v1.addRoute('users.create', { authRequired: true }, {
}

if (this.bodyParams.customFields) {
RocketChat.validateCustomFields(this.bodyParams.customFields);
validateCustomFields(this.bodyParams.customFields);
}

const newUserId = RocketChat.saveUser(this.userId, this.bodyParams);
const newUserId = saveUser(this.userId, this.bodyParams);

if (this.bodyParams.customFields) {
RocketChat.saveCustomFieldsWithoutValidation(newUserId, this.bodyParams.customFields);
saveCustomFieldsWithoutValidation(newUserId, this.bodyParams.customFields);
}


Expand Down Expand Up @@ -191,7 +199,7 @@ API.v1.addRoute('users.register', { authRequired: false }, {
username: String,
}));

if (!RocketChat.checkUsernameAvailability(this.bodyParams.username)) {
if (!checkUsernameAvailability(this.bodyParams.username)) {
return API.v1.failure('Username is already in use');
}

Expand Down Expand Up @@ -246,7 +254,7 @@ API.v1.addRoute('users.setAvatar', { authRequired: true }, {

Meteor.runAsUser(user._id, () => {
if (this.bodyParams.avatarUrl) {
RocketChat.setUserAvatar(user, this.bodyParams.avatarUrl, '', 'url');
setUserAvatar(user, this.bodyParams.avatarUrl, '', 'url');
} else {
const busboy = new Busboy({ headers: this.request.headers });
const fields = {};
Expand Down Expand Up @@ -281,7 +289,7 @@ API.v1.addRoute('users.setAvatar', { authRequired: true }, {
return callback(new Meteor.Error('error-not-allowed', 'Not allowed'));
}
}
RocketChat.setUserAvatar(user, Buffer.concat(imageData), mimetype, 'rest');
setUserAvatar(user, Buffer.concat(imageData), mimetype, 'rest');
callback();
}));
}));
Expand Down Expand Up @@ -318,10 +326,10 @@ API.v1.addRoute('users.update', { authRequired: true }, {

const userData = _.extend({ _id: this.bodyParams.userId }, this.bodyParams.data);

Meteor.runAsUser(this.userId, () => RocketChat.saveUser(this.userId, userData));
Meteor.runAsUser(this.userId, () => saveUser(this.userId, userData));

if (this.bodyParams.data.customFields) {
RocketChat.saveCustomFields(this.bodyParams.userId, this.bodyParams.data.customFields);
saveCustomFields(this.bodyParams.userId, this.bodyParams.data.customFields);
}

if (typeof this.bodyParams.data.active !== 'undefined') {
Expand Down Expand Up @@ -440,7 +448,7 @@ API.v1.addRoute('users.setPreferences', { authRequired: true }, {
userData.language = language;
}

Meteor.runAsUser(this.userId, () => RocketChat.saveUser(this.userId, userData));
Meteor.runAsUser(this.userId, () => saveUser(this.userId, userData));
const user = Users.findOneById(userId, {
fields: {
'settings.preferences': 1,
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-apps/client/admin/appInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { ReactiveVar } from 'meteor/reactive-var';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Template } from 'meteor/templating';
import { API } from 'meteor/rocketchat:api';

Template.appInstall.helpers({
appFile() {
Expand Down Expand Up @@ -72,9 +73,9 @@ Template.appInstall.events({
let result;

if (isUpdating) {
result = await RocketChat.API.post(`apps/${ t.isUpdatingId.get() }`, { url });
result = await API.post(`apps/${ t.isUpdatingId.get() }`, { url });
} else {
result = await RocketChat.API.post('apps', { url });
result = await API.post('apps', { url });
}

if (result.compilerErrors.length !== 0 || result.app.status === 'compiler_error') {
Expand Down Expand Up @@ -115,9 +116,9 @@ Template.appInstall.events({
let result;

if (isUpdating) {
result = await RocketChat.API.upload(`apps/${ t.isUpdatingId.get() }`, data);
result = await API.upload(`apps/${ t.isUpdatingId.get() }`, data);
} else {
result = await RocketChat.API.upload('apps', data);
result = await API.upload('apps', data);
}

console.log('install result', result);
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-apps/client/admin/appLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReactiveVar } from 'meteor/reactive-var';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/tap:i18n';
import { API } from 'meteor/rocketchat:api';
import moment from 'moment';
import hljs from 'highlight.js';

Expand All @@ -17,8 +18,8 @@ Template.appLogs.onCreated(function() {
const id = this.id.get();

Promise.all([
RocketChat.API.get(`apps/${ id }`),
RocketChat.API.get(`apps/${ id }/logs`),
API.get(`apps/${ id }`),
API.get(`apps/${ id }/logs`),
]).then((results) => {

instance.app.set(results[0].app);
Expand Down
13 changes: 7 additions & 6 deletions packages/rocketchat-apps/client/admin/appManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/tap:i18n';
import { TAPi18next } from 'meteor/tap:i18n';
import { isEmail } from 'meteor/rocketchat:utils';
import { API } from 'meteor/rocketchat:api';
import _ from 'underscore';
import s from 'underscore.string';
import toastr from 'toastr';
Expand All @@ -21,7 +22,7 @@ function getApps(instance) {

return Promise.all([
fetch(`${ HOST }/v1/apps/${ id }?version=${ RocketChat.Info.marketplaceApiVersion }`).then((data) => data.json()),
RocketChat.API.get('apps/').then((result) => result.apps.filter((app) => app.id === id)),
API.get('apps/').then((result) => result.apps.filter((app) => app.id === id)),
]).then(([remoteApps, [localApp]]) => {
remoteApps = remoteApps.sort((a, b) => {
if (semver.gt(a.version, b.version)) {
Expand Down Expand Up @@ -114,7 +115,7 @@ Template.appManage.onCreated(function() {
return;
}

RocketChat.API.get(`apps/${ id }/settings`).then((result) => {
API.get(`apps/${ id }/settings`).then((result) => {
_morphSettings(result.settings);
});
};
Expand Down Expand Up @@ -263,7 +264,7 @@ async function setActivate(actiavate, e, t) {
const status = actiavate ? 'manually_enabled' : 'manually_disabled';

try {
const result = await RocketChat.API.post(`apps/${ t.id.get() }/status`, { status });
const result = await API.post(`apps/${ t.id.get() }/status`, { status });
const info = t.app.get();
info.status = result.status;
t.app.set(info);
Expand Down Expand Up @@ -301,7 +302,7 @@ Template.appManage.events({
'click .js-uninstall': async(e, t) => {
t.ready.set(false);
try {
await RocketChat.API.delete(`apps/${ t.id.get() }`);
await API.delete(`apps/${ t.id.get() }`);
FlowRouter.go('/admin/apps');
} catch (err) {
console.warn('Error:', err);
Expand All @@ -321,7 +322,7 @@ Template.appManage.events({

const api = app.newVersion ? `apps/${ t.id.get() }` : 'apps/';

RocketChat.API.post(api, { url }).then(() => {
API.post(api, { url }).then(() => {
getApps(t).then(() => {
el.prop('disabled', false);
el.removeClass('loading');
Expand Down Expand Up @@ -371,7 +372,7 @@ Template.appManage.events({
if (toSave.length === 0) {
throw 'Nothing to save..';
}
const result = await RocketChat.API.post(`apps/${ t.id.get() }/settings`, undefined, { settings: toSave });
const result = await API.post(`apps/${ t.id.get() }/settings`, undefined, { settings: toSave });
console.log('Updating results:', result);
result.updated.forEach((setting) => {
settings[setting.id].value = settings[setting.id].oldValue = setting.value;
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-apps/client/admin/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FlowRouter } from 'meteor/kadira:flow-router';
import { Template } from 'meteor/templating';
import { t } from 'meteor/rocketchat:utils';
import { AppEvents } from '../communication';
import { API } from 'meteor/rocketchat:api';

const ENABLED_STATUS = ['auto_enabled', 'manually_enabled'];
const HOST = 'https://marketplace.rocket.chat';
Expand Down Expand Up @@ -48,7 +49,7 @@ const getApps = (instance) => {

const getInstalledApps = (instance) => {

RocketChat.API.get('apps').then((data) => {
API.get('apps').then((data) => {
const apps = data.apps.map((app) => ({ latest: app }));

instance.installedApps.set(apps);
Expand Down Expand Up @@ -253,7 +254,7 @@ Template.apps.events({

const url = `${ HOST }/v1/apps/${ this.latest.id }/download/${ this.latest.version }`;

RocketChat.API.post('apps/', { url }).then(() => {
API.post('apps/', { url }).then(() => {
getInstalledApps(template);
}).catch((e) => {
toastr.error((e.xhr.responseJSON && e.xhr.responseJSON.error) || e.message);
Expand Down
7 changes: 4 additions & 3 deletions packages/rocketchat-apps/client/communication/websockets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { API } from 'meteor/rocketchat:api';

export const AppEvents = Object.freeze({
APP_ADDED: 'app/added',
Expand Down Expand Up @@ -49,7 +50,7 @@ export class AppWebsocketReceiver {
}

onAppAdded(appId) {
RocketChat.API.get(`apps/${ appId }/languages`).then((result) => {
API.get(`apps/${ appId }/languages`).then((result) => {
this.orch.parseAndLoadLanguages(result.languages, appId);
});

Expand All @@ -73,7 +74,7 @@ export class AppWebsocketReceiver {
}

onCommandAdded(command) {
RocketChat.API.v1.get('commands.get', { command }).then((result) => {
API.v1.get('commands.get', { command }).then((result) => {
RocketChat.slashCommands.commands[command] = result.command;
});
}
Expand All @@ -83,7 +84,7 @@ export class AppWebsocketReceiver {
}

onCommandUpdated(command) {
RocketChat.API.v1.get('commands.get', { command }).then((result) => {
API.v1.get('commands.get', { command }).then((result) => {
RocketChat.slashCommands.commands[command] = result.command;
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-apps/client/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Utilities } from '../lib/misc/Utilities';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { TAPi18next } from 'meteor/tap:i18n';
import { API } from 'meteor/rocketchat:api';

class AppClientOrchestrator {
constructor() {
Expand Down Expand Up @@ -71,7 +72,7 @@ class AppClientOrchestrator {
}

_loadLanguages() {
return RocketChat.API.get('apps/languages').then((info) => {
return API.get('apps/languages').then((info) => {
info.apps.forEach((rlInfo) => this.parseAndLoadLanguages(rlInfo.languages, rlInfo.id));
});
}
Expand All @@ -92,7 +93,7 @@ class AppClientOrchestrator {
}

async getAppApis(appId) {
const result = await RocketChat.API.get(`apps/${ appId }/apis`);
const result = await API.get(`apps/${ appId }/apis`);
return result.apis;
}
}
Expand Down
Loading

0 comments on commit c977598

Please sign in to comment.