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

Remove some dependencies inside rocketchat-lib/server/functions #13214

Merged
merged 4 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/rocketchat-integrations/server/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import '../lib/rocketchat';
import './logger';
import './lib/validation';
import './models/Integrations';
import './models/IntegrationHistory';
import './publications/integrations';
import './publications/integrationHistory';
import './methods/incoming/addIncomingIntegration';
Expand Down
7 changes: 3 additions & 4 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Package.onUse(function(api) {
api.use('konecty:multiple-instances-status');
api.use('rocketchat:file');
api.use('rocketchat:file-upload');
api.use('rocketchat:push');
api.use('rocketchat:push-notifications', { unordered: true });
api.use('rocketchat:push-notifications');

api.use('templating', 'client');
api.use('kadira:flow-router');
Expand Down Expand Up @@ -111,7 +110,7 @@ Package.onUse(function(api) {
api.addFiles('server/functions/archiveRoom.js', 'server');
api.addFiles('server/functions/checkUsernameAvailability.js', 'server');
api.addFiles('server/functions/checkEmailAvailability.js', 'server');
api.addFiles('server/functions/composeMessageObjectWithUser.js', 'server');
api.addFiles('server/functions/composeMessageObjectWithUser_import.js', 'server');
api.addFiles('server/functions/createRoom.js', 'server');
api.addFiles('server/functions/cleanRoomHistory.js', 'server');
api.addFiles('server/functions/deleteMessage.js', 'server');
Expand All @@ -137,7 +136,7 @@ Package.onUse(function(api) {

// SERVER LIB
api.addFiles('server/lib/configLogger.js', 'server');
api.addFiles('server/lib/PushNotification.js', 'server');
api.addFiles('server/lib/PushNotification_import.js', 'server');
api.addFiles('server/lib/defaultBlockedDomainsList.js', 'server');
api.addFiles('server/lib/interceptDirectReplyEmails.js', 'server');
api.addFiles('server/lib/loginErrorMessageOverride.js', 'server');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Rooms, Subscriptions, Messages } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { callbacks } from 'meteor/rocketchat:callbacks';

RocketChat.addUserToDefaultChannels = function(user, silenced) {
RocketChat.callbacks.run('beforeJoinDefaultChannels', user);
const defaultRooms = RocketChat.models.Rooms.findByDefaultAndTypes(true, ['c', 'p'], { fields: { usernames: 0 } }).fetch();
callbacks.run('beforeJoinDefaultChannels', user);
const defaultRooms = Rooms.findByDefaultAndTypes(true, ['c', 'p'], { fields: { usernames: 0 } }).fetch();
defaultRooms.forEach((room) => {

// put user in default rooms
const muted = room.ro && !RocketChat.authz.hasPermission(user._id, 'post-readonly');
const muted = room.ro && !hasPermission(user._id, 'post-readonly');
if (muted) {
RocketChat.models.Rooms.muteUsernameByRoomId(room._id, user.username);
Rooms.muteUsernameByRoomId(room._id, user.username);
}

if (!RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user._id)) {
if (!Subscriptions.findOneByRoomIdAndUserId(room._id, user._id)) {

// Add a subscription to this user
RocketChat.models.Subscriptions.createWithRoomAndUser(room, user, {
Subscriptions.createWithRoomAndUser(room, user, {
ts: new Date(),
open: true,
alert: true,
Expand All @@ -23,7 +27,7 @@ RocketChat.addUserToDefaultChannels = function(user, silenced) {

// Insert user joined message
if (!silenced) {
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(room._id, user);
Messages.createUserJoinWithRoomIdAndUser(room._id, user);
}
}
});
Expand Down
21 changes: 12 additions & 9 deletions packages/rocketchat-lib/server/functions/addUserToRoom.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { Meteor } from 'meteor/meteor';
import { Rooms, Subscriptions, Messages } from 'meteor/rocketchat:models';
import { hasPermission } from 'meteor/rocketchat:authorization';
import { callbacks } from 'meteor/rocketchat:callbacks';

RocketChat.addUserToRoom = function(rid, user, inviter, silenced) {
const now = new Date();
const room = RocketChat.models.Rooms.findOneById(rid);
const room = Rooms.findOneById(rid);

// Check if user is already in room
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
if (subscription) {
return;
}

if (room.t === 'c' || room.t === 'p') {
RocketChat.callbacks.run('beforeJoinRoom', user, room);
callbacks.run('beforeJoinRoom', user, room);
}

const muted = room.ro && !RocketChat.authz.hasPermission(user._id, 'post-readonly');
const muted = room.ro && !hasPermission(user._id, 'post-readonly');
if (muted) {
RocketChat.models.Rooms.muteUsernameByRoomId(rid, user.username);
Rooms.muteUsernameByRoomId(rid, user.username);
}

RocketChat.models.Subscriptions.createWithRoomAndUser(room, user, {
Subscriptions.createWithRoomAndUser(room, user, {
ts: now,
open: true,
alert: true,
Expand All @@ -30,21 +33,21 @@ RocketChat.addUserToRoom = function(rid, user, inviter, silenced) {

if (!silenced) {
if (inviter) {
RocketChat.models.Messages.createUserAddedWithRoomIdAndUser(rid, user, {
Messages.createUserAddedWithRoomIdAndUser(rid, user, {
ts: now,
u: {
_id: inviter._id,
username: inviter.username,
},
});
} else {
RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(rid, user, { ts: now });
Messages.createUserJoinWithRoomIdAndUser(rid, user, { ts: now });
}
}

if (room.t === 'c' || room.t === 'p') {
Meteor.defer(function() {
RocketChat.callbacks.run('afterJoinRoom', user, room);
callbacks.run('afterJoinRoom', user, room);
});
}

Expand Down
8 changes: 5 additions & 3 deletions packages/rocketchat-lib/server/functions/archiveRoom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { Rooms, Subscriptions } from 'meteor/rocketchat:models';
import { callbacks } from 'meteor/rocketchat:callbacks';

RocketChat.archiveRoom = function(rid) {
RocketChat.models.Rooms.archiveById(rid);
RocketChat.models.Subscriptions.archiveByRoomId(rid);
Rooms.archiveById(rid);
Subscriptions.archiveByRoomId(rid);

RocketChat.callbacks.run('afterRoomArchived', RocketChat.models.Rooms.findOneById(rid), Meteor.user());
callbacks.run('afterRoomArchived', Rooms.findOneById(rid), Meteor.user());
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Meteor } from 'meteor/meteor';
import s from 'underscore.string';

import { settings } from 'meteor/rocketchat:settings';

let usernameBlackList = [];

const toRegExp = (username) => new RegExp(`^${ s.escapeRegExp(username).trim() }$`, 'i');

RocketChat.settings.get('Accounts_BlockedUsernameList', (key, value) => {
settings.get('Accounts_BlockedUsernameList', (key, value) => {
usernameBlackList = value.split(',').map(toRegExp);
});

Expand Down
12 changes: 7 additions & 5 deletions packages/rocketchat-lib/server/functions/cleanRoomHistory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TAPi18n } from 'meteor/tap:i18n';
import { FileUpload } from 'meteor/rocketchat:file-upload';
import { Messages, Rooms } from 'meteor/rocketchat:models';
import { Notifications } from 'meteor/rocketchat:notifications';

RocketChat.cleanRoomHistory = function({ rid, latest = new Date(), oldest = new Date('0001-01-01T00:00:00Z'), inclusive = true, limit = 0, excludePinned = true, filesOnly = false, fromUsers = [] }) {
const gt = inclusive ? '$gte' : '$gt';
Expand All @@ -10,7 +12,7 @@ RocketChat.cleanRoomHistory = function({ rid, latest = new Date(), oldest = new
const text = `_${ TAPi18n.__('File_removed_by_prune') }_`;

let fileCount = 0;
RocketChat.models.Messages.findFilesByRoomIdPinnedTimestampAndUsers(
Messages.findFilesByRoomIdPinnedTimestampAndUsers(
rid,
excludePinned,
ts,
Expand All @@ -20,18 +22,18 @@ RocketChat.cleanRoomHistory = function({ rid, latest = new Date(), oldest = new
FileUpload.getStore('Uploads').deleteById(document.file._id);
fileCount++;
if (filesOnly) {
RocketChat.models.Messages.update({ _id: document._id }, { $unset: { file: 1 }, $set: { attachments: [{ color: '#FD745E', text }] } });
Messages.update({ _id: document._id }, { $unset: { file: 1 }, $set: { attachments: [{ color: '#FD745E', text }] } });
}
});
if (filesOnly) {
return fileCount;
}

const count = limit ? RocketChat.models.Messages.removeByIdPinnedTimestampLimitAndUsers(rid, excludePinned, ts, limit, fromUsers) : RocketChat.models.Messages.removeByIdPinnedTimestampAndUsers(rid, excludePinned, ts, fromUsers);
const count = limit ? Messages.removeByIdPinnedTimestampLimitAndUsers(rid, excludePinned, ts, limit, fromUsers) : Messages.removeByIdPinnedTimestampAndUsers(rid, excludePinned, ts, fromUsers);

if (count) {
RocketChat.models.Rooms.resetLastMessageById(rid);
RocketChat.Notifications.notifyRoom(rid, 'deleteMessageBulk', {
Rooms.resetLastMessageById(rid);
Notifications.notifyRoom(rid, 'deleteMessageBulk', {
rid,
excludePinned,
ts,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { composeMessageObjectWithUser } from 'meteor/rocketchat:utils';

RocketChat.composeMessageObjectWithUser = composeMessageObjectWithUser;
28 changes: 16 additions & 12 deletions packages/rocketchat-lib/server/functions/createRoom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Users, Rooms, Subscriptions } from 'meteor/rocketchat:models';
import { callbacks } from 'meteor/rocketchat:callbacks';
import { hasPermission, addUserRoles } from 'meteor/rocketchat:authorization';
import { getValidRoomName } from 'meteor/rocketchat:utils';
import _ from 'underscore';
import s from 'underscore.string';

Expand All @@ -11,7 +15,7 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData
throw new Meteor.Error('error-invalid-name', 'Invalid name', { function: 'RocketChat.createRoom' });
}

owner = RocketChat.models.Users.findOneByUsername(owner, { fields: { username: 1 } });
owner = Users.findOneByUsername(owner, { fields: { username: 1 } });
if (!owner) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'RocketChat.createRoom' });
}
Expand All @@ -27,7 +31,7 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData

const now = new Date();
let room = Object.assign({
name: RocketChat.getValidRoomName(name),
name: getValidRoomName(name),
fname: name,
t: type,
msgs: 0,
Expand Down Expand Up @@ -62,21 +66,21 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData
}

if (type === 'c') {
RocketChat.callbacks.run('beforeCreateChannel', owner, room);
callbacks.run('beforeCreateChannel', owner, room);
}

room = RocketChat.models.Rooms.createWithFullRoomData(room);
room = Rooms.createWithFullRoomData(room);

for (const username of members) {
const member = RocketChat.models.Users.findOneByUsername(username, { fields: { username: 1, 'settings.preferences': 1 } });
const member = Users.findOneByUsername(username, { fields: { username: 1, 'settings.preferences': 1 } });
const isTheOwner = username === owner.username;
if (!member) {
continue;
}

// make all room members (Except the owner) muted by default, unless they have the post-readonly permission
if (readOnly === true && !RocketChat.authz.hasPermission(member._id, 'post-readonly') && !isTheOwner) {
RocketChat.models.Rooms.muteUsernameByRoomId(room._id, username);
if (readOnly === true && !hasPermission(member._id, 'post-readonly') && !isTheOwner) {
Rooms.muteUsernameByRoomId(room._id, username);
}

const extra = { open: true };
Expand All @@ -85,22 +89,22 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData
extra.ls = now;
}

RocketChat.models.Subscriptions.createWithRoomAndUser(room, member, extra);
Subscriptions.createWithRoomAndUser(room, member, extra);
}

RocketChat.authz.addUserRoles(owner._id, ['owner'], room._id);
addUserRoles(owner._id, ['owner'], room._id);

if (type === 'c') {
Meteor.defer(() => {
RocketChat.callbacks.run('afterCreateChannel', owner, room);
callbacks.run('afterCreateChannel', owner, room);
});
} else if (type === 'p') {
Meteor.defer(() => {
RocketChat.callbacks.run('afterCreatePrivateGroup', owner, room);
callbacks.run('afterCreatePrivateGroup', owner, room);
});
}
Meteor.defer(() => {
RocketChat.callbacks.run('afterCreateRoom', owner, room);
callbacks.run('afterCreateRoom', owner, room);
});

if (Apps && Apps.isLoaded()) {
Expand Down
30 changes: 17 additions & 13 deletions packages/rocketchat-lib/server/functions/deleteMessage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Meteor } from 'meteor/meteor';
import { FileUpload } from 'meteor/rocketchat:file-upload';
import { settings } from 'meteor/rocketchat:settings';
import { Messages, Uploads, Rooms } from 'meteor/rocketchat:models';
import { Notifications } from 'meteor/rocketchat:notifications';
import { callbacks } from 'meteor/rocketchat:callbacks';

RocketChat.deleteMessage = function(message, user) {
const keepHistory = RocketChat.settings.get('Message_KeepHistory');
const showDeletedStatus = RocketChat.settings.get('Message_ShowDeletedStatus');
const deletedMsg = RocketChat.models.Messages.findOneById(message._id);
const keepHistory = settings.get('Message_KeepHistory');
const showDeletedStatus = settings.get('Message_ShowDeletedStatus');
const deletedMsg = Messages.findOneById(message._id);

if (deletedMsg && Apps && Apps.isLoaded()) {
const prevent = Promise.await(Apps.getBridges().getListenerBridge().messageEvent('IPreMessageDeletePrevent', deletedMsg));
Expand All @@ -15,17 +19,17 @@ RocketChat.deleteMessage = function(message, user) {

if (keepHistory) {
if (showDeletedStatus) {
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id);
Messages.cloneAndSaveAsHistoryById(message._id);
} else {
RocketChat.models.Messages.setHiddenById(message._id, true);
Messages.setHiddenById(message._id, true);
}

if (message.file && message.file._id) {
RocketChat.models.Uploads.update(message.file._id, { $set: { _hidden: true } });
Uploads.update(message.file._id, { $set: { _hidden: true } });
}
} else {
if (!showDeletedStatus) {
RocketChat.models.Messages.removeById(message._id);
Messages.removeById(message._id);
}

if (message.file && message.file._id) {
Expand All @@ -34,21 +38,21 @@ RocketChat.deleteMessage = function(message, user) {
}

Meteor.defer(function() {
RocketChat.callbacks.run('afterDeleteMessage', deletedMsg);
callbacks.run('afterDeleteMessage', deletedMsg);
});

// update last message
if (RocketChat.settings.get('Store_Last_Message')) {
const room = RocketChat.models.Rooms.findOneById(message.rid, { fields: { lastMessage: 1 } });
if (settings.get('Store_Last_Message')) {
const room = Rooms.findOneById(message.rid, { fields: { lastMessage: 1 } });
if (!room.lastMessage || room.lastMessage._id === message._id) {
RocketChat.models.Rooms.resetLastMessageById(message.rid, message._id);
Rooms.resetLastMessageById(message.rid, message._id);
}
}

if (showDeletedStatus) {
RocketChat.models.Messages.setAsDeletedByIdAndUser(message._id, user);
Messages.setAsDeletedByIdAndUser(message._id, user);
} else {
RocketChat.Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id });
Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id });
}

if (Apps && Apps.isLoaded()) {
Expand Down
Loading