Skip to content

Commit

Permalink
Add basic support for teams
Browse files Browse the repository at this point in the history
  • Loading branch information
auterium committed May 2, 2018
1 parent ac16e93 commit 7a0e697
Show file tree
Hide file tree
Showing 35 changed files with 124 additions and 83 deletions.
2 changes: 1 addition & 1 deletion client/startup/unread.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Meteor.startup(function() {
}
}

if (RoomManager.openedRooms[subscription.t + subscription.name]) {
if (RoomManager.openedRooms[subscription.t + subscription.team + '/' + subscription.name]) {
readMessage.refreshUnreadMark(subscription.rid);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function findChannelByIdOrName({ params, checkedArchived = true, returnUsernames
if (params.roomId) {
room = RocketChat.models.Rooms.findOneById(params.roomId, { fields });
} else if (params.roomName) {
room = RocketChat.models.Rooms.findOneByName(params.roomName, { fields });
room = RocketChat.models.Rooms.findOneByName(params.roomName, params.team, { fields });
}

if (!room || room.t !== 'c') {
Expand Down
12 changes: 9 additions & 3 deletions packages/rocketchat-api/server/v1/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ function findPrivateGroupByIdOrName({ params, userId, checkedArchived = true })
let roomSub;
if (params.roomId) {
roomSub = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(params.roomId, userId);
} else if (params.roomName) {
roomSub = RocketChat.models.Subscriptions.findOneByRoomNameAndUserId(params.roomName, userId);
} else if (params.roomName && params.team) {
// Add team
roomSub = RocketChat.models.Subscriptions.findOneByRoomNameAndUserId(params.team, params.roomName, userId);
}

if (!roomSub || roomSub.t !== 'p') {
Expand Down Expand Up @@ -118,6 +119,10 @@ RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, {
return RocketChat.API.v1.failure('Body param "name" is required');
}

if (!this.bodyParams.team) {
return RocketChat.API.v1.failure('Body param "team" is required');
}

if (this.bodyParams.members && !_.isArray(this.bodyParams.members)) {
return RocketChat.API.v1.failure('Body param "members" must be an array if provided');
}
Expand All @@ -132,8 +137,9 @@ RocketChat.API.v1.addRoute('groups.create', { authRequired: true }, {
}

let id;
const { name, team, members, customFields } = this.bodyParams;
Meteor.runAsUser(this.userId, () => {
id = Meteor.call('createPrivateGroup', this.bodyParams.name, this.bodyParams.members ? this.bodyParams.members : [], readOnly, this.bodyParams.customFields);
id = Meteor.call('createPrivateGroup', name, members || [], readOnly, customFields, { team });
});

return RocketChat.API.v1.success({
Expand Down
3 changes: 2 additions & 1 deletion packages/rocketchat-api/server/v1/im.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function findDirectMessageRoom(params, user) {
const room = RocketChat.getRoomByNameOrIdWithOptionToJoin({
currentUserId: user._id,
nameOrId: params.username || params.roomId,
type: 'd'
type: 'd',
team: params.team
});

if (!room || room.t !== 'd') {
Expand Down
5 changes: 4 additions & 1 deletion packages/rocketchat-apps/server/bridges/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class AppRoomBridge {
console.log(`The App ${ appId } is creating a new room.`, room);

const rcRoom = this.orch.getConverters().get('rooms').convertAppRoom(room);
const params = [rcRoom.usernames];
let method;

switch (room.type) {
Expand All @@ -17,14 +18,16 @@ export class AppRoomBridge {
break;
case RoomType.PRIVATE_GROUP:
method = 'createPrivateGroup';
// params.push(team)
throw new Error('Team not set');
break;
default:
throw new Error('Only channels and private groups can be created.');
}

let rid;
Meteor.runAsUser(room.creator.id, () => {
const info = Meteor.call(method, rcRoom.usernames);
const info = Meteor.call(method, ...params);
rid = info.rid;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Meteor.startup(function() {
if (msg.t === 'room_changed_privacy') {
if (Session.get('openedRoom') === msg.rid) {
const type = FlowRouter.current().route.name === 'channel' ? 'c' : 'p';
RoomManager.close(type + FlowRouter.getParam('name'));
RoomManager.close(type + FlowRouter.getParam('team') + '/' + FlowRouter.getParam('name'));

const subscription = ChatSubscription.findOne({ rid: msg.rid });
const route = subscription.t === 'c' ? 'channel' : 'group';
FlowRouter.go(route, { name: subscription.name }, FlowRouter.current().queryParams);
FlowRouter.go(route, { name: subscription.name, team: subscription.team }, FlowRouter.current().queryParams);
}
}
});
Expand All @@ -24,7 +24,7 @@ Meteor.startup(function() {
if (Session.get('openedRoom') === msg.rid) {
const room = ChatRoom.findOne(msg.rid);
if (room.name !== FlowRouter.getParam('name')) {
RoomManager.close(room.t + FlowRouter.getParam('name'));
RoomManager.close(room.t + FlowRouter.getParam('team') + '/' + FlowRouter.getParam('name'));
RocketChat.roomTypes.openRouteLink(room.t, room, FlowRouter.current().queryParams);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RocketChat.saveRoomName = function(rid, displayName, user, sendMessage = true) {
return;
}

const slugifiedRoomName = RocketChat.getValidRoomName(displayName, rid);
const slugifiedRoomName = RocketChat.getValidRoomName(displayName, team, rid);

const update = RocketChat.models.Rooms.setNameById(rid, slugifiedRoomName, displayName) && RocketChat.models.Subscriptions.updateNameAndAlertByRoomId(rid, slugifiedRoomName, displayName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class ErrorHandler {
};
}

getRoomId(roomName) {
getRoomId(roomName, team) {
roomName = roomName.replace('#');
const room = RocketChat.models.Rooms.findOneByName(roomName, { fields: { _id: 1, t: 1 } });
const room = RocketChat.models.Rooms.findOneByName(roomName, team, { fields: { _id: 1, t: 1 } });
if (room && (room.t === 'c' || room.t === 'p')) {
return room._id;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import s from 'underscore.string';
this.processWebhookMessage = function(messageObj, user, defaultValues = { channel: '', alias: '', avatar: '', emoji: '' }, mustBeJoined = false) {
const sentData = [];
const channels = [].concat(messageObj.channel || messageObj.roomId || defaultValues.channel);
const team = messageObj.team;

for (const channel of channels) {
const channelType = channel[0];
Expand All @@ -13,22 +14,22 @@ this.processWebhookMessage = function(messageObj, user, defaultValues = { channe

switch (channelType) {
case '#':
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, joinChannel: true });
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, team, nameOrId: channelValue, joinChannel: true });
break;
case '@':
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, type: 'd' });
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, team, nameOrId: channelValue, type: 'd' });
break;
default:
channelValue = channelType + channelValue;

//Try to find the room by id or name if they didn't include the prefix.
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, joinChannel: true, errorOnEmpty: false });
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, team, nameOrId: channelValue, joinChannel: true, errorOnEmpty: false });
if (room) {
break;
}

//We didn't get a room, let's try finding direct messages
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, nameOrId: channelValue, type: 'd', tryDirectByUserIdOnly: true });
room = RocketChat.getRoomByNameOrIdWithOptionToJoin({ currentUserId: user._id, team, nameOrId: channelValue, type: 'd', tryDirectByUserIdOnly: true });
if (room) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/client/lib/cachedCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class CachedCollection {
this.log('record received', t, record);
if (t === 'removed') {
this.collection.remove(record._id);
RoomManager.close(record.t+record.name);
RoomManager.close(record.t + record.team + '/' + record.name);
} else {
delete record.$loki;
this.collection.upsert({ _id: record._id }, _.omit(record, '_id'));
Expand Down
20 changes: 10 additions & 10 deletions packages/rocketchat-lib/client/lib/openRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'underscore';

currentTracker = undefined;

function openRoom(type, name) {
function openRoom(type, name, team) {
Session.set('openedRoom', null);

return Meteor.defer(() =>
Expand All @@ -14,7 +14,7 @@ function openRoom(type, name) {
return;
}

if (RoomManager.open(type + name).ready() !== true) {
if (RoomManager.open(type + team + '/' + name).ready() !== true) {
BlazeLayout.render('main', { modal: RocketChat.Layout.isEmbedded(), center: 'loading' });
return;
}
Expand All @@ -23,13 +23,13 @@ function openRoom(type, name) {
}
c.stop();

const room = RocketChat.roomTypes.findRoom(type, name, user);
const room = RocketChat.roomTypes.findRoom(type, team, name, user);
if (room == null) {
if (type === 'd') {
Meteor.call('createDirectMessage', name, function(err) {
Meteor.call('createDirectMessage', name, team, function(err) {
if (!err) {
RoomManager.close(type + name);
return openRoom('d', name);
RoomManager.close(type + team + '/' + name);
return openRoom('d', name, team);
} else {
Session.set('roomNotFound', {type, name});
BlazeLayout.render('main', {center: 'roomNotFound'});
Expand All @@ -44,8 +44,8 @@ function openRoom(type, name) {
} else {
delete record.$loki;
RocketChat.models.Rooms.upsert({ _id: record._id }, _.omit(record, '_id'));
RoomManager.close(type + name);
return openRoom(type, name);
RoomManager.close(type + team + '/' + name);
return openRoom(type, name, team);
}
});
}
Expand All @@ -57,7 +57,7 @@ function openRoom(type, name) {
for (const child of Array.from(mainNode.children)) {
if (child) { mainNode.removeChild(child); }
}
const roomDom = RoomManager.getDomOfRoom(type + name, room._id);
const roomDom = RoomManager.getDomOfRoom(type + team + '/' + name, room._id);
mainNode.appendChild(roomDom);
if (roomDom.classList.contains('room-container')) {
roomDom.querySelector('.messages-box > .wrapper').scrollTop = roomDom.oldScrollTop;
Expand All @@ -70,7 +70,7 @@ function openRoom(type, name) {
fireGlobalEvent('room-opened', _.omit(room, 'usernames'));

Session.set('editRoomTitle', false);
RoomManager.updateMentionsMarksOfRoom(type + name);
RoomManager.updateMentionsMarksOfRoom(type + team + '/' + name);
Meteor.setTimeout(() => readMessage.readNow(), 2000);
// KonchatNotification.removeRoomNotification(params._id)
// update user's room subscription
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-lib/client/lib/roomTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ RocketChat.roomTypes = new class RocketChatRoomTypes extends RoomTypesCommon {
getUserStatus(roomType, roomId) {
return this.roomTypes[roomType] && typeof this.roomTypes[roomType].getUserStatus === 'function' && this.roomTypes[roomType].getUserStatus(roomId);
}
findRoom(roomType, identifier, user) {
return this.roomTypes[roomType] && this.roomTypes[roomType].findRoom(identifier, user);
findRoom(roomType, team, identifier, user) {
return this.roomTypes[roomType] && this.roomTypes[roomType].findRoom(identifier, team, user);
}
canSendMessage(roomId) {
return ChatSubscription.find({
Expand Down
6 changes: 3 additions & 3 deletions packages/rocketchat-lib/lib/RoomTypesCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export class RoomTypesCommon {
if (this.roomTypes[roomType] && this.roomTypes[roomType].route && this.roomTypes[roomType].route.link) {
routeData = this.roomTypes[roomType].route.link(subData);
} else if (subData && subData.name) {
routeData = {
name: subData.name
};
routeData = { name: subData.name };
}

routeData.team = subData.team;

return FlowRouter.path(this.roomTypes[roomType].route.name, routeData);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/rocketchat-lib/lib/getValidRoomName.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import s from 'underscore.string';

RocketChat.getValidRoomName = function getValidRoomName(displayName, rid = '') {
RocketChat.getValidRoomName = function getValidRoomName(displayName, team, rid = '') {
let slugifiedName = displayName;

if (RocketChat.settings.get('UI_Allow_room_names_with_special_chars')) {
const room = RocketChat.models.Rooms.findOneByDisplayName(displayName);
const room = RocketChat.models.Rooms.findOneByDisplayName(displayName, team);
if (room && room._id !== rid) {
if (room.archived) {
throw new Meteor.Error('error-archived-duplicate-name', `There's an archived channel with name ${ displayName }`, { function: 'RocketChat.getValidRoomName', channel_name: displayName });
Expand All @@ -28,7 +28,7 @@ RocketChat.getValidRoomName = function getValidRoomName(displayName, rid = '') {
});
}

const room = RocketChat.models.Rooms.findOneByName(slugifiedName);
const room = RocketChat.models.Rooms.findOneByName(slugifiedName, team);
if (room && room._id !== rid) {
if (RocketChat.settings.get('UI_Allow_room_names_with_special_chars')) {
let tmpName = slugifiedName;
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-lib/lib/roomTypes/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export class DirectMessageRoomRoute extends RoomTypeRouteConfig {
constructor() {
super({
name: 'direct',
path: '/direct/:username'
path: '/:team/direct/:username'
});
}

action(params) {
return openRoom('d', params.username);
return openRoom('d', params.username, params.team);
}

link(sub) {
Expand All @@ -28,10 +28,11 @@ export class DirectMessageRoomType extends RoomTypeConfig {
});
}

findRoom(identifier) {
findRoom(identifier, team) {
const query = {
t: 'd',
name: identifier
name: identifier,
team
};

const subscription = ChatSubscription.findOne(query);
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-lib/lib/roomTypes/private.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export class PrivateRoomRoute extends RoomTypeRouteConfig {
constructor() {
super({
name: 'group',
path: '/group/:name'
path: '/:team/group/:name'
});
}

action(params) {
return openRoom('p', params.name);
return openRoom('p', params.name, params.team);
}
}

Expand All @@ -25,10 +25,11 @@ export class PrivateRoomType extends RoomTypeConfig {
});
}

findRoom(identifier) {
findRoom(identifier, team) {
const query = {
t: 'p',
name: identifier
name: identifier,
team
};

return ChatRoom.findOne(query);
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-lib/lib/roomTypes/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export class PublicRoomRoute extends RoomTypeRouteConfig {
constructor() {
super({
name: 'channel',
path: '/channel/:name'
path: '/:team/channel/:name'
});
}

action(params) {
return openRoom('c', params.name);
return openRoom('c', params.name, params.team);
}
}

Expand All @@ -25,10 +25,11 @@ export class PublicRoomType extends RoomTypeConfig {
});
}

findRoom(identifier) {
findRoom(identifier, team) {
const query = {
t: 'c',
name: identifier
name: identifier,
team
};
return ChatRoom.findOne(query);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-lib/server/functions/createRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'RocketChat.createRoom' });
}

const slugifiedRoomName = RocketChat.getValidRoomName(name);
const slugifiedRoomName = RocketChat.getValidRoomName(name, extraData.team);

const now = new Date();
if (!_.contains(members, owner.username)) {
Expand Down
Loading

0 comments on commit 7a0e697

Please sign in to comment.