Move the groups v1 api calls out of the huge routes.coffee file.

pull/5140/head
Bradley Hilton 9 years ago
parent e266509bb8
commit 3b55372c30
  1. 1
      packages/rocketchat-api/package.js
  2. 29
      packages/rocketchat-api/server/routes.coffee
  3. 440
      packages/rocketchat-api/server/v1/groups.js
  4. 2
      packages/rocketchat-lib/server/functions/createRoom.js

@ -16,6 +16,7 @@ Package.onUse(function(api) {
api.addFiles('server/api.coffee', 'server');
api.addFiles('server/routes.coffee', 'server');
api.addFiles('server/v1/groups.js', 'server');
api.addFiles('server/settings.js', 'server');
});

@ -141,7 +141,7 @@ RocketChat.API.v1.addRoute 'channels.history', authRequired: true,
return RocketChat.API.v1.failure e.name + ': ' + e.message
return RocketChat.API.v1.success
result: result
messages: result.messages
RocketChat.API.v1.addRoute 'channels.cleanHistory', authRequired: true,
post: ->
@ -172,11 +172,7 @@ RocketChat.API.v1.addRoute 'channels.cleanHistory', authRequired: true,
return RocketChat.API.v1.success
success: true
# List Private Groups a user has access to
RocketChat.API.v1.addRoute 'groups.list', authRequired: true,
get: ->
roomIds = _.pluck RocketChat.models.Subscriptions.findByTypeAndUserId('p', @userId).fetch(), 'rid'
return { groups: RocketChat.models.Rooms.findByIds(roomIds).fetch() }
# Add All Users to Channel
RocketChat.API.v1.addRoute 'channel.addall', authRequired: true,
@ -340,25 +336,4 @@ RocketChat.API.v1.addRoute 'users.setAvatar', authRequired: true,
return RocketChat.API.v1.success()
# Create Private Group
RocketChat.API.v1.addRoute 'groups.create', authRequired: true,
post: ->
if not @bodyParams.name?
return RocketChat.API.v1.failure 'Body param "name" is required'
if not RocketChat.authz.hasPermission(@userId, 'create-p')
return RocketChat.API.v1.unauthorized()
id = undefined
try
if not @bodyParams.members?
Meteor.runAsUser this.userId, =>
id = Meteor.call 'createPrivateGroup', @bodyParams.name, []
else
Meteor.runAsUser this.userId, =>
id = Meteor.call 'createPrivateGroup', @bodyParams.name, @bodyParams.members, []
catch e
return RocketChat.API.v1.failure e.name + ': ' + e.message
return RocketChat.API.v1.success
group: RocketChat.models.Rooms.findOneById(id.rid)

@ -0,0 +1,440 @@
//Returns the private group subscription IF found otherwise it will reutrn the failure of why it didn't. Check the `statusCode` property
function findPrivateGroupById(roomId, userId) {
if (!roomId || !roomId.trim()) {
return RocketChat.API.v1.failure('Body param "roomId" is required');
}
const roomSub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(roomId, userId);
if (!roomSub || roomSub.t !== 'p') {
return RocketChat.API.v1.failure(`No private group found by the id of: ${roomId}`);
}
return roomSub;
}
//Archives a private group only if it wasn't
RocketChat.API.v1.addRoute('groups.archive', { authRequired: true }, {
post: function() {
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is already archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('archiveRoom', findResult.rid);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});
RocketChat.API.v1.addRoute('groups.close', { authRequired: true }, {
post: function() {
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (!findResult.open) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is already closed to the sender`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('hideRoom', findResult.rid);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});
//Create Private Group
RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, {
post: function() {
if (!RocketChat.authz.hasPermission(this.userId, 'create-p')) {
return RocketChat.API.v1.unauthorized();
}
if (!this.bodyParams.name) {
return RocketChat.API.v1.failure('Body param "name" is required');
}
if (this.bodyParams.members && !_.isArray(this.bodyParams.members)) {
return RocketChat.API.v1.failure('Body param "members" must be an array if provided');
}
let id = undefined;
try {
Meteor.runAsUser(this.userId, () => {
id = Meteor.call('createPrivateGroup', this.bodyParams.name, this.bodyParams.members ? this.bodyParams.members : []);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(id.rid)
});
}
});
RocketChat.API.v1.addRoute('groups.history', { authRequired: true }, {
get: function() {
const findResult = findPrivateGroupById(this.queryParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
let latestDate = new Date();
if (this.queryParams.latest) {
latestDate = new Date(this.queryParams.latest);
}
let oldestDate = undefined;
if (this.queryParams.oldest) {
oldestDate = new Date(this.queryParams.oldest);
}
let inclusive = false;
if (this.queryParams.inclusive) {
inclusive = this.queryParams.inclusive;
}
let count = 20;
if (this.queryParams.count) {
count = parseInt(this.queryParams.count);
}
let unreads = false;
if (this.queryParams.unreads) {
unreads = this.queryParams.unreads;
}
let result = {};
try {
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getChannelHistory', { rid: findResult.rid, latest: latestDate, oldest: oldestDate, inclusive, count, unreads });
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
messages: result.messages
});
}
});
RocketChat.API.v1.addRoute('groups.info', { authRequired: true }, {
get: function() {
const findResult = findPrivateGroupById(this.queryParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid)
});
}
});
RocketChat.API.v1.addRoute('groups.invite', { authRequired: true }, {
post: function() {
if (!this.bodyParams.userId || !this.bodyParams.userId.trim()) {
return RocketChat.API.v1.failure('Body param "userId" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
const user = RocketChat.models.Users.findOneById(this.bodyParams.userId);
if (!user) {
return RocketChat.API.v1.failure(`There is not a user with the id: ${this.bodyParams.userId}`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('addUserToRoom', { rid: findResult.rid, username: user.username });
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid)
});
}
});
RocketChat.API.v1.addRoute('groups.kick', { authRequired: true }, {
post: function() {
if (!this.bodyParams.userId || !this.bodyParams.userId.trim()) {
return RocketChat.API.v1.failure('Body param "userId" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
const user = RocketChat.models.Users.findOneById(this.bodyParams.userId);
if (!user) {
return RocketChat.API.v1.failure(`There is not a user with the id: ${this.bodyParams.userId}`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('removeUserFromRoom', { rid: findResult.rid, username: user.username });
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});
RocketChat.API.v1.addRoute('groups.leave', { authRequired: true }, {
post: function() {
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('leaveRoom', findResult.rid);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});
//List Private Groups a user has access to
RocketChat.API.v1.addRoute('groups.list', { authRequired: true }, {
get: function() {
const roomIds = _.pluck(RocketChat.models.Subscriptions.findByTypeAndUserId('p', this.userId).fetch(), 'rid');
return RocketChat.API.v1.success({
groups: RocketChat.models.Rooms.findByIds(roomIds).fetch()
});
}
});
RocketChat.API.v1.addRoute('groups.open', { authRequired: true }, {
post: function() {
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.open) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is already open for the sender`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('openRoom', findResult.rid);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});
RocketChat.API.v1.addRoute('groups.rename', { authRequired: true }, {
post: function() {
if (!this.bodyParams.name || !this.bodyParams.name.trim()) {
return RocketChat.API.v1.failure('The bodyParam "name" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult.rid, 'roomName', this.bodyParams.name);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
channel: RocketChat.models.Rooms.findOneById(findResult.rid)
});
}
});
RocketChat.API.v1.addRoute('groups.setDescription', { authRequired: true }, {
post: function() {
if (!this.bodyParams.description || !this.bodyParams.description.trim()) {
return RocketChat.API.v1.failure('The bodyParam "description" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult.rid, 'roomDescription', this.bodyParams.description);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
description: this.bodyParams.description
});
}
});
RocketChat.API.v1.addRoute('groups.setPurpose', { authRequired: true }, {
post: function() {
if (!this.bodyParams.purpose || !this.bodyParams.purpose.trim()) {
return RocketChat.API.v1.failure('The bodyParam "purpose" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult.rid, 'roomDescription', this.bodyParams.purpose);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
purpose: this.bodyParams.purpose
});
}
});
RocketChat.API.v1.addRoute('groups.setTopic', { authRequired: true }, {
post: function() {
if (!this.bodyParams.topic || !this.bodyParams.topic.trim()) {
return RocketChat.API.v1.failure('The bodyParam "topic" is required');
}
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('saveRoomSettings', findResult.rid, 'roomTopic', this.bodyParams.topic);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success({
topic: this.bodyParams.topic
});
}
});
RocketChat.API.v1.addRoute('groups.unarchive', { authRequired: true }, {
post: function() {
const findResult = findPrivateGroupById(this.bodyParams.roomId, this.userId);
//The find method returns either with the group or the failure
if (findResult.statusCode) {
return findResult;
}
if (!findResult.archived) {
return RocketChat.API.v1.failure(`The private group, ${this.bodyParams.name}, is not archived`);
}
try {
Meteor.runAsUser(this.userId, () => {
Meteor.call('unarchiveRoom', findResult.rid);
});
} catch (e) {
return RocketChat.API.v1.failure(`${e.name}: ${e.message}`);
}
return RocketChat.API.v1.success();
}
});

@ -54,7 +54,7 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly) {
});
}
room = RocketChat.models.Rooms.createWithTypeNameUserAndUsernames(type, name, owner.username, members, {
room = RocketChat.models.Rooms.createWithTypeNameUserAndUsernames(type, name, owner, members, {
ts: now,
ro: readOnly === true,
sysMes: readOnly !== true

Loading…
Cancel
Save