Skip to content

Commit

Permalink
Merge pull request #8491 from RocketChat/hotfix/join-code
Browse files Browse the repository at this point in the history
[FIX] Invalid Code message for password protected channel
  • Loading branch information
rodrigok committed Oct 17, 2017
1 parent 342ed3a commit a38d917
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/methods/joinRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Meteor.methods({
}

if ((room.joinCodeRequired === true) && (code !== room.joinCode) && !RocketChat.authz.hasPermission(Meteor.userId(), 'join-without-join-code')) {
throw new Meteor.Error('error-code-invalid', 'Invalid Code', { method: 'joinRoom' });
throw new Meteor.Error('error-code-invalid', 'Invalid Room Password', { method: 'joinRoom' });
}

return RocketChat.addUserToRoom(rid, Meteor.user());
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-ui-message/client/messageBox.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<div>
{{{_ "you_are_in_preview_mode_of" room_name=roomName}}}
{{#if joinCodeRequired}}
<input type="text" name="joinCode" placeholder="{{_ 'Code'}}" style="width: 100px">
<input type="text" name="joinCode" placeholder="{{_ 'Password'}}" style="width: 100px">
{{/if}}
<button class="button join"><span><i class="icon-login"></i> {{_ "join"}}</span></button>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/rocketchat-ui-message/client/messageBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ Meteor.startup(function() {
});
RocketChat.callbacks.add('enter-room', function() {
setTimeout(()=> {
chatMessages[RocketChat.openedRoom].input.focus();
if (chatMessages[RocketChat.openedRoom].input) {
chatMessages[RocketChat.openedRoom].input.focus();
}
}, 200);
});
});
2 changes: 1 addition & 1 deletion packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ this.ChatMessages = class ChatMessages {

restoreText(rid) {
const text = localStorage.getItem(`messagebox_${ rid }`);
if (typeof text === 'string') {
if (typeof text === 'string' && this.input) {
this.input.value = text;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ Template.room.onRendered(function() {
}
});
Tracker.autorun(function() {
const subRoom = ChatSubscription.findOne({rid:template.data._id});
if (!subRoom) {
const room = RocketChat.models.Rooms.findOne({ _id: template.data._id });
if (!room) {
FlowRouter.go('home');
}
});
Expand Down
85 changes: 50 additions & 35 deletions server/publications/room.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
const options = {
fields: {
_id: 1,
name: 1,
fname: 1,
t: 1,
cl: 1,
u: 1,
// usernames: 1,
topic: 1,
announcement: 1,
muted: 1,
_updatedAt: 1,
archived: 1,
jitsiTimeout: 1,
description: 1,
default: 1,
customFields: 1,

// @TODO create an API to register this fields based on room type
livechatData: 1,
tags: 1,
sms: 1,
code: 1,
open: 1,
v: 1,
label: 1,
ro: 1,
sentiment: 1
}
const fields = {
_id: 1,
name: 1,
fname: 1,
t: 1,
cl: 1,
u: 1,
// usernames: 1,
topic: 1,
announcement: 1,
muted: 1,
_updatedAt: 1,
archived: 1,
jitsiTimeout: 1,
description: 1,
default: 1,
customFields: 1,

// @TODO create an API to register this fields based on room type
livechatData: 1,
tags: 1,
sms: 1,
code: 1,
joinCodeRequired: 1,
open: 1,
v: 1,
label: 1,
ro: 1,
sentiment: 1
};


const roomMap = (record) => {
const roomMap = (record, fields) => {
if (record._room) {
return _.pick(record._room, ...Object.keys(options.fields));
return _.pick(record._room, ...Object.keys(fields));
}
console.log('Empty Room for Subscription', record);
return {};
};

function getFieldsForUserId(userId) {
if (RocketChat.authz.hasPermission(userId, 'view-join-code')) {
return {
...fields,
joinCode: 1
};
}

return fields;
}

Meteor.methods({
'rooms/get'(updatedAt) {
let options = {fields};

if (!Meteor.userId()) {
if (RocketChat.settings.get('Accounts_AllowAnonymousRead') === true) {
return RocketChat.models.Rooms.findByDefaultAndTypes(true, ['c'], options).fetch();
Expand All @@ -51,6 +62,10 @@ Meteor.methods({

this.unblock();

options = {
fields: getFieldsForUserId(this.userId)
};

if (updatedAt instanceof Date) {
return {
update: RocketChat.models.Rooms.findBySubscriptionUserIdUpdatedAfter(Meteor.userId(), updatedAt, options).fetch(),
Expand Down Expand Up @@ -84,22 +99,22 @@ Meteor.methods({
throw new Meteor.Error('error-no-permission', 'No permission', { method: 'getRoomByTypeAndName' });
}

return roomMap({_room: room});
return roomMap({_room: room}, getFieldsForUserId(this.userId));
}
});

RocketChat.models.Rooms.cache.on('sync', (type, room/*, diff*/) => {
const records = RocketChat.models.Subscriptions.findByRoomId(room._id).fetch();
for (const record of records) {
RocketChat.Notifications.notifyUserInThisInstance(record.u._id, 'rooms-changed', type, roomMap({_room: room}));
RocketChat.Notifications.notifyUserInThisInstance(record.u._id, 'rooms-changed', type, roomMap({_room: room}, getFieldsForUserId(record.u._id)));
}
});

RocketChat.models.Subscriptions.on('changed', (type, subscription/*, diff*/) => {
if (type === 'inserted' || type === 'removed') {
const room = RocketChat.models.Rooms.findOneById(subscription.rid);
if (room) {
RocketChat.Notifications.notifyUserInThisInstance(subscription.u._id, 'rooms-changed', type, roomMap({_room: room}));
RocketChat.Notifications.notifyUserInThisInstance(subscription.u._id, 'rooms-changed', type, roomMap({_room: room}, getFieldsForUserId(subscription.u._id)));
}
}
});

0 comments on commit a38d917

Please sign in to comment.