Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Remove dependency of RC namespace in rc-api #13277

Merged
merged 10 commits into from
Feb 6, 2019
2 changes: 1 addition & 1 deletion packages/rocketchat-api/server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './settings';
import './api';
export { API } from './api';
import './helpers/composeRoomWithLastMessage';
import './helpers/deprecationWarning';
import './helpers/getLoggedInUser';
Expand Down
48 changes: 26 additions & 22 deletions packages/rocketchat-api/server/v1/misc.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { TAPi18n } from 'meteor/tap:i18n';
import { RocketChat } from 'meteor/rocketchat:lib';
import { hasRole } from 'meteor/rocketchat:authorization';
import { Info } from 'meteor/rocketchat:utils';
import { Users } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import { API } from '../api';

RocketChat.API.v1.addRoute('info', { authRequired: false }, {
API.v1.addRoute('info', { authRequired: false }, {
get() {
const user = this.getLoggedInUser();

if (user && RocketChat.authz.hasRole(user._id, 'admin')) {
return RocketChat.API.v1.success({
info: RocketChat.Info,
if (user && hasRole(user._id, 'admin')) {
return API.v1.success({
info: Info,
});
}

return RocketChat.API.v1.success({
return API.v1.success({
info: {
version: RocketChat.Info.version,
version: Info.version,
},
});
},
});

RocketChat.API.v1.addRoute('me', { authRequired: true }, {
API.v1.addRoute('me', { authRequired: true }, {
get() {
return RocketChat.API.v1.success(this.getUserInfo(RocketChat.models.Users.findOneById(this.userId)));
return API.v1.success(this.getUserInfo(Users.findOneById(this.userId)));
},
});

let onlineCache = 0;
let onlineCacheDate = 0;
const cacheInvalid = 60000; // 1 minute
RocketChat.API.v1.addRoute('shield.svg', { authRequired: false }, {
API.v1.addRoute('shield.svg', { authRequired: false }, {
get() {
const { type, channel, name, icon } = this.queryParams;
if (!RocketChat.settings.get('API_Enable_Shields')) {
if (!settings.get('API_Enable_Shields')) {
throw new Meteor.Error('error-endpoint-disabled', 'This endpoint is disabled', { route: '/api/v1/shield.svg' });
}

const types = RocketChat.settings.get('API_Shield_Types');
const types = settings.get('API_Shield_Types');
if (type && (types !== '*' && !types.split(',').map((t) => t.trim()).includes(type))) {
throw new Meteor.Error('error-shield-disabled', 'This shield type is disabled', { route: '/api/v1/shield.svg' });
}

const hideIcon = icon === 'false';
if (hideIcon && (!name || !name.trim())) {
return RocketChat.API.v1.failure('Name cannot be empty when icon is hidden');
return API.v1.failure('Name cannot be empty when icon is hidden');
}

let text;
let backgroundColor = '#4c1';
switch (type) {
case 'online':
if (Date.now() - onlineCacheDate > cacheInvalid) {
onlineCache = RocketChat.models.Users.findUsersNotOffline().count();
onlineCache = Users.findUsersNotOffline().count();
onlineCacheDate = Date.now();
}

text = `${ onlineCache } ${ TAPi18n.__('Online') }`;
break;
case 'channel':
if (!channel) {
return RocketChat.API.v1.failure('Shield channel is required for type "channel"');
return API.v1.failure('Shield channel is required for type "channel"');
}

text = `#${ channel }`;
Expand All @@ -69,7 +73,7 @@ RocketChat.API.v1.addRoute('shield.svg', { authRequired: false }, {
const user = this.getUserFromParams();

// Respect the server's choice for using their real names or not
if (user.name && RocketChat.settings.get('UI_Use_Real_Name')) {
if (user.name && settings.get('UI_Use_Real_Name')) {
text = `${ user.name }`;
} else {
text = `@${ user.username }`;
Expand Down Expand Up @@ -127,7 +131,7 @@ RocketChat.API.v1.addRoute('shield.svg', { authRequired: false }, {
},
});

RocketChat.API.v1.addRoute('spotlight', { authRequired: true }, {
API.v1.addRoute('spotlight', { authRequired: true }, {
get() {
check(this.queryParams, {
query: String,
Expand All @@ -139,18 +143,18 @@ RocketChat.API.v1.addRoute('spotlight', { authRequired: true }, {
Meteor.call('spotlight', query)
);

return RocketChat.API.v1.success(result);
return API.v1.success(result);
},
});

RocketChat.API.v1.addRoute('directory', { authRequired: true }, {
API.v1.addRoute('directory', { authRequired: true }, {
get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

const { text, type } = query;
if (sort && Object.keys(sort).length > 1) {
return RocketChat.API.v1.failure('This method support only one "sort" parameter');
return API.v1.failure('This method support only one "sort" parameter');
}
const sortBy = sort ? Object.keys(sort)[0] : undefined;
const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc';
Expand All @@ -165,9 +169,9 @@ RocketChat.API.v1.addRoute('directory', { authRequired: true }, {
}));

if (!result) {
return RocketChat.API.v1.failure('Please verify the parameters');
return API.v1.failure('Please verify the parameters');
}
return RocketChat.API.v1.success({
return API.v1.success({
result: result.results,
count: result.results.length,
offset,
Expand Down
34 changes: 18 additions & 16 deletions packages/rocketchat-api/server/v1/permissions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { RocketChat } from 'meteor/rocketchat:lib';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { Permissions, Roles } from 'meteor/rocketchat:models';
import { API } from '../api';

/**
This API returns all permissions that exists
Expand All @@ -9,24 +11,24 @@ import { RocketChat } from 'meteor/rocketchat:lib';
Method: GET
Route: api/v1/permissions
*/
RocketChat.API.v1.addRoute('permissions', { authRequired: true }, {
API.v1.addRoute('permissions', { authRequired: true }, {
get() {
const warningMessage = 'The endpoint "permissions" is deprecated and will be removed after version v0.69';
console.warn(warningMessage);

const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get'));

return RocketChat.API.v1.success(result);
return API.v1.success(result);
},
});

// DEPRECATED
// TODO: Remove this after three versions have been released. That means at 0.85 this should be gone.
RocketChat.API.v1.addRoute('permissions.list', { authRequired: true }, {
API.v1.addRoute('permissions.list', { authRequired: true }, {
get() {
const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get'));

return RocketChat.API.v1.success(this.deprecationWarning({
return API.v1.success(this.deprecationWarning({
endpoint: 'permissions.list',
versionWillBeRemove: '0.85',
response: {
Expand All @@ -36,7 +38,7 @@ RocketChat.API.v1.addRoute('permissions.list', { authRequired: true }, {
},
});

RocketChat.API.v1.addRoute('permissions.listAll', { authRequired: true }, {
API.v1.addRoute('permissions.listAll', { authRequired: true }, {
get() {
const { updatedSince } = this.queryParams;

Expand All @@ -59,14 +61,14 @@ RocketChat.API.v1.addRoute('permissions.listAll', { authRequired: true }, {
};
}

return RocketChat.API.v1.success(result);
return API.v1.success(result);
},
});

RocketChat.API.v1.addRoute('permissions.update', { authRequired: true }, {
API.v1.addRoute('permissions.update', { authRequired: true }, {
post() {
if (!RocketChat.authz.hasPermission(this.userId, 'access-permissions')) {
return RocketChat.API.v1.failure('Editing permissions is not allowed', 'error-edit-permissions-not-allowed');
if (!hasPermission(this.userId, 'access-permissions')) {
return API.v1.failure('Editing permissions is not allowed', 'error-edit-permissions-not-allowed');
}

check(this.bodyParams, {
Expand All @@ -83,34 +85,34 @@ RocketChat.API.v1.addRoute('permissions.update', { authRequired: true }, {
Object.keys(this.bodyParams.permissions).forEach((key) => {
const element = this.bodyParams.permissions[key];

if (!RocketChat.models.Permissions.findOneById(element._id)) {
if (!Permissions.findOneById(element._id)) {
permissionNotFound = true;
}

Object.keys(element.roles).forEach((key) => {
const subelement = element.roles[key];

if (!RocketChat.models.Roles.findOneById(subelement)) {
if (!Roles.findOneById(subelement)) {
roleNotFound = true;
}
});
});

if (permissionNotFound) {
return RocketChat.API.v1.failure('Invalid permission', 'error-invalid-permission');
return API.v1.failure('Invalid permission', 'error-invalid-permission');
} else if (roleNotFound) {
return RocketChat.API.v1.failure('Invalid role', 'error-invalid-role');
return API.v1.failure('Invalid role', 'error-invalid-role');
}

Object.keys(this.bodyParams.permissions).forEach((key) => {
const element = this.bodyParams.permissions[key];

RocketChat.models.Permissions.createOrUpdate(element._id, element.roles);
Permissions.createOrUpdate(element._id, element.roles);
});

const result = Meteor.runAsUser(this.userId, () => Meteor.call('permissions/get'));

return RocketChat.API.v1.success({
return API.v1.success({
permissions: result,
});
},
Expand Down
10 changes: 5 additions & 5 deletions packages/rocketchat-api/server/v1/push.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Push } from 'meteor/rocketchat:push';
import { API } from '../api';

RocketChat.API.v1.addRoute('push.token', { authRequired: true }, {
API.v1.addRoute('push.token', { authRequired: true }, {
post() {
const { type, value, appName } = this.bodyParams;
let { id } = this.bodyParams;
Expand Down Expand Up @@ -35,7 +35,7 @@ RocketChat.API.v1.addRoute('push.token', { authRequired: true }, {
userId: this.userId,
}));

return RocketChat.API.v1.success({ result });
return API.v1.success({ result });
},
delete() {
const { token } = this.bodyParams;
Expand All @@ -54,9 +54,9 @@ RocketChat.API.v1.addRoute('push.token', { authRequired: true }, {
});

if (affectedRecords === 0) {
return RocketChat.API.v1.notFound();
return API.v1.notFound();
}

return RocketChat.API.v1.success();
return API.v1.success();
},
});
21 changes: 11 additions & 10 deletions packages/rocketchat-api/server/v1/roles.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Roles } from 'meteor/rocketchat:models';
import { API } from '../api';

RocketChat.API.v1.addRoute('roles.list', { authRequired: true }, {
API.v1.addRoute('roles.list', { authRequired: true }, {
get() {
const roles = RocketChat.models.Roles.find({}, { fields: { _updatedAt: 0 } }).fetch();
const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch();

return RocketChat.API.v1.success({ roles });
return API.v1.success({ roles });
},
});

RocketChat.API.v1.addRoute('roles.create', { authRequired: true }, {
API.v1.addRoute('roles.create', { authRequired: true }, {
post() {
check(this.bodyParams, {
name: String,
Expand All @@ -28,13 +29,13 @@ RocketChat.API.v1.addRoute('roles.create', { authRequired: true }, {
Meteor.call('authorization:saveRole', roleData);
});

return RocketChat.API.v1.success({
role: RocketChat.models.Roles.findOneByIdOrName(roleData.name, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
return API.v1.success({
role: Roles.findOneByIdOrName(roleData.name, { fields: API.v1.defaultFieldsToExclude }),
});
},
});

RocketChat.API.v1.addRoute('roles.addUserToRole', { authRequired: true }, {
API.v1.addRoute('roles.addUserToRole', { authRequired: true }, {
post() {
check(this.bodyParams, {
roleName: String,
Expand All @@ -48,8 +49,8 @@ RocketChat.API.v1.addRoute('roles.addUserToRole', { authRequired: true }, {
Meteor.call('authorization:addUserToRole', this.bodyParams.roleName, user.username, this.bodyParams.roomId);
});

return RocketChat.API.v1.success({
role: RocketChat.models.Roles.findOneByIdOrName(this.bodyParams.roleName, { fields: RocketChat.API.v1.defaultFieldsToExclude }),
return API.v1.success({
role: Roles.findOneByIdOrName(this.bodyParams.roleName, { fields: API.v1.defaultFieldsToExclude }),
});
},
});
Loading