Skip to content

Commit

Permalink
[IMPROVE] Rocket.Chat.Apps (#12680)
Browse files Browse the repository at this point in the history
* Allow apps to create direct messages

* Implement getMembers;

* Adds ability to search direct rooms by usernames
  • Loading branch information
marceloschmidt authored and rodrigok committed Nov 22, 2018
1 parent 85210b3 commit 36acdea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 26 additions & 2 deletions packages/rocketchat-apps/server/bridges/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export class AppRoomBridge {
case RoomType.PRIVATE_GROUP:
method = 'createPrivateGroup';
break;
case RoomType.DIRECT_MESSAGE:
method = 'createDirectMessage';
break;
default:
throw new Error('Only channels and private groups can be created.');
throw new Error('Only channels, private groups and direct messages can be created.');
}

let rid;
Expand All @@ -30,7 +33,13 @@ export class AppRoomBridge {
delete extraData.t;
delete extraData.ro;
delete extraData.customFields;
const info = Meteor.call(method, rcRoom.name, members, rcRoom.ro, rcRoom.customFields, extraData);
let info;
if (room.type === RoomType.DIRECT_MESSAGE) {
members.splice(members.indexOf(room.creator.username), 1);
info = Meteor.call(method, members[0]);
} else {
info = Meteor.call(method, rcRoom.name, members, rcRoom.ro, rcRoom.customFields, extraData);
}
rid = info.rid;
});

Expand Down Expand Up @@ -73,6 +82,21 @@ export class AppRoomBridge {
return this.orch.getConverters().get('users').convertById(room.u._id);
}

async getMembers(roomId, appId) {
console.log(`The App ${ appId } is getting the room's members by room id: "${ roomId }"`);
const subscriptions = await RocketChat.models.Subscriptions.findByRoomId(roomId);
return subscriptions.map((sub) => this.orch.getConverters().get('users').convertById(sub.u && sub.u._id));
}

async getDirectByUsernames(usernames, appId) {
console.log(`The App ${ appId } is getting direct room by usernames: "${ usernames }"`);
const room = await RocketChat.models.Rooms.findDirectRoomContainingAllUsernames(usernames);
if (!room) {
return undefined;
}
return this.orch.getConverters().get('rooms').convertRoom(room);
}

async update(room, members = [], appId) {
console.log(`The App ${ appId } is updating a room.`);

Expand Down
9 changes: 9 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ class ModelRooms extends RocketChat.models._Base {
return this.find(query, options);
}

findDirectRoomContainingAllUsernames(usernames, options) {
const query = {
t: 'd',
usernames: { $size: usernames.length, $all: usernames },
};

return this.findOne(query, options);
}

findByTypeAndName(type, name, options) {
const query = {
name,
Expand Down

0 comments on commit 36acdea

Please sign in to comment.