Skip to content

Commit

Permalink
Add all users in threads of non-public-channels - fixes RocketChat#445 (
Browse files Browse the repository at this point in the history
RocketChat#457)

Conflicts solved, tests fail in an independent place
  • Loading branch information
mrsimpson committed Sep 6, 2018
1 parent 367a0a5 commit d383082
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions packages/assistify-threading/server/methods/createThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class ThreadBuilder {
this._openingQuestion.u = Meteor.user();
}
this._parentRoomId = parentRoomId;
this._parentRoom = ThreadBuilder.getRoom(this._parentRoomId);
this.rocketCatUser = RocketChat.models.Users.findOneByUsername('rocket.cat');
}

Expand Down Expand Up @@ -120,7 +121,7 @@ export class ThreadBuilder {

_getMembers() {
const checkRoles = ['owner', 'moderator', 'leader'];
const members = [];
let members = [];
const users = RocketChat.models.Subscriptions.findByRoomIdWhenUsernameExists(this._parentRoomId, {
fields: {
'u._id': 1,
Expand All @@ -132,42 +133,48 @@ export class ThreadBuilder {
username: s.u.username
};
});
if (this._parentRoom.t === 'c') {
// filter on owner, moderators and those online (see @here-implementation)
for (const user of users) {
if (!RocketChat.authz.hasRole(user.id, checkRoles, this._parentRoomId)) {
for (const user of users) {
if (!RocketChat.authz.hasRole(user.id, checkRoles, this._parentRoomId)) {
// TODO: Use a mass-read-access: Filter the non-owner/moderators and use them in an $in-query. Afterwards, add them all
RocketChat.models.Users.findOne({
_id: user.id,
status: {
$in: ['online', 'away', 'busy']
}
});
if (!user) {
continue;
RocketChat.models.Users.findOne({
_id: user.id,
status: {
$in: ['online', 'away', 'busy']
}
});
if (!user) {
continue;
// user.splice(users.indexOf(user), 1); //remove offline user
}
} else {
// has a special role in the parent room
members.push(user.username);
}
}
members.push(user.username);
} else {
// in direct messages and groups, add all users as members of the thread
members = users.map(user=>user.username);
}
return members;
}

create() {
const parentRoom = ThreadBuilder.getRoom(this._parentRoomId);
// Generate RoomName for the new room to be created.
this.name = `${ parentRoom.name || parentRoom.usernames.join('-') }-${ ThreadBuilder.getNextId() }`;
const threadRoomType = parentRoom.t === 'd' ? 'p' : parentRoom.t;
this.name = `${ this._parentRoom.name || this._parentRoom.usernames.join('-') }-${ ThreadBuilder.getNextId() }`;
const threadRoomType = this._parentRoom.t === 'd' ? 'p' : this._parentRoom.t;
const threadRoomCreationResult = RocketChat.createRoom(threadRoomType, this.name, Meteor.user() && Meteor.user().username, this._getMembers(), false,
{
announcement: this._openingQuestion.msg,
topic: parentRoom.name ? parentRoom.name : '',
topic: this._parentRoom.name ? this._parentRoom.name : '',
parentRoomId: this._parentRoomId
});

// Create messages in the newly created thread and it's parent which link the two rooms
const threadRoom = RocketChat.models.Rooms.findOneById(threadRoomCreationResult.rid);
if (threadRoom && parentRoom) {
this._threadWelcomeMessage(threadRoom, parentRoom);
if (threadRoom && this._parentRoom) {
this._threadWelcomeMessage(threadRoom, this._parentRoom);

// Post message
const repostedMessage = this._postMessage(
Expand All @@ -178,7 +185,7 @@ export class ThreadBuilder {
);

// Create messages linking the parent room and the thread
this._linkMessages(threadRoom, parentRoom, repostedMessage);
this._linkMessages(threadRoom, this._parentRoom, repostedMessage);
}

return threadRoom;
Expand Down

0 comments on commit d383082

Please sign in to comment.