Regression: Update .invite endpoints to support multiple users at once (#21328)

pull/21351/head
Kevin Aleman 5 years ago committed by GitHub
parent 86ecb4ce58
commit 1c704d8d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      app/api/server/helpers/getUserFromParams.js
  2. 8
      app/api/server/v1/channels.js
  3. 8
      app/api/server/v1/groups.js
  4. 10
      app/models/server/models/Users.js

@ -25,3 +25,28 @@ API.helperMethods.set('getUserFromParams', function _getUserFromParams() {
return user;
});
API.helperMethods.set('getUserListFromParams', function _getUserListFromParams() {
let users;
const params = this.requestParams();
// if params.userId is provided, include it as well
const soleUser = params.userId || params.username || params.user;
let userListParam = params.userIds || params.usernames || [];
userListParam.push(soleUser);
userListParam = userListParam.filter(Boolean);
// deduplicate to avoid errors
userListParam = [...new Set(userListParam)];
if (!userListParam.length) {
throw new Meteor.Error('error-users-params-not-provided', 'Please provide "userId" or "username" or "userIds" or "usernames" as param');
}
if (params.userIds || params.userId) {
users = Users.findByIds(userListParam);
} else {
users = Users.findByUsernamesIgnoringCase(userListParam);
}
return users.fetch();
});

@ -422,10 +422,14 @@ API.v1.addRoute('channels.invite', { authRequired: true }, {
post() {
const findResult = findChannelByIdOrName({ params: this.requestParams() });
const user = this.getUserFromParams();
const users = this.getUserListFromParams();
if (!users.length) {
return API.v1.failure('invalid-user-invite-list', 'Cannot invite if no users are provided');
}
Meteor.runAsUser(this.userId, () => {
Meteor.call('addUserToRoom', { rid: findResult._id, username: user.username });
Meteor.call('addUsersToRoom', { rid: findResult._id, users: users.map((u) => u.username) });
});
return API.v1.success({

@ -393,9 +393,13 @@ API.v1.addRoute('groups.invite', { authRequired: true }, {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group');
}
const { username } = this.getUserFromParams();
const users = this.getUserListFromParams();
Meteor.runAsUser(this.userId, () => Meteor.call('addUserToRoom', { rid, username }));
if (!users.length) {
throw new Meteor.Error('error-empty-invite-list', 'Cannot invite if no valid users are provided');
}
Meteor.runAsUser(this.userId, () => Meteor.call('addUsersToRoom', { rid, users: users.map((u) => u.username) }));
return API.v1.success({
group: this.composeRoomWithLastMessage(Rooms.findOneById(rid, { fields: API.v1.defaultFieldsToExclude }), this.userId),

@ -717,6 +717,16 @@ export class Users extends Base {
return this.find(query, options);
}
findByUsernamesIgnoringCase(usernames, options) {
const query = {
username: {
$in: usernames.filter(Boolean).map((u) => new RegExp(`^${ escapeRegExp(u) }$`, 'i')),
},
};
return this.find(query, options);
}
findActive(options = {}) {
return this.find({
active: true,

Loading…
Cancel
Save