Skip to content

Commit

Permalink
Remove some dependencies inside rocketchat-lib/server/methods (#13218)
Browse files Browse the repository at this point in the history
* Move integrations models to rc-models

* Move composeMessage function to rc-utils

* Move PushNotifications class to push-notifications package

* Import variables to remove dependency of RC namespace

* Import variables to remove RC namespace dependency inside rc-lib/server/lib

* Import variables to remove RC namespace inside rc-lib/server/methods
  • Loading branch information
MarcosSpessatto authored and rodrigok committed Jan 22, 2019
1 parent 2bf63fb commit 3f1451f
Show file tree
Hide file tree
Showing 40 changed files with 215 additions and 140 deletions.
36 changes: 19 additions & 17 deletions packages/rocketchat-lib/server/methods/addOAuthService.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions packages/rocketchat-lib/server/methods/addUsersToRoom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Match } from 'meteor/check';
import { Rooms, Subscriptions, Users } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
addUsersToRoom(data = {}) {
Expand All @@ -17,9 +19,9 @@ Meteor.methods({
}

// Get user and room details
const room = RocketChat.models.Rooms.findOneById(data.rid);
const room = Rooms.findOneById(data.rid);
const userId = Meteor.userId();
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(data.rid, userId, { fields: { _id: 1 } });
const subscription = Subscriptions.findOneByRoomIdAndUserId(data.rid, userId, { fields: { _id: 1 } });
const userInRoom = subscription != null;

// Can't add to direct room ever
Expand All @@ -31,11 +33,11 @@ Meteor.methods({

// Can add to any room you're in, with permission, otherwise need specific room type permission
let canAddUser = false;
if (userInRoom && RocketChat.authz.hasPermission(userId, 'add-user-to-joined-room', room._id)) {
if (userInRoom && hasPermission(userId, 'add-user-to-joined-room', room._id)) {
canAddUser = true;
} else if (room.t === 'c' && RocketChat.authz.hasPermission(userId, 'add-user-to-any-c-room')) {
} else if (room.t === 'c' && hasPermission(userId, 'add-user-to-any-c-room')) {
canAddUser = true;
} else if (room.t === 'p' && RocketChat.authz.hasPermission(userId, 'add-user-to-any-p-room')) {
} else if (room.t === 'p' && hasPermission(userId, 'add-user-to-any-p-room')) {
canAddUser = true;
}

Expand All @@ -56,7 +58,7 @@ Meteor.methods({
// Validate each user, then add to room
const user = Meteor.user();
data.users.forEach((username) => {
const newUser = RocketChat.models.Users.findOneByUsername(username);
const newUser = Users.findOneByUsername(username);
if (!newUser) {
throw new Meteor.Error('error-invalid-username', 'Invalid username', {
method: 'addUsersToRoom',
Expand Down
6 changes: 4 additions & 2 deletions packages/rocketchat-lib/server/methods/archiveRoom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Rooms } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
archiveRoom(rid) {
Expand All @@ -10,13 +12,13 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'archiveRoom' });
}

const room = RocketChat.models.Rooms.findOneById(rid);
const room = Rooms.findOneById(rid);

if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'archiveRoom' });
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'archive-room', room._id)) {
if (!hasPermission(Meteor.userId(), 'archive-room', room._id)) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'archiveRoom' });
}

Expand Down
7 changes: 4 additions & 3 deletions packages/rocketchat-lib/server/methods/blockUser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Subscriptions } from 'meteor/rocketchat:models';

Meteor.methods({
blockUser({ rid, blocked }) {
Expand All @@ -11,14 +12,14 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'blockUser' });
}

const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId());
const subscription2 = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, blocked);
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId());
const subscription2 = Subscriptions.findOneByRoomIdAndUserId(rid, blocked);

if (!subscription || !subscription2) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'blockUser' });
}

RocketChat.models.Subscriptions.setBlockedByRoomId(rid, blocked, Meteor.userId());
Subscriptions.setBlockedByRoomId(rid, blocked, Meteor.userId());

return true;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { settings } from 'meteor/rocketchat:settings';

Meteor.methods({
checkRegistrationSecretURL(hash) {

check(hash, String);

return hash === RocketChat.settings.get('Accounts_RegistrationForm_SecretURL');
return hash === settings.get('Accounts_RegistrationForm_SecretURL');
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { settings } from 'meteor/rocketchat:settings';

Meteor.methods({
checkUsernameAvailability(username) {
Expand All @@ -11,7 +12,7 @@ Meteor.methods({

const user = Meteor.user();

if (user.username && !RocketChat.settings.get('Accounts_AllowUsernameChange')) {
if (user.username && !settings.get('Accounts_AllowUsernameChange')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setUsername' });
}

Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/server/methods/cleanRoomHistory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
cleanRoomHistory({ roomId, latest, oldest, inclusive = true, limit, excludePinned = false, filesOnly = false, fromUsers = [] }) {
Expand All @@ -18,7 +19,7 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'cleanRoomHistory' });
}

if (!RocketChat.authz.hasPermission(userId, 'clean-channel-history', roomId)) {
if (!hasPermission(userId, 'clean-channel-history', roomId)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'cleanRoomHistory' });
}

Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/server/methods/createChannel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
createChannel(name, members, readOnly = false, customFields = {}, extraData = {}) {
Expand All @@ -10,7 +11,7 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'createChannel' });
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'create-c')) {
if (!hasPermission(Meteor.userId(), 'create-c')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'createChannel' });
}
return RocketChat.createRoom('c', name, Meteor.user() && Meteor.user().username, members, readOnly, { customFields, ...extraData });
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/server/methods/createPrivateGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
createPrivateGroup(name, members, readOnly = false, customFields = {}, extraData = {}) {
Expand All @@ -10,7 +11,7 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'createPrivateGroup' });
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'create-p')) {
if (!hasPermission(Meteor.userId(), 'create-p')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'createPrivateGroup' });
}

Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-lib/server/methods/createToken.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { hasPermission } from 'meteor/rocketchat:authorization';

Meteor.methods({
createToken(userId) {
if (Meteor.userId() !== userId && !RocketChat.authz.hasPermission(Meteor.userId(), 'user-generate-access-token')) {
if (Meteor.userId() !== userId && !hasPermission(Meteor.userId(), 'user-generate-access-token')) {
throw new Meteor.Error('error-not-authorized', 'Not authorized', { method: 'createToken' });
}
const token = Accounts._generateStampedLoginToken();
Expand Down
15 changes: 9 additions & 6 deletions packages/rocketchat-lib/server/methods/deleteMessage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { settings } from 'meteor/rocketchat:settings';
import { Messages } from 'meteor/rocketchat:models';
import moment from 'moment';

Meteor.methods({
Expand All @@ -12,7 +15,7 @@ Meteor.methods({
method: 'deleteMessage',
});
}
const originalMessage = RocketChat.models.Messages.findOneById(message._id, {
const originalMessage = Messages.findOneById(message._id, {
fields: {
u: 1,
rid: 1,
Expand All @@ -26,17 +29,17 @@ Meteor.methods({
action: 'Delete_message',
});
}
const forceDelete = RocketChat.authz.hasPermission(Meteor.userId(), 'force-delete-message', originalMessage.rid);
const hasPermission = RocketChat.authz.hasPermission(Meteor.userId(), 'delete-message', originalMessage.rid);
const deleteAllowed = RocketChat.settings.get('Message_AllowDeleting');
const forceDelete = hasPermission(Meteor.userId(), 'force-delete-message', originalMessage.rid);
const _hasPermission = hasPermission(Meteor.userId(), 'delete-message', originalMessage.rid);
const deleteAllowed = settings.get('Message_AllowDeleting');
const deleteOwn = originalMessage && originalMessage.u && originalMessage.u._id === Meteor.userId();
if (!(hasPermission || (deleteAllowed && deleteOwn)) && !(forceDelete)) {
if (!(_hasPermission || (deleteAllowed && deleteOwn)) && !(forceDelete)) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'deleteMessage',
action: 'Delete_message',
});
}
const blockDeleteInMinutes = RocketChat.settings.get('Message_AllowDeleting_BlockDeleteInMinutes');
const blockDeleteInMinutes = settings.get('Message_AllowDeleting_BlockDeleteInMinutes');
if (blockDeleteInMinutes != null && blockDeleteInMinutes !== 0 && !forceDelete) {
if (originalMessage.ts == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base';
import { settings } from 'meteor/rocketchat:settings';
import { Users } from 'meteor/rocketchat:models';
import s from 'underscore.string';

Meteor.methods({
Expand All @@ -12,12 +14,12 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'deleteUserOwnAccount' });
}

if (!RocketChat.settings.get('Accounts_AllowDeleteOwnAccount')) {
if (!settings.get('Accounts_AllowDeleteOwnAccount')) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'deleteUserOwnAccount' });
}

const userId = Meteor.userId();
const user = RocketChat.models.Users.findOneById(userId);
const user = Users.findOneById(userId);

if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'deleteUserOwnAccount' });
Expand Down
Loading

0 comments on commit 3f1451f

Please sign in to comment.