Add option to get group by name in api/v1/groups.info?roomName=testing

pull/5595/head
hussamEddinAlhendi 9 years ago
parent ec0df3a9d6
commit 0258fb5c45
  1. 26
      packages/rocketchat-api/server/v1/groups.js
  2. 11
      packages/rocketchat-lib/server/models/Subscriptions.coffee

@ -16,6 +16,23 @@ function findPrivateGroupById({ roomId, userId, checkedArchived = true }) {
return roomSub;
}
function findPrivateGroupByName({ roomName, userId, checkedArchived = true }) {
if (!roomName || !roomName.trim()) {
return RocketChat.API.v1.failure('Body param "roomName" is required');
}
const roomSub = RocketChat.models.Subscriptions.findOneByRoomNameAndUserId(roomName, userId);
if (!roomSub || roomSub.t !== 'p') {
return RocketChat.API.v1.failure(`No private group found by the name of: ${roomName}`);
}
if (checkedArchived && roomSub.archived) {
return RocketChat.API.v1.failure(`The private group, ${roomSub.name}, is already archived`);
}
return roomSub;
}
RocketChat.API.v1.addRoute('groups.addModerator', { authRequired: true }, {
post: function() {
@ -227,7 +244,12 @@ RocketChat.API.v1.addRoute('groups.history', { authRequired: true }, {
RocketChat.API.v1.addRoute('groups.info', { authRequired: true }, {
get: function() {
const findResult = findPrivateGroupById({ roomId: this.queryParams.roomId, userId: this.userId, checkedArchived: false });
let findResult;
if(this.queryParams.roomId) {
findResult = findPrivateGroupById({ roomId: this.queryParams.roomId, userId: this.userId, checkedArchived: false });
} else if(this.queryParams.roomName) {
findResult = findPrivateGroupByName({ roomName: this.queryParams.roomName, userId: this.userId, checkedArchived: false });
}
//The find method returns either with the group or the failure
if (findResult.statusCode) {
@ -235,7 +257,7 @@ RocketChat.API.v1.addRoute('groups.info', { authRequired: true }, {
}
return RocketChat.API.v1.success({
group: RocketChat.models.Rooms.findOneById(findResult.rid, { fields: RocketChat.API.v1.defaultFieldsToExclude })
group: RocketChat.models.Rooms.findOneByName(findResult.name, { fields: RocketChat.API.v1.defaultFieldsToExclude })
});
}
});

@ -19,19 +19,28 @@ class ModelSubscriptions extends RocketChat.models._Base
this.cache.ensureIndex('rid', 'array')
this.cache.ensureIndex('u._id', 'array')
this.cache.ensureIndex(['rid', 'u._id'], 'unique')
this.cache.ensureIndex(['name', 'u._id'], 'unique')
# FIND ONE
findOneByRoomIdAndUserId: (roomId, userId) ->
if this.useCache
return this.cache.findByIndex('rid,u._id', [roomId, userId]).fetch()
query =
rid: roomId
"u._id": userId
return @findOne query
findOneByRoomNameAndUserId: (roomName, userId) ->
if this.useCache
return this.cache.findByIndex('name,u._id', [roomName, userId]).fetch()
query =
name: roomName
"u._id": userId
return @findOne query
# FIND
findByUserId: (userId, options) ->
if this.useCache

Loading…
Cancel
Save