The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-lib/server/functions/createRoom.js

101 lines
2.8 KiB

/* globals RocketChat */
9 years ago
RocketChat.createRoom = function(type, name, owner, members, readOnly, extraData={}) {
name = s.trim(name);
owner = s.trim(owner);
members = [].concat(members);
if (!name) {
throw new Meteor.Error('error-invalid-name', 'Invalid name', { function: 'RocketChat.createRoom' });
}
owner = RocketChat.models.Users.findOneByUsername(owner, { fields: { username: 1 }});
if (!owner) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: 'RocketChat.createRoom' });
}
let nameValidation;
try {
nameValidation = new RegExp(`^${ RocketChat.settings.get('UTF8_Names_Validation') }$`);
} catch (error) {
nameValidation = new RegExp('^[0-9a-zA-Z-_.]+$');
}
if (!nameValidation.test(name)) {
throw new Meteor.Error('error-invalid-name', 'Invalid name', { function: 'RocketChat.createRoom' });
}
const now = new Date();
if (!_.contains(members, owner.username)) {
members.push(owner.username);
}
// avoid duplicate names
let room = RocketChat.models.Rooms.findOneByName(name);
if (room) {
if (room.archived) {
throw new Meteor.Error('error-archived-duplicate-name', `There's an archived channel with name ${ name }`, { function: 'RocketChat.createRoom', room_name: name });
} else {
throw new Meteor.Error('error-duplicate-channel-name', `A channel with name '${ name }' exists`, { function: 'RocketChat.createRoom', room_name: name });
}
}
if (type === 'c') {
RocketChat.callbacks.run('beforeCreateChannel', owner, {
t: 'c',
name,
ts: now,
ro: readOnly === true,
sysMes: readOnly !== true,
usernames: members,
u: {
_id: owner._id,
username: owner.username
}
});
}
9 years ago
extraData = Object.assign({}, extraData, {
ts: now,
ro: readOnly === true,
sysMes: readOnly !== true
9 years ago
});
room = RocketChat.models.Rooms.createWithTypeNameUserAndUsernames(type, name, owner, members, extraData);
for (const username of members) {
const member = RocketChat.models.Users.findOneByUsername(username, { fields: { username: 1 }});
if (!member) {
continue;
}
// make all room members muted by default, unless they have the post-readonly permission
if (readOnly === true && !RocketChat.authz.hasPermission(member._id, 'post-readonly')) {
RocketChat.models.Rooms.muteUsernameByRoomId(room._id, username);
}
const extra = { open: true };
if (username === owner.username) {
extra.ls = now;
}
RocketChat.models.Subscriptions.createWithRoomAndUser(room, member, extra);
}
RocketChat.authz.addUserRoles(owner._id, ['owner'], room._id);
if (type === 'c') {
Meteor.defer(() => {
RocketChat.callbacks.run('afterCreateChannel', owner, room);
});
Enhance outgoing webhooks and add history (#5823) * Convert the majority of the integrations package to JavaScript * Move the trigger handler to it's own class * Start trying to generalize integration items * Yay, additional events trigger outgoing webhooks * Silence codacy and fix eslint problems. * Started work on user created event for outgoing integrations * Finish the outgoing integration work on the user created event. * Add user join and leave room events for outgoing webhooks * Add fields to the rest api integration creation and clean up the processWebhookMessage * Add the HTTP to the incoming webhook context * Don't check for property on result if it isn't there. Closes #4175, #5762, and #5896. * Add archive room and file upload events for the webhooks * Disable integrations when the user to run/post as gets deleted. * Try to fix the tests failing due to the requird events property * Add history of integration which get fired * Add the missing file for the IntegrationHistory * Fix deleting users being broke due to my code * Add the outgoing webhook integration history viewing page along with v1 rest api to retrieve it * Integration history replays are now enabled along with advanced settings * Work on the advanced settings and enable paging on the integration history * Enable i18n for the history and advanced settings * Update the wording on the word placement * Move migration to be 88 now. * Add missing translations for the webhooks * Fix en.i18n.json identation * Fix integrations.html indentation * Fix more identations * Fix identation of integrationsOutgoing.html
9 years ago
} else if (type === 'p') {
Meteor.defer(() => {
RocketChat.callbacks.run('afterCreatePrivateGroup', owner, room);
});
}
return {
rid: room._id
};
};