The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/server/methods/addAllUserToRoom.js

63 lines
1.7 KiB

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { hasRole } from 'meteor/rocketchat:authorization';
import { Users, Rooms, Subscriptions, Messages } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import { callbacks } from 'meteor/rocketchat:callbacks';
Meteor.methods({
addAllUserToRoom(rid, activeUsersOnly = false) {
check (rid, String);
check (activeUsersOnly, Boolean);
if (hasRole(this.userId, 'admin') === true) {
const userCount = Users.find().count();
if (userCount > settings.get('API_User_Limit')) {
throw new Meteor.Error('error-user-limit-exceeded', 'User Limit Exceeded', {
method: 'addAllToRoom',
});
}
const room = Rooms.findOneById(rid);
if (room == null) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
method: 'addAllToRoom',
});
}
const userFilter = {};
if (activeUsersOnly === true) {
userFilter.active = true;
}
const users = Users.find(userFilter).fetch();
const now = new Date();
users.forEach(function(user) {
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
if (subscription != null) {
return;
}
callbacks.run('beforeJoinRoom', user, room);
Subscriptions.createWithRoomAndUser(room, user, {
ts: now,
open: true,
alert: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
});
Messages.createUserJoinWithRoomIdAndUser(rid, user, {
ts: now,
});
Meteor.defer(function() {});
return callbacks.run('afterJoinRoom', user, room);
});
return true;
} else {
throw (new Meteor.Error(403, 'Access to Method Forbidden', {
method: 'addAllToRoom',
}));
}
},
});